Skip to content

Commit

Permalink
added fast starts with
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Jan 21, 2025
1 parent ea29623 commit 022cd46
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 55 deletions.
98 changes: 97 additions & 1 deletion ModTek/Features/Logging/FastMemCpy.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,108 @@
using System;
using System.Runtime.CompilerServices;
using ModTek.Util.Stopwatch;

namespace ModTek.Features.Logging;

// effectively used only for byte arrays sized <= 14 (112bit)
internal static class FastMemCpy
{
internal static unsafe bool FastStartsWith(this string text, string prefix)
{
if (text == null || prefix == null)
{
return false;
}
if (text.Length < prefix.Length)
{
return false;
}
fixed (char* prefixPtr = prefix)
{
fixed (char* textPtr = text)
{
return FastStartsWith((ushort*)prefixPtr, (ushort*)textPtr, prefix.Length);
}
}
}

private static unsafe bool FastStartsWith(ushort* prefix, ushort* text, int size)
{
// search backwards, failures are faster that way
prefix += size;
text += size;
{
const int BatchSize = 16 * sizeof(ulong)/sizeof(ushort);
for (; size >= BatchSize; size -= BatchSize)
{
prefix -= BatchSize;
text -= BatchSize;
if (
*((ulong*)prefix+ 0) != *((ulong*)text+ 0)
|| *((ulong*)prefix+ 1) != *((ulong*)text+ 1)
|| *((ulong*)prefix+ 2) != *((ulong*)text+ 2)
|| *((ulong*)prefix+ 3) != *((ulong*)text+ 3)
|| *((ulong*)prefix+ 4) != *((ulong*)text+ 4)
|| *((ulong*)prefix+ 5) != *((ulong*)text+ 5)
|| *((ulong*)prefix+ 6) != *((ulong*)text+ 6)
|| *((ulong*)prefix+ 7) != *((ulong*)text+ 7)
|| *((ulong*)prefix+ 8) != *((ulong*)text+ 8)
|| *((ulong*)prefix+ 9) != *((ulong*)text+ 9)
|| *((ulong*)prefix+10) != *((ulong*)text+10)
|| *((ulong*)prefix+11) != *((ulong*)text+11)
|| *((ulong*)prefix+12) != *((ulong*)text+12)
|| *((ulong*)prefix+13) != *((ulong*)text+13)
|| *((ulong*)prefix+14) != *((ulong*)text+14)
|| *((ulong*)prefix+15) != *((ulong*)text+15)
)
{
return false;
}
}
}
{
const int BatchSize = 4 * sizeof(ulong)/sizeof(ushort);
for (; size >= BatchSize; size -= BatchSize)
{
prefix -= BatchSize;
text -= BatchSize;
if (
*(ulong*)prefix != *(ulong*)text
|| *((ulong*)prefix+1) != *((ulong*)text+1)
|| *((ulong*)prefix+2) != *((ulong*)text+2)
|| *((ulong*)prefix+3) != *((ulong*)text+3)
)
{
return false;
}
}
}
{
const int BatchSize = sizeof(ulong)/sizeof(ushort);
for (; size >= BatchSize; size -= BatchSize)
{
prefix -= BatchSize;
text -= BatchSize;
if (*(ulong*)prefix != *(ulong*)text)
{
return false;
}
}
}
{
const int BatchSize = sizeof(ushort)/sizeof(ushort);
for (; size >= BatchSize; size -= BatchSize)
{
prefix -= BatchSize;
text -= BatchSize;
if (*prefix != *text)
{
return false;
}
}
}
return true;
}

internal static unsafe void BlockCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length)
{
if (length > Threshold) // 700-1300 bytes are typical
Expand Down
Loading

0 comments on commit 022cd46

Please sign in to comment.