-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize SSE key encoding, and remove the AVX key encoder. Per the comment in this article https://stackoverflow.com/questions/35663635/why-do-processors-with-only-avx-out-perform-avx2-processors-for-many-simd-algori it seems using AVX does not make sense for this use case. After some benchmarking, AVX generally did not outperform SSE. Given the extra cost of using AVX registers and no additional performance, it seems like SSE is the clear winner. Also, fall back to the standard binary encoding (instead of the slow path) if SSE isn't available.
- Loading branch information
Showing
4 changed files
with
103 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#if NETCOREAPP3_1 || NET5_0 | ||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Intrinsics; | ||
using System.Runtime.Intrinsics.X86; | ||
|
||
namespace Knet.Kudu.Client.Tablet | ||
{ | ||
public static partial class KeyEncoder | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private static int EncodeBinary( | ||
ReadOnlySpan<byte> source, Span<byte> destination) | ||
{ | ||
if (Sse41.IsSupported) | ||
{ | ||
return EncodeBinarySse(source, destination); | ||
} | ||
|
||
return EncodeBinaryStandard(source, destination); | ||
} | ||
|
||
private static unsafe int EncodeBinarySse( | ||
ReadOnlySpan<byte> source, Span<byte> destination) | ||
{ | ||
var length = (uint)source.Length; | ||
|
||
if ((uint)destination.Length < length * 2) | ||
ThrowException(); | ||
|
||
fixed (byte* src = source) | ||
fixed (byte* dest = destination) | ||
{ | ||
var srcCurrent = src; | ||
var destCurrent = dest; | ||
|
||
var remainder = length % 16; | ||
var lastBlockIndex = length - remainder; | ||
var blockEnd = src + lastBlockIndex; | ||
var end = src + length; | ||
|
||
while (srcCurrent < blockEnd) | ||
{ | ||
var data = Sse2.LoadVector128(srcCurrent); | ||
var zeros = Vector128<byte>.Zero; | ||
|
||
var zeroBytes = Sse2.CompareEqual(data, zeros); | ||
bool allZeros = Sse41.TestZ(zeroBytes, zeroBytes); | ||
|
||
if (allZeros) | ||
Sse2.Store(destCurrent, data); | ||
else | ||
break; | ||
|
||
srcCurrent += 16; | ||
destCurrent += 16; | ||
} | ||
|
||
while (srcCurrent < end) | ||
{ | ||
byte value = *srcCurrent++; | ||
if (value == 0) | ||
{ | ||
*destCurrent++ = 0; | ||
*destCurrent++ = 1; | ||
} | ||
else | ||
{ | ||
*destCurrent++ = value; | ||
} | ||
} | ||
|
||
var written = destCurrent - dest; | ||
return (int)written; | ||
} | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.NoInlining)] | ||
private static void ThrowException() => | ||
throw new Exception("Destination must be at least double source"); | ||
} | ||
} | ||
#endif |