|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +//! Microbenchmark for the bounding-box pre-check used by constant-vs-column geometry predicates. |
| 5 | +//! |
| 6 | +//! `disjoint` rows are rejected by their bounding boxes, while `candidate` rows overlap the |
| 7 | +//! constant's box and therefore pay for both the pre-check and the exact predicate. Comparing the |
| 8 | +//! candidate cases tracks the worst-case overhead of the optimization. |
| 9 | +//! |
| 10 | +//! Run with `cargo bench -p vortex-geo --bench predicate_bbox`. |
| 11 | +
|
| 12 | +use std::f64::consts::TAU; |
| 13 | +use std::sync::LazyLock; |
| 14 | + |
| 15 | +use divan::Bencher; |
| 16 | +use divan::counter::ItemsCount; |
| 17 | +use geo::BoundingRect; |
| 18 | +use geo::Contains; |
| 19 | +use geo::Intersects; |
| 20 | +use geo_types::Geometry; |
| 21 | +use geo_types::LineString; |
| 22 | +use geo_types::Point; |
| 23 | +use geo_types::Polygon; |
| 24 | + |
| 25 | +fn main() { |
| 26 | + divan::main(); |
| 27 | +} |
| 28 | + |
| 29 | +const ROWS: usize = 1 << 13; |
| 30 | +const VERTICES: usize = 128; |
| 31 | + |
| 32 | +static QUERY: LazyLock<Geometry<f64>> = LazyLock::new(|| { |
| 33 | + let mut ring: Vec<_> = (0..VERTICES) |
| 34 | + .map(|i| { |
| 35 | + let theta = TAU * i as f64 / VERTICES as f64; |
| 36 | + (10.0 * theta.cos(), 10.0 * theta.sin()) |
| 37 | + }) |
| 38 | + .collect(); |
| 39 | + ring.push(ring[0]); |
| 40 | + Geometry::Polygon(Polygon::new(LineString::from(ring), vec![])) |
| 41 | +}); |
| 42 | + |
| 43 | +static DISJOINT: LazyLock<Vec<Geometry<f64>>> = LazyLock::new(|| { |
| 44 | + (0..ROWS) |
| 45 | + .map(|i| Geometry::Point(Point::new(100.0 + ordinate(i), 100.0 + ordinate(i + 1)))) |
| 46 | + .collect() |
| 47 | +}); |
| 48 | + |
| 49 | +static CANDIDATES: LazyLock<Vec<Geometry<f64>>> = LazyLock::new(|| { |
| 50 | + (0..ROWS) |
| 51 | + .map(|i| { |
| 52 | + Geometry::Point(Point::new( |
| 53 | + 20.0 * ordinate(i) - 10.0, |
| 54 | + 20.0 * ordinate(i + 1) - 10.0, |
| 55 | + )) |
| 56 | + }) |
| 57 | + .collect() |
| 58 | +}); |
| 59 | + |
| 60 | +/// Deterministic pseudo-random value in `[0, 1)`. |
| 61 | +fn ordinate(i: usize) -> f64 { |
| 62 | + (i.wrapping_mul(2_654_435_761) % 10_000) as f64 / 10_000.0 |
| 63 | +} |
| 64 | + |
| 65 | +fn exact_intersects(query: &Geometry<f64>, rows: &[Geometry<f64>]) -> usize { |
| 66 | + rows.iter().filter(|row| query.intersects(*row)).count() |
| 67 | +} |
| 68 | + |
| 69 | +fn bbox_intersects(query: &Geometry<f64>, rows: &[Geometry<f64>]) -> usize { |
| 70 | + let query_rect = query.bounding_rect(); |
| 71 | + rows.iter() |
| 72 | + .filter(|row| { |
| 73 | + let rejected = query_rect |
| 74 | + .zip(row.bounding_rect()) |
| 75 | + .is_some_and(|(query, row)| !query.intersects(&row)); |
| 76 | + !rejected && query.intersects(*row) |
| 77 | + }) |
| 78 | + .count() |
| 79 | +} |
| 80 | + |
| 81 | +fn exact_contains(query: &Geometry<f64>, rows: &[Geometry<f64>]) -> usize { |
| 82 | + rows.iter().filter(|row| query.contains(*row)).count() |
| 83 | +} |
| 84 | + |
| 85 | +fn bbox_contains(query: &Geometry<f64>, rows: &[Geometry<f64>]) -> usize { |
| 86 | + let query_rect = query.bounding_rect(); |
| 87 | + rows.iter() |
| 88 | + .filter(|row| { |
| 89 | + let rejected = query_rect |
| 90 | + .zip(row.bounding_rect()) |
| 91 | + .is_some_and(|(query, row)| !query.contains(&row)); |
| 92 | + !rejected && query.contains(*row) |
| 93 | + }) |
| 94 | + .count() |
| 95 | +} |
| 96 | + |
| 97 | +#[divan::bench] |
| 98 | +fn intersects_exact_disjoint(bencher: Bencher) { |
| 99 | + bencher |
| 100 | + .counter(ItemsCount::new(ROWS)) |
| 101 | + .with_inputs(|| (&*QUERY, &*DISJOINT)) |
| 102 | + .bench_refs(|(query, rows)| exact_intersects(query, rows)); |
| 103 | +} |
| 104 | + |
| 105 | +#[divan::bench] |
| 106 | +fn intersects_bbox_disjoint(bencher: Bencher) { |
| 107 | + bencher |
| 108 | + .counter(ItemsCount::new(ROWS)) |
| 109 | + .with_inputs(|| (&*QUERY, &*DISJOINT)) |
| 110 | + .bench_refs(|(query, rows)| bbox_intersects(query, rows)); |
| 111 | +} |
| 112 | + |
| 113 | +#[divan::bench] |
| 114 | +fn intersects_exact_candidate(bencher: Bencher) { |
| 115 | + bencher |
| 116 | + .counter(ItemsCount::new(ROWS)) |
| 117 | + .with_inputs(|| (&*QUERY, &*CANDIDATES)) |
| 118 | + .bench_refs(|(query, rows)| exact_intersects(query, rows)); |
| 119 | +} |
| 120 | + |
| 121 | +#[divan::bench] |
| 122 | +fn intersects_bbox_candidate(bencher: Bencher) { |
| 123 | + bencher |
| 124 | + .counter(ItemsCount::new(ROWS)) |
| 125 | + .with_inputs(|| (&*QUERY, &*CANDIDATES)) |
| 126 | + .bench_refs(|(query, rows)| bbox_intersects(query, rows)); |
| 127 | +} |
| 128 | + |
| 129 | +#[divan::bench] |
| 130 | +fn contains_exact_disjoint(bencher: Bencher) { |
| 131 | + bencher |
| 132 | + .counter(ItemsCount::new(ROWS)) |
| 133 | + .with_inputs(|| (&*QUERY, &*DISJOINT)) |
| 134 | + .bench_refs(|(query, rows)| exact_contains(query, rows)); |
| 135 | +} |
| 136 | + |
| 137 | +#[divan::bench] |
| 138 | +fn contains_bbox_disjoint(bencher: Bencher) { |
| 139 | + bencher |
| 140 | + .counter(ItemsCount::new(ROWS)) |
| 141 | + .with_inputs(|| (&*QUERY, &*DISJOINT)) |
| 142 | + .bench_refs(|(query, rows)| bbox_contains(query, rows)); |
| 143 | +} |
| 144 | + |
| 145 | +#[divan::bench] |
| 146 | +fn contains_exact_candidate(bencher: Bencher) { |
| 147 | + bencher |
| 148 | + .counter(ItemsCount::new(ROWS)) |
| 149 | + .with_inputs(|| (&*QUERY, &*CANDIDATES)) |
| 150 | + .bench_refs(|(query, rows)| exact_contains(query, rows)); |
| 151 | +} |
| 152 | + |
| 153 | +#[divan::bench] |
| 154 | +fn contains_bbox_candidate(bencher: Bencher) { |
| 155 | + bencher |
| 156 | + .counter(ItemsCount::new(ROWS)) |
| 157 | + .with_inputs(|| (&*QUERY, &*CANDIDATES)) |
| 158 | + .bench_refs(|(query, rows)| bbox_contains(query, rows)); |
| 159 | +} |
0 commit comments