Rust acceleration engine
The Rust acceleration engine is the native compute and drawing core for xy. It keeps large row scans, screen-sized geometry work, tile reuse, and native image export out of slow Python loops while Python keeps chart policy and user-facing decisions. The engine runs in the Python process through a stable C interface and returns bounded buffers or export bytes.
System boundary
Native engine boundary
The native engine is a compiled Rust library loaded by the Python package. Python passes views of its data to the engine, and the engine writes results into caller-owned buffers or returns a small status value. This boundary gives all supported Python versions one native library per platform rather than one build for each Python version. A missing library or a version mismatch stops import with a clear error; the product does not silently switch to a slower path. References: src/lib.rs• L1-19, src/lib.rs• L83-91, python/…/_native.py• L1-10, python/…/_native.py• L72-83
Work ownership
The engine owns work that grows with the number of data rows or visible pixels. Python owns data cleanup, scale and tier choice, channel policy, scene assembly, validation, and widget transport. This split lets Python choose what should be drawn while Rust performs the repeated work needed to produce that choice. References: spec/…/rust-engine.md• L10-22, spec/…/rust-engine.md• L319-326, python/…/kernels.py• L1-27
The main path from chart data to a displayed or exported result is shown below.
flowchart LR
A[Python data and chart policy] --> B[Native row scans and geometry]
B --> C[Screen-bounded buffers]
C --> D[WebGL or SVG output]
C --> E[Native RGBA raster]
E --> F[PNG bytes]
Accelerated data processing
Compute kernels
The compute kernels turn large columns into compact data for interaction and drawing. They cover column summaries, relative float encoding, line reduction, range and validity selection, binning, sampling, category encoding, density and histogram work, signal transforms, statistics, and geometry such as triangles, contours, streamlines, and vector segments. Python still decides the view, labels, style, and level of detail. References: src/kernels.rs• L1-12, python/…/kernels.py• L29-92, spec/…/rust-engine.md• L24-38
The engine uses relative float encoding when it sends numeric geometry toward the GPU. It keeps a local offset and scale so data with large absolute values and small changes, such as time or finance data, keeps the useful digits. Pixel-aware reduction keeps the first, minimum, maximum, and last value in each screen bucket so a line can stay faithful without sending every row. References: src/kernels.rs• L3-12, src/kernels.rs• L870-881, src/kernels.rs• L2482
SIMD acceleration
SIMD, or single-instruction multiple-data processing, speeds selected exact scans on CPUs that support AVX2. The engine uses it for work such as min/max, bin cell selection, local density gathering, and range scans when measured results show a gain. It keeps floating-point sums on the scalar path when a different operation order could change the result. A runtime check selects the fast path, and XY_SIMD=0 provides a scalar path for diagnosis. References: src/simd.rs• L1-30, src/simd.rs• L47-60, src/simd.rs• L301-317
Multi-resolution tiles
The tile system builds a multi-resolution count view for a trace once and reuses it across viewport changes. A viewport can then compose a grid from the level that matches its screen size instead of scanning all source rows again. Colored density views keep a matching mean-color plane, so count and color remain aligned. If a view asks for more detail than the pyramid can provide, the system refuses the approximation and lets the caller use an exact re-bin. References: src/tiles.rs• L1-21, src/tiles.rs• L28-43, src/tiles.rs• L315-355
Tile state also supports count queries and in-place append for count-only pyramids. An append must stay inside the original domain and is checked before it changes the pyramid. Colored pyramids reject this short path because new color data is required; the caller can invalidate the old result and rebuild it later. References: src/tiles.rs• L175-216, src/tiles.rs• L297-313, src/tiles.rs• L594-620
Native output support
Raster output
The native rasterizer turns a Python-built display list into an RGBA image. The display list describes chart marks in screen space, while Rust handles clipping, anti-aliasing, fills, strokes, points, segments, triangles, images, density and heatmap layers, and text. The same path can return a framebuffer or encode PNG bytes directly, which avoids a full image round trip through Python. For the mark-level view, see Native rendering primitives. References: src/raster.rs• L1-15, src/raster.rs• L17-38, src/raster.rs• L3085-3145, python/…/_raster.py• L1-3
The rasterizer also supports a shared polar clip for marks that occupy an annular sector. This keeps fills, strokes, symbols, and images inside the same chart boundary instead of giving each primitive a different clip rule. References: src/raster.rs• L40-45, src/raster.rs• L156-177
SVG output
SVG scene construction remains a Python responsibility, but the native engine can serialize high-cardinality screen-space polylines. This removes one Python string allocation per point while keeping scale, scene, and export policy in the SVG layer. Invalid or non-finite coordinate pairs are rejected rather than written as broken path data. References: src/svg.rs• L1-5, src/svg.rs• L26-43, python/…/_native.py• L183-190
Font output
Native PNG text uses a generated grayscale DejaVu Sans atlas that the engine scales and blits during rasterization. The atlas covers common Latin text plus a bounded set of symbols and Greek characters. Unsupported characters use a visible replacement glyph, so missing coverage does not silently remove text; broader text coverage belongs to the browser export path. References: src/font.rs• L1-21, src/raster.rs• L1630-1667, spec/…/rust-engine.md• L147-189
Transition identity
Stable row identity
Transitions need a stable identity for each row so data can move between chart states instead of appearing as unrelated rows. The native transition encoder creates the same type-aware keys as Python for supported homogeneous fixed-width strings, bytes, booleans, integers, and floats. It reports duplicate keys, digest collisions, invalid layouts, and unsupported data as separate outcomes so Python can either use a reference path or show the right error. References: src/transition.rs• L1-7, src/transition.rs• L460-496, src/lib.rs• L94-165
Interface and safety model
C interface
The C interface exposes stateless calls for short-lived arrays and opaque handles for long-lived tile state. Existing symbols keep their meaning after release; new capabilities use new entry points, and the Python binding checks the ABI version before it calls them. Rust does not call back into Python. References: spec/…/rust-engine.md• L191-239, spec/…/rust-engine.md• L241-290, python/…/_native.py• L27-38
Error boundary
Native calls report bad input or internal failure through return values instead of allowing a Rust panic to cross into the Python process. Python translates those results into the public behavior and keeps policy decisions, warnings, and error messages at its layer. Output buffers are not valid when a call reports failure. References: src/lib.rs• L41-53, src/lib.rs• L219-229, spec/…/rust-engine.md• L219-229
Related knowledge
The child pages provide the next level of detail without changing this system view:
- Native rendering primitives explains the mark and shape operations that the raster path serves.
- Native kernels and Python bindings explains the compute interface and the Python-to-Rust call boundary.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the ABI version and every exported signature aligned between src/lib.rs and python/…/_native.py. A mismatch must fail at load time.
- Keep display-list opcodes and payload layouts aligned between src/raster.rs and python/…/_raster.py. A new command affects both the producer and the painter.
- Keep tile level choice, append invalidation, and exact re-bin fallback aligned between src/tiles.rs and the Python LOD and interaction paths.
- Keep transition key token rules aligned between src/transition.rs and python/…/components.py. A change can break row matching without changing the chart shape.
- Keep the native font atlas and the SVG font metrics in step. A glyph metric change can alter both text width and label placement.
- Add SIMD only when scalar and fast paths have identical results. Do not move order-sensitive floating-point reductions into a vector path.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Native library is missing or its ABI does not match the Python binding | Import fails instead of using an untracked slow path | Install a matching wheel or build the Rust library from the same source revision |
| A pyramid append contains a finite point outside its original domain | The append is rejected and the old pyramid remains unchanged | Invalidate and rebuild when the data domain grows |
| A colored pyramid receives an append without matching color data | The incremental update is refused | Rebuild the colored pyramid with the new color source |
| A viewport needs more detail than the selected tile level can provide | Composition returns no usable level and the result needs an exact re-bin | Let the caller disclose the fallback and run the exact path |
| A native text label uses a codepoint outside the baked atlas | The output shows a replacement glyph rather than the intended character | Use the browser export path when wider font coverage is required |
| A display-list command has invalid lengths or non-finite values | The native call reports failure and its output buffer is not valid | Validate command payloads before use and handle the native status |
Key files
| File | Why to read it |
|---|---|
| python/…/kernels.py | Shows the public Python view of the native compute surface and the no-fallback rule |
| python/…/_native.py | Shows library loading, ABI checks, and the Python-to-C buffer boundary |
| src/lib.rs | Shows the native entry boundary, status handling, and exported capabilities |
| src/kernels.rs | Shows the broad set of row scans and screen-data reductions |
| src/tiles.rs | Shows how multi-resolution count and color views support repeated viewport changes |
| src/raster.rs | Shows the native raster and PNG output path |
| src/simd.rs | Shows the limits and safeguards for CPU vector acceleration |
| src/transition.rs | Shows how native row identity supports animated data changes |
| src/svg.rs | Shows the focused native SVG path serialization role |
| src/font.rs | Shows the bounded text coverage used by native raster output |
| Cargo.toml | Shows the native library form and the small release-time dependency surface |