r/Python • • Mar 09 '26

Showcase Fast Hilbert curves in Python (Numba): ~1.8 ns/point, 3–4 orders faster than existing PyPI packages

What My Project Does

While building a query engine for spatial data in Python, I needed a way to serialize the data (2D/3D → 1D) while preserving spatial locality so it can be indexed efficiently. I chose Hilbert space-filling curves, since they generally preserve locality better than Z-order (Morton) curves. The downside is that Hilbert mappings are more involved algorithmically and usually more expensive to compute.

So I built HilbertSFC, a high-throughput Hilbert encoder/decoder fully in Python using numba, optimized for kernel structure and compiler friendliness. It achieves:

  • ~1.8 ns/pt (~8 CPU cycles) for 2D encode/decode (32-bit)
  • ~500M–4B points/sec single-threaded depending on number of bits/dtype
  • Multi-threaded throughput saturates memory-bandwidth. It can’t get faster than reading coordinates and writing indices
  • 3–4 orders of magnitude faster than existing Python packages
  • ~6× faster than the Rust crate fast_hilbert

Target Audience

HilbertSFC is aimed at Python developers and engineers who need: 1. A high-performance hilbert encoder/decoder for indexing or point cloud processing. 2. A pure-Python/Numba solution without requiring compiled extensions or external dependencies 3. A production-ready PyPI package

Application domains: scientific computing, GIS, spatial databases, or machine/deep learning.

Comparison

I benchmarked HilbertSFC against existing Python and Rust implementations:

2D Points - Random, nbits=32, n=5,000,000

Implementationns/pt (enc)ns/pt (dec)Mpts/s (enc)Mpts/s (dec)
hilbertsfc (multi-threaded)0.530.571883.521742.08
hilbertsfc (Python)1.841.88543.60532.77
fast_hilbert (Rust)12.2412.0381.6783.11
hilbert_2d (Rust)121.23101.348.259.87
hilbert-bytes (Python)2997.512642.860.3340.378
numpy-hilbert-curve (Python)7606.885075.080.1310.197
hilbertcurve (Python)14355.7610411.200.06970.0961

System: Intel Core Ultra 7 258v, Ubuntu 24.04.4, Python 3.12.12, Numba 0.63.

Full benchmark methodology: https://github.com/remcofl/HilbertSFC/blob/main/benchmark.md

Why HilbertSFC is faster than Rust implementations: The speedup is actually not due to language choice, as both Rust and Numba lower through LLVM. Instead, it comes from architectural optimizations, including:

  • Fixed-structure finite state machine
  • State-independent LUT indexing (L1-cache friendly)
  • Fully unrolled inner loops
  • Bit-plane tiling
  • Short dependency chains
  • Vectorization-friendly loops

In contrast, Rust implementations rely on state-dependent LUTs inside variable-bound loops with runtime bit skipping, limiting instruction-level parallelism and (aggressive) unrolling/vectorization.

Source Code

https://github.com/remcofl/HilbertSFC

Example Usage (2D data)

from hilbertsfc import hilbert_encode_2d, hilbert_decode_2d

index = hilbert_encode_2d(17, 23, nbits=10)  # index = 534
x, y = hilbert_decode_2d(index, nbits=10)    # x, y = (17, 23)
21 Upvotes

31 comments sorted by

View all comments

Show parent comment

-1

u/scrapheaper_ Mar 10 '26

Why would you bother using Python for computer graphics?

4

u/empathetic_asshole Mar 10 '26

Why would anyone bother engaging you in conversation?