Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GeoHash coordinate conversions ~40-100x faster #348

Merged
merged 38 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cb61c41
Refactor Haversine-distance calculation
PaulusParssinen Apr 30, 2024
16b67b2
Do not spill the coordinate ranges to heap when encoding/decoding
PaulusParssinen Apr 30, 2024
85a09a5
Add shared AsciiUtils to Garnet.common to simplify the unit conversions
PaulusParssinen Apr 30, 2024
60fda90
Slightly adjust GeoHash tests
PaulusParssinen Apr 30, 2024
f137c0d
dotnet format
PaulusParssinen Apr 30, 2024
0e1f0ce
Restore flag bit for GetGeoHashCode for now
PaulusParssinen Apr 30, 2024
40baf28
Optimize GeoToLongValue to use float quantization trick and do the Z-…
PaulusParssinen May 1, 2024
1b34837
Further optimize Geohash & Base32 encoding and decoding
PaulusParssinen May 2, 2024
07862c7
typo
PaulusParssinen May 2, 2024
a3c0c96
Further clarify the quantization method
PaulusParssinen May 2, 2024
57d136c
Calculate the center of bounding-box
PaulusParssinen May 2, 2024
6673066
Make the bounding-box center fix-up use constants
PaulusParssinen May 2, 2024
746c7be
Add more test-data and restore original epsilon calc.
PaulusParssinen May 2, 2024
f982c8f
tests: sqc8b49rnyt -> sqc8b49rnys
PaulusParssinen May 2, 2024
c282a62
tests: nsqdtr74hyu1 -> nsqdtr74hyu0
PaulusParssinen May 2, 2024
97b4454
Exponent is 1023, not 0
PaulusParssinen May 2, 2024
463785b
Add USE_PDEP_PEXT switch for PDEP/PEXT Z-curve en/decode
PaulusParssinen May 3, 2024
0fa699a
Use FusedMultiplyAdd to do (x+y)*z in one op to avoid intermediate ro…
PaulusParssinen May 3, 2024
7bc0fba
Remove #define USE_PDEP_PEXT, tests passed
PaulusParssinen May 3, 2024
e0384e6
Move GeoHash specific unit tests own file
PaulusParssinen May 3, 2024
dc4632f
Mark Z-curve encode/decode with MethodImpl.AI
PaulusParssinen May 3, 2024
ef64efb
Add GeoHash specific benchmarks
PaulusParssinen May 3, 2024
61faa37
oops
PaulusParssinen May 3, 2024
10397c6
format
PaulusParssinen May 3, 2024
6ce1d36
Add UsePdepPext build switch.
PaulusParssinen May 3, 2024
3a0c08b
Avoid shifting by using already shifted mask for the PDEP/PEXT
PaulusParssinen May 3, 2024
cc86c31
Use AVX512 support to guard PDEP/PEXT usage.
PaulusParssinen May 3, 2024
6336e08
Add MemoryDiagnoser back
PaulusParssinen May 3, 2024
8ecc8b9
Fix incorrect quantization approach
PaulusParssinen May 7, 2024
6dd973a
Make it little bit more clear what happens in the corner-case guard
PaulusParssinen May 8, 2024
b35045c
Remove not needed pow2 trick
PaulusParssinen May 8, 2024
8f6a3e6
Let JIT do its thing, it's pretty good
PaulusParssinen May 8, 2024
0e70b77
Adjust comments a bit
PaulusParssinen May 8, 2024
ddf3263
Fix comment typos
PaulusParssinen May 8, 2024
a1897d3
Adjust comments
PaulusParssinen May 10, 2024
e50cdb0
Adjust comments
PaulusParssinen May 10, 2024
3383ef0
Add third-party notices to NOTICE.md
PaulusParssinen May 13, 2024
3f39acc
Merge branch 'main' into opt-geohash
yrajas May 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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