You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "jesspav (via GitHub)" <gi...@apache.org> on 2025/12/12 20:15:33 UTC
[PR] Improve perf of CRS [sedona-db]
jesspav opened a new pull request, #446:
URL: https://github.com/apache/sedona-db/pull/446
In flamegraphs for PR #430 revealed that the CRS serialization had some opportunities for improvement that would be essential for raster/geo per item CRS functions. This was going to be especially important for CRS comparisons in joins.
Added a simple benchmarks for lnglat and auth code equality that was run repeatedly with different performance optimizations. At the start it took 61ms for auth codes and 37ms for lnglat. By the end auth codes have improved by nearly 20x and lnglat by over 10x, with both running in 3.4ms.
There are basically two changes: an LRU cache and avoiding unnecessary string allocations.
Breakdown of performance changes:
```
Starting point:
Running benches/crs_benchmarks.rs (target/release/deps/crs_benchmarks-c62a15af9667b709)
equality_lnglat_crs time: [37.442 ms 37.712 ms 37.960 ms]
change: [+2.5665% +4.0155% +5.4781%] (p = 0.00 < 0.05)
Performance has regressed.
Found 15 outliers among 100 measurements (15.00%)
6 (6.00%) low severe
9 (9.00%) low mild
Benchmarking equality_different_crs: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 6.3s, or reduce sample count to 70.
equality_different_crs time: [61.892 ms 62.119 ms 62.362 ms]
change: [+2.1679% +2.6956% +3.2470%] (p = 0.00 < 0.05)
Performance has regressed.
Found 7 outliers among 100 measurements (7.00%)
1 (1.00%) low mild
1 (1.00%) high mild
5 (5.00%) high severe
After cache:
equality_lnglat_crs time: [28.265 ms 28.361 ms 28.461 ms]
change: [−25.338% −24.795% −24.189%] (p = 0.00 < 0.05)
Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high mild
equality_different_crs time: [27.473 ms 27.530 ms 27.586 ms]
change: [−55.878% −55.683% −55.494%] (p = 0.00 < 0.05)
Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high mild
Time after cache + switch value to str + + colon rather than split
equality_lnglat_crs time: [11.324 ms 11.354 ms 11.385 ms]
change: [−60.148% −59.967% −59.798%] (p = 0.00 < 0.05)
Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) low mild
1 (1.00%) high mild
equality_different_crs time: [11.655 ms 11.701 ms 11.746 ms]
change: [−57.674% −57.497% −57.308%] (p = 0.00 < 0.05)
Performance has improved.
Time after cache + switch value to str + colon rather than split + pre-alloc instead of format for eq:
Benchmarking equality_lnglat_crs: Collecting 100 samples in estimated 5.4946 s (800 iterat
equality_lnglat_crs time: [6.6983 ms 6.7258 ms 6.7557 ms]
change: [+0.7120% +1.2645% +1.8552%] (p = 0.00 < 0.05)
Change within noise threshold.
Found 5 outliers among 100 measurements (5.00%)
1 (1.00%) high mild
4 (4.00%) high severe
Benchmarking equality_different_crs: Collecting 100 samples in estimated 5.4518 s (800 ite
equality_different_crs time: [6.7116 ms 6.7429 ms 6.7758 ms]
change: [+0.7410% +1.5266% +2.2864%] (p = 0.00 < 0.05)
Change within noise threshold.
Found 4 outliers among 100 measurements (4.00%)
4 (4.00%) high mild
All of the above + not splitting up the auth code:
equality_lnglat_crs time: [3.3993 ms 3.4098 ms 3.4200 ms]
change: [−49.633% −49.425% −49.229%] (p = 0.00 < 0.05)
Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
3 (3.00%) low mild
equality_different_crs time: [3.4080 ms 3.4170 ms 3.4258 ms]
change: [−49.183% −48.996% −48.819%] (p = 0.00 < 0.05)
Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
1 (1.00%) low mild
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] perf(rust/sedona-schema): Improve performance of `deserialize_crs()` [sedona-db]
Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot merged PR #446:
URL: https://github.com/apache/sedona-db/pull/446
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve perf of CRS [sedona-db]
Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #446:
URL: https://github.com/apache/sedona-db/pull/446#discussion_r2615770610
##########
rust/sedona-schema/src/crs.rs:
##########
@@ -15,41 +15,61 @@
// specific language governing permissions and limitations
// under the License.
use datafusion_common::{DataFusionError, Result};
+use lru::LruCache;
use std::fmt::{Debug, Display};
+use std::num::NonZeroUsize;
use std::str::FromStr;
use std::sync::Arc;
+use std::sync::Mutex;
+use std::sync::OnceLock;
use serde_json::Value;
+/// LRU cache for CRS deserialization
+static CRS_CACHE: OnceLock<Mutex<LruCache<String, Crs>>> = OnceLock::new();
+
+fn get_crs_cache() -> &'static Mutex<LruCache<String, Crs>> {
+ CRS_CACHE.get_or_init(|| {
+ // Cache up to 256 CRS strings
+ Mutex::new(LruCache::new(NonZeroUsize::new(256).unwrap()))
+ })
+}
Review Comment:
Can/should this be thread local to avoid contention when CRS computations are happening on many partitions at once?
##########
rust/sedona-schema/src/datatypes.rs:
##########
@@ -312,7 +312,13 @@ fn deserialize_edges_and_crs(value: &Option<String>) -> Result<(Edges, Crs)> {
};
let crs = match json_value.get("crs") {
- Some(crs_value) => deserialize_crs(crs_value)?,
+ Some(crs_value) => {
+ if let Some(s) = crs_value.as_str() {
+ deserialize_crs(s)?
+ } else {
+ deserialize_crs(&crs_value.to_string())?
Review Comment:
The original reason for keeping the `Value` here was to avoid the re-serialization. We don't benchmark query planning but this code path gets called every time we take a `FieldRef` from DataFusion and convert it to a `SedonaType` (at least once per batch when calling our scalar functions).
Maybe:
```rust
match &crs_value {
Value::Object(obj) => // convert using the old path,
Value::String(s) => deserialize_crs(s)?,
Value::Null => None
}
```
...would improve the current state of this PR (which is FieldRef metadata -> serde_json Value -> string -> serde_json Value for PROJJSON crses).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve perf of CRS [sedona-db]
Posted by "paleolimbot (via GitHub)" <gi...@apache.org>.
paleolimbot commented on code in PR #446:
URL: https://github.com/apache/sedona-db/pull/446#discussion_r2619838646
##########
rust/sedona-schema/src/datatypes.rs:
##########
@@ -312,8 +312,13 @@ fn deserialize_edges_and_crs(value: &Option<String>) -> Result<(Edges, Crs)> {
};
let crs = match json_value.get("crs") {
- Some(crs_value) => deserialize_crs(crs_value)?,
- None => Crs::None,
+ Some(crs_value) => match &crs_value {
+ Value::Object(_obj) => deserialize_crs_from_obj(crs_value)?,
+ Value::String(s) => deserialize_crs(s)?,
+ Value::Number(s) => deserialize_crs(&s.to_string())?,
+ _ => None,
Review Comment:
This case should probably error (I think it would be the case where CRS is a JSON list)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve perf of CRS [sedona-db]
Posted by "jesspav (via GitHub)" <gi...@apache.org>.
jesspav commented on code in PR #446:
URL: https://github.com/apache/sedona-db/pull/446#discussion_r2615655633
##########
c/sedona-proj/src/sd_order_lnglat.rs:
##########
@@ -190,8 +189,7 @@ mod test {
assert_eq!(&result, &expected);
// Check the "not already lnglat" case
- let crs =
- sedona_schema::crs::deserialize_crs(&Value::String("EPSG:3857".to_string())).unwrap();
Review Comment:
@paleolimbot These string allocations were clearly in the flame graph, and all tests continued to pass without them. What do you think about this change?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve perf of CRS [sedona-db]
Posted by "jesspav (via GitHub)" <gi...@apache.org>.
jesspav commented on code in PR #446:
URL: https://github.com/apache/sedona-db/pull/446#discussion_r2617252123
##########
rust/sedona-schema/src/crs.rs:
##########
@@ -15,41 +15,61 @@
// specific language governing permissions and limitations
// under the License.
use datafusion_common::{DataFusionError, Result};
+use lru::LruCache;
use std::fmt::{Debug, Display};
+use std::num::NonZeroUsize;
use std::str::FromStr;
use std::sync::Arc;
+use std::sync::Mutex;
+use std::sync::OnceLock;
use serde_json::Value;
+/// LRU cache for CRS deserialization
+static CRS_CACHE: OnceLock<Mutex<LruCache<String, Crs>>> = OnceLock::new();
+
+fn get_crs_cache() -> &'static Mutex<LruCache<String, Crs>> {
+ CRS_CACHE.get_or_init(|| {
+ // Cache up to 256 CRS strings
+ Mutex::new(LruCache::new(NonZeroUsize::new(256).unwrap()))
+ })
+}
Review Comment:
I made it 50 for now. I think this should be configurable given the perf implications. I'll can add an issue so we don't forget.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve perf of CRS [sedona-db]
Posted by "jesspav (via GitHub)" <gi...@apache.org>.
jesspav commented on code in PR #446:
URL: https://github.com/apache/sedona-db/pull/446#discussion_r2617190761
##########
rust/sedona-schema/src/crs.rs:
##########
@@ -15,41 +15,61 @@
// specific language governing permissions and limitations
// under the License.
use datafusion_common::{DataFusionError, Result};
+use lru::LruCache;
use std::fmt::{Debug, Display};
+use std::num::NonZeroUsize;
use std::str::FromStr;
use std::sync::Arc;
+use std::sync::Mutex;
+use std::sync::OnceLock;
use serde_json::Value;
+/// LRU cache for CRS deserialization
+static CRS_CACHE: OnceLock<Mutex<LruCache<String, Crs>>> = OnceLock::new();
+
+fn get_crs_cache() -> &'static Mutex<LruCache<String, Crs>> {
+ CRS_CACHE.get_or_init(|| {
+ // Cache up to 256 CRS strings
+ Mutex::new(LruCache::new(NonZeroUsize::new(256).unwrap()))
+ })
+}
Review Comment:
Yes!!! That should have an impact on the perf of a single thread, too, since it avoids the lock.
any opinion on the magic number for size of the cache? this could get very large if all the keys are json!!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve perf of CRS [sedona-db]
Posted by "jesspav (via GitHub)" <gi...@apache.org>.
jesspav commented on code in PR #446:
URL: https://github.com/apache/sedona-db/pull/446#discussion_r2617111639
##########
rust/sedona-schema/src/datatypes.rs:
##########
@@ -312,7 +312,13 @@ fn deserialize_edges_and_crs(value: &Option<String>) -> Result<(Edges, Crs)> {
};
let crs = match json_value.get("crs") {
- Some(crs_value) => deserialize_crs(crs_value)?,
+ Some(crs_value) => {
+ if let Some(s) = crs_value.as_str() {
+ deserialize_crs(s)?
+ } else {
+ deserialize_crs(&crs_value.to_string())?
Review Comment:
makes sense. I'll switch it back
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve perf of CRS [sedona-db]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #446:
URL: https://github.com/apache/sedona-db/pull/446#discussion_r2615472627
##########
rust/sedona-schema/src/crs.rs:
##########
@@ -170,43 +185,33 @@ impl LngLat {
/// Implementation of an authority:code CoordinateReferenceSystem
#[derive(Debug)]
struct AuthorityCode {
- authority: String,
- code: String,
+ auth_code: String,
}
/// Implementation of an authority:code
impl AuthorityCode {
/// Create a Crs from an authority:code string
/// Example: "EPSG:4269"
- pub fn crs(auth_code: String) -> Crs {
- let (authority, code) = Self::split_auth_code(&auth_code).unwrap();
- Crs::Some(Arc::new(AuthorityCode { authority, code }))
+ pub fn crs(auth_code: &str) -> Crs {
+ let ac = if Self::validate_epsg_code(auth_code) {
+ format!("EPSG:{}", auth_code)
+ } else {
+ auth_code.to_string()
+ };
+ Some(Arc::new(AuthorityCode { auth_code: ac }))
}
/// Check if a Value is an authority:code string
/// Note: this can be expanded to more types in the future
- pub fn is_authority_code(value: &Value) -> bool {
- if let Some(string_value) = value.as_str() {
- match Self::split_auth_code(string_value) {
- Some((authority, code)) => {
- return Self::validate_authority(&authority) && Self::validate_code(&code)
- }
- None => return false,
- }
- }
-
- false
- }
-
- /// Split an authority:code string into its components
- fn split_auth_code(auth_code: &str) -> Option<(String, String)> {
- let parts: Vec<&str> = auth_code.split(':').collect();
- if parts.len() == 2 {
- Some((parts[0].to_string(), parts[1].to_string()))
- } else if parts.len() == 1 && Self::validate_epsg_code(auth_code) {
- Some(("EPSG".to_string(), auth_code.to_string()))
+ pub fn is_authority_code(auth_code: &str) -> bool {
+ // Expecting <authority>:<code> format
+ if let Some(colon_pos) = auth_code.find(':') {
+ let authority = auth_code[..colon_pos].to_string();
+ let code = auth_code[colon_pos + 1..].to_string();
+ Self::validate_authority(&authority) && Self::validate_code(&code)
Review Comment:
These string allocations are unnecessary for validation. Since `validate_authority` and `validate_code` take `&str`, pass string slices directly: `&auth_code[..colon_pos]` and `&auth_code[colon_pos + 1..]`.
```suggestion
let authority = &auth_code[..colon_pos];
let code = &auth_code[colon_pos + 1..];
Self::validate_authority(authority) && Self::validate_code(code)
```
##########
rust/sedona-schema/src/datatypes.rs:
##########
@@ -312,8 +312,14 @@ fn deserialize_edges_and_crs(value: &Option<String>) -> Result<(Edges, Crs)> {
};
let crs = match json_value.get("crs") {
- Some(crs_value) => deserialize_crs(crs_value)?,
- None => Crs::None,
+ Some(crs_value) => {
+ if let Some(s) = crs_value.as_str() {
+ deserialize_crs(s)?
+ } else {
+ deserialize_crs(&crs_value.to_string())?
+ }
+ }
+ None => None,
Review Comment:
This should return `Crs::None` instead of `None`. The function returns `Result<(Edges, Crs)>`, and `Crs` is a type alias for `Option<Arc<dyn CoordinateReferenceSystem + Send + Sync>>`, so this creates a nested Option. The original code correctly used `Crs::None`.
```suggestion
None => Crs::None,
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@sedona.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org