From 04e6941500b880876c48638863056e360a57d282 Mon Sep 17 00:00:00 2001 From: Ezra Singh Date: Sat, 10 Aug 2024 14:02:58 -0400 Subject: [PATCH] Replaced haversine crate with haversine-rs --- Cargo.lock | 8 ++++---- geoprox-core/CHANGELOG.md | 2 +- geoprox-core/Cargo.toml | 2 +- geoprox-core/src/cache.rs | 2 +- geoprox-core/src/metric.rs | 13 ++++++++----- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99faf91..2b07430 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -946,7 +946,7 @@ dependencies = [ "criterion", "geohash", "hashbrown 0.14.5", - "haversine", + "haversine-rs", "itertools 0.13.0", "kiddo", "log", @@ -1046,10 +1046,10 @@ dependencies = [ ] [[package]] -name = "haversine" -version = "0.2.1" +name = "haversine-rs" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef9482608b217d89a3caeb5fe2336a0491d1dc0be5d89c67125ea514b1f6a660" +checksum = "a938f96298bab2f2315301ac84f6574c1f29a84d95956944cb4d380e6c0ff71e" [[package]] name = "heck" diff --git a/geoprox-core/CHANGELOG.md b/geoprox-core/CHANGELOG.md index 863daa6..b43746f 100644 --- a/geoprox-core/CHANGELOG.md +++ b/geoprox-core/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Marked `cache::build_search_space` as `inline` +- Replaced the [`haversine`](https://crates.io/crates/haversine) crate with [`haversine-rs`](https://crates.io/crates/haversine-rs) after noticing it performed better in benchmarks. ## 0.4.1 diff --git a/geoprox-core/Cargo.toml b/geoprox-core/Cargo.toml index 6afa65b..14218c8 100644 --- a/geoprox-core/Cargo.toml +++ b/geoprox-core/Cargo.toml @@ -14,7 +14,7 @@ license = "MIT OR Apache-2.0" ahash = "0.8.11" geohash = "0.13.1" hashbrown = { version = "0.14.5", features = ["serde", "rayon"] } -haversine = "0.2.1" +haversine-rs = "0.3.0" itertools = "0.13.0" kiddo = { git = "https://github.com/sdd/kiddo.git", version = "4.2.0", rev = "cc62c6d76accdeba75c0f55db80c28eab8a21ae7", features = [ "serialize", diff --git a/geoprox-core/src/cache.rs b/geoprox-core/src/cache.rs index 9583301..537c971 100644 --- a/geoprox-core/src/cache.rs +++ b/geoprox-core/src/cache.rs @@ -23,7 +23,7 @@ fn build_search_space( for (ghash, members) in prefix_tree.iter_prefix(subregion_hash) { if let Ok((position, _, _)) = geohash::decode(&ghash) { - members.iter().for_each(|id: &ObjectIdentifier| { + members.into_iter().for_each(|id: &ObjectIdentifier| { debug!("adding object to kd-tree: id={} geohash={}", id, ghash); // ? geohash position uses (lng, lat) search_tree.add(&[position.y, position.x], *id); diff --git a/geoprox-core/src/metric.rs b/geoprox-core/src/metric.rs index f847733..f81df8b 100644 --- a/geoprox-core/src/metric.rs +++ b/geoprox-core/src/metric.rs @@ -1,25 +1,28 @@ -use haversine::Units; +use haversine_rs::point::Point; +use haversine_rs::units::Unit; use kiddo::distance_metric::DistanceMetric; pub struct HaversineDistance; impl DistanceMetric for HaversineDistance { /// Computes the Haversine distance between two 2D points given latitude and longitude. + #[inline] fn dist(a: &[f64; 2], b: &[f64; 2]) -> f64 { - haversine::distance( - haversine::Location { + haversine_rs::distance( + Point { latitude: a[0], longitude: a[1], }, - haversine::Location { + Point { latitude: b[0], longitude: b[1], }, - Units::Kilometers, + Unit::Kilometers, ) } /// Computes the absolute difference between two values along a single axis (latitude or longitude). + #[inline] fn dist1(a: f64, b: f64) -> f64 { (a - b).abs() }