Skip to content

Commit

Permalink
Make GeoHash coordinate conversions ~40-100x faster (#348)
Browse files Browse the repository at this point in the history
* Refactor Haversine-distance calculation

* See https://github.com/dotnet/runtime/blob/333fb71d54bd84256e740aa08f8b836d4cd71d98/src/libraries/System.Private.CoreLib/src/System/Numerics/ITrigonometricFunctions.cs#L65-L113

* Do not spill the coordinate ranges to heap when encoding/decoding

* Keep them in registers (or atmost spill to stack)
* Also some other misc. simplification

* Add shared AsciiUtils to Garnet.common to simplify the unit conversions

* Slightly adjust GeoHash tests

* dotnet format

* Restore flag bit for  GetGeoHashCode for now

* I will return to this method in a follow-up

* Optimize GeoToLongValue to use float quantization trick and do the Z-curve encoding more directly

* Credits to https://mmcloughlin.com/posts/geohash-assembly for the quantization approach!

* Further optimize Geohash & Base32 encoding and decoding

* Abuse IEEE-754 binary representation in the encoding too
* Implement Z-curve decoding more efficiently
* Add GetGeoErrorByPrecision to calculate the error at given bit precision (52 for us).
* Optimize base32 string encoding.
* Test still fail. Will need to investigate more.

* typo

* Further clarify the quantization method

* Calculate the center of bounding-box

* Clarify dequantization method

* Make the bounding-box center fix-up use constants

* Add more test-data and restore original epsilon calc.

* tests: sqc8b49rnyt -> sqc8b49rnys

* tests: nsqdtr74hyu1 -> nsqdtr74hyu0

* Exponent is 1023, not 0

* Add USE_PDEP_PEXT switch for PDEP/PEXT Z-curve en/decode

* And define it to run the tests with it

* Use FusedMultiplyAdd to do (x+y)*z in one op to avoid intermediate rounding

* More accurate and faster, what not to love

* Remove #define USE_PDEP_PEXT, tests passed

* Move GeoHash specific unit tests own file

* Mark Z-curve encode/decode with MethodImpl.AI

* Little extra encouragement to JIT.

* Add GeoHash specific benchmarks

* oops

* format

* Add UsePdepPext build switch.

* And add GeoHashBenchmark job with it enabled

* Avoid shifting by using already shifted mask for the PDEP/PEXT

* Use AVX512 support to guard PDEP/PEXT usage.

* Add MemoryDiagnoser back

* Fix incorrect quantization approach

* Make it little bit more clear what happens in the corner-case guard

* We might not even wan't cmov/csel which might stall out-of-order execution. Doesn't matter what is emitted tbh.

* Remove not needed pow2 trick

* Let JIT do its thing, it's pretty good

* Adjust comments a bit

* Fix comment typos

* Adjust comments

* Adjust comments

* Add third-party notices to NOTICE.md

* The GeoHash class incorporates material from mmcloughlin/geohash and georust/geohash, both licensed under MIT License. Thank you for sharing!

---------

Co-authored-by: Yoganand Rajasekaran <60369795+yrajas@users.noreply.github.com>
  • Loading branch information
PaulusParssinen and yrajas authored May 21, 2024
1 parent 5c4041e commit e0027d1
Show file tree
Hide file tree
Showing 10 changed files with 441 additions and 225 deletions.
1 change: 0 additions & 1 deletion Garnet.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31808.319
Expand Down
56 changes: 56 additions & 0 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,59 @@ the Author.
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.

## mmcloughlin/geohash

**Source**: https://github.com/mmcloughlin/geohash

The MIT License (MIT)

Copyright (c) 2015 Michael McLoughlin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## georust/geohash

**Source**: https://github.com/georust/geohash

Copyright (c) 2016 Ning Sun

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
27 changes: 27 additions & 0 deletions benchmark/BDN.benchmark/GeoHashBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BenchmarkDotNet.Attributes;

using Garnet.server;

namespace BDN.benchmark
{
[MemoryDiagnoser]
public class GeoHashBenchmarks
{
private const double Latitude = 47.642219912251285;
private const double Longitude = -122.14205560231471;

private const long GeoHashInteger = 1557413161902764;

[Benchmark]
public long GeoToLongValue() => GeoHash.GeoToLongValue(Latitude, Longitude);

[Benchmark]
public (double, double) GetCoordinatesFromLong() => GeoHash.GetCoordinatesFromLong(GeoHashInteger);

[Benchmark]
public string GetGeoHashCode() => GeoHash.GetGeoHashCode(GeoHashInteger);
}
}
33 changes: 24 additions & 9 deletions benchmark/BDN.benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Running;

var config = DefaultConfig.Instance
.AddJob(Job.Default
.WithRuntime(CoreRuntime.Core60)
.WithId(".NET 6"))
.AddJob(Job.Default
.WithRuntime(CoreRuntime.Core80)
.WithEnvironmentVariables(new EnvironmentVariable("DOTNET_TieredPGO", "0"))
.WithId(".NET 8"));
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, new BaseConfig());

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
public class BaseConfig : ManualConfig
{
public Job Net6BaseJob { get; }
public Job Net8BaseJob { get; }

public BaseConfig()
{
AddLogger(ConsoleLogger.Default);
AddExporter(DefaultExporters.Markdown);
AddColumnProvider(DefaultColumnProviders.Instance);

var baseJob = Job.Default.WithGcServer(true);

Net6BaseJob = baseJob.WithRuntime(CoreRuntime.Core60);
Net8BaseJob = baseJob.WithRuntime(CoreRuntime.Core80)
.WithEnvironmentVariables(new EnvironmentVariable("DOTNET_TieredPGO", "0"));

AddJob(Net6BaseJob.WithId(".NET 6"), Net8BaseJob.WithId(".NET 8"));
}
}
20 changes: 8 additions & 12 deletions benchmark/BDN.benchmark/RecoveryBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using Embedded.perftest;
using Garnet.server;

namespace BDN.benchmark
{
public class CustomConfig : ManualConfig
[Config(typeof(Config))]
public class RecoveryBenchmark
{
public CustomConfig()
private class Config : BaseConfig
{
AddColumn(StatisticColumn.Mean);
AddColumn(StatisticColumn.StdDev);
AddColumn(StatisticColumn.Median);
AddColumn(StatisticColumn.P90);
AddColumn(StatisticColumn.P95);
public Config()
{
AddColumn(StatisticColumn.P90);
AddColumn(StatisticColumn.P95);
}
}
}

[Config(typeof(CustomConfig))]
public class RecoveryBenchmark
{
[ParamsSource(nameof(CommandLineArgsProvider))]
public string LogDir { get; set; }

Expand Down
17 changes: 17 additions & 0 deletions libs/common/AsciiUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

namespace Garnet.common;

/// <summary>
/// Utilites for ASCII parsing and manipulation.
/// </summary>
public static class AsciiUtils
{
public static byte ToLower(byte value)
{
if ((uint)(value - 'A') <= (uint)('Z' - 'A')) // Is in [A-Z]
value = (byte)(value | 0x20);
return value;
}
}
19 changes: 7 additions & 12 deletions libs/server/GlobUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using Garnet.common;

namespace Garnet.server
{
/// <summary>
Expand All @@ -14,13 +16,6 @@ public static class GlobUtils
/// <returns>Whether match was found</returns>
public static unsafe bool Match(byte* pattern, int patternLen, byte* key, int stringLen, bool ignoreCase = false)
{
static byte ToLowerAscii(byte value)
{
if ((uint)(value - 'A') <= (uint)('Z' - 'A')) // Is in [A-Z]
value = (byte)(value | 0x20);
return value;
}

while (patternLen > 0 && stringLen > 0)
{
switch (pattern[0])
Expand Down Expand Up @@ -89,9 +84,9 @@ static byte ToLowerAscii(byte value)

if (ignoreCase)
{
start = ToLowerAscii(start);
end = ToLowerAscii(end);
c = ToLowerAscii(c);
start = AsciiUtils.ToLower(start);
end = AsciiUtils.ToLower(end);
c = AsciiUtils.ToLower(c);
}
pattern += 2;
patternLen -= 2;
Expand All @@ -107,7 +102,7 @@ static byte ToLowerAscii(byte value)
}
else
{
if (ToLowerAscii(pattern[0]) == ToLowerAscii(key[0]))
if (AsciiUtils.ToLower(pattern[0]) == AsciiUtils.ToLower(key[0]))
match = true;
}
}
Expand Down Expand Up @@ -141,7 +136,7 @@ static byte ToLowerAscii(byte value)
}
else
{
if (ToLowerAscii(pattern[0]) != ToLowerAscii(key[0]))
if (AsciiUtils.ToLower(pattern[0]) != AsciiUtils.ToLower(key[0]))
return false; /* no match */
}
key++;
Expand Down
Loading

0 comments on commit e0027d1

Please sign in to comment.