v9: Radically improve memory footprint#258
Open
mourner wants to merge 8 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Across four independent changes in this PR, building a Supercluster index over 1M points (
maxZoom=17) now uses 94% less transient allocation, 57% lower peak heap, 65% less retained memory, and runs 23% faster — with no public API changes and zero precision drift at GL JS tile params. An equivalent of similar improvements ingeojson-vtmapbox/geojson-vt#1911M uniform random points sample
*held = after dropping the caller's input reference.
Real-world dataset: 1.6M Overture places
The build-time delta is modest on this dataset (real geographic distribution leaves no plateau zooms — every level produces fewer clusters than the previous), but the memory story is dramatic: a working set that previously needed nearly 1 GB peak and held 642 MB after build now peaks at 470 MB and retains just 82 MB once the caller releases their input.
What changed
Phase 1 — Int32 slab. The per-point cluster slab is now an
Int32Arrayusing a centered encoding(coord − 0.5) × 2³⁰, putting every stored value and everysqDistsubtraction inside V8's 31-bit SMI fast path. Working precision improved 64× vs the priorMath.froundpath. KDBush also moved toInt32Arraystorage. The per-zoom slab is sized tight to the actual output count, so slack stays bounded by N across the whole hierarchy.Phase 2 —
withinIntotyped-array result (KDBush 4.1.0). The~14Mtree.withincalls during a 1M build each allocated a growable JS Array for results, a growable JS Array for the DFS stack, and an iterator object at the consumer. KDBush 4.1.0 addswithinInto(qx, qy, r, out) → countwith a caller-owned typed array and moves its DFS stack to a module-levelUint32Array(96). Supercluster passes a per-zoomUint32Array(numItems), sized tight and cache-friendly. This phase alone cut transient alloc from 3.2 GB to 0.5 GB.Phase 3 — Drop retained GeoJSON wrappers. The instance no longer pins the caller's
pointsarray. We retain only what output paths actually read:props(references to caller-ownedproperties),coords(Float64 mercator, 16 B/point — needed for drift-free tile coords atextent: 8192, z18), and a lazily-allocatedidsarray (only if any input hasid !== undefined). Released 132 MB of retained memory on the 1M bench.Phase 4 — Reuse KDBush across plateau zooms. When
_clusterreturnswritten === numItems, no cluster branch was ever taken and the output is bit-identical to the input. We settrees[z] = trees[z+1]to share the parent KDBush by reference, leaveprev/prevNumunchanged, and recycle the freshly-allocatedoutslab into the next iteration's allocation. Skips both a KDBush rebuild and an Int32 slab allocation per plateau zoom — biggest impact on real geographic datasets with sparse high-zoom data (−11% ms on the populated-places fixture).Compatibility
getClusters/getTile/getChildren/getLeavesreturn the same JSON shape.