Skip to content

Commit 9f8c6b6

Browse files
authored
Add cached envelope benchmark (#137)
* Add cached envelope benchmark * circular_polygon no longer pub, avoid intermediate random poly collection * No need to map twice
1 parent 31a8407 commit 9f8c6b6

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

rstar-benches/benches/benchmarks.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use geo::{Coord, LineString, MapCoords, Polygon};
1212
use rand::{Rng, SeedableRng};
1313
use rand_hc::Hc128Rng;
1414

15+
use rstar::primitives::CachedEnvelope;
1516
use rstar::{RStarInsertionStrategy, RTree, RTreeParams};
1617

1718
use criterion::Criterion;
@@ -53,15 +54,30 @@ fn bulk_load_comparison(c: &mut Criterion) {
5354
}
5455

5556
fn bulk_load_complex_geom(c: &mut Criterion) {
56-
c.bench_function("Bulk load complex geom", move |b| {
57-
let polys: Vec<_> = create_random_polygons(DEFAULT_BENCHMARK_TREE_SIZE, 4096, SEED_1);
57+
c.bench_function("Bulk load complex geo-types geom", move |b| {
58+
let polys: Vec<_> =
59+
create_random_polygons(DEFAULT_BENCHMARK_TREE_SIZE, 4096, SEED_1).collect();
5860

5961
b.iter(|| {
6062
RTree::<Polygon<f64>, Params>::bulk_load_with_params(polys.clone());
6163
});
6264
});
6365
}
6466

67+
fn bulk_load_complex_geom_cached(c: &mut Criterion) {
68+
c.bench_function(
69+
"Bulk load complex geo-types geom with cached envelope",
70+
move |b| {
71+
let cached: Vec<_> = create_random_polygons(DEFAULT_BENCHMARK_TREE_SIZE, 4096, SEED_1)
72+
.map(CachedEnvelope::new)
73+
.collect();
74+
b.iter(|| {
75+
RTree::<CachedEnvelope<_>, Params>::bulk_load_with_params(cached.clone());
76+
});
77+
},
78+
);
79+
}
80+
6581
fn tree_creation_quality(c: &mut Criterion) {
6682
const SIZE: usize = 100_000;
6783
let points: Vec<_> = create_random_points(SIZE, SEED_1);
@@ -112,6 +128,7 @@ criterion_group!(
112128
bulk_load_baseline,
113129
bulk_load_comparison,
114130
bulk_load_complex_geom,
131+
bulk_load_complex_geom_cached,
115132
tree_creation_quality,
116133
locate_successful,
117134
locate_unsuccessful
@@ -123,23 +140,25 @@ fn create_random_points(num_points: usize, seed: &[u8; 32]) -> Vec<[f64; 2]> {
123140
(0..num_points).map(|_| rng.gen()).collect()
124141
}
125142

126-
fn create_random_polygons(num_points: usize, size: usize, seed: &[u8; 32]) -> Vec<Polygon<f64>> {
143+
fn create_random_polygons(
144+
num_points: usize,
145+
size: usize,
146+
seed: &[u8; 32],
147+
) -> impl Iterator<Item = Polygon<f64>> {
127148
let mut rng = Hc128Rng::from_seed(*seed);
128149
let base_polygon = circular_polygon(size);
129150

130-
(0..num_points)
131-
.map(|_| {
132-
let [scale_x, scale_y]: [f64; 2] = rng.gen();
133-
let [shift_x, shift_y]: [f64; 2] = rng.gen();
134-
base_polygon.clone().map_coords(|c| Coord {
135-
x: (shift_x + c.x) * scale_x,
136-
y: (shift_y + c.y) * scale_y,
137-
})
151+
(0..num_points).map(move |_| {
152+
let [scale_x, scale_y]: [f64; 2] = rng.gen();
153+
let [shift_x, shift_y]: [f64; 2] = rng.gen();
154+
base_polygon.clone().map_coords(|c| Coord {
155+
x: (shift_x + c.x) * scale_x,
156+
y: (shift_y + c.y) * scale_y,
138157
})
139-
.collect()
158+
})
140159
}
141160

142-
pub fn circular_polygon(steps: usize) -> Polygon<f64> {
161+
fn circular_polygon(steps: usize) -> Polygon<f64> {
143162
let delta = 2. * PI / steps as f64;
144163
let r = 1.0;
145164

rstar/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Added
44
- Add optional support for the [mint](https://docs.rs/mint/0.5.9/mint/index.html) crate
5+
- Added cached envelope bulk load benchmark
56

67
## Changed
78
- Fixed a stack overflow error in `DrainIterator::next`

0 commit comments

Comments
 (0)