Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions vortex-geo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,9 @@ harness = false
name = "length"
harness = false

[[bench]]
name = "area"
harness = false

[lints]
workspace = true
136 changes: 136 additions & 0 deletions vortex-geo/benches/area.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Microbenchmarks for native `ST_Area` over polygons and multipolygons.
//!
//! The cases separate the costs of vertex traversal, interior rings, nested polygons, and strict
//! null propagation. They execute through the scalar function and materialize the `f64` result.
//!
//! Run with `cargo bench -p vortex-geo --bench area`.

#![expect(clippy::unwrap_used)]

use std::sync::LazyLock;

use divan::Bencher;
use divan::counter::ItemsCount;
use mimalloc::MiMalloc;
use vortex_array::ArrayRef;
use vortex_array::Canonical;
use vortex_array::ExecutionCtx;
use vortex_array::IntoArray;
use vortex_array::VortexSessionExecute;
use vortex_array::arrays::MaskedArray;
use vortex_array::validity::Validity;
use vortex_geo::scalar_fn::area::GeoArea;
use vortex_geo::test_harness::geo_session;
use vortex_geo::test_harness::multipolygon_column;
use vortex_geo::test_harness::polygon_column;
use vortex_session::VortexSession;

// Scalar function execution allocates its output inside the timed region, so use the vendored
// allocator instead of measuring glibc differences between CodSpeed runner images.
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

static SESSION: LazyLock<VortexSession> = LazyLock::new(geo_session);

const ROWS: usize = 512;

fn main() {
divan::main();
}

/// A closed square ring centered at `(cx, cy)`.
fn square(cx: f64, cy: f64, radius: f64) -> Vec<(f64, f64)> {
vec![
(cx - radius, cy - radius),
(cx + radius, cy - radius),
(cx + radius, cy + radius),
(cx - radius, cy + radius),
(cx - radius, cy - radius),
]
}

fn simple_polygons() -> ArrayRef {
polygon_column(
(0..ROWS)
.map(|row| vec![square(row as f64, row as f64, 10.0)])
.collect(),
)
.unwrap()
}

fn polygons_with_holes() -> ArrayRef {
polygon_column(
(0..ROWS)
.map(|row| {
let center = row as f64;
vec![
square(center, center, 10.0),
square(center - 4.0, center, 1.0),
square(center + 4.0, center, 1.0),
]
})
.collect(),
)
.unwrap()
}

fn multipolygons() -> ArrayRef {
multipolygon_column(
(0..ROWS)
.map(|row| {
let center = row as f64;
vec![
vec![square(center - 12.0, center, 5.0)],
vec![square(center, center, 5.0)],
vec![square(center + 12.0, center, 5.0)],
]
})
.collect(),
)
.unwrap()
}

fn areas(geometry: &ArrayRef, ctx: &mut ExecutionCtx) -> ArrayRef {
GeoArea::try_new_array(geometry.clone())
.unwrap()
.into_array()
.execute::<Canonical>(ctx)
.unwrap()
.into_array()
}

fn bench_area(bencher: Bencher, geometry: ArrayRef) {
let mut ctx = SESSION.create_execution_ctx();
bencher
.counter(ItemsCount::new(ROWS))
.bench_local(|| areas(&geometry, &mut ctx));
}

#[divan::bench]
fn simple_polygon(bencher: Bencher) {
bench_area(bencher, simple_polygons());
}

#[divan::bench]
fn polygon_with_holes(bencher: Bencher) {
bench_area(bencher, polygons_with_holes());
}

#[divan::bench]
fn multipolygon(bencher: Bencher) {
bench_area(bencher, multipolygons());
}

#[divan::bench]
fn nullable_polygon(bencher: Bencher) {
let geometry = MaskedArray::try_new(
simple_polygons(),
Validity::from_iter((0..ROWS).map(|row| !row.is_multiple_of(8))),
)
.unwrap()
.into_array();
bench_area(bencher, geometry);
}
2 changes: 2 additions & 0 deletions vortex-geo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::extension::Rect;
use crate::extension::WellKnownBinary;
use crate::prune::GeoDistancePrune;
use crate::prune::GeoIntersectsPrune;
use crate::scalar_fn::area::GeoArea;
use crate::scalar_fn::contains::GeoContains;
use crate::scalar_fn::distance::GeoDistance;
use crate::scalar_fn::envelope::GeoEnvelope;
Expand Down Expand Up @@ -66,6 +67,7 @@ pub fn initialize(session: &VortexSession) {
session.arrow().register_importer(Arc::new(Rect));

// Register the geometry scalar functions.
session.scalar_fns().register(GeoArea);
session.scalar_fns().register(GeoEnvelope);
session.scalar_fns().register(GeoContains);
session.scalar_fns().register(GeoDistance);
Expand Down
Loading
Loading