Skip to content

Commit d5a2a09

Browse files
committed
use alloc-free neighbor search (nice speedup)
1 parent 97b12b7 commit d5a2a09

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,12 @@ export default class Supercluster {
282282

283283
_cluster(data, numItems, zoom, out) {
284284
const {radius, extent, reduce, minPoints, maxZoom} = this.options;
285-
const r = radius / (extent * Math.pow(2, zoom)) * SCALE;
285+
const r = radius / (extent * (1 << zoom)) * SCALE;
286286
const notProcessed = maxZoom + 1;
287287
const tree = this.trees[zoom + 1];
288288
const stride = this.stride;
289289
const limit = numItems * stride;
290+
const neighborIds = new Uint32Array(numItems);
290291
let cursor = 0;
291292

292293
// loop through each point
@@ -298,14 +299,14 @@ export default class Supercluster {
298299
// find all nearby points
299300
const x = data[i];
300301
const y = data[i + 1];
301-
const neighborIds = tree.within(x, y, r);
302+
const neighborCount = tree.withinInto(x, y, r, neighborIds);
302303

303304
const numPointsOrigin = data[i + OFFSET_NUM];
304305
let numPoints = numPointsOrigin;
305306

306307
// count the number of points in a potential cluster
307-
for (const neighborId of neighborIds) {
308-
const k = neighborId * stride;
308+
for (let n = 0; n < neighborCount; n++) {
309+
const k = neighborIds[n] * stride;
309310
// filter out neighbors that are already processed
310311
if (data[k + OFFSET_ZOOM] > zoom) numPoints += data[k + OFFSET_NUM];
311312
}
@@ -321,8 +322,8 @@ export default class Supercluster {
321322
// encode both zoom and point index on which the cluster originated -- offset by total length of features
322323
const id = ((i / stride | 0) << 5) + (zoom + 1) + this.points.length;
323324

324-
for (const neighborId of neighborIds) {
325-
const k = neighborId * stride;
325+
for (let n = 0; n < neighborCount; n++) {
326+
const k = neighborIds[n] * stride;
326327

327328
if (data[k + OFFSET_ZOOM] <= zoom) continue;
328329
data[k + OFFSET_ZOOM] = zoom; // save the zoom (so it doesn't get processed twice)
@@ -352,8 +353,8 @@ export default class Supercluster {
352353
cursor += stride;
353354

354355
if (numPoints > 1) {
355-
for (const neighborId of neighborIds) {
356-
const k = neighborId * stride;
356+
for (let n = 0; n < neighborCount; n++) {
357+
const k = neighborIds[n] * stride;
357358
if (data[k + OFFSET_ZOOM] <= zoom) continue;
358359
data[k + OFFSET_ZOOM] = zoom;
359360
for (let j = 0; j < stride; j++) out[cursor + j] = data[k + j];

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"author": "Vladimir Agafonkin",
3535
"license": "ISC",
3636
"dependencies": {
37-
"kdbush": "^4.0.2"
37+
"kdbush": "^4.1.0"
3838
},
3939
"devDependencies": {
4040
"@rollup/plugin-node-resolve": "^16.0.3",

0 commit comments

Comments
 (0)