From a1ea88a2bccc83ff4a7a7a7ef35d1644408a9971 Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 10 Jul 2017 23:35:59 -0700 Subject: [PATCH] Improve indexofbytes speed ~10% faster wew removes range check when checking successive bytes --- pk3DS.Core/Util.cs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/pk3DS.Core/Util.cs b/pk3DS.Core/Util.cs index fa4df2b..f016904 100644 --- a/pk3DS.Core/Util.cs +++ b/pk3DS.Core/Util.cs @@ -170,16 +170,20 @@ public static char[] Reverse(char[] charArray) public static int IndexOfBytes(byte[] array, byte[] pattern, int startIndex, int count) { int len = pattern.Length; - int endIndex = count > 0 ? startIndex + count + 1 : array.Length - pattern.Length; - for (int i = startIndex; i < endIndex; i++) + int endIndex = count > 0 ? startIndex + count : array.Length - pattern.Length; + int i = startIndex; + int j = 0; + while (true) { - for (int j = 0; j < len; j++) - if (pattern[j] != array[i + j]) - goto next_i; - return i; - next_i:; + if (pattern[j] != array[i + j]) + { + if (++i == endIndex) + return -1; + j = 0; + } + else if (++j == len) + return i; } - return -1; } // Misc