Skip to content

Commit 80a445a

Browse files
Remove unnecessary unsafe from GetGeoHashCode
1 parent f33ff6a commit 80a445a

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

libs/server/Objects/SortedSetGeo/GeoHash.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Runtime.CompilerServices;
6-
using System.Runtime.InteropServices;
76
using System.Runtime.Intrinsics.X86;
87

98
namespace Garnet.server
@@ -234,21 +233,17 @@ static uint Squash(ulong x)
234233
/// <returns>The standard textual representation of the given 52-bit GeoHash integer</returns>
235234
public static string GetGeoHashCode(long hash)
236235
{
237-
return string.Create(CodeLength, state: hash, static (chars, hashState) =>
236+
return string.Create(CodeLength, state: hash, static (chars, hash) =>
238237
{
239-
// Reference to the start of the base-32 char table, which is stored as constant data.
240-
ref var base32CharsBase = ref MemoryMarshal.GetReference("0123456789bcdefghjkmnpqrstuvwxyz"u8);
238+
var base32Chars = "0123456789bcdefghjkmnpqrstuvwxyz"u8;
241239

242240
for (var i = 0; i < chars.Length; i++)
243241
{
244-
// Shift and mask the five most significant bits.
245-
var tableIndex = (nuint)(hashState >> (BitsOfPrecision - 5)) & 0x1F;
246-
247-
// By masking the five bits, the tableIndex is now guaranteed to be <= 31 so this is safe.
248-
chars[i] = (char)Unsafe.Add(ref base32CharsBase, tableIndex);
242+
// Shift and mask the five most significant bits for index to the base-32 table.
243+
chars[i] = (char)base32Chars[(int)(hash >> (BitsOfPrecision - 5)) & 0x1F];
249244

250245
// Shift the encoded bits out.
251-
hashState <<= 5;
246+
hash <<= 5;
252247
}
253248
});
254249
}

0 commit comments

Comments
 (0)