Optimize weighted clustering using Supercluster v9 storage and allocation techniques

Aperta
#126 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
15/100
Tipo di issue
Refactoring
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
typescript

Direzione di ricerca

Start in packages/data/src/cluster/cluster.ts, globe-projection.ts and ClusterIndex.ts, and read test/node/clustering.test.js with the flat-clusters.json fixture. The first step is a measured baseline of clusterLocations and buildIndex, not a code change. Done means the fixture and globe tests still pass, weights affect merges at every zoom, and before/after timings and memory are reported.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

Our location-clustering implementation is based on an older Supercluster implementation and still allocates objects for individual points and clusters. Modern Supercluster offers useful memory and allocation optimizations, but replacing our implementation directly would change weighted clustering and globe behavior.

Optimize our implementation by adapting the relevant Supercluster v9 techniques while preserving the existing clustering semantics and public hierarchy format.

Findings from the compatibility review

Reviewed Supercluster 9.1.0 and the local flowmap.gl checkout at 758f7605efe20cf5ec764345593a93460b11ec5b.

  • Custom weights still do not affect Supercluster geometry. Its centers are weighted by original point counts. map/reduce aggregate properties but do not affect centers or subsequent merges. Our weights are normally max(abs(total incoming), abs(total outgoing)), with zero weights treated as 1 during clustering.
  • Post-processing centers is insufficient. Moving a center changes which locations merge at subsequent zoom levels, so weighting must participate in hierarchy construction.
  • Globe clustering requires separate geometry. Our current checkout supports spherical neighbor distances and weighted spherical centers, including dateline and polar neighbors. Supercluster uses Mercator coordinates and exposes no projection hook.
  • The hierarchy contracts differ. We emit ClusterLevel[], compact unchanged levels, handle duplicate-coordinate levels specially, and use level-specific cluster nodes. Supercluster carries cluster IDs across zooms. Our ClusterIndex stores nodes by ID and uses each node's zoom, so directly adapting those IDs would require care.
  • The spatial index already uses typed arrays. The lockfile resolves KDBush 4.1.0, and the Mercator path constructs it with Float32Array. The opportunity is chiefly internal point/cluster storage and temporary allocations, rather than merely upgrading KDBush.

Relevant local source:

  • packages/data/src/cluster/cluster.ts
  • packages/data/src/cluster/globe-projection.ts
  • packages/data/src/cluster/ClusterIndex.ts
  • packages/data/src/FlowmapSelectors.ts
  • test/node/clustering.test.js
  • test/node/flat-clusters.json

Proposed approach

  1. Establish an end-to-end baseline.

    • Measure clusterLocations, buildIndex, and their combined cost separately.
    • Include representative flow datasets and seeded synthetic distributions: dense clusters, sparse/global points, duplicate coordinates, highly unequal weights, and globe dateline/polar cases.
    • Record warm-up, repeated-run timings, dataset sizes, runtime versions, and memory measurements. Include typed-array/ArrayBuffer memory rather than relying only on JS heap.
    • Measure hierarchy materialization too: our consumers require the full hierarchy, unlike a benchmark that only builds an upstream index.
  2. Replace internal point/cluster objects with compact numeric storage.

    • Evaluate flat typed arrays or separate typed arrays for coordinates, weights, point counts, processed zooms, IDs, and parent IDs.
    • Keep original locations and external string/number IDs outside numeric buffers.
    • Keep weight separate from point count; retain fractional/large weights without integer truncation or overflow.
    • Preserve sufficient coordinate precision and the globe's third coordinate. Do not mechanically copy Supercluster's Int32 coordinate encoding: changed rounding can affect neighbor boundaries, centers, and hierarchy membership.
    • Materialize the existing public ClusterLevel[] objects at the boundary.
  3. Reduce temporary allocations in neighbor queries and level construction.

    • Evaluate KDBush 4.1.0's withinInto with reusable neighbor buffers on the Mercator path.
    • Reuse scratch buffers where ownership permits; do not overwrite arrays retained by earlier hierarchy levels or indexes.
    • Preserve existing unchanged-level compaction and duplicate-location handling.
    • Profile the globe spatial hash separately and apply relevant buffer reuse there without replacing spherical geometry with Mercator.
  4. Land focused, measured changes.

    • Prefer a benchmark/compatibility baseline followed by independently reviewable optimizations.
    • Compare results after each step and retain changes justified by measured improvements.
    • Preserve upstream license notices for adapted code.

Behavior to preserve / acceptance criteria

  • Custom weights affect centers and subsequent merges at every zoom; point count remains distinct from weight.
  • Preserve the current zero-weight fallback and flow-derived weight calculation.
  • Existing flat hierarchy fixture remains unchanged, including IDs, centers, zoom levels, children, and duplicate-coordinate behavior. Any intentional precision change requires explicit review.
  • Globe tests preserve dateline/polar neighbor behavior, finite centers, and descendants through independent merges.
  • Every retained level preserves all leaves exactly once.
  • Flow aggregation preserves total magnitude and correct origin/destination grouping.
  • Public ClusterLevel[], custom ID/name callbacks, supplied hierarchies, manual clustering levels, selections, and selector caching continue to work.
  • Report before/after construction time and memory for representative Mercator and globe workloads, including hierarchy output and buildIndex.
  • Run the relevant regression suite; document any tradeoffs or regressions.

Focused comparisons already performed

Using the current local algorithm and Supercluster 9.1.0 source with KDBush 4.1.0:

Case Current implementation Supercluster 9.1.0
Equatorial points at longitude 0° and 10°, weights 1 and 9, maxZoom: 0 Center at 9° Center at approximately 5°, even with weight aggregation
Add an equatorial point at 35° with weight 1; maxZoom: 1, default radius/extent One cluster at zoom 0 Two nodes at zoom 0
Points at 179° and −179°, latitude 10°, weights 1 and 3, maxZoom: 5 Globe mode merges the pair Pair remains separate at zoom 0

The existing serialized flat-hierarchy fixture also passed in the standalone comparison. These were focused checks, not a full test-suite run or an end-to-end performance benchmark.

Upstream references

Both weighted-points proposals remained open at review time. Adopting unmodified Supercluster is therefore not the proposed implementation.

Lingua principale
TypeScript
Stelle
143
Fork
24
Merge medio
1g 4h
PR unite (30g)
18

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di visgl/flowmap.gl

Tutte le issue di visgl/flowmap.gl

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.