@daneren2005/shared-memory-objects 1.3.0 → 1.5.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 +192 -13
- package/dist/allocated-memory.js +1 -1
- package/dist/allocated-memory.js.map +1 -1
- package/dist/constant-string.js +2 -2
- package/dist/constant-string.js.map +1 -1
- package/dist/local-pool.d.ts +5 -5
- package/dist/local-pool.js +15 -12
- package/dist/local-pool.js.map +1 -1
- package/dist/lock/read-write-lock.js +25 -11
- package/dist/lock/read-write-lock.js.map +1 -1
- package/dist/lock/simple-lock.js +7 -2
- package/dist/lock/simple-lock.js.map +1 -1
- package/dist/memory-heap.d.ts +2 -0
- package/dist/memory-heap.js +28 -23
- package/dist/memory-heap.js.map +1 -1
- package/dist/serialize-object.js +1 -1
- package/dist/serialize-object.js.map +1 -1
- package/dist/shared-list.d.ts +11 -6
- package/dist/shared-list.js +55 -59
- package/dist/shared-list.js.map +1 -1
- package/dist/shared-map.d.ts +35 -12
- package/dist/shared-map.js +185 -82
- package/dist/shared-map.js.map +1 -1
- package/dist/shared-pointer-list.js +3 -3
- package/dist/shared-pointer-list.js.map +1 -1
- package/dist/shared-pool.d.ts +16 -7
- package/dist/shared-pool.js +74 -57
- package/dist/shared-pool.js.map +1 -1
- package/dist/shared-stack.d.ts +9 -8
- package/dist/shared-stack.js +68 -79
- package/dist/shared-stack.js.map +1 -1
- package/dist/shared-string.js +3 -3
- package/dist/shared-string.js.map +1 -1
- package/dist/shared-vector.d.ts +8 -6
- package/dist/shared-vector.js +34 -57
- package/dist/shared-vector.js.map +1 -1
- package/dist/spatial/shared-quadtree.d.ts +67 -0
- package/dist/spatial/shared-quadtree.js +361 -0
- package/dist/spatial/shared-quadtree.js.map +1 -0
- package/dist/spatial/shared-spatial-grid.d.ts +78 -0
- package/dist/spatial/shared-spatial-grid.js +426 -0
- package/dist/spatial/shared-spatial-grid.js.map +1 -0
- package/dist/spatial/shared-spatial-map.d.ts +87 -0
- package/dist/spatial/shared-spatial-map.js +555 -0
- package/dist/spatial/shared-spatial-map.js.map +1 -0
- package/dist/spatial/spatial-entity.d.ts +11 -0
- package/dist/spatial/spatial-entity.js +19 -0
- package/dist/spatial/spatial-entity.js.map +1 -0
- package/dist/spatial/spatial-neighbors.d.ts +21 -0
- package/dist/spatial/spatial-neighbors.js +55 -0
- package/dist/spatial/spatial-neighbors.js.map +1 -0
- package/dist/utils/array-type.d.ts +20 -0
- package/dist/utils/array-type.js +36 -0
- package/dist/utils/array-type.js.map +1 -0
- package/dist/utils/hash.d.ts +3 -0
- package/dist/utils/hash.js +21 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/pointer.d.ts +8 -6
- package/dist/utils/pointer.js +22 -17
- package/dist/utils/pointer.js.map +1 -1
- package/package.json +8 -2
package/dist/shared-map.js
CHANGED
|
@@ -1,107 +1,210 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getPointer as e, storeRawPointer as t } from "./utils/pointer.js";
|
|
2
2
|
import n from "./allocated-memory.js";
|
|
3
|
-
import r from "./
|
|
3
|
+
import { getArrayTypeCode as r, getByteMultipler as i, makeArrayView as a } from "./utils/array-type.js";
|
|
4
|
+
import { lock as o, unlock as s } from "./lock/simple-lock.js";
|
|
5
|
+
import { get32BitHash as c, mix32 as l } from "./utils/hash.js";
|
|
4
6
|
//#region src/shared-map.ts
|
|
5
|
-
var
|
|
6
|
-
static ALLOCATE_COUNT =
|
|
7
|
+
var u = 0, d = 1, f = 2, p = 16, m = 3, h = 4, g = 0, _ = 1, v = 2, y = 3, b = 4, x = 5, S = class S {
|
|
8
|
+
static ALLOCATE_COUNT = 6;
|
|
7
9
|
memory;
|
|
8
10
|
pointerMemory;
|
|
11
|
+
ownsFirstBlock = !0;
|
|
9
12
|
lock;
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
cachedType;
|
|
14
|
+
cachedValueUnits = 1;
|
|
15
|
+
cachedTablePointer = 0;
|
|
16
|
+
cachedTableMemory;
|
|
17
|
+
cachedSlots;
|
|
18
|
+
cachedValues;
|
|
19
|
+
cachedCapacity = 0;
|
|
20
|
+
ensureTable() {
|
|
21
|
+
let t = this.pointerMemory.data, r = t[g], i = t[b];
|
|
22
|
+
if (r === this.cachedTablePointer && i === this.cachedCapacity && this.cachedSlots) return;
|
|
23
|
+
let a = new n(this.memory, e(r, this.memory.positionBits));
|
|
24
|
+
this.cachedTableMemory = a, this.cachedSlots = a.data, this.cachedValues = this.makeValuesView(a, i), this.cachedCapacity = i, this.cachedTablePointer = r;
|
|
25
|
+
}
|
|
26
|
+
makeValuesView(e, t) {
|
|
27
|
+
let n = e.bufferByteOffset + t * 2 * Uint32Array.BYTES_PER_ELEMENT;
|
|
28
|
+
return a(this.cachedType, e.data.buffer, n, t);
|
|
29
|
+
}
|
|
30
|
+
tableLength(e) {
|
|
31
|
+
return e * (2 + this.cachedValueUnits);
|
|
12
32
|
}
|
|
13
33
|
get length() {
|
|
14
|
-
return Atomics.load(this.pointerMemory.data,
|
|
34
|
+
return Atomics.load(this.pointerMemory.data, v);
|
|
35
|
+
}
|
|
36
|
+
get capacity() {
|
|
37
|
+
return Atomics.load(this.pointerMemory.data, b);
|
|
15
38
|
}
|
|
16
39
|
get maxHash() {
|
|
17
|
-
return
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
40
|
+
return this.capacity;
|
|
41
|
+
}
|
|
42
|
+
get type() {
|
|
43
|
+
return this.cachedType;
|
|
44
|
+
}
|
|
45
|
+
get usedMemory() {
|
|
46
|
+
o(this.lock);
|
|
47
|
+
try {
|
|
48
|
+
return this.ensureTable(), (this.ownsFirstBlock ? this.pointerMemory.usedMemory : 0) + this.cachedTableMemory.usedMemory;
|
|
49
|
+
} finally {
|
|
50
|
+
s(this.lock);
|
|
25
51
|
}
|
|
26
|
-
|
|
52
|
+
}
|
|
53
|
+
constructor(e, t) {
|
|
54
|
+
this.memory = e, t && "firstBlock" in t ? t.firstBlock instanceof n ? (this.pointerMemory = t.firstBlock, this.initialize(t), this.ownsFirstBlock = !1) : this.pointerMemory = new n(e, t.firstBlock) : (this.pointerMemory = e.allocUI32(S.ALLOCATE_COUNT), this.initialize(t)), this.lock = new Int32Array(this.pointerMemory.data.buffer, this.pointerMemory.bufferByteOffset + _ * Uint32Array.BYTES_PER_ELEMENT, 1), this.cachedType = Atomics.load(this.pointerMemory.data, x), this.cachedValueUnits = i(this.cachedType);
|
|
55
|
+
}
|
|
56
|
+
initialize(e) {
|
|
57
|
+
let n = e && "type" in e ? e.type : void 0, a = r(n ?? Uint32Array);
|
|
58
|
+
this.cachedValueUnits = i(a);
|
|
59
|
+
let o = e && "capacity" in e ? e.capacity : void 0, s = p;
|
|
60
|
+
for (; s < (o ?? p);) s *= 2;
|
|
61
|
+
if (s * (2 + this.cachedValueUnits) > this.memory.maxAllocationLength) throw Error(`SharedMap capacity ${s} exceeds the largest contiguous allocation`);
|
|
62
|
+
let c = this.memory.allocUI32(s * (2 + this.cachedValueUnits));
|
|
63
|
+
t(this.pointerMemory.data, g, c.pointer), Atomics.store(this.pointerMemory.data, b, s), Atomics.store(this.pointerMemory.data, x, a);
|
|
27
64
|
}
|
|
28
65
|
set(e, t) {
|
|
29
|
-
this.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
let s = this.hash(a, i), c, l = e(n.data, s);
|
|
35
|
-
if (l.bufferByteOffset === 0) {
|
|
36
|
-
c = new r(this.memory, { dataLength: 2 });
|
|
37
|
-
let e = c.getSharedMemory();
|
|
38
|
-
t(n.data, s, e.firstBlock.bufferPosition, e.firstBlock.bufferByteOffset);
|
|
39
|
-
} else c = new r(this.memory, { firstBlock: l });
|
|
40
|
-
let u = !0;
|
|
41
|
-
return c.deleteValue(a) && (u = !1), c.insert([a, o]), u || c.compact(), u;
|
|
42
|
-
}
|
|
43
|
-
get(t) {
|
|
44
|
-
let n = o(t), i = this.hash(n, this.maxHash), a = e(this.hashMemory.data, i);
|
|
45
|
-
if (a.bufferByteOffset === 0) return;
|
|
46
|
-
let s = new r(this.memory, { firstBlock: a });
|
|
47
|
-
for (let { data: e } of s) if (e[0] === n) return e[1];
|
|
48
|
-
}
|
|
49
|
-
has(t) {
|
|
50
|
-
let n = o(t), i = this.hash(n, this.maxHash), a = e(this.hashMemory.data, i);
|
|
51
|
-
if (a.bufferByteOffset === 0) return !1;
|
|
52
|
-
let s = new r(this.memory, { firstBlock: a });
|
|
53
|
-
for (let { data: e } of s) if (e[0] === n) return !0;
|
|
54
|
-
return !1;
|
|
55
|
-
}
|
|
56
|
-
delete(t) {
|
|
57
|
-
let n = o(t), i = this.hash(n, this.maxHash), a = e(this.hashMemory.data, i);
|
|
58
|
-
if (a.bufferByteOffset === 0) return !1;
|
|
59
|
-
let s = new r(this.memory, { firstBlock: a }), c = !1;
|
|
60
|
-
for (let { data: e, deleteCurrent: t } of s) if (e[0] === n) {
|
|
61
|
-
t(), Atomics.sub(this.pointerMemory.data, 2, 1), c = !0;
|
|
62
|
-
break;
|
|
66
|
+
o(this.lock);
|
|
67
|
+
try {
|
|
68
|
+
this.setNoLock(e, t);
|
|
69
|
+
} finally {
|
|
70
|
+
s(this.lock);
|
|
63
71
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
let
|
|
70
|
-
if (n
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
}
|
|
73
|
+
setAll(e) {
|
|
74
|
+
let t = Array.from(e);
|
|
75
|
+
o(this.lock);
|
|
76
|
+
try {
|
|
77
|
+
let e = this.pointerMemory.data, n = e[v] + e[y] + t.length;
|
|
78
|
+
if (n * h > e[b] * m) {
|
|
79
|
+
let t = e[b];
|
|
80
|
+
for (; n * h > t * m;) t *= 2;
|
|
81
|
+
this.resize(t);
|
|
82
|
+
}
|
|
83
|
+
for (let [e, n] of t) this.setNoLock(e, n);
|
|
84
|
+
} finally {
|
|
85
|
+
s(this.lock);
|
|
74
86
|
}
|
|
75
|
-
t(this.pointerMemory.data, 0, a.bufferPosition, a.bufferByteOffset), Atomics.store(this.pointerMemory.data, 3, i), o.free();
|
|
76
87
|
}
|
|
77
|
-
|
|
78
|
-
|
|
88
|
+
setNoLock(e, t) {
|
|
89
|
+
let n = c(e), r = this.pointerMemory.data;
|
|
90
|
+
(r[v] + r[y] + 1) * h > r[b] * m && this.resize(), this.ensureTable();
|
|
91
|
+
let i = this.cachedSlots, a = this.cachedValues, o = this.cachedCapacity, s = o - 1, p = o, g = l(n) & s, _ = -1;
|
|
92
|
+
for (;;) {
|
|
93
|
+
let e = i[g];
|
|
94
|
+
if (e === u) {
|
|
95
|
+
let e = _ >= 0 ? _ : g;
|
|
96
|
+
i[e] = d, i[p + e] = n, a[e] = t, r[v]++, _ >= 0 && r[y]--;
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (e === f) _ < 0 && (_ = g);
|
|
100
|
+
else if (i[p + g] === n) {
|
|
101
|
+
a[g] = t;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
g = g + 1 & s;
|
|
105
|
+
}
|
|
79
106
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
107
|
+
update(e, t) {
|
|
108
|
+
let n = c(e);
|
|
109
|
+
o(this.lock);
|
|
110
|
+
try {
|
|
111
|
+
let e = this.pointerMemory.data;
|
|
112
|
+
(e[v] + e[y] + 1) * h > e[b] * m && this.resize(), this.ensureTable();
|
|
113
|
+
let r = this.cachedSlots, i = this.cachedValues, a = this.cachedCapacity, o = a - 1, s = a, c = l(n) & o, p = -1;
|
|
114
|
+
for (;;) {
|
|
115
|
+
let a = r[c];
|
|
116
|
+
if (a === u) {
|
|
117
|
+
let a = t(void 0), o = p >= 0 ? p : c;
|
|
118
|
+
return r[o] = d, r[s + o] = n, i[o] = a, e[v]++, p >= 0 && e[y]--, a;
|
|
119
|
+
}
|
|
120
|
+
if (a === f) p < 0 && (p = c);
|
|
121
|
+
else if (r[s + c] === n) {
|
|
122
|
+
let e = t(i[c]);
|
|
123
|
+
return i[c] = e, e;
|
|
124
|
+
}
|
|
125
|
+
c = c + 1 & o;
|
|
126
|
+
}
|
|
127
|
+
} finally {
|
|
128
|
+
s(this.lock);
|
|
84
129
|
}
|
|
85
|
-
|
|
130
|
+
}
|
|
131
|
+
get(e) {
|
|
132
|
+
o(this.lock);
|
|
133
|
+
try {
|
|
134
|
+
return this.getNoLock(e);
|
|
135
|
+
} finally {
|
|
136
|
+
s(this.lock);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
getAll(e) {
|
|
140
|
+
o(this.lock);
|
|
141
|
+
try {
|
|
142
|
+
let t = [];
|
|
143
|
+
for (let n of e) t.push(this.getNoLock(n));
|
|
144
|
+
return t;
|
|
145
|
+
} finally {
|
|
146
|
+
s(this.lock);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
getNoLock(e) {
|
|
150
|
+
let t = c(e);
|
|
151
|
+
this.ensureTable();
|
|
152
|
+
let n = this.cachedSlots, r = this.cachedValues, i = this.cachedCapacity, a = i - 1, o = i, s = l(t) & a;
|
|
153
|
+
for (;;) {
|
|
154
|
+
let e = n[s];
|
|
155
|
+
if (e === u) return;
|
|
156
|
+
if (e === d && n[o + s] === t) return r[s];
|
|
157
|
+
s = s + 1 & a;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
has(e) {
|
|
161
|
+
return this.get(e) !== void 0;
|
|
162
|
+
}
|
|
163
|
+
delete(e) {
|
|
164
|
+
let t = c(e);
|
|
165
|
+
o(this.lock);
|
|
166
|
+
try {
|
|
167
|
+
this.ensureTable();
|
|
168
|
+
let e = this.pointerMemory.data, n = this.cachedSlots, r = this.cachedCapacity, i = r - 1, a = r, o = l(t) & i;
|
|
169
|
+
for (;;) {
|
|
170
|
+
let r = n[o];
|
|
171
|
+
if (r === u) return !1;
|
|
172
|
+
if (r === d && n[a + o] === t) return n[o] = f, e[v]--, e[y]++, !0;
|
|
173
|
+
o = o + 1 & i;
|
|
174
|
+
}
|
|
175
|
+
} finally {
|
|
176
|
+
s(this.lock);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
*[Symbol.iterator]() {
|
|
180
|
+
this.ensureTable();
|
|
181
|
+
let e = this.cachedSlots, t = this.cachedValues, n = this.cachedCapacity, r = n;
|
|
182
|
+
for (let i = 0; i < n; i++) e[i] === d && (yield [e[r + i], t[i]]);
|
|
183
|
+
}
|
|
184
|
+
resize(e) {
|
|
185
|
+
this.ensureTable();
|
|
186
|
+
let n = this.cachedTableMemory, r = this.cachedSlots, i = this.cachedValues, a = this.cachedCapacity, o = a, s = Atomics.load(this.pointerMemory.data, v), c = e ?? a;
|
|
187
|
+
if (e === void 0 && (s + 1) * h > a * m && (c = a * 2), this.tableLength(c) > this.memory.maxAllocationLength) {
|
|
188
|
+
let e = Math.floor(this.memory.maxAllocationLength / (2 + this.cachedValueUnits) * m / h);
|
|
189
|
+
throw Error(`SharedMap can't grow to capacity ${c}: its table would exceed the largest contiguous allocation (${this.memory.maxAllocationLength} u32s, ~${e} entries). Use larger buffers or shard across multiple maps.`);
|
|
190
|
+
}
|
|
191
|
+
let f = this.memory.allocUI32(this.tableLength(c)), p = f.data, _ = this.makeValuesView(f, c), x = c - 1, S = c;
|
|
192
|
+
for (let e = 0; e < a; e++) {
|
|
193
|
+
if (r[e] !== d) continue;
|
|
194
|
+
let t = r[o + e], n = i[e], a = l(t) & x;
|
|
195
|
+
for (; p[a] !== u;) a = a + 1 & x;
|
|
196
|
+
p[a] = d, p[S + a] = t, _[a] = n;
|
|
197
|
+
}
|
|
198
|
+
t(this.pointerMemory.data, g, f.pointer), Atomics.store(this.pointerMemory.data, b, c), Atomics.store(this.pointerMemory.data, y, 0), this.cachedTablePointer = 0, this.cachedSlots = void 0, n.free();
|
|
199
|
+
}
|
|
200
|
+
free() {
|
|
201
|
+
this.ensureTable(), this.cachedTableMemory.free(), this.ownsFirstBlock && this.pointerMemory.free();
|
|
86
202
|
}
|
|
87
203
|
getSharedMemory() {
|
|
88
204
|
return { firstBlock: this.pointerMemory.getSharedMemory() };
|
|
89
205
|
}
|
|
90
206
|
};
|
|
91
|
-
function o(e) {
|
|
92
|
-
return typeof e == "number" ? e : typeof e == "string" ? s(e) : e;
|
|
93
|
-
}
|
|
94
|
-
function s(e) {
|
|
95
|
-
let t = e.length, n = 17 ^ t, r = 0, i;
|
|
96
|
-
for (; t >= 4;) i = e.charCodeAt(r) & 255 | (e.charCodeAt(++r) & 255) << 8 | (e.charCodeAt(++r) & 255) << 16 | (e.charCodeAt(++r) & 255) << 14, i = (i & 65535) * 1540483477 + (((i >>> 16) * 1540483477 & 65535) << 16), i ^= i >>> 14, i = (i & 65535) * 1540483477 + (((i >>> 16) * 1540483477 & 65535) << 16), n = (n & 65535) * 1540483477 + (((n >>> 16) * 1540483477 & 65535) << 16) ^ i, t -= 4, ++r;
|
|
97
|
-
switch (t) {
|
|
98
|
-
case 3: n ^= (e.charCodeAt(r + 2) & 255) << 16;
|
|
99
|
-
case 2: n ^= (e.charCodeAt(r + 1) & 255) << 8;
|
|
100
|
-
case 1: n ^= e.charCodeAt(r) & 255, n = (n & 65535) * 1540483477 + (((n >>> 16) * 1540483477 & 65535) << 16);
|
|
101
|
-
}
|
|
102
|
-
return n ^= n >>> 13, n = (n & 65535) * 1540483477 + (((n >>> 16) * 1540483477 & 65535) << 16), n ^= n >>> 15, n >>>= 0, n;
|
|
103
|
-
}
|
|
104
207
|
//#endregion
|
|
105
|
-
export {
|
|
208
|
+
export { S as default };
|
|
106
209
|
|
|
107
210
|
//# sourceMappingURL=shared-map.js.map
|
package/dist/shared-map.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-map.js","names":[],"sources":["../src/shared-map.ts"],"sourcesContent":["import AllocatedMemory, { type SharedAllocatedMemory } from './allocated-memory';\nimport type MemoryHeap from './memory-heap';\nimport SharedList from './shared-list';\nimport { loadPointer, storePointer } from './utils/pointer';\n\n// TODO: Grow hashMemory\n// TODO: Add iterator\n// TODO: Add read/write locks\nconst DEFAULT_HASH_SIZE = 10;\nexport default class SharedMap<K extends string | number> {\n\tstatic readonly ALLOCATE_COUNT = 4;\n\n\tprivate memory: MemoryHeap;\n\t// Memory order: Pointer, Lock, Length, MaxHash\n\tprivate pointerMemory: AllocatedMemory;\n\tprivate lock: Int32Array;\n\tprivate get hashMemory(): AllocatedMemory {\n\t\treturn new AllocatedMemory(this.memory, loadPointer(this.pointerMemory.data, 0));\n\t}\n\n\tget length(): number {\n\t\treturn Atomics.load(this.pointerMemory.data, 2);\n\t}\n\tget maxHash(): number {\n\t\treturn Atomics.load(this.pointerMemory.data, 3);\n\t}\n\n\tconstructor(memory: MemoryHeap, clone?: SharedMapMemory) {\n\t\tthis.memory = memory;\n\n\t\tif(clone) {\n\t\t\tthis.pointerMemory = new AllocatedMemory(memory, clone.firstBlock);\n\t\t} else {\n\t\t\tthis.pointerMemory = memory.allocUI32(SharedMap.ALLOCATE_COUNT);\n\t\t\tlet hashMemory = memory.allocUI32(DEFAULT_HASH_SIZE);\n\t\t\tstorePointer(this.pointerMemory.data, 0, hashMemory.bufferPosition, hashMemory.bufferByteOffset);\n\t\t\tAtomics.store(this.pointerMemory.data, 3, DEFAULT_HASH_SIZE);\n\t\t}\n\t\tthis.lock = new Int32Array(this.pointerMemory.data.buffer, this.pointerMemory.bufferByteOffset + Uint32Array.BYTES_PER_ELEMENT, 1);\n\t}\n\n\tset(key: K, value: number) {\n\t\tif(this.length >= this.maxHash * 2) {\n\t\t\tthis.growHashTable();\n\t\t}\n\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tif(this.setHashKey(this.hashMemory, this.maxHash, fullHashKey, value)) {\n\t\t\tAtomics.add(this.pointerMemory.data, 2, 1);\n\t\t}\n\t}\n\tprivate setHashKey(hashMemory: AllocatedMemory, maxHash: number, fullHashKey: number, value: number) {\n\t\tlet hashKey = this.hash(fullHashKey, maxHash);\n\t\t\n\t\tlet list: SharedList;\n\t\tlet pointer = loadPointer(hashMemory.data, hashKey);\n\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\t// Initialize a list here\n\t\t\tlist = new SharedList(this.memory, {\n\t\t\t\tdataLength: 2,\n\t\t\t});\n\n\t\t\tlet listMemory = list.getSharedMemory();\n\t\t\tstorePointer(hashMemory.data, hashKey, listMemory.firstBlock.bufferPosition, listMemory.firstBlock.bufferByteOffset);\n\t\t} else {\n\t\t\tlist = new SharedList(this.memory, {\n\t\t\t\tfirstBlock: pointer,\n\t\t\t});\n\t\t}\n\n\t\t// Check if any other items in list have the same key and delete them\n\t\tlet inserted = true;\n\t\tif(list.deleteValue(fullHashKey)) {\n\t\t\tinserted = false;\n\t\t}\n\t\tlist.insert([fullHashKey, value]);\n\t\t// An overwrite leaves a tombstone behind - reclaim it so buckets don't grow on repeated updates\n\t\tif(!inserted) {\n\t\t\tlist.compact();\n\t\t}\n\n\t\treturn inserted;\n\t}\n\n\tget(key: K): number | undefined {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tlet hashKey = this.hash(fullHashKey, this.maxHash);\n\n\t\tlet pointer = loadPointer(this.hashMemory.data, hashKey);\n\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tlet list = new SharedList(this.memory, {\n\t\t\tfirstBlock: pointer,\n\t\t});\n\n\t\tfor(let { data } of list) {\n\t\t\tif(data[0] === fullHashKey) {\n\t\t\t\treturn data[1];\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n\thas(key: K): boolean {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tlet hashKey = this.hash(fullHashKey, this.maxHash);\n\n\t\tlet pointer = loadPointer(this.hashMemory.data, hashKey);\n\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\treturn false;\n\t\t}\n\n\t\tlet list = new SharedList(this.memory, {\n\t\t\tfirstBlock: pointer,\n\t\t});\n\n\t\tfor(let { data } of list) {\n\t\t\tif(data[0] === fullHashKey) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tdelete(key: K): boolean {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tlet hashKey = this.hash(fullHashKey, this.maxHash);\n\n\t\tlet pointer = loadPointer(this.hashMemory.data, hashKey);\n\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\treturn false;\n\t\t}\n\n\t\tlet list = new SharedList(this.memory, {\n\t\t\tfirstBlock: pointer,\n\t\t});\n\n\t\tlet deleted = false;\n\t\tfor(let { data, deleteCurrent } of list) {\n\t\t\tif(data[0] === fullHashKey) {\n\t\t\t\tdeleteCurrent();\n\n\t\t\t\tAtomics.sub(this.pointerMemory.data, 2, 1);\n\t\t\t\tdeleted = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\t// compact only after iteration finishes - unlinking/freeing mid-iteration would corrupt the walk\n\t\tif(deleted) {\n\t\t\tlist.compact();\n\t\t}\n\n\t\treturn deleted;\n\t}\n\n\tprivate growHashTable() {\n\t\tlet oldMaxHash = this.maxHash;\n\t\tlet newMaxHash = oldMaxHash * 2;\n\t\tlet newHashMemory = this.memory.allocUI32(newMaxHash);\n\t\tlet oldHashMemory = this.hashMemory;\n\n\t\t// Copy each old hash value into new hash memory\n\t\tfor(let i = 0; i < oldMaxHash; i++) {\n\t\t\tlet pointer = loadPointer(oldHashMemory.data, i);\n\t\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet list = new SharedList(this.memory, {\n\t\t\t\tfirstBlock: pointer,\n\t\t\t});\n\t\t\tfor(let { data } of list) {\n\t\t\t\tthis.setHashKey(newHashMemory, newMaxHash, data[0], data[1]);\n\t\t\t}\n\n\t\t\t// The entries have been copied into freshly allocated lists in the new hash table so the old list can be freed\n\t\t\tlist.free();\n\t\t}\n\n\t\tstorePointer(this.pointerMemory.data, 0, newHashMemory.bufferPosition, newHashMemory.bufferByteOffset);\n\t\tAtomics.store(this.pointerMemory.data, 3, newMaxHash);\n\n\t\t// pointerMemory now references newHashMemory so the old hash table memory can be freed\n\t\toldHashMemory.free();\n\t}\n\n\tprivate hash(key: number, maxHash: number) {\n\t\treturn key % maxHash;\n\t}\n\n\tfree() {\n\t\t// Loop through and free lists in hash table first\n\t\tfor(let i = 0; i < this.maxHash; i++) {\n\t\t\tlet pointer = loadPointer(this.hashMemory.data, i);\n\t\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet list = new SharedList(this.memory, {\n\t\t\t\tfirstBlock: pointer,\n\t\t\t});\n\t\t\tlist.free();\n\t\t}\n\n\t\tthis.hashMemory.free();\n\t\tthis.pointerMemory.free();\n\t}\n\n\tgetSharedMemory(): SharedMapMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.pointerMemory.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedMapMemory {\n\tfirstBlock: SharedAllocatedMemory\n}\n\nfunction get32BitHash<K extends string | number>(key: K): number {\n\tif(typeof key === 'number') {\n\t\treturn key;\n\t} else if(typeof key === 'string') {\n\t\treturn hashString(key);\n\t} else {\n\t\treturn key;\n\t}\n}\n\n// Copied from https://github.com/mmomtchev/SharedMap/blob/master/index.js - MurmurHash2\nfunction hashString(str: string): number {\n\tlet\n\t\tl = str.length,\n\t\th = 17 ^ l,\n\t\ti = 0,\n\t\tk;\n\twhile(l >= 4) {\n\t\tk =\n\t\t\t((str.charCodeAt(i) & 0xff)) |\n\t\t\t((str.charCodeAt(++i) & 0xff) << 8) |\n\t\t\t((str.charCodeAt(++i) & 0xff) << 16) |\n\t\t\t((str.charCodeAt(++i) & 0xff) << 14);\n\t\tk = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));\n\t\tk ^= k >>> 14;\n\t\tk = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));\n\t\th = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16)) ^ k;\n\t\tl -= 4;\n\t\t++i;\n\t}\n\t/* eslint-disable no-fallthrough */\n\tswitch(l) {\n\t\tcase 3: h ^= (str.charCodeAt(i + 2) & 0xff) << 16;\n\t\tcase 2: h ^= (str.charCodeAt(i + 1) & 0xff) << 8;\n\t\tcase 1: h ^= (str.charCodeAt(i) & 0xff);\n\t\t\th = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));\n\t}\n\t/* eslint-enable no-fallthrough */\n\th ^= h >>> 13;\n\th = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));\n\th ^= h >>> 15;\n\th = h >>> 0;\n\treturn h;\n}\n\nexport type { SharedMapMemory };"],"mappings":";;;;AAQA,IAAM,IAAoB,IACL,IAArB,MAAqB,EAAqC;CACzD,OAAgB,iBAAiB;CAEjC;CAEA;CACA;CACA,IAAY,aAA8B;EACzC,OAAO,IAAI,EAAgB,KAAK,QAAQ,EAAY,KAAK,cAAc,MAAM,CAAC,CAAC;CAChF;CAEA,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAAK,KAAK,cAAc,MAAM,CAAC;CAC/C;CACA,IAAI,UAAkB;EACrB,OAAO,QAAQ,KAAK,KAAK,cAAc,MAAM,CAAC;CAC/C;CAEA,YAAY,GAAoB,GAAyB;EAGxD,IAFA,KAAK,SAAS,GAEX,GACF,KAAK,gBAAgB,IAAI,EAAgB,GAAQ,EAAM,UAAU;OAC3D;GACN,KAAK,gBAAgB,EAAO,UAAU,EAAU,cAAc;GAC9D,IAAI,IAAa,EAAO,UAAU,CAAiB;GAEnD,AADA,EAAa,KAAK,cAAc,MAAM,GAAG,EAAW,gBAAgB,EAAW,gBAAgB,GA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,GACJ,AADO,KAAM,EAAI,WAAW,CAAC,IAAI,KACjC,KAAO,IAAI,SAAU,gBAAkB,MAAM,MAAM,aAAc,UAAW;CAC9E;CAMA,OAJA,KAAK,MAAM,IACX,KAAO,IAAI,SAAU,gBAAkB,MAAM,MAAM,aAAc,UAAW,KAC5E,KAAK,MAAM,IACX,OAAU,GACH;AACR"}
|
|
1
|
+
{"version":3,"file":"shared-map.js","names":[],"sources":["../src/shared-map.ts"],"sourcesContent":["import AllocatedMemory, { type SharedAllocatedMemory } from './allocated-memory';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, storeRawPointer } from './utils/pointer';\nimport { get32BitHash, mix32 } from './utils/hash';\nimport { lock, unlock, SIMPLE_LOCK_ALLOCATE_COUNT } from './lock/simple-lock';\nimport { getArrayTypeCode, getByteMultipler, makeArrayView, type NumericArray, type NumericArrayIO, type TypedArrayConstructor } from './utils/array-type';\n\n// Open-addressing hash map with linear probing, held in one contiguous table (no per-bucket lists, no per-operation\n// allocation). A single exclusive lock guards the table so slot reads/writes inside the critical section can be plain\n// memory access instead of per-slot Atomics; the lock's acquire/release provides the cross-thread happens-before.\n//\n// The table uses a Structure-of-Arrays layout: three contiguous regions - states | keys | values - so probing touches\n// the dense states and keys regions (more candidates per cache line) and iteration walks values densely. capacity is\n// always a power of two, so slot = mix(key) & (capacity - 1). state distinguishes EMPTY/FULL/TOMBSTONE, so key 0 and\n// value 0 need no sentinel.\n//\n// Keys are always stored as the 32-bit hash (a numeric key is its own hash); values are stored in the typed array the\n// map was created with (Uint32Array by default, or Int32Array / Float32Array). Iteration is a best-effort scan that is\n// not lock-guarded, so it must not run concurrently with mutation from another thread. A single allocation can't span\n// buffers, so the table is bounded by MemoryHeap.maxAllocationLength.\n\nconst EMPTY = 0;\nconst FULL = 1;\nconst TOMBSTONE = 2;\n\nconst DEFAULT_CAPACITY = 16;\n// Grow when (occupied + tombstones) / capacity exceeds this. Kept as a fraction to avoid floating point in the hot path.\nconst LOAD_NUMERATOR = 3;\nconst LOAD_DENOMINATOR = 4;\n\n// Metadata block layout (pointerMemory)\nconst TABLE_POINTER_INDEX = 0;\nconst LOCK_INDEX = 1;\nconst LENGTH_INDEX = 2;\nconst TOMBSTONE_INDEX = 3;\nconst CAPACITY_INDEX = 4;\nconst TYPE_INDEX = 5;\n\nexport default class SharedMap<K extends string | number, V extends NumericArray = Uint32Array> {\n\tstatic readonly ALLOCATE_COUNT = 6;\n\n\tprivate memory: MemoryHeap;\n\tprivate pointerMemory: AllocatedMemory;\n\t// False when pointerMemory is a slice of another structure's memory (e.g. inlined in SharedQuadtree); that owner frees it\n\tprivate ownsFirstBlock = true;\n\tprivate lock: Int32Array;\n\t// Value type is written once at creation and never changes, so resolve it once instead of reading metadata per op\n\tprivate cachedType: number;\n\t// u32 units per value slot: 1 for 32-bit types, 2 for 64-bit (float64/int64/uint64). The values region and every\n\t// table allocation scale by this.\n\tprivate cachedValueUnits = 1;\n\n\t// Cache the resolved views so most operations avoid rebuilding them. Rebuild whenever the table pointer OR the capacity\n\t// changes: the pointer alone is not enough because the allocator recycles freed addresses (split:false), so a rehash can\n\t// land a differently-sized table at the same address (ABA). The cached views are plain windows into shared memory, so\n\t// they always reflect the current bytes at that address; only the derived capacity (mask/offsets) can go stale, and the\n\t// capacity check catches exactly that.\n\tprivate cachedTablePointer = 0;\n\tprivate cachedTableMemory?: AllocatedMemory;\n\tprivate cachedSlots?: Uint32Array; // whole block as u32: states region, then keys region\n\tprivate cachedValues?: V; // typed view over the values region\n\tprivate cachedCapacity = 0;\n\t// Callers hold the lock, so the acquire fence already published the latest pointer/capacity - plain reads are enough\n\t// and avoid two Atomics.load per operation on the hot path.\n\tprivate ensureTable() {\n\t\tlet data = this.pointerMemory.data;\n\t\tlet pointer = data[TABLE_POINTER_INDEX];\n\t\tlet capacity = data[CAPACITY_INDEX];\n\t\tif(pointer === this.cachedTablePointer && capacity === this.cachedCapacity && this.cachedSlots) {\n\t\t\treturn;\n\t\t}\n\n\t\tlet table = new AllocatedMemory(this.memory, getPointer(pointer, this.memory.positionBits));\n\t\tthis.cachedTableMemory = table;\n\t\tthis.cachedSlots = table.data;\n\t\tthis.cachedValues = this.makeValuesView(table, capacity);\n\t\tthis.cachedCapacity = capacity;\n\t\tthis.cachedTablePointer = pointer;\n\t}\n\tprivate makeValuesView(table: AllocatedMemory, capacity: number): V {\n\t\t// states and keys each take one u32 per slot, so the values region starts after 2 * capacity u32s\n\t\tlet byteOffset = table.bufferByteOffset + capacity * 2 * Uint32Array.BYTES_PER_ELEMENT;\n\t\treturn makeArrayView(this.cachedType, table.data.buffer, byteOffset, capacity) as V;\n\t}\n\t// u32s a table of this capacity needs: states + keys (one each per slot) plus the values region, which is\n\t// cachedValueUnits u32s per slot.\n\tprivate tableLength(capacity: number): number {\n\t\treturn capacity * (2 + this.cachedValueUnits);\n\t}\n\n\tget length(): number {\n\t\treturn Atomics.load(this.pointerMemory.data, LENGTH_INDEX);\n\t}\n\tget capacity(): number {\n\t\treturn Atomics.load(this.pointerMemory.data, CAPACITY_INDEX);\n\t}\n\tget maxHash(): number {\n\t\treturn this.capacity;\n\t}\n\tget type(): number {\n\t\treturn this.cachedType;\n\t}\n\t// Metadata block plus the single table allocation the map currently points at.\n\tget usedMemory(): number {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tthis.ensureTable();\n\t\t\t// When inlined, the owner already counts pointerMemory as part of its own firstBlock\n\t\t\tlet ownMemory = this.ownsFirstBlock ? this.pointerMemory.usedMemory : 0;\n\t\t\treturn ownMemory + this.cachedTableMemory!.usedMemory;\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedMapConfig<V> | SharedMapMemory | SharedMapMemory & SharedMapConfig<V>) {\n\t\tthis.memory = memory;\n\n\t\tif(config && 'firstBlock' in config) {\n\t\t\t// An AllocatedMemory instance is a caller-reserved slice (e.g. inlined in SharedQuadtree) for us to initialize in\n\t\t\t// place; a plain pointer is the serialized clone path, whose table already exists and is read from metadata.\n\t\t\tif(config.firstBlock instanceof AllocatedMemory) {\n\t\t\t\tthis.pointerMemory = config.firstBlock;\n\t\t\t\tthis.initialize(config);\n\t\t\t\tthis.ownsFirstBlock = false;\n\t\t\t} else {\n\t\t\t\tthis.pointerMemory = new AllocatedMemory(memory, config.firstBlock);\n\t\t\t}\n\t\t} else {\n\t\t\tthis.pointerMemory = memory.allocUI32(SharedMap.ALLOCATE_COUNT);\n\t\t\tthis.initialize(config);\n\t\t}\n\t\tthis.lock = new Int32Array(this.pointerMemory.data.buffer, this.pointerMemory.bufferByteOffset + LOCK_INDEX * Uint32Array.BYTES_PER_ELEMENT, SIMPLE_LOCK_ALLOCATE_COUNT);\n\t\tthis.cachedType = Atomics.load(this.pointerMemory.data, TYPE_INDEX);\n\t\tthis.cachedValueUnits = getByteMultipler(this.cachedType);\n\t}\n\n\t// Allocate the initial table and write the metadata. pointerMemory must already be set. Used by both the fresh-alloc\n\t// path and the inline-in-place path (where pointerMemory is a slice reserved by the owner).\n\tprivate initialize(config?: SharedMapConfig<V> | SharedMapMemory) {\n\t\tlet type = config && 'type' in config ? config.type : undefined;\n\t\tlet typeCode = getArrayTypeCode(type ?? Uint32Array);\n\t\tthis.cachedValueUnits = getByteMultipler(typeCode);\n\t\tlet requestedCapacity = config && 'capacity' in config ? config.capacity : undefined;\n\t\tlet capacity = DEFAULT_CAPACITY;\n\t\twhile(capacity < (requestedCapacity ?? DEFAULT_CAPACITY)) {\n\t\t\tcapacity *= 2;\n\t\t}\n\t\tif(capacity * (2 + this.cachedValueUnits) > this.memory.maxAllocationLength) {\n\t\t\tthrow new Error(`SharedMap capacity ${capacity} exceeds the largest contiguous allocation`);\n\t\t}\n\t\tlet table = this.memory.allocUI32(capacity * (2 + this.cachedValueUnits));\n\t\tstoreRawPointer(this.pointerMemory.data, TABLE_POINTER_INDEX, table.pointer);\n\t\tAtomics.store(this.pointerMemory.data, CAPACITY_INDEX, capacity);\n\t\tAtomics.store(this.pointerMemory.data, TYPE_INDEX, typeCode);\n\t}\n\n\tset(key: K, value: V[number]) {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tthis.setNoLock(key, value);\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tsetAll(entries: Iterable<readonly [K, V[number]]>) {\n\t\tlet batch = Array.from(entries);\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tlet data = this.pointerMemory.data;\n\t\t\tlet projected = data[LENGTH_INDEX] + data[TOMBSTONE_INDEX] + batch.length;\n\t\t\tif(projected * LOAD_DENOMINATOR > data[CAPACITY_INDEX] * LOAD_NUMERATOR) {\n\t\t\t\tlet capacity = data[CAPACITY_INDEX];\n\t\t\t\twhile(projected * LOAD_DENOMINATOR > capacity * LOAD_NUMERATOR) {\n\t\t\t\t\tcapacity *= 2;\n\t\t\t\t}\n\t\t\t\tthis.resize(capacity);\n\t\t\t}\n\t\t\tfor(let [key, value] of batch) {\n\t\t\t\tthis.setNoLock(key, value);\n\t\t\t}\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tprivate setNoLock(key: K, value: V[number]) {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tlet data = this.pointerMemory.data;\n\t\tlet length = data[LENGTH_INDEX];\n\t\tlet tombstones = data[TOMBSTONE_INDEX];\n\t\tif((length + tombstones + 1) * LOAD_DENOMINATOR > data[CAPACITY_INDEX] * LOAD_NUMERATOR) {\n\t\t\tthis.resize();\n\t\t}\n\n\t\tthis.ensureTable();\n\t\tlet slots = this.cachedSlots!;\n\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\tlet capacity = this.cachedCapacity;\n\t\tlet mask = capacity - 1;\n\t\tlet keysBase = capacity;\n\t\tlet idx = mix32(fullHashKey) & mask;\n\t\tlet firstTomb = -1;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\tlet state = slots[idx];\n\t\t\tif(state === EMPTY) {\n\t\t\t\tlet target = firstTomb >= 0 ? firstTomb : idx;\n\t\t\t\tslots[target] = FULL;\n\t\t\t\tslots[keysBase + target] = fullHashKey;\n\t\t\t\tvalues[target] = value;\n\t\t\t\tdata[LENGTH_INDEX]++;\n\t\t\t\tif(firstTomb >= 0) {\n\t\t\t\t\tdata[TOMBSTONE_INDEX]--;\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t} else if(state === TOMBSTONE) {\n\t\t\t\tif(firstTomb < 0) {\n\t\t\t\t\tfirstTomb = idx;\n\t\t\t\t}\n\t\t\t} else if(slots[keysBase + idx] === fullHashKey) {\n\t\t\t\tvalues[idx] = value;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tidx = (idx + 1) & mask;\n\t\t}\n\t}\n\n\t// Atomic read-modify-write under the table lock: resolves the current value (undefined if the key is\n\t// absent), calls updater, and stores the result - so two threads mutating the same key never lose an\n\t// update the way a separate get() + set() can. updater runs inside the lock and must be synchronous and\n\t// must not touch this same map (the lock is not reentrant).\n\tupdate(key: K, updater: (current: V[number] | undefined) => V[number]): V[number] {\n\t\tlet fullHashKey = get32BitHash(key);\n\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tlet data = this.pointerMemory.data;\n\t\t\tlet length = data[LENGTH_INDEX];\n\t\t\tlet tombstones = data[TOMBSTONE_INDEX];\n\t\t\tif((length + tombstones + 1) * LOAD_DENOMINATOR > data[CAPACITY_INDEX] * LOAD_NUMERATOR) {\n\t\t\t\tthis.resize();\n\t\t\t}\n\n\t\t\tthis.ensureTable();\n\t\t\tlet slots = this.cachedSlots!;\n\t\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\t\tlet capacity = this.cachedCapacity;\n\t\t\tlet mask = capacity - 1;\n\t\t\tlet keysBase = capacity;\n\t\t\tlet idx = mix32(fullHashKey) & mask;\n\t\t\tlet firstTomb = -1;\n\t\t\t// eslint-disable-next-line no-constant-condition\n\t\t\twhile(true) {\n\t\t\t\tlet state = slots[idx];\n\t\t\t\tif(state === EMPTY) {\n\t\t\t\t\tlet newValue = updater(undefined);\n\t\t\t\t\tlet target = firstTomb >= 0 ? firstTomb : idx;\n\t\t\t\t\tslots[target] = FULL;\n\t\t\t\t\tslots[keysBase + target] = fullHashKey;\n\t\t\t\t\tvalues[target] = newValue;\n\t\t\t\t\tdata[LENGTH_INDEX]++;\n\t\t\t\t\tif(firstTomb >= 0) {\n\t\t\t\t\t\tdata[TOMBSTONE_INDEX]--;\n\t\t\t\t\t}\n\t\t\t\t\treturn newValue;\n\t\t\t\t} else if(state === TOMBSTONE) {\n\t\t\t\t\tif(firstTomb < 0) {\n\t\t\t\t\t\tfirstTomb = idx;\n\t\t\t\t\t}\n\t\t\t\t} else if(slots[keysBase + idx] === fullHashKey) {\n\t\t\t\t\tlet newValue = updater(values[idx]);\n\t\t\t\t\tvalues[idx] = newValue;\n\t\t\t\t\treturn newValue;\n\t\t\t\t}\n\n\t\t\t\tidx = (idx + 1) & mask;\n\t\t\t}\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tget(key: K): V[number] | undefined {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\treturn this.getNoLock(key);\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tgetAll(keys: Iterable<K>): Array<V[number] | undefined> {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tlet values: Array<V[number] | undefined> = [];\n\t\t\tfor(let key of keys) {\n\t\t\t\tvalues.push(this.getNoLock(key));\n\t\t\t}\n\t\t\treturn values;\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tprivate getNoLock(key: K): V[number] | undefined {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tthis.ensureTable();\n\t\tlet slots = this.cachedSlots!;\n\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\tlet capacity = this.cachedCapacity;\n\t\tlet mask = capacity - 1;\n\t\tlet keysBase = capacity;\n\t\tlet idx = mix32(fullHashKey) & mask;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\tlet state = slots[idx];\n\t\t\tif(state === EMPTY) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tif(state === FULL && slots[keysBase + idx] === fullHashKey) {\n\t\t\t\treturn values[idx];\n\t\t\t}\n\t\t\tidx = (idx + 1) & mask;\n\t\t}\n\t}\n\thas(key: K): boolean {\n\t\treturn this.get(key) !== undefined;\n\t}\n\n\tdelete(key: K): boolean {\n\t\tlet fullHashKey = get32BitHash(key);\n\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tthis.ensureTable();\n\t\t\tlet data = this.pointerMemory.data;\n\t\t\tlet slots = this.cachedSlots!;\n\t\t\tlet capacity = this.cachedCapacity;\n\t\t\tlet mask = capacity - 1;\n\t\t\tlet keysBase = capacity;\n\t\t\tlet idx = mix32(fullHashKey) & mask;\n\t\t\t// eslint-disable-next-line no-constant-condition\n\t\t\twhile(true) {\n\t\t\t\tlet state = slots[idx];\n\t\t\t\tif(state === EMPTY) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\tif(state === FULL && slots[keysBase + idx] === fullHashKey) {\n\t\t\t\t\tslots[idx] = TOMBSTONE;\n\t\t\t\t\tdata[LENGTH_INDEX]--;\n\t\t\t\t\tdata[TOMBSTONE_INDEX]++;\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tidx = (idx + 1) & mask;\n\t\t\t}\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\t// Best-effort scan (see class note): not lock-guarded, so must not run concurrently with writes from another thread.\n\t*[Symbol.iterator](): IterableIterator<[number, V[number]]> {\n\t\tthis.ensureTable();\n\t\tlet slots = this.cachedSlots!;\n\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\tlet capacity = this.cachedCapacity;\n\t\tlet keysBase = capacity;\n\t\tfor(let i = 0; i < capacity; i++) {\n\t\t\tif(slots[i] === FULL) {\n\t\t\t\tyield [slots[keysBase + i], values[i]];\n\t\t\t}\n\t\t}\n\t}\n\n\t// Rehash into a fresh table. Doubles capacity when live entries are crowding the table; when tombstones are the cause,\n\t// a same-size table already reclaims them. Caller must hold the lock.\n\tprivate resize(requestedCapacity?: number) {\n\t\tthis.ensureTable();\n\t\tlet oldTable = this.cachedTableMemory!;\n\t\tlet oldSlots = this.cachedSlots!;\n\t\tlet oldValues = this.cachedValues! as NumericArrayIO;\n\t\tlet oldCapacity = this.cachedCapacity;\n\t\tlet oldKeysBase = oldCapacity;\n\t\tlet length = Atomics.load(this.pointerMemory.data, LENGTH_INDEX);\n\n\t\tlet newCapacity = requestedCapacity ?? oldCapacity;\n\t\tif(requestedCapacity === undefined && (length + 1) * LOAD_DENOMINATOR > oldCapacity * LOAD_NUMERATOR) {\n\t\t\tnewCapacity = oldCapacity * 2;\n\t\t}\n\t\t// A table is one contiguous allocation, so it can never exceed what a single buffer can hand out. Fail here with an\n\t\t// actionable message instead of letting allocUI32 throw a cryptic \"Unable to allocate N numbers\" from deep inside the\n\t\t// heap. Mirrors the same guard in initialize().\n\t\tif(this.tableLength(newCapacity) > this.memory.maxAllocationLength) {\n\t\t\tlet maxEntries = Math.floor(this.memory.maxAllocationLength / (2 + this.cachedValueUnits) * LOAD_NUMERATOR / LOAD_DENOMINATOR);\n\t\t\tthrow new Error(`SharedMap can't grow to capacity ${newCapacity}: its table would exceed the largest contiguous `\n\t\t\t\t+ `allocation (${this.memory.maxAllocationLength} u32s, ~${maxEntries} entries). Use larger buffers or shard across multiple maps.`);\n\t\t}\n\t\tlet newTable = this.memory.allocUI32(this.tableLength(newCapacity));\n\t\tlet newSlots = newTable.data;\n\t\tlet newValues = this.makeValuesView(newTable, newCapacity) as NumericArrayIO;\n\t\tlet mask = newCapacity - 1;\n\t\tlet newKeysBase = newCapacity;\n\n\t\tfor(let i = 0; i < oldCapacity; i++) {\n\t\t\tif(oldSlots[i] !== FULL) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet key = oldSlots[oldKeysBase + i];\n\t\t\tlet value = oldValues[i];\n\t\t\tlet idx = mix32(key) & mask;\n\t\t\t// Fresh table has no tombstones, so probe straight to the first empty slot\n\t\t\twhile(newSlots[idx] !== EMPTY) {\n\t\t\t\tidx = (idx + 1) & mask;\n\t\t\t}\n\t\t\tnewSlots[idx] = FULL;\n\t\t\tnewSlots[newKeysBase + idx] = key;\n\t\t\tnewValues[idx] = value;\n\t\t}\n\n\t\tstoreRawPointer(this.pointerMemory.data, TABLE_POINTER_INDEX, newTable.pointer);\n\t\tAtomics.store(this.pointerMemory.data, CAPACITY_INDEX, newCapacity);\n\t\tAtomics.store(this.pointerMemory.data, TOMBSTONE_INDEX, 0);\n\t\t// Invalidate the cached views so the next access rebuilds against the new table\n\t\tthis.cachedTablePointer = 0;\n\t\tthis.cachedSlots = undefined;\n\t\toldTable.free();\n\t}\n\n\tfree() {\n\t\tthis.ensureTable();\n\t\tthis.cachedTableMemory!.free();\n\t\tif(this.ownsFirstBlock) {\n\t\t\tthis.pointerMemory.free();\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedMapMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.pointerMemory.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedMapConfig<V extends NumericArray> {\n\ttype?: TypedArrayConstructor<V>\n\tcapacity?: number\n}\ninterface SharedMapMemory {\n\tfirstBlock: SharedAllocatedMemory | AllocatedMemory\n}\n\nexport type { SharedMapConfig, SharedMapMemory };\n"],"mappings":";;;;;;AAqBA,IAAM,IAAQ,GACR,IAAO,GACP,IAAY,GAEZ,IAAmB,IAEnB,IAAiB,GACjB,IAAmB,GAGnB,IAAsB,GACtB,IAAa,GACb,IAAe,GACf,IAAkB,GAClB,IAAiB,GACjB,IAAa,GAEE,IAArB,MAAqB,EAA2E;CAC/F,OAAgB,iBAAiB;CAEjC;CACA;CAEA,iBAAyB;CACzB;CAEA;CAGA,mBAA2B;CAO3B,qBAA6B;CAC7B;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,KAAK,YAAY,CAAW,CAAC,GAC9D,IAAW,EAAS,MACpB,IAAY,KAAK,eAAe,GAAU,CAAW,GACrD,IAAO,IAAc,GACrB,IAAc;EAElB,KAAI,IAAI,IAAI,GAAG,IAAI,GAAa,KAAK;GACpC,IAAG,EAAS,OAAO,GAClB;GAGD,IAAI,IAAM,EAAS,IAAc,IAC7B,IAAQ,EAAU,IAClB,IAAM,EAAM,CAAG,IAAI;GAEvB,OAAM,EAAS,OAAS,IACvB,IAAO,IAAM,IAAK;GAInB,AAFA,EAAS,KAAO,GAChB,EAAS,IAAc,KAAO,GAC9B,EAAU,KAAO;EAClB;EAQA,AANA,EAAgB,KAAK,cAAc,MAAM,GAAqB,EAAS,OAAO,GAC9E,QAAQ,MAAM,KAAK,cAAc,MAAM,GAAgB,CAAW,GAClE,QAAQ,MAAM,KAAK,cAAc,MAAM,GAAiB,CAAC,GAEzD,KAAK,qBAAqB,GAC1B,KAAK,cAAc,KAAA,GACnB,EAAS,KAAK;CACf;CAEA,OAAO;EAGN,AAFA,KAAK,YAAY,GACjB,KAAK,kBAAmB,KAAK,GAC1B,KAAK,kBACP,KAAK,cAAc,KAAK;CAE1B;CAEA,kBAAmC;EAClC,OAAO,EACN,YAAY,KAAK,cAAc,gBAAgB,EAChD;CACD;AACD"}
|
|
@@ -12,10 +12,10 @@ var i = class {
|
|
|
12
12
|
return this.list.length;
|
|
13
13
|
}
|
|
14
14
|
insert(t) {
|
|
15
|
-
this.list.insert(e(t.memory.bufferPosition, t.memory.bufferByteOffset));
|
|
15
|
+
this.list.insert(e(t.memory.bufferPosition, t.memory.bufferByteOffset, this.memory.positionBits));
|
|
16
16
|
}
|
|
17
17
|
delete(t) {
|
|
18
|
-
let n = this.list.deleteValue(e(t.memory.bufferPosition, t.memory.bufferByteOffset));
|
|
18
|
+
let n = this.list.deleteValue(e(t.memory.bufferPosition, t.memory.bufferByteOffset, this.memory.positionBits));
|
|
19
19
|
return this.list.compact(), n;
|
|
20
20
|
}
|
|
21
21
|
compact() {
|
|
@@ -24,7 +24,7 @@ var i = class {
|
|
|
24
24
|
*[Symbol.iterator]() {
|
|
25
25
|
let e = this.list[Symbol.iterator]();
|
|
26
26
|
for (let { data: r } of e) {
|
|
27
|
-
let { bufferPosition: e, bufferByteOffset: i } = t(r, 0), a = new n(this.memory, {
|
|
27
|
+
let { bufferPosition: e, bufferByteOffset: i } = t(r, 0, this.memory.positionBits), a = new n(this.memory, {
|
|
28
28
|
bufferPosition: e,
|
|
29
29
|
bufferByteOffset: i
|
|
30
30
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-pointer-list.js","names":[],"sources":["../src/shared-pointer-list.ts"],"sourcesContent":["import AllocatedMemory from './allocated-memory';\nimport type MemoryHeap from './memory-heap';\nimport type { SharedListMemory } from './shared-list';\nimport SharedList from './shared-list';\nimport { createPointer, loadPointer } from './utils/pointer';\n\nexport default abstract class SharedPointerList<T extends PointerItem> implements Iterable<T> {\n\tprivate memory: MemoryHeap;\n\tprivate list: SharedList;\n\n\tconstructor(heap: MemoryHeap, memory?: SharedListMemory) {\n\t\tthis.memory = heap;\n\n\t\tif(memory) {\n\t\t\tthis.list = new SharedList(heap, memory);\n\t\t} else {\n\t\t\tthis.list = new SharedList(heap);\n\t\t}\n\t}\n\n\tget length() {\n\t\treturn this.list.length;\n\t}\n\n\tinsert(item: T) {\n\t\tthis.list.insert(createPointer(item.memory.bufferPosition, item.memory.bufferByteOffset));\n\t}\n\tdelete(item: T) {\n\t\tlet deleted = this.list.deleteValue(createPointer(item.memory.bufferPosition, item.memory.bufferByteOffset));\n\t\t// This wrapper is single-threaded per instance, so reclaim the tombstoned node right away\n\t\tthis.list.compact();\n\t\treturn deleted;\n\t}\n\n\tcompact() {\n\t\tthis.list.compact();\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet iterator = this.list[Symbol.iterator]();\n\n\t\tfor(let { data: pointerData } of iterator) {\n\t\t\tlet { bufferPosition, bufferByteOffset } = loadPointer(pointerData, 0);\n\t\t\tlet allocatedMemory = new AllocatedMemory(this.memory, {\n\t\t\t\tbufferPosition,\n\t\t\t\tbufferByteOffset,\n\t\t\t});\n\t\t\tyield this.createItem(allocatedMemory);\n\t\t}\n\t}\n\tforEach(callback: (item: T) => void) {\n\t\tfor(let value of this) {\n\t\t\tcallback(value);\n\t\t}\n\t}\n\n\tfind(callback: (item: T) => boolean): T | undefined {\n\t\tfor(let value of this) {\n\t\t\tif(callback(value)) {\n\t\t\t\treturn value;\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\tgetSharedMemory() {\n\t\treturn this.list.getSharedMemory();\n\t}\n\n\tprotected abstract createItem(allocatedMemory: AllocatedMemory): T;\n\n\tfree() {\n\t\tfor(let item of this) {\n\t\t\t// NOTE: Anything that allocates it's own memory (ie: type for Item) needs to call free for that class so we can clear all memory recursively\n\t\t\tif('free' in item && typeof item.free === 'function') {\n\t\t\t\titem.free();\n\t\t\t} else {\n\t\t\t\titem.memory.free();\n\t\t\t}\n\t\t}\n\n\t\tthis.list.free();\n\t}\n}\n\ninterface PointerItem {\n\treadonly memory: AllocatedMemory\n}"],"mappings":";;;;AAMA,IAA8B,IAA9B,MAA8F;CAC7F;CACA;CAEA,YAAY,GAAkB,GAA2B;EAGxD,AAFA,KAAK,SAAS,GAEd,AAGC,KAAK,OAHH,IACU,IAAI,EAAW,GAAM,CAAM,IAE3B,IAAI,EAAW,CAAI;CAEjC;CAEA,IAAI,SAAS;EACZ,OAAO,KAAK,KAAK;CAClB;CAEA,OAAO,GAAS;EACf,KAAK,KAAK,OAAO,EAAc,EAAK,OAAO,gBAAgB,EAAK,OAAO,
|
|
1
|
+
{"version":3,"file":"shared-pointer-list.js","names":[],"sources":["../src/shared-pointer-list.ts"],"sourcesContent":["import AllocatedMemory from './allocated-memory';\nimport type MemoryHeap from './memory-heap';\nimport type { SharedListMemory } from './shared-list';\nimport SharedList from './shared-list';\nimport { createPointer, loadPointer } from './utils/pointer';\n\nexport default abstract class SharedPointerList<T extends PointerItem> implements Iterable<T> {\n\tprivate memory: MemoryHeap;\n\tprivate list: SharedList;\n\n\tconstructor(heap: MemoryHeap, memory?: SharedListMemory) {\n\t\tthis.memory = heap;\n\n\t\tif(memory) {\n\t\t\tthis.list = new SharedList(heap, memory);\n\t\t} else {\n\t\t\tthis.list = new SharedList(heap);\n\t\t}\n\t}\n\n\tget length() {\n\t\treturn this.list.length;\n\t}\n\n\tinsert(item: T) {\n\t\tthis.list.insert(createPointer(item.memory.bufferPosition, item.memory.bufferByteOffset, this.memory.positionBits));\n\t}\n\tdelete(item: T) {\n\t\tlet deleted = this.list.deleteValue(createPointer(item.memory.bufferPosition, item.memory.bufferByteOffset, this.memory.positionBits));\n\t\t// This wrapper is single-threaded per instance, so reclaim the tombstoned node right away\n\t\tthis.list.compact();\n\t\treturn deleted;\n\t}\n\n\tcompact() {\n\t\tthis.list.compact();\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet iterator = this.list[Symbol.iterator]();\n\n\t\tfor(let { data: pointerData } of iterator) {\n\t\t\tlet { bufferPosition, bufferByteOffset } = loadPointer(pointerData, 0, this.memory.positionBits);\n\t\t\tlet allocatedMemory = new AllocatedMemory(this.memory, {\n\t\t\t\tbufferPosition,\n\t\t\t\tbufferByteOffset,\n\t\t\t});\n\t\t\tyield this.createItem(allocatedMemory);\n\t\t}\n\t}\n\tforEach(callback: (item: T) => void) {\n\t\tfor(let value of this) {\n\t\t\tcallback(value);\n\t\t}\n\t}\n\n\tfind(callback: (item: T) => boolean): T | undefined {\n\t\tfor(let value of this) {\n\t\t\tif(callback(value)) {\n\t\t\t\treturn value;\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\tgetSharedMemory() {\n\t\treturn this.list.getSharedMemory();\n\t}\n\n\tprotected abstract createItem(allocatedMemory: AllocatedMemory): T;\n\n\tfree() {\n\t\tfor(let item of this) {\n\t\t\t// NOTE: Anything that allocates it's own memory (ie: type for Item) needs to call free for that class so we can clear all memory recursively\n\t\t\tif('free' in item && typeof item.free === 'function') {\n\t\t\t\titem.free();\n\t\t\t} else {\n\t\t\t\titem.memory.free();\n\t\t\t}\n\t\t}\n\n\t\tthis.list.free();\n\t}\n}\n\ninterface PointerItem {\n\treadonly memory: AllocatedMemory\n}"],"mappings":";;;;AAMA,IAA8B,IAA9B,MAA8F;CAC7F;CACA;CAEA,YAAY,GAAkB,GAA2B;EAGxD,AAFA,KAAK,SAAS,GAEd,AAGC,KAAK,OAHH,IACU,IAAI,EAAW,GAAM,CAAM,IAE3B,IAAI,EAAW,CAAI;CAEjC;CAEA,IAAI,SAAS;EACZ,OAAO,KAAK,KAAK;CAClB;CAEA,OAAO,GAAS;EACf,KAAK,KAAK,OAAO,EAAc,EAAK,OAAO,gBAAgB,EAAK,OAAO,kBAAkB,KAAK,OAAO,YAAY,CAAC;CACnH;CACA,OAAO,GAAS;EACf,IAAI,IAAU,KAAK,KAAK,YAAY,EAAc,EAAK,OAAO,gBAAgB,EAAK,OAAO,kBAAkB,KAAK,OAAO,YAAY,CAAC;EAGrI,OADA,KAAK,KAAK,QAAQ,GACX;CACR;CAEA,UAAU;EACT,KAAK,KAAK,QAAQ;CACnB;CAEA,EAAE,OAAO,YAAY;EACpB,IAAI,IAAW,KAAK,KAAK,OAAO,SAAS,CAAC;EAE1C,KAAI,IAAI,EAAE,MAAM,OAAiB,GAAU;GAC1C,IAAI,EAAE,mBAAgB,wBAAqB,EAAY,GAAa,GAAG,KAAK,OAAO,YAAY,GAC3F,IAAkB,IAAI,EAAgB,KAAK,QAAQ;IACtD;IACA;GACD,CAAC;GACD,MAAM,KAAK,WAAW,CAAe;EACtC;CACD;CACA,QAAQ,GAA6B;EACpC,KAAI,IAAI,KAAS,MAChB,EAAS,CAAK;CAEhB;CAEA,KAAK,GAA+C;EACnD,KAAI,IAAI,KAAS,MAChB,IAAG,EAAS,CAAK,GAChB,OAAO;CAKV;CAEA,kBAAkB;EACjB,OAAO,KAAK,KAAK,gBAAgB;CAClC;CAIA,OAAO;EACN,KAAI,IAAI,KAAQ,MAEf,AAAG,UAAU,KAAQ,OAAO,EAAK,QAAS,aACzC,EAAK,KAAK,IAEV,EAAK,OAAO,KAAK;EAInB,KAAK,KAAK,KAAK;CAChB;AACD"}
|
package/dist/shared-pool.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { SharedAllocatedMemory } from './allocated-memory';
|
|
2
|
-
import
|
|
2
|
+
import AllocatedMemory from './allocated-memory';
|
|
3
3
|
import type MemoryHeap from './memory-heap';
|
|
4
|
-
|
|
4
|
+
import { type NumericArray, type TypedArrayConstructor } from './utils/array-type';
|
|
5
|
+
export default class SharedPool<T extends NumericArray = Uint32Array> implements Iterable<T> {
|
|
5
6
|
static readonly ALLOCATE_COUNT = 6;
|
|
7
|
+
static getAllocateCount(memory: MemoryHeap, config?: SharedPoolConfig<NumericArray>): number;
|
|
6
8
|
private memory;
|
|
7
9
|
private firstBlock;
|
|
8
10
|
private uint16Array;
|
|
@@ -10,6 +12,7 @@ export default class SharedPool<T extends Uint32Array | Int32Array | Float32Arra
|
|
|
10
12
|
private pointerStack;
|
|
11
13
|
private recycleStack;
|
|
12
14
|
readonly maxLength: number;
|
|
15
|
+
private ownsFirstBlock;
|
|
13
16
|
private cachedFullDataBlock;
|
|
14
17
|
private readonly cachedMaxChunkSize;
|
|
15
18
|
private readonly cachedDataLength;
|
|
@@ -25,19 +28,25 @@ export default class SharedPool<T extends Uint32Array | Int32Array | Float32Arra
|
|
|
25
28
|
get bufferLength(): number;
|
|
26
29
|
get byteMultipler(): number;
|
|
27
30
|
get usedMemory(): number;
|
|
28
|
-
constructor(memory: MemoryHeap, config?: SharedPoolConfig<T> | SharedPoolMemory);
|
|
31
|
+
constructor(memory: MemoryHeap, config?: SharedPoolConfig<T> | SharedPoolMemory | (SharedPoolMemory & SharedPoolConfig<T>));
|
|
32
|
+
private initialize;
|
|
29
33
|
at(index: number): T;
|
|
30
|
-
get(index: number, dataIndex?: number): number;
|
|
31
|
-
|
|
34
|
+
get(index: number, dataIndex?: number): T[number];
|
|
35
|
+
set(index: number, dataIndex: number, value: T[number]): void;
|
|
36
|
+
blockFor(index: number): T;
|
|
37
|
+
blockOffset(index: number): number;
|
|
38
|
+
push(values: T[number] | Array<T[number]>): number;
|
|
39
|
+
reserveContiguous(count: number): number;
|
|
32
40
|
deleteIndex(index: number): void;
|
|
33
41
|
clear(): void;
|
|
34
42
|
[Symbol.iterator](): Generator<T, void, unknown>;
|
|
43
|
+
entries(): IterableIterator<[number, T]>;
|
|
35
44
|
private getFullDataBlock;
|
|
36
45
|
private getDataBlock;
|
|
37
46
|
free(): void;
|
|
38
47
|
getSharedMemory(): SharedPoolMemory;
|
|
39
48
|
}
|
|
40
|
-
interface SharedPoolConfig<T extends
|
|
49
|
+
interface SharedPoolConfig<T extends NumericArray> {
|
|
41
50
|
maxChunkSize?: number;
|
|
42
51
|
maxLength?: number;
|
|
43
52
|
maxRecycledLength?: number;
|
|
@@ -45,6 +54,6 @@ interface SharedPoolConfig<T extends Uint32Array | Int32Array | Float32Array | F
|
|
|
45
54
|
dataLength?: number;
|
|
46
55
|
}
|
|
47
56
|
interface SharedPoolMemory {
|
|
48
|
-
firstBlock: SharedAllocatedMemory;
|
|
57
|
+
firstBlock: SharedAllocatedMemory | AllocatedMemory;
|
|
49
58
|
}
|
|
50
59
|
export type { SharedPoolConfig, SharedPoolMemory };
|