rbush-rs 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # rbush-rs
2
+ A high-performance(>5x faster), 100% Rust port of [RBush](https://github.com/mourner/rbush) spatial index library, compiled to WebAssembly.
3
+
4
+ `rbush-rs` is designed to be a near drop-in replacement for `rbush`, offering significant performance improvements for bulk loading and searching massive datasets.
5
+
6
+ ```diff
7
+ + Import { RBush } from 'rbush-rs';
8
+ - Import { RBush } from 'rbush';
9
+ ```
10
+
11
+ ## Benchmark
12
+
13
+ Tests performed on a dataset of 10,000 rectangles.
14
+
15
+ | Operation | JS (rbush) | WASM (rbush-rs) | Speedup |
16
+ | --- | --- | --- | --- |
17
+ | **Bulk Load (Hybrid)** | 20.23 ms | **3.60 ms** | **~5.6x Faster** |
18
+ | **Insert (1000 items)** | 36.58 ms | **4.04 ms** | **~9.1x Faster** |
19
+ | **Remove (1000 items)** | 58.03 ms | **7.85 ms** | **~7.4x Faster** |
20
+ | **Search** | 0.042 ms | **0.027 ms** | **~1.6x Faster** |
21
+ | **Collides** | 0.003 ms | 0.003 ms | **1x Faster** |
22
+ | **Clear** | 0.001 ms | 0.001 ms | **1x Faster** |
23
+
24
+ *\*Standard load is slower due to the overhead of reading JS objects into WASM. Use Hybrid Load for maximum performance.*
25
+
26
+ ## Installation
27
+ You can install `rbush-rs` via:
28
+
29
+ ### npm
30
+ ```bash
31
+ npm install rbush-rs
32
+ ```
33
+
34
+ ### pnpm
35
+ ```bash
36
+ pnpm install rbush-rs
37
+ ```
38
+
39
+ ### yarn
40
+ ```bash
41
+ yarn add rbush-rs
42
+ ```
43
+
44
+
45
+ ## Usage
46
+
47
+ ### The "Drop-in" Replacement
48
+
49
+ Use this mode if you want to switch libraries with minimal code changes. It accepts the same data format as the original `rbush`.
50
+
51
+ ```javascript
52
+ import { RBush } from 'rbush-rs';
53
+
54
+ // Initialize (max entries per node, default 9)
55
+ const tree = new RBush(9);
56
+
57
+ // Load standard data (Array of objects with minX, minY, maxX, maxY)
58
+ const items = [
59
+ { minX: 10, minY: 10, maxX: 20, maxY: 20, id: 'a' },
60
+ { minX: 50, minY: 50, maxX: 60, maxY: 60, id: 'b' }
61
+ ];
62
+ tree.load(items);
63
+
64
+ // Search
65
+ const results = tree.search({ minX: 0, minY: 0, maxX: 30, maxY: 30 });
66
+ console.log(results); // [{ minX: 10, ... }]
67
+
68
+ ```
69
+
70
+ ### The "Hybrid" Load (Recommended for MAX Speed)
71
+
72
+ To unlock the 5-6x load speedup, pass the coordinates as a `Float64Array` alongside your items. This allows Rust to sort the data instantly without expensive JavaScript object lookups.
73
+
74
+ ```javascript
75
+ import { RBush } from 'rbush-rs';
76
+
77
+ const tree = new RBush(9);
78
+ const items = [];
79
+ const count = 10000;
80
+
81
+ // Create a flat array for coordinates [minX, minY, maxX, maxY, minX...]
82
+ // Size = count * 4
83
+ const flatCoords = new Float64Array(count * 4);
84
+
85
+ for(let i = 0; i < count; i++) {
86
+ const item = { minX: Math.random() * 100, minY: Math.random() * 100, maxX: ..., maxY: ..., id: i };
87
+
88
+ // standard items array
89
+ items.push(item);
90
+
91
+ // flat array
92
+ flatCoords[i*4] = item.minX;
93
+ flatCoords[i*4 + 1] = item.minY;
94
+ flatCoords[i*4 + 2] = item.maxX;
95
+ flatCoords[i*4 + 3] = item.maxY;
96
+ }
97
+
98
+ // Load both at once
99
+ tree.loadHybrid(flatCoords, items);
100
+
101
+ // Search is exactly the same
102
+ const results = tree.search({ minX: 0, minY: 0, maxX: 50, maxY: 50 });
103
+
104
+ ```
105
+
106
+ ### Other Operations
107
+
108
+ All operations below work regardless of how you loaded the data (Standard or Hybrid).
109
+
110
+ #### Single Insertion
111
+
112
+ `rbush-rs` is highly optimized for dynamic updates, performing ~9x faster than JS.
113
+
114
+ ```javascript
115
+ const item = { minX: 20, minY: 20, maxX: 30, maxY: 30, id: 'c' };
116
+ tree.insert(item);
117
+ ```
118
+
119
+ #### Removal
120
+
121
+ Removal is ~7x faster than JS. The item object passed must match the one in the tree (by reference equality).
122
+
123
+ ```javascript
124
+ tree.remove(item);
125
+ ```
126
+
127
+ #### Collision Detection
128
+
129
+ Checks if there are any items in the bounding box. Faster than `search` if you don't need the actual items.
130
+
131
+ ```javascript
132
+ const hasCollision = tree.collides({ minX: 10, minY: 10, maxX: 20, maxY: 20 });
133
+ // returns true or false
134
+ ```
135
+
136
+ #### Retrieve All Items
137
+
138
+ Returns all items currently stored in the tree.
139
+
140
+ ```javascript
141
+ const allItems = tree.all();
142
+ ```
143
+
144
+ #### Clear Tree
145
+
146
+ Removes all items and resets the tree.
147
+
148
+ ```javascript
149
+ tree.clear();
150
+ ```
151
+
152
+ ## 🔧 API Reference
153
+
154
+ * **`new RBush(maxEntries?: number)`**: Creates a new tree.
155
+ * **`load(items: array)`**: Bulk loads standard JS objects.
156
+ * **`loadHybrid(coords: Float64Array, items: array)`**: High-performance bulk load.
157
+ * **`insert(item: object)`**: Inserts a single item.
158
+ * **`remove(item: object)`**: Removes a specific item.
159
+ * **`search(bbox: object)`**: Returns an array of items intersecting the bbox.
160
+ * **`collides(bbox: object)`**: Returns `true` if any item intersects the bbox.
161
+ * **`all()`**: Returns all items in the tree.
162
+ * **`clear()`**: Removes all items.
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "rbush-rs",
3
+ "type": "module",
4
+ "version": "0.1.0",
5
+ "files": [
6
+ "rbush_rs_bg.wasm",
7
+ "rbush_rs.js",
8
+ "rbush_rs.d.ts"
9
+ ],
10
+ "main": "rbush_rs.js",
11
+ "types": "rbush_rs.d.ts",
12
+ "sideEffects": [
13
+ "./snippets/*"
14
+ ]
15
+ }
package/rbush_rs.d.ts ADDED
@@ -0,0 +1,59 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ export class RBush {
5
+ free(): void;
6
+ [Symbol.dispose](): void;
7
+ all(): Array<any>;
8
+ clear(): void;
9
+ collides(bbox_js: any): boolean;
10
+ insert(item: any): void;
11
+ load(data: Array<any>): void;
12
+ loadHybrid(fast_coords: Float64Array, items: Array<any>): void;
13
+ constructor(max_entries?: number | null);
14
+ remove(item: any): void;
15
+ search(bbox_js: any): Array<any>;
16
+ }
17
+
18
+ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
19
+
20
+ export interface InitOutput {
21
+ readonly memory: WebAssembly.Memory;
22
+ readonly __wbg_rbush_free: (a: number, b: number) => void;
23
+ readonly rbush_all: (a: number) => any;
24
+ readonly rbush_clear: (a: number) => void;
25
+ readonly rbush_collides: (a: number, b: any) => number;
26
+ readonly rbush_insert: (a: number, b: any) => void;
27
+ readonly rbush_load: (a: number, b: any) => void;
28
+ readonly rbush_loadHybrid: (a: number, b: number, c: number, d: any) => void;
29
+ readonly rbush_new: (a: number) => number;
30
+ readonly rbush_remove: (a: number, b: any) => void;
31
+ readonly rbush_search: (a: number, b: any) => any;
32
+ readonly __wbindgen_exn_store: (a: number) => void;
33
+ readonly __externref_table_alloc: () => number;
34
+ readonly __wbindgen_externrefs: WebAssembly.Table;
35
+ readonly __wbindgen_malloc: (a: number, b: number) => number;
36
+ readonly __wbindgen_start: () => void;
37
+ }
38
+
39
+ export type SyncInitInput = BufferSource | WebAssembly.Module;
40
+
41
+ /**
42
+ * Instantiates the given `module`, which can either be bytes or
43
+ * a precompiled `WebAssembly.Module`.
44
+ *
45
+ * @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
46
+ *
47
+ * @returns {InitOutput}
48
+ */
49
+ export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
50
+
51
+ /**
52
+ * If `module_or_path` is {RequestInfo} or {URL}, makes a request and
53
+ * for everything else, calls `WebAssembly.instantiate` directly.
54
+ *
55
+ * @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
56
+ *
57
+ * @returns {Promise<InitOutput>}
58
+ */
59
+ export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
package/rbush_rs.js ADDED
@@ -0,0 +1,303 @@
1
+ /* @ts-self-types="./rbush_rs.d.ts" */
2
+
3
+ export class RBush {
4
+ __destroy_into_raw() {
5
+ const ptr = this.__wbg_ptr;
6
+ this.__wbg_ptr = 0;
7
+ RBushFinalization.unregister(this);
8
+ return ptr;
9
+ }
10
+ free() {
11
+ const ptr = this.__destroy_into_raw();
12
+ wasm.__wbg_rbush_free(ptr, 0);
13
+ }
14
+ /**
15
+ * @returns {Array<any>}
16
+ */
17
+ all() {
18
+ const ret = wasm.rbush_all(this.__wbg_ptr);
19
+ return ret;
20
+ }
21
+ clear() {
22
+ wasm.rbush_clear(this.__wbg_ptr);
23
+ }
24
+ /**
25
+ * @param {any} bbox_js
26
+ * @returns {boolean}
27
+ */
28
+ collides(bbox_js) {
29
+ const ret = wasm.rbush_collides(this.__wbg_ptr, bbox_js);
30
+ return ret !== 0;
31
+ }
32
+ /**
33
+ * @param {any} item
34
+ */
35
+ insert(item) {
36
+ wasm.rbush_insert(this.__wbg_ptr, item);
37
+ }
38
+ /**
39
+ * @param {Array<any>} data
40
+ */
41
+ load(data) {
42
+ wasm.rbush_load(this.__wbg_ptr, data);
43
+ }
44
+ /**
45
+ * @param {Float64Array} fast_coords
46
+ * @param {Array<any>} items
47
+ */
48
+ loadHybrid(fast_coords, items) {
49
+ const ptr0 = passArrayF64ToWasm0(fast_coords, wasm.__wbindgen_malloc);
50
+ const len0 = WASM_VECTOR_LEN;
51
+ wasm.rbush_loadHybrid(this.__wbg_ptr, ptr0, len0, items);
52
+ }
53
+ /**
54
+ * @param {number | null} [max_entries]
55
+ */
56
+ constructor(max_entries) {
57
+ const ret = wasm.rbush_new(isLikeNone(max_entries) ? 0x100000001 : (max_entries) >>> 0);
58
+ this.__wbg_ptr = ret >>> 0;
59
+ RBushFinalization.register(this, this.__wbg_ptr, this);
60
+ return this;
61
+ }
62
+ /**
63
+ * @param {any} item
64
+ */
65
+ remove(item) {
66
+ wasm.rbush_remove(this.__wbg_ptr, item);
67
+ }
68
+ /**
69
+ * @param {any} bbox_js
70
+ * @returns {Array<any>}
71
+ */
72
+ search(bbox_js) {
73
+ const ret = wasm.rbush_search(this.__wbg_ptr, bbox_js);
74
+ return ret;
75
+ }
76
+ }
77
+ if (Symbol.dispose) RBush.prototype[Symbol.dispose] = RBush.prototype.free;
78
+
79
+ function __wbg_get_imports() {
80
+ const import0 = {
81
+ __proto__: null,
82
+ __wbg___wbindgen_jsval_eq_11888390b0186270: function(arg0, arg1) {
83
+ const ret = arg0 === arg1;
84
+ return ret;
85
+ },
86
+ __wbg___wbindgen_number_get_8ff4255516ccad3e: function(arg0, arg1) {
87
+ const obj = arg1;
88
+ const ret = typeof(obj) === 'number' ? obj : undefined;
89
+ getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
90
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
91
+ },
92
+ __wbg___wbindgen_throw_be289d5034ed271b: function(arg0, arg1) {
93
+ throw new Error(getStringFromWasm0(arg0, arg1));
94
+ },
95
+ __wbg_get_9b94d73e6221f75c: function(arg0, arg1) {
96
+ const ret = arg0[arg1 >>> 0];
97
+ return ret;
98
+ },
99
+ __wbg_get_b3ed3ad4be2bc8ac: function() { return handleError(function (arg0, arg1) {
100
+ const ret = Reflect.get(arg0, arg1);
101
+ return ret;
102
+ }, arguments); },
103
+ __wbg_length_35a7bace40f36eac: function(arg0) {
104
+ const ret = arg0.length;
105
+ return ret;
106
+ },
107
+ __wbg_new_3eb36ae241fe6f44: function() {
108
+ const ret = new Array();
109
+ return ret;
110
+ },
111
+ __wbg_push_8ffdcb2063340ba5: function(arg0, arg1) {
112
+ const ret = arg0.push(arg1);
113
+ return ret;
114
+ },
115
+ __wbindgen_cast_0000000000000001: function(arg0, arg1) {
116
+ // Cast intrinsic for `Ref(String) -> Externref`.
117
+ const ret = getStringFromWasm0(arg0, arg1);
118
+ return ret;
119
+ },
120
+ __wbindgen_init_externref_table: function() {
121
+ const table = wasm.__wbindgen_externrefs;
122
+ const offset = table.grow(4);
123
+ table.set(0, undefined);
124
+ table.set(offset + 0, undefined);
125
+ table.set(offset + 1, null);
126
+ table.set(offset + 2, true);
127
+ table.set(offset + 3, false);
128
+ },
129
+ };
130
+ return {
131
+ __proto__: null,
132
+ "./rbush_rs_bg.js": import0,
133
+ };
134
+ }
135
+
136
+ const RBushFinalization = (typeof FinalizationRegistry === 'undefined')
137
+ ? { register: () => {}, unregister: () => {} }
138
+ : new FinalizationRegistry(ptr => wasm.__wbg_rbush_free(ptr >>> 0, 1));
139
+
140
+ function addToExternrefTable0(obj) {
141
+ const idx = wasm.__externref_table_alloc();
142
+ wasm.__wbindgen_externrefs.set(idx, obj);
143
+ return idx;
144
+ }
145
+
146
+ let cachedDataViewMemory0 = null;
147
+ function getDataViewMemory0() {
148
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
149
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
150
+ }
151
+ return cachedDataViewMemory0;
152
+ }
153
+
154
+ let cachedFloat64ArrayMemory0 = null;
155
+ function getFloat64ArrayMemory0() {
156
+ if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) {
157
+ cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer);
158
+ }
159
+ return cachedFloat64ArrayMemory0;
160
+ }
161
+
162
+ function getStringFromWasm0(ptr, len) {
163
+ ptr = ptr >>> 0;
164
+ return decodeText(ptr, len);
165
+ }
166
+
167
+ let cachedUint8ArrayMemory0 = null;
168
+ function getUint8ArrayMemory0() {
169
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
170
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
171
+ }
172
+ return cachedUint8ArrayMemory0;
173
+ }
174
+
175
+ function handleError(f, args) {
176
+ try {
177
+ return f.apply(this, args);
178
+ } catch (e) {
179
+ const idx = addToExternrefTable0(e);
180
+ wasm.__wbindgen_exn_store(idx);
181
+ }
182
+ }
183
+
184
+ function isLikeNone(x) {
185
+ return x === undefined || x === null;
186
+ }
187
+
188
+ function passArrayF64ToWasm0(arg, malloc) {
189
+ const ptr = malloc(arg.length * 8, 8) >>> 0;
190
+ getFloat64ArrayMemory0().set(arg, ptr / 8);
191
+ WASM_VECTOR_LEN = arg.length;
192
+ return ptr;
193
+ }
194
+
195
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
196
+ cachedTextDecoder.decode();
197
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
198
+ let numBytesDecoded = 0;
199
+ function decodeText(ptr, len) {
200
+ numBytesDecoded += len;
201
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
202
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
203
+ cachedTextDecoder.decode();
204
+ numBytesDecoded = len;
205
+ }
206
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
207
+ }
208
+
209
+ let WASM_VECTOR_LEN = 0;
210
+
211
+ let wasmModule, wasm;
212
+ function __wbg_finalize_init(instance, module) {
213
+ wasm = instance.exports;
214
+ wasmModule = module;
215
+ cachedDataViewMemory0 = null;
216
+ cachedFloat64ArrayMemory0 = null;
217
+ cachedUint8ArrayMemory0 = null;
218
+ wasm.__wbindgen_start();
219
+ return wasm;
220
+ }
221
+
222
+ async function __wbg_load(module, imports) {
223
+ if (typeof Response === 'function' && module instanceof Response) {
224
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
225
+ try {
226
+ return await WebAssembly.instantiateStreaming(module, imports);
227
+ } catch (e) {
228
+ const validResponse = module.ok && expectedResponseType(module.type);
229
+
230
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
231
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
232
+
233
+ } else { throw e; }
234
+ }
235
+ }
236
+
237
+ const bytes = await module.arrayBuffer();
238
+ return await WebAssembly.instantiate(bytes, imports);
239
+ } else {
240
+ const instance = await WebAssembly.instantiate(module, imports);
241
+
242
+ if (instance instanceof WebAssembly.Instance) {
243
+ return { instance, module };
244
+ } else {
245
+ return instance;
246
+ }
247
+ }
248
+
249
+ function expectedResponseType(type) {
250
+ switch (type) {
251
+ case 'basic': case 'cors': case 'default': return true;
252
+ }
253
+ return false;
254
+ }
255
+ }
256
+
257
+ function initSync(module) {
258
+ if (wasm !== undefined) return wasm;
259
+
260
+
261
+ if (module !== undefined) {
262
+ if (Object.getPrototypeOf(module) === Object.prototype) {
263
+ ({module} = module)
264
+ } else {
265
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
266
+ }
267
+ }
268
+
269
+ const imports = __wbg_get_imports();
270
+ if (!(module instanceof WebAssembly.Module)) {
271
+ module = new WebAssembly.Module(module);
272
+ }
273
+ const instance = new WebAssembly.Instance(module, imports);
274
+ return __wbg_finalize_init(instance, module);
275
+ }
276
+
277
+ async function __wbg_init(module_or_path) {
278
+ if (wasm !== undefined) return wasm;
279
+
280
+
281
+ if (module_or_path !== undefined) {
282
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
283
+ ({module_or_path} = module_or_path)
284
+ } else {
285
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
286
+ }
287
+ }
288
+
289
+ if (module_or_path === undefined) {
290
+ module_or_path = new URL('rbush_rs_bg.wasm', import.meta.url);
291
+ }
292
+ const imports = __wbg_get_imports();
293
+
294
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
295
+ module_or_path = fetch(module_or_path);
296
+ }
297
+
298
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
299
+
300
+ return __wbg_finalize_init(instance, module);
301
+ }
302
+
303
+ export { initSync, __wbg_init as default };
Binary file