Skip to content

Commit

Permalink
Add comments about LUT cache friendliness
Browse files Browse the repository at this point in the history
  • Loading branch information
mqudsi committed May 16, 2024
1 parent 400fecc commit 47ec32b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions UrlBase64/UrlBase64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public static class UrlBase64
private const PaddingPolicy DefaultPaddingPolicy = PaddingPolicy.Discard;

#if WITH_SPAN
// 64-byte LUT, takes only one L1 cache line. Extremely efficient. See https://specbranch.com/posts/lookup-tables/
//
// Forward mapping from any of 64 binary values to their URL-safe Base64 equivalents.
// In our dictionary, - takes the place of + and _ takes the place of /.
private readonly static ReadOnlyMemory<byte> ToBase64 = new byte[] {
Expand All @@ -28,6 +30,10 @@ public static class UrlBase64
(byte)'4', (byte)'5', (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'-', (byte)'_'
};

// 256-byte (4 cache line) lookup table, but not accessed uniformly - 3rd and 4th cache lines
// for invalid chars only and most values are in second - so it should still perform well and
// remain hot in L1.
//
// Reverse mapping of base64 alphabet to numerical value, such that table[(byte)'A'] = 0, ...
private readonly static ReadOnlyMemory<byte> FromBase64 = new byte[] {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
Expand Down

0 comments on commit 47ec32b

Please sign in to comment.