Skip to content

Commit 72d6bca

Browse files
committed
Use Convert.ToHexString (2x faster, 1/2 memory usage)
1 parent a8e9e64 commit 72d6bca

1 file changed

Lines changed: 1 addition & 24 deletions

File tree

src/JsonApiDotNetCore/Serialization/Response/FingerprintGenerator.cs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ namespace JsonApiDotNetCore.Serialization.Response;
77
internal sealed class FingerprintGenerator : IFingerprintGenerator
88
{
99
private static readonly byte[] Separator = "|"u8.ToArray();
10-
private static readonly uint[] LookupTable = Enumerable.Range(0, 256).Select(ToLookupEntry).ToArray();
11-
12-
private static uint ToLookupEntry(int index)
13-
{
14-
string hex = index.ToString("X2");
15-
return hex[0] + ((uint)hex[1] << 16);
16-
}
1710

1811
/// <inheritdoc />
1912
public string Generate(IEnumerable<string> elements)
@@ -30,22 +23,6 @@ public string Generate(IEnumerable<string> elements)
3023
}
3124

3225
byte[] hash = hasher.GetHashAndReset();
33-
return ByteArrayToHex(hash);
34-
}
35-
36-
private static string ByteArrayToHex(byte[] bytes)
37-
{
38-
// https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa
39-
40-
char[] buffer = new char[bytes.Length * 2];
41-
42-
for (int index = 0; index < bytes.Length; index++)
43-
{
44-
uint value = LookupTable[bytes[index]];
45-
buffer[2 * index] = (char)value;
46-
buffer[2 * index + 1] = (char)(value >> 16);
47-
}
48-
49-
return new string(buffer);
26+
return Convert.ToHexString(hash);
5027
}
5128
}

0 commit comments

Comments
 (0)