Hare Meets GEOS: A Geometry Experiment Against Rust

Update, 30 November 2026: I added Mojo and Inko to the experiment. Both prevent memory errors without a garbage collector. The Mojo port is in Update: Mojo, the SIMD results are in SIMD in Mojo, and the Inko port is in Update: Inko. All numbers in this post are from one new run of all eight implementations.

This week I set up a small Arch Linux VM in OrbStack on my Mac and installed Hare, a small systems programming language with a simple compiler. Hello world built in seconds. Then the GIS person in me asked the obvious question: can Hare do real geometry work, and how fast is it compared with Rust? I wrote an experiment to find out.

The source is on GitHub: mraad/hare-geo.

This is an experiment, not a library. The numbers come from one virtual machine and one synthetic data set.

Port GEOS or bind it

GEOS is the C++ geometry engine under PostGIS, QGIS and Shapely. It has approximately 300,000 lines of C++. A full port to Hare is months of work, so I split the question in two:

  • Binding. Hare calls the GEOS C API (libgeos_c) for every operation. This part measures the cost of the binding.
  • Native port. I wrote five simple algorithms in plain Hare: WKB read, area, centroid, convex hull and point in polygon. This part measures the speed of Hare code.

For the Rust side, I used the geo crate from the georust project (pure Rust) and the geos crate, which binds the same GEOS library. The geos crate is the control. It runs the same engine as my Hare binding, so any difference between the two is the cost of the language and the binding.

ImplementationLanguageGeometry engine
rust-geoRustgeo 0.33, pure Rust
rust-geosRustgeos 11.3 crate → GEOS 3.15.1
hare-geosHaremy Hare binding → GEOS 3.15.1
hare-nativeHaremy native port, pure Hare
mojo-nativeMojomy native port, pure Mojo, bounds checks on (the default)
mojo-uncheckedMojothe same Mojo code, bounds checks off
mojo-simdMojoSIMD versions of area and point in polygon
inko-nativeInkomy native port, pure Inko, safe code only
Diagram. A data file feeds the benchmark programs. rust-geo calls the geo crate. rust-geos calls the geos crate, and hare-geos calls the Hare geos module. Both bindings call libgeos_c. hare-native calls the native Hare module. mojo-native calls the native Mojo module. inko-native calls the native Inko module.
All implementations read the same data. Two of them reach GEOS through a binding.

The Hare binding

Hare calls a C function through a declaration with the @symbol attribute and no body. I used the reentrant GEOS API, the _r functions, where each call takes a context handle:

export type context = opaque;
export type geom = opaque;

export @symbol("GEOS_init_r") fn init_r() nullable *context;
export @symbol("GEOSGeomFromWKB_buf_r") fn geom_from_wkb_buf_r(
	ctx: *context,
	wkb: *const u8,
	sz: size,
) nullable *geom;
export @symbol("GEOSArea_r") fn area_r(ctx: *context, g: *const geom, out: *f64) int;

A second file wraps each call. A NULL return or an exception code becomes a Hare error that carries the last GEOS message:

export type error = !str;

export fn area(ctx: *context, g: *const geom) (f64 | error) = {
	let a = 0.0;
	if (area_r(ctx, g, &a) == 0) return fail();
	return a;
};

GEOS sends its error messages to a callback. The callback type GEOSMessageHandler_r is not variadic, so a plain Hare function can be the callback. The full binding, with tests, is approximately 170 lines.

One trap cost me a few minutes. The first link failed with this error:

relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `environ@@GLIBC_2.17'
which may bind externally can not be used when making a shared object

The Arch Linux gcc makes position-independent executables by default. harec does not make position-independent code. The fix is one environment variable:

LDFLAGS=-no-pie hare build -R -lgeos_c -o bench-hare cmd/bench

The data

A small Rust generator makes the data from a fixed seed, so each run uses the same geometry. Each polygon is star-shaped with 64 vertices. The generator sorts random angles around a center, which gives a ring with no self-intersections. Neighbouring polygons overlap, so the intersection test always has work to do. There are 1,000,000 test points in total. Each polygon has its own points, at random positions in its bounding box. I ran two sizes: N = 10,000 and N = 100,000 polygons.

Four overlapping star-shaped blue polygons. Dark points are inside their polygon. Gray points are outside.
The first four polygons and their 100 test points each, from the N = 10,000 data set.

Each program runs each operation over all polygons. One run over all polygons is a pass. The method is the same for each implementation:

  1. Do 2 warm-up passes.
  2. Do 10 timed passes.
  3. Record the median time.
  4. Repeat for 3 rounds and take the median of the round medians.

Each pass also gives a checksum, for example the sum of all areas. The run script compares every checksum with the rust-geos checksum and stops if one is different. All checksums agree. All programs use one thread. Rust uses --release with fat LTO. Hare uses hare build -R. Mojo uses mojo build, which optimizes at level 3 by default. Inko uses inko build --release.

Results

Time in ms for one pass at N = 100,000 polygons. Lower is better.

Operationrust-georust-geoshare-geoshare-nativemojo-nativemojo-uncheckedmojo-simdinko-native
load (WKB)5.7872.672.283.38.748.73—58.6
area2.973.593.976.898.492.401.975.12
centroid7.6868.368.87.708.574.56—10.7
convex hull143407406956186159—382
point in polygon74.526626913499.955.141.0144
intersection235461306107—————
buffer 0.11178396379636—————
Dot plot on a log scale of time per pass for seven operations and eight implementations. The rust-geos and hare-geos markers sit at the same position for each operation. mojo-simd is fastest for area and point in polygon.
Time per pass at N = 100,000, log scale. The two GEOS bindings sit on top of each other.

What the numbers say about Hare

The Hare binding adds almost no cost. hare-geos and rust-geos are within 2% of each other for 6 of the 7 operations. The largest difference is area, at 10%. Area is the fastest GEOS call, approximately 36 ns, so the extra cost is approximately 4 ns per call. For the heavy operations, the time is in GEOS, and the calling language has no measurable effect.

Rust geo is faster than GEOS for most operations. This surprised me. geo is 2.6× faster for intersection and 2.8× faster for convex hull. GEOS is faster only for buffer, by 1.2×. GEOS is 9× slower for centroid because each call makes a new point geometry that the caller must then delete.

Native Hare is near Rust for simple loops. Centroid takes 1.00× the Rust time. Point in polygon takes 1.8×, and area takes 2.3×. These loops do only floating-point arithmetic on a slice. Hare uses the qbe compiler backend, which does less optimization than LLVM and makes no vector instructions.

Native Hare is slow for generic and byte-level code. Convex hull takes 6.7× the Rust time. Hare's sort::sort calls the compare function through a pointer for each comparison, and Rust inlines it. WKB load takes 14× the Rust time. Hare reads each f64 through calls to endian::legetu64 and math::f64frombits that qbe does not inline. Rust reads each f64 with one load instruction.

Two panels. Panel A: hare-geos time divided by rust-geos time is between 0.99 and 1.10 for all seven operations. Panel B: hare-native time divided by rust-geo time is 1.00 for centroid, 1.80 for point in polygon, 2.32 for area, 6.68 for convex hull and 14.4 for load.
A: the binding cost. B: native Hare against native Rust. 1× is the same speed.

One more result is worth a note. At N = 100,000, each polygon has only 10 test points. GEOS spends most of its point-in-polygon time to prepare each polygon, and its time goes from 113 ms at N = 10,000 to 266 ms at N = 100,000 for the same 1,000,000 points. My simple Hare crossing test beats it there, at 134 ms. A prepared geometry pays off only when you test many points against one polygon.

Update: Mojo

Hare is simple, but it does not prevent memory errors, and the qbe backend explains most of its slow results. So I went looking for a language that is in active development, has Python or C syntax, prevents memory errors like Rust does, and is less complex than Rust. I looked at V, Swift, Nim, Inko, Fil-C, Odin, C3 and a few research languages. Mojo was the only one that met all of those requirements:

  • The syntax is Python.
  • The compiler checks ownership. A value cannot be destroyed while a reference to it is still in use. There is no garbage collector.
  • It compiles through MLIR and LLVM. Rust also uses LLVM, so this time the comparison measures the language, not the compiler backend.
  • Mojo 1.0 shipped in August 2026 and the compiler is now open source. I used 1.2.0.

Inko is also on my list. Its single-ownership model looks interesting, and it is my next candidate. The full survey table is in the README.

I did not write a Mojo binding to GEOS. The Hare result already shows that a binding costs almost nothing, so a Mojo binding would only measure GEOS again. Instead, I ported the same five native algorithms to Mojo. The port has 108 lines of code. Here is the point type. It implements the Comparable trait, so the standard sort can inline the comparison, which is the thing Hare could not do:

@fieldwise_init
struct Point(Comparable, Copyable, ImplicitlyCopyable, Movable):
    var x: Float64
    var y: Float64

    # Lexicographic order, for the convex hull sort.
    def __lt__(self, other: Self) -> Bool:
        return self.x < other.x or (self.x == other.x and self.y < other.y)

I liked one design choice a lot. To read an f64 from WKB bytes, you need a pointer, and Mojo 1.2 gives every pointer operation the prefix unsafe_. The unsafe code is easy to find:

# The callers check the record length first, so each read is in bounds.
@always_inline
def getf64(b: Span[UInt8, _], i: Int) -> Float64:
    return b.unsafe_ptr().unsafe_offset(i).unsafe_bitcast[Float64]().unsafe_load[alignment=1]()

Install tip: the pixi installer is a curl | sh script. On the VM, the PyPI package worked fine:

python3 -m venv ~/mojo-env
~/mojo-env/bin/pip install mojo

The Mojo results

The first run had a surprise. Area in Mojo was 2.9× slower than Rust, and slower than Hare. The cause is bounds checks. Mojo checks each list index by default, and in loops that read p[i] to p[i + 3] the optimizer does not remove the checks. So I built the same code two times: once with the default checks, and once with -D ASSERT=none.

Operationmojo-native ÷ rust-geomojo-unchecked ÷ rust-geomojo-native ÷ GEOS
load (WKB)1.51×1.51×0.12×
area2.86×0.81×2.36×
centroid1.11×0.59×0.13×
convex hull1.30×1.11×0.46×
point in polygon1.34×0.74×0.38×
Four panels. Panel A: mojo-native time divided by rust-geo time is between 1.11 and 2.86. Panel B: mojo-unchecked time divided by rust-geo time is between 0.59 and 1.51. Panel C: mojo-native time divided by GEOS time is between 0.12 and 2.36. Panel D: mojo-simd time divided by rust-geo time is 0.66 for area and 0.55 for point in polygon.
A: safe Mojo against Rust geo. B: Mojo without bounds checks against Rust geo. C: safe Mojo against GEOS. D: SIMD Mojo against Rust geo. Below 1× means Mojo is faster.

Safe Mojo is close to Rust. With the default checks, Mojo takes 1.1× to 1.3× the Rust time for centroid, convex hull and point in polygon. Load takes 1.5×, and area is the outlier, at 2.9×.

Without bounds checks, Mojo beats Rust in the tight loops. Area, centroid and point in polygon take 0.59× to 0.81× the Rust time. But that build is not safe code, and Rust geo is safe without paying this cost, because its loops use iterators and the compiler removes the checks.

Why is unchecked Mojo faster than Rust? My first guess was the CPU target, because Mojo compiles for the host CPU by default. I rebuilt Rust with -C target-cpu=native, and nothing changed: area went from 2.85 ms to 2.86 ms. The real cause is in the geo source. Its area function shifts each coordinate by the first coordinate to reduce rounding errors, which adds two subtractions for each vertex. Its centroid and point-in-polygon functions also handle every geometry type and the boundary cases. My ports do less work for each vertex. So the Mojo lead does not show that Mojo makes faster code than Rust.

Can safe Mojo avoid the checks? I tried five safe forms of the area loop: a while loop, a for range with the length in a variable, element iteration with no index, a Span, and unsigned indices. All of them took 8.1 to 10.1 ms. Only p.unsafe_get(i) got down to 2.35 ms, the same as the unchecked build. The loop condition i + 3 < len(p) keeps each read in bounds, so a small unsafe_get loop is the practical method for hot loops in Mojo 1.2.

Safe Mojo beats GEOS for simple operations. It is faster for load (8.3×), centroid (8.0×), convex hull (2.2×) and point in polygon (2.7×). That is not a like-for-like comparison: GEOS makes geometry objects, checks for errors and supports every geometry type on each call.

Mojo fixes the Hare problems. Mojo's convex hull is 5.1× faster than Hare's, and its WKB load is 9.5× faster. LLVM, inlined generics and direct pointer reads make the difference.

SIMD in Mojo

Hare's qbe backend makes no vector instructions. Mojo has SIMD types in the language, so I wrote SIMD versions of the two tightest loops: area and point in polygon. Each step loads the start and end vertices of W edges, splits the x and y values into separate lanes with deinterleave(), and calculates all W edges at the same time:

while e + W <= edges:
    var a = p.unsafe_ptr().unsafe_offset(2 * e).unsafe_load[width=2 * W]().deinterleave()
    var b = p.unsafe_ptr().unsafe_offset(2 * e + 2).unsafe_load[width=2 * W]().deinterleave()
    acc += a[0] * b[1] - b[0] * a[1]
    e += W

The loop condition keeps each load inside the ring, and the scalar code does the remaining edges. For point in polygon, each step counts the crossings with a mask, and the point is inside when the total count is odd. The tests compare both SIMD functions with the scalar versions on a 37-vertex star, which uses the vector path and the scalar tail.

Operationmojo-simd (ms)÷ rust-geo÷ mojo-unchecked÷ GEOS
area1.970.66×0.82×0.55×
point in polygon41.00.55×0.75×0.15×

SIMD Mojo is the fastest of the eight implementations for both operations. The gain over the scalar code without bounds checks is only 1.2× to 1.3×. Each polygon has 64 edges, so the loop over polygons, the memory loads and the scalar tail take a large part of the time.

The best vector width surprised me. On Apple silicon, one NEON register holds two f64 values. I measured W = 2, 4, 8 and 16 edges per step. Area was fastest at W = 8 (2.01 ms), and point in polygon at W = 4 (42.0 ms). Wider steps let LLVM use more registers in parallel, until the polygon is too small to fill them. So each function uses its own width.

Update: Inko

Inko was the next candidate from my language survey. It has single ownership like Rust and no garbage collector. The compiler checks ownership and moves, but Inko counts borrows at run time instead of using a compile-time borrow checker. It compiles through LLVM. I ported the same five algorithms to Inko 0.21.1.

The install had one trap. Inko 0.21.1 asks for LLVM 18, and Arch Linux ARM has no LLVM 18 package, only 20 to 23. The inkwell crate under Inko supports LLVM 21, so I installed llvm21 and changed one line in the build file:

sed -i 's/"llvm18-1", "llvm18-1-prefer-static"/"llvm21-1", "llvm21-1-prefer-dynamic"/' compiler/Cargo.toml
LLVM_SYS_211_PREFIX=/usr/lib/llvm21 make PREFIX=$HOME/.local
make install PREFIX=$HOME/.local

The port is safe Inko only. Each array read is p.get(i).or_panic, which checks the bounds, and the public API has no unchecked read. Here is the point-in-polygon test:

fn pub contains(p: ref Array[Float], x: Float, y: Float) -> Bool {
  let mut n = 0
  let mut i = 0

  while i + 3 < p.size {
    let xa = p.get(i).or_panic
    let ya = p.get(i + 1).or_panic
    let xb = p.get(i + 2).or_panic
    let yb = p.get(i + 3).or_panic

    if (ya > y) != (yb > y) and x < (xa + ((y - ya) * (xb - xa) / (yb - ya))) {
      n += 1
    }

    i += 2
  }

  n % 2 == 1
}

Two Inko rules surprised me. First, Inko has no operator precedence: it evaluates binary operators from left to right, so b.size != 13 + (16 * n) means (b.size != 13) + (16 * n). The compiler caught it as a type error, and now every mixed expression has parentheses. Second, the run-time borrow check found a real bug in my test: I dropped a temporary array while an iterator still used it, and Inko stopped with can't drop a value of type 'Array' as it still has 1 reference(s).

Operationinko-native (ms)÷ rust-geo÷ mojo-native÷ hare-native÷ GEOS
load (WKB)58.610.1×6.70×0.70×0.81×
area5.121.72×0.60×0.74×1.42×
centroid10.71.39×1.25×1.39×0.16×
convex hull3822.67×2.05×0.40×0.94×
point in polygon1441.94×1.44×1.08×0.54×
Three panels. Panel A: inko-native time divided by rust-geo time is between 1.39 and 10.1. Panel B: inko-native time divided by mojo-native time is between 0.60 and 6.70. Panel C: inko-native time divided by GEOS time is between 0.16 and 1.42.
A: Inko against Rust geo. B: Inko against safe Mojo; both check bounds. C: Inko against GEOS. Below 1× means Inko is faster.

Safe Inko is between Hare and Mojo. The tight loops take 1.4× to 1.9× the Rust time. One result surprised me: the Inko area loop takes 0.60× the time of the safe Mojo loop, although both check every read. I did not examine the generated code, so I do not know why yet.

The sort and the byte reads are the slow parts. Convex hull takes 2.7× the Rust time, because the sort calls a closure for each comparison. That is still 2.5× faster than Hare's function-pointer sort. WKB load takes 10× the Rust time, because each value goes through a bounds-checked read_i64 call.

Inko beats GEOS for four of five operations. Only area is faster in GEOS.

Limits

The Hare native comparison measures the compiler as much as the language: qbe against LLVM. Mojo, Inko and Rust all use LLVM, and the CPU target made no difference. My Inko build uses LLVM 21 instead of the LLVM 18 it asks for. OrbStack cannot pin a process to one CPU, so I used medians to reduce the noise. The data is synthetic. Real data has holes, multipolygons and very different vertex counts, and the results can be different. Hare and Inko are not at version 1.0, and Mojo 1.2 is new, so all three languages and their standard libraries can still change.

My take: a Hare binding to GEOS is small and fast, and it is the right way to do geometry in Hare today. A larger native Hare port is not worth it. Mojo is a different story. Python syntax, compile-time memory safety, speed close to Rust and SIMD in the language make it a real option for native geometry kernels. Inko is fully safe and easy to read, but for this kind of number crunching it is 1.4× to 2.7× slower than Rust, and its byte reads are slow. For overlay, buffer and predicates, I still use GEOS or Rust geo. They have years of work on the hard edge cases.

Next

The experiment did not measure these, and they are on my list:

  • Spatial index joins: the GEOS STRtree against the Rust rstar crate.
  • Real data with holes and multipolygons.
  • Multithreaded passes.

The code, the raw results and the script that draws the figures are in mraad/hare-geo. The README has the full method and the steps to reproduce each number. More to come.

Comments

Popular posts from this blog

How to use Esri Flex API on Android and iPhone

Augmented Reality, iPhone and ArcGIS Server

Image Flare Symbol For Clustered Graphics