@daneren2005/shared-memory-objects 1.1.0 → 1.3.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 +91 -56
- package/dist/{src/allocated-memory.d.ts → allocated-memory.d.ts} +6 -4
- package/dist/allocated-memory.js +47 -37
- package/dist/allocated-memory.js.map +1 -1
- package/dist/{src/cached-item-list.d.ts → cached-item-list.d.ts} +5 -4
- package/dist/cached-item-list.js +75 -75
- package/dist/cached-item-list.js.map +1 -1
- package/dist/constant-string.d.ts +15 -0
- package/dist/constant-string.js +67 -0
- package/dist/constant-string.js.map +1 -0
- package/dist/interfaces/pow2.js +0 -2
- package/dist/interfaces/typed-array-constructor.js +0 -2
- package/dist/interfaces/typed-array.js +0 -2
- package/dist/{src/local-pool.d.ts → local-pool.d.ts} +2 -2
- package/dist/local-pool.js +89 -94
- package/dist/local-pool.js.map +1 -1
- package/dist/lock/read-write-lock.js +15 -21
- package/dist/lock/read-write-lock.js.map +1 -1
- package/dist/lock/simple-lock.js +9 -12
- package/dist/lock/simple-lock.js.map +1 -1
- package/dist/{src/memory-buffer.d.ts → memory-buffer.d.ts} +8 -5
- package/dist/memory-buffer.js +269 -326
- package/dist/memory-buffer.js.map +1 -1
- package/dist/{src/memory-heap.d.ts → memory-heap.d.ts} +4 -2
- package/dist/memory-heap.js +105 -115
- package/dist/memory-heap.js.map +1 -1
- package/dist/{src/serialize-object.d.ts → serialize-object.d.ts} +2 -2
- package/dist/serialize-object.js +43 -55
- package/dist/serialize-object.js.map +1 -1
- package/dist/{src/shared-list.d.ts → shared-list.d.ts} +11 -5
- package/dist/shared-list.js +162 -164
- package/dist/shared-list.js.map +1 -1
- package/dist/{src/shared-map.d.ts → shared-map.d.ts} +2 -2
- package/dist/shared-map.js +105 -138
- package/dist/shared-map.js.map +1 -1
- package/dist/{src/shared-pointer-list.d.ts → shared-pointer-list.d.ts} +4 -3
- package/dist/shared-pointer-list.js +49 -48
- package/dist/shared-pointer-list.js.map +1 -1
- package/dist/{src/shared-pool.d.ts → shared-pool.d.ts} +15 -8
- package/dist/shared-pool.js +151 -164
- package/dist/shared-pool.js.map +1 -1
- package/dist/shared-stack.d.ts +59 -0
- package/dist/shared-stack.js +221 -0
- package/dist/shared-stack.js.map +1 -0
- package/dist/{src/shared-string.d.ts → shared-string.d.ts} +2 -2
- package/dist/shared-string.js +72 -78
- package/dist/shared-string.js.map +1 -1
- package/dist/{src/shared-vector.d.ts → shared-vector.d.ts} +5 -4
- package/dist/shared-vector.js +150 -164
- package/dist/shared-vector.js.map +1 -1
- package/dist/utils/16-from-32-array.js +14 -16
- package/dist/utils/16-from-32-array.js.map +1 -1
- package/dist/utils/16-from-64-array.js +15 -10
- package/dist/utils/16-from-64-array.js.map +1 -1
- package/dist/utils/atomic-math.js +30 -32
- package/dist/utils/atomic-math.js.map +1 -1
- package/dist/utils/float32-atomics.js +18 -22
- package/dist/utils/float32-atomics.js.map +1 -1
- package/dist/utils/float64-atomics.js +18 -22
- package/dist/utils/float64-atomics.js.map +1 -1
- package/dist/utils/pointer.js +34 -44
- package/dist/utils/pointer.js.map +1 -1
- package/dist/{src/utils → utils}/typedarray.d.ts +47 -47
- package/dist/utils/typedarray.js +36 -43
- package/dist/utils/typedarray.js.map +1 -1
- package/package.json +23 -22
- package/dist/interfaces/pow2.js.map +0 -1
- package/dist/interfaces/typed-array-constructor.js.map +0 -1
- package/dist/interfaces/typed-array.js.map +0 -1
- /package/dist/{src/interfaces → interfaces}/pow2.d.ts +0 -0
- /package/dist/{src/interfaces → interfaces}/typed-array-constructor.d.ts +0 -0
- /package/dist/{src/interfaces → interfaces}/typed-array.d.ts +0 -0
- /package/dist/{src/lock → lock}/read-write-lock.d.ts +0 -0
- /package/dist/{src/lock → lock}/simple-lock.d.ts +0 -0
- /package/dist/{src/utils → utils}/16-from-32-array.d.ts +0 -0
- /package/dist/{src/utils → utils}/16-from-64-array.d.ts +0 -0
- /package/dist/{src/utils → utils}/atomic-math.d.ts +0 -0
- /package/dist/{src/utils → utils}/float32-atomics.d.ts +0 -0
- /package/dist/{src/utils → utils}/float64-atomics.d.ts +0 -0
- /package/dist/{src/utils → utils}/pointer.d.ts +0 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { getPointer as e, storePointer as t } from "./utils/pointer.js";
|
|
2
|
+
import n from "./allocated-memory.js";
|
|
3
|
+
import { lock as r, unlock as i } from "./lock/simple-lock.js";
|
|
4
|
+
//#region src/shared-stack.ts
|
|
5
|
+
var a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = 6, f = 7, p = 4, m = 20, h = 22, g = (1 << h) - 1, _ = 4194302, v = 1 << h, y = class h {
|
|
6
|
+
static BASE_ALLOCATE_COUNT = 8;
|
|
7
|
+
static DEFAULT_ALLOCATE_COUNT = this.BASE_ALLOCATE_COUNT + m;
|
|
8
|
+
static getMaxSegmentLength(e, t = p) {
|
|
9
|
+
let n = t;
|
|
10
|
+
for (; n * 4 <= e;) n *= 2;
|
|
11
|
+
return n;
|
|
12
|
+
}
|
|
13
|
+
static getMaxSegments(e, t = p, n = Infinity) {
|
|
14
|
+
let r = 0, i = 0, a = t;
|
|
15
|
+
for (; i < e;) i += a, r++, a = Math.min(a * 2, n);
|
|
16
|
+
return r;
|
|
17
|
+
}
|
|
18
|
+
static getCapacity(e, t = p, n = Infinity) {
|
|
19
|
+
let r = 0, i = t;
|
|
20
|
+
for (let t = 0; t < e; t++) r += i, i = Math.min(i * 2, n);
|
|
21
|
+
return r;
|
|
22
|
+
}
|
|
23
|
+
static getAllocateCount(e, t = p, n = Infinity) {
|
|
24
|
+
return this.BASE_ALLOCATE_COUNT + this.getMaxSegments(e, t, n);
|
|
25
|
+
}
|
|
26
|
+
memory;
|
|
27
|
+
firstBlock;
|
|
28
|
+
uint16Array;
|
|
29
|
+
growLock;
|
|
30
|
+
maxLength;
|
|
31
|
+
ownsFirstBlock;
|
|
32
|
+
cachedSegments = [];
|
|
33
|
+
cachedSequences = [];
|
|
34
|
+
cachedBaseSegmentLength = 0;
|
|
35
|
+
cachedMaxSegmentLength = 0;
|
|
36
|
+
cachedCapSegmentCount = 0;
|
|
37
|
+
cachedGeometricCapacity = 0;
|
|
38
|
+
get length() {
|
|
39
|
+
return Atomics.load(this.firstBlock.data, a) & g;
|
|
40
|
+
}
|
|
41
|
+
get segmentCount() {
|
|
42
|
+
return Atomics.load(this.firstBlock.data, u);
|
|
43
|
+
}
|
|
44
|
+
set segmentCount(e) {
|
|
45
|
+
this.firstBlock.data[u] = e;
|
|
46
|
+
}
|
|
47
|
+
get type() {
|
|
48
|
+
return this.uint16Array[0];
|
|
49
|
+
}
|
|
50
|
+
set type(e) {
|
|
51
|
+
Atomics.store(this.uint16Array, 0, e);
|
|
52
|
+
}
|
|
53
|
+
get baseSegmentLength() {
|
|
54
|
+
return this.firstBlock.data[s];
|
|
55
|
+
}
|
|
56
|
+
set baseSegmentLength(e) {
|
|
57
|
+
this.firstBlock.data[s] = e;
|
|
58
|
+
}
|
|
59
|
+
get maxSegments() {
|
|
60
|
+
return this.firstBlock.data[c];
|
|
61
|
+
}
|
|
62
|
+
set maxSegments(e) {
|
|
63
|
+
this.firstBlock.data[c] = e;
|
|
64
|
+
}
|
|
65
|
+
get maxSegmentLength() {
|
|
66
|
+
return this.firstBlock.data[l];
|
|
67
|
+
}
|
|
68
|
+
set maxSegmentLength(e) {
|
|
69
|
+
this.firstBlock.data[l] = e;
|
|
70
|
+
}
|
|
71
|
+
get pointer() {
|
|
72
|
+
return this.firstBlock.pointer;
|
|
73
|
+
}
|
|
74
|
+
get usedMemory() {
|
|
75
|
+
let t = this.ownsFirstBlock ? this.firstBlock.usedMemory : 0;
|
|
76
|
+
for (let r = 0; r < this.segmentCount; r++) {
|
|
77
|
+
let i = e(Atomics.load(this.firstBlock.data, f + r));
|
|
78
|
+
t += new n(this.memory, i).usedMemory;
|
|
79
|
+
}
|
|
80
|
+
return t;
|
|
81
|
+
}
|
|
82
|
+
constructor(e, t) {
|
|
83
|
+
if (this.memory = e, t && "firstBlock" in t) this.firstBlock = t.firstBlock instanceof n ? t.firstBlock : new n(e, t.firstBlock), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + o * Uint32Array.BYTES_PER_ELEMENT, 2), "type" in t || "baseSegmentLength" in t || "maxLength" in t ? (this.initialize(t), this.ownsFirstBlock = !1) : this.ownsFirstBlock = !0;
|
|
84
|
+
else {
|
|
85
|
+
let n = t?.baseSegmentLength ?? p, r = this.resolveMaxSegmentLength(n, t?.maxSegmentLength), i = this.resolveMaxSegments(t?.maxLength, n, r);
|
|
86
|
+
this.firstBlock = e.allocUI32(h.BASE_ALLOCATE_COUNT + i), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + o * Uint32Array.BYTES_PER_ELEMENT, 2), this.ownsFirstBlock = !0, this.initialize(t);
|
|
87
|
+
}
|
|
88
|
+
if (this.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + d * Uint32Array.BYTES_PER_ELEMENT, 1), this.cachedBaseSegmentLength = this.baseSegmentLength, this.cachedMaxSegmentLength = this.maxSegmentLength, this.cachedCapSegmentCount = 32 - Math.clz32(this.cachedMaxSegmentLength / this.cachedBaseSegmentLength | 0), this.cachedGeometricCapacity = this.cachedBaseSegmentLength * ((1 << this.cachedCapSegmentCount) - 1), this.maxLength = h.getCapacity(this.maxSegments, this.cachedBaseSegmentLength, this.cachedMaxSegmentLength), this.maxLength > _) throw Error(`SharedStack maxLength ${this.maxLength} exceeds the ${_} the versioned length counter can address`);
|
|
89
|
+
}
|
|
90
|
+
resolveMaxSegmentLength(e, t) {
|
|
91
|
+
let n = h.getMaxSegmentLength(this.memory.maxAllocationLength, e);
|
|
92
|
+
if (t) {
|
|
93
|
+
let r = e;
|
|
94
|
+
for (; r * 2 <= t;) r *= 2;
|
|
95
|
+
n = Math.min(n, r);
|
|
96
|
+
}
|
|
97
|
+
return n;
|
|
98
|
+
}
|
|
99
|
+
resolveMaxSegments(e, t, n) {
|
|
100
|
+
let r = e === void 0 ? m : h.getMaxSegments(e, t, n), i = this.memory.maxAllocationLength - h.BASE_ALLOCATE_COUNT;
|
|
101
|
+
return Math.min(r, i);
|
|
102
|
+
}
|
|
103
|
+
initialize(e) {
|
|
104
|
+
let n = e?.baseSegmentLength ?? p, r = this.resolveMaxSegmentLength(n, e?.maxSegmentLength), i = this.memory.allocUI32(n * 2);
|
|
105
|
+
t(this.firstBlock.data, f, i.bufferPosition, i.bufferByteOffset), this.baseSegmentLength = n, this.maxSegmentLength = r, this.maxSegments = this.resolveMaxSegments(e?.maxLength, n, r), this.segmentCount = 1;
|
|
106
|
+
let a = e?.type ?? Uint32Array;
|
|
107
|
+
a === Uint32Array ? this.type = 0 : a === Int32Array ? this.type = 1 : a === Float32Array && (this.type = 2);
|
|
108
|
+
}
|
|
109
|
+
at(e) {
|
|
110
|
+
let t = this.length;
|
|
111
|
+
if (e >= t || e < 0) throw Error(`${e} is out of bounds ${t}`);
|
|
112
|
+
let n = this.cachedGeometricCapacity, r, i;
|
|
113
|
+
if (e < n) {
|
|
114
|
+
let t = this.cachedBaseSegmentLength;
|
|
115
|
+
r = 31 - Math.clz32((e / t | 0) + 1), i = e - t * ((1 << r) - 1);
|
|
116
|
+
} else {
|
|
117
|
+
let t = this.cachedMaxSegmentLength, a = e - n, o = a / t | 0;
|
|
118
|
+
r = this.cachedCapSegmentCount + o, i = a - o * t;
|
|
119
|
+
}
|
|
120
|
+
return (this.cachedSegments[r] ?? this.getSegment(r))[i];
|
|
121
|
+
}
|
|
122
|
+
push(e) {
|
|
123
|
+
let t = this.firstBlock.data, n = this.cachedGeometricCapacity;
|
|
124
|
+
for (;;) {
|
|
125
|
+
let r = t[a], i = r & g;
|
|
126
|
+
if (i >= this.maxLength) throw Error(`${i + 1} is out of bounds ${this.maxLength}`);
|
|
127
|
+
let o, s;
|
|
128
|
+
if (i < n) {
|
|
129
|
+
let e = this.cachedBaseSegmentLength;
|
|
130
|
+
o = 31 - Math.clz32((i / e | 0) + 1), s = i - e * ((1 << o) - 1);
|
|
131
|
+
} else {
|
|
132
|
+
let e = this.cachedMaxSegmentLength, t = i - n, r = t / e | 0;
|
|
133
|
+
o = this.cachedCapSegmentCount + r, s = t - r * e;
|
|
134
|
+
}
|
|
135
|
+
let c = this.cachedSegments[o] ?? this.getSegment(o), l = this.cachedSequences[o], u = Atomics.load(l, s);
|
|
136
|
+
if (!(u & 1) && Atomics.compareExchange(t, a, r, r + v + 1) === r) return c[s] = e, Atomics.store(l, s, u + 1), i;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
pop() {
|
|
140
|
+
let e = this.firstBlock.data, t = this.cachedGeometricCapacity;
|
|
141
|
+
for (;;) {
|
|
142
|
+
let n = e[a], r = n & g;
|
|
143
|
+
if (r <= 0) return;
|
|
144
|
+
let i = r - 1, o, s;
|
|
145
|
+
if (i < t) {
|
|
146
|
+
let e = this.cachedBaseSegmentLength;
|
|
147
|
+
o = 31 - Math.clz32((i / e | 0) + 1), s = i - e * ((1 << o) - 1);
|
|
148
|
+
} else {
|
|
149
|
+
let e = this.cachedMaxSegmentLength, n = i - t, r = n / e | 0;
|
|
150
|
+
o = this.cachedCapSegmentCount + r, s = n - r * e;
|
|
151
|
+
}
|
|
152
|
+
let c = this.cachedSegments[o] ?? this.getSegment(o), l = this.cachedSequences[o], u = Atomics.load(l, s);
|
|
153
|
+
if (!(u & 1) || Atomics.compareExchange(e, a, n, n + v - 1) !== n) continue;
|
|
154
|
+
let d = c[s];
|
|
155
|
+
return Atomics.store(l, s, u + 1), d;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
clear() {
|
|
159
|
+
let e = this.cachedBaseSegmentLength, t = this.cachedGeometricCapacity, n = this.cachedMaxSegmentLength, r = Atomics.load(this.firstBlock.data, a) & g;
|
|
160
|
+
for (let i = 0; i < r; i++) {
|
|
161
|
+
let r, a;
|
|
162
|
+
if (i < t) r = 31 - Math.clz32((i / e | 0) + 1), a = i - e * ((1 << r) - 1);
|
|
163
|
+
else {
|
|
164
|
+
let e = i - t, o = e / n | 0;
|
|
165
|
+
r = this.cachedCapSegmentCount + o, a = e - o * n;
|
|
166
|
+
}
|
|
167
|
+
let o = this.cachedSequences[r] ?? (this.getSegment(r), this.cachedSequences[r]), s = Atomics.load(o, a);
|
|
168
|
+
s & 1 && Atomics.store(o, a, s + 1);
|
|
169
|
+
}
|
|
170
|
+
Atomics.store(this.firstBlock.data, a, 0);
|
|
171
|
+
}
|
|
172
|
+
*[Symbol.iterator]() {
|
|
173
|
+
let e = this.length, t = this.cachedMaxSegmentLength, n = this.baseSegmentLength, r = 0, i = this.getSegment(r), a = 0;
|
|
174
|
+
for (let o = 0; o < e; o++) yield i[a], a++, a >= n && (r++, i = this.getSegment(r), a = 0, n = Math.min(n * 2, t));
|
|
175
|
+
}
|
|
176
|
+
getSegment(a) {
|
|
177
|
+
if (this.cachedSegments[a]) return this.cachedSegments[a];
|
|
178
|
+
let o = a < this.cachedCapSegmentCount ? this.cachedBaseSegmentLength << a : this.cachedMaxSegmentLength, s;
|
|
179
|
+
if (a >= this.segmentCount) {
|
|
180
|
+
r(this.growLock);
|
|
181
|
+
try {
|
|
182
|
+
a >= this.segmentCount && (s = this.memory.allocUI32(o * 2), t(this.firstBlock.data, f + a, s.bufferPosition, s.bufferByteOffset), Atomics.add(this.firstBlock.data, u, 1));
|
|
183
|
+
} finally {
|
|
184
|
+
i(this.growLock);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (!s) {
|
|
188
|
+
let t = Atomics.load(this.firstBlock.data, f + a), r = e(t);
|
|
189
|
+
s = new n(this.memory, r);
|
|
190
|
+
}
|
|
191
|
+
let c;
|
|
192
|
+
switch (this.type) {
|
|
193
|
+
case 1:
|
|
194
|
+
c = new Int32Array(s.data.buffer, s.bufferByteOffset, o);
|
|
195
|
+
break;
|
|
196
|
+
case 0:
|
|
197
|
+
c = new Uint32Array(s.data.buffer, s.bufferByteOffset, o);
|
|
198
|
+
break;
|
|
199
|
+
case 2:
|
|
200
|
+
c = new Float32Array(s.data.buffer, s.bufferByteOffset, o);
|
|
201
|
+
break;
|
|
202
|
+
default: throw Error(`Unknown data block type ${this.type}`);
|
|
203
|
+
}
|
|
204
|
+
let l = s.bufferByteOffset + o * Uint32Array.BYTES_PER_ELEMENT;
|
|
205
|
+
return this.cachedSequences[a] = new Int32Array(s.data.buffer, l, o), this.cachedSegments[a] = c, c;
|
|
206
|
+
}
|
|
207
|
+
free() {
|
|
208
|
+
for (let t = 0; t < this.segmentCount; t++) {
|
|
209
|
+
let r = Atomics.load(this.firstBlock.data, f + t), i = e(r);
|
|
210
|
+
new n(this.memory, i).free();
|
|
211
|
+
}
|
|
212
|
+
this.ownsFirstBlock && this.firstBlock.free();
|
|
213
|
+
}
|
|
214
|
+
getSharedMemory() {
|
|
215
|
+
return { firstBlock: this.firstBlock.getSharedMemory() };
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
//#endregion
|
|
219
|
+
export { y as default };
|
|
220
|
+
|
|
221
|
+
//# sourceMappingURL=shared-stack.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared-stack.js","names":[],"sources":["../src/shared-stack.ts"],"sourcesContent":["import type { SharedAllocatedMemory } from './allocated-memory';\nimport AllocatedMemory from './allocated-memory';\nimport type { TypedArrayConstructor } from './interfaces/typed-array-constructor';\nimport { lock, unlock } from './lock/simple-lock';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, storePointer } from './utils/pointer';\n\nenum TYPE {\n\tuint32,\n\tint32,\n\tfloat32,\n}\n\nconst LENGTH_INDEX = 0;\nconst TYPE_INDEX = 1;\nconst BASE_SEGMENT_LENGTH_INDEX = 2;\nconst MAX_SEGMENTS_INDEX = 3;\nconst MAX_SEGMENT_LENGTH_INDEX = 4;\nconst SEGMENT_COUNT_INDEX = 5;\nconst GROW_LOCK_INDEX = 6;\nconst SEGMENT_START_INDEX = 7;\n\nconst DEFAULT_BASE_SEGMENT_LENGTH = 4;\n// Default segment budget. With uncapped power-of-two growth these 20 segments spanned 4,194,300 slots, but that ceiling\n// was never reachable: past segment 14 a segment's block outgrows a 1MB buffer and allocation throws. Capping segments at\n// what a buffer holds, the honest capacity of 20 segments is getCapacity(20, base, cap) instead.\nconst DEFAULT_MAX_SEGMENTS = 20;\n\n// The LENGTH word packs an ABA version counter (high bits) with the live count (low LEN_BITS). Both push and pop bump\n// the version on every successful CAS, so the CAS fails on ANY intervening op - the count can never ABA back to the\n// same value with a slot at a different generation. 22 count bits cover the default max length (4,194,300); the\n// remaining 10 version bits (1024) only need to outlast a single thread's load->CAS window.\nconst LEN_BITS = 22;\nconst LEN_MASK = (1 << LEN_BITS) - 1;\nconst MAX_COUNT = LEN_MASK - 1;\n// Adding this to the packed word bumps the version (high bits) by one. Combined with +1/-1 for the count it repacks the\n// whole word in a single add: the count is guarded from overflowing into the version bits, and a version overflow just\n// wraps harmlessly when the value is coerced back to Uint32 on the atomic store.\nconst VERSION_STEP = 1 << LEN_BITS;\n\n// Per-slot publish handshake. Each data slot has a companion sequence number: even means the slot is empty (free to\n// fill) at some lap, odd means the value is published (safe to read). It only ever increases. push/pop read the slot's\n// sequence and bail out (retry) unless it has the parity they need, then CAS the length; because the versioned CAS\n// rejects any intervening op, the sequence read stays valid all the way through the publish store below.\n\nexport default class SharedStack<T extends Uint32Array | Int32Array | Float32Array = Uint32Array> implements Iterable<number> {\n\tstatic readonly BASE_ALLOCATE_COUNT = 8;\n\tstatic readonly DEFAULT_ALLOCATE_COUNT = this.BASE_ALLOCATE_COUNT + DEFAULT_MAX_SEGMENTS;\n\n\t// Largest power-of-two segment length whose block (segmentLength data slots plus an equal run of publish sequences)\n\t// still fits in a single buffer. Segments double until they reach this, then stay flat so a segment can always be\n\t// allocated in one contiguous block. maxAllocationLength is MemoryHeap.maxAllocationLength.\n\tstatic getMaxSegmentLength(maxAllocationLength: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH): number {\n\t\tlet segmentLength = baseSegmentLength;\n\t\t// A segment of length L needs a block of 2L u32s, so the next double only fits while 2 * (2L) <= maxAllocationLength\n\t\twhile(segmentLength * 4 <= maxAllocationLength) {\n\t\t\tsegmentLength *= 2;\n\t\t}\n\t\treturn segmentLength;\n\t}\n\n\t// Smallest segment count whose summed capacity covers maxLength. Segments double from baseSegmentLength but never grow\n\t// past maxSegmentLength, so once capped they each add a flat maxSegmentLength. Looped rather than solved in closed form\n\t// to avoid float rounding landing a segment short or over.\n\tstatic getMaxSegments(maxLength: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, maxSegmentLength = Infinity): number {\n\t\tlet segments = 0;\n\t\tlet capacity = 0;\n\t\tlet segmentLength = baseSegmentLength;\n\t\twhile(capacity < maxLength) {\n\t\t\tcapacity += segmentLength;\n\t\t\tsegments++;\n\t\t\tsegmentLength = Math.min(segmentLength * 2, maxSegmentLength);\n\t\t}\n\t\treturn segments;\n\t}\n\t// Total capacity of the first `segments` segments under the same doubling-then-capped growth as getMaxSegments\n\tstatic getCapacity(segments: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, maxSegmentLength = Infinity): number {\n\t\tlet capacity = 0;\n\t\tlet segmentLength = baseSegmentLength;\n\t\tfor(let i = 0; i < segments; i++) {\n\t\t\tcapacity += segmentLength;\n\t\t\tsegmentLength = Math.min(segmentLength * 2, maxSegmentLength);\n\t\t}\n\t\treturn capacity;\n\t}\n\tstatic getAllocateCount(maxLength: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, maxSegmentLength = Infinity): number {\n\t\treturn this.BASE_ALLOCATE_COUNT + this.getMaxSegments(maxLength, baseSegmentLength, maxSegmentLength);\n\t}\n\tprivate memory: MemoryHeap;\n\n\t// Length, Type, Base Segment Length, Max Segments, Max Segment Length, Segment Count, Grow Lock, Segments\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\tprivate growLock: Int32Array;\n\treadonly maxLength: number;\n\n\t// False when firstBlock is a slice of another structure's memory (e.g. inlined in SharedPool); that owner frees it\n\tprivate ownsFirstBlock: boolean;\n\n\tprivate cachedSegments: T[] = [];\n\t// Parallel to cachedSegments: the per-slot publish sequences for each slot of that segment. Populated together with\n\t// the data view in getSegment, so whenever cachedSegments[i] is set cachedSequences[i] is too.\n\tprivate cachedSequences: Int32Array[] = [];\n\tprivate cachedBaseSegmentLength = 0;\n\t// Segment growth caps at cachedMaxSegmentLength. Segments 0..(cachedCapSegmentCount - 1) double geometrically and cover\n\t// the first cachedGeometricCapacity slots; every segment beyond that is a flat cachedMaxSegmentLength. Indexing splits\n\t// on cachedGeometricCapacity: below it the fast log2 mapping, above it a plain linear division into capped segments.\n\tprivate cachedMaxSegmentLength = 0;\n\tprivate cachedCapSegmentCount = 0;\n\tprivate cachedGeometricCapacity = 0;\n\n\tget length(): number {\n\t\treturn Atomics.load(this.firstBlock.data, LENGTH_INDEX) & LEN_MASK;\n\t}\n\tget segmentCount(): number {\n\t\treturn Atomics.load(this.firstBlock.data, SEGMENT_COUNT_INDEX);\n\t}\n\tprivate set segmentCount(value: number) {\n\t\tthis.firstBlock.data[SEGMENT_COUNT_INDEX] = value;\n\t}\n\t\n\tget type(): number {\n\t\treturn this.uint16Array[0];\n\t}\n\tprivate set type(value: number) {\n\t\tAtomics.store(this.uint16Array, 0, value);\n\t}\n\n\tget baseSegmentLength() {\n\t\treturn this.firstBlock.data[BASE_SEGMENT_LENGTH_INDEX];\n\t}\n\tprivate set baseSegmentLength(value: number) {\n\t\tthis.firstBlock.data[BASE_SEGMENT_LENGTH_INDEX] = value;\n\t}\n\tget maxSegments() {\n\t\treturn this.firstBlock.data[MAX_SEGMENTS_INDEX];\n\t}\n\tprivate set maxSegments(value: number) {\n\t\tthis.firstBlock.data[MAX_SEGMENTS_INDEX] = value;\n\t}\n\tget maxSegmentLength() {\n\t\treturn this.firstBlock.data[MAX_SEGMENT_LENGTH_INDEX];\n\t}\n\tprivate set maxSegmentLength(value: number) {\n\t\tthis.firstBlock.data[MAX_SEGMENT_LENGTH_INDEX] = value;\n\t}\n\n\tget pointer() {\n\t\treturn this.firstBlock.pointer;\n\t}\n\n\t// Own internal memory plus every allocated segment. The firstBlock is excluded when inlined in another structure\n\t// (e.g. SharedPool) since that owner already counts it as part of its own internal memory.\n\tget usedMemory(): number {\n\t\tlet total = this.ownsFirstBlock ? this.firstBlock.usedMemory : 0;\n\t\tfor(let i = 0; i < this.segmentCount; i++) {\n\t\t\tlet pointer = getPointer(Atomics.load(this.firstBlock.data, SEGMENT_START_INDEX + i));\n\t\t\ttotal += new AllocatedMemory(this.memory, pointer).usedMemory;\n\t\t}\n\t\treturn total;\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedStackConfig<T> | SharedStackMemory | SharedStackMemory & SharedStackConfig<T>) {\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 SharedPool); a plain pointer is the\n\t\t\t// serialized clone path. Taking the instance directly keeps us off the pointer-resolving construction branch.\n\t\t\tthis.firstBlock = config.firstBlock instanceof AllocatedMemory ? config.firstBlock : new AllocatedMemory(memory, config.firstBlock);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\n\t\t\t// Memory passed together with config means the caller reserved the firstBlock slot for us to initialize in place\n\t\t\tif('type' in config || 'baseSegmentLength' in config || 'maxLength' in config) {\n\t\t\t\tthis.initialize(config);\n\t\t\t\tthis.ownsFirstBlock = false;\n\t\t\t} else {\n\t\t\t\tthis.ownsFirstBlock = true;\n\t\t\t}\n\t\t} else {\n\t\t\tlet baseSegmentLength = config?.baseSegmentLength ?? DEFAULT_BASE_SEGMENT_LENGTH;\n\t\t\tlet maxSegmentLength = this.resolveMaxSegmentLength(baseSegmentLength, config?.maxSegmentLength);\n\t\t\tlet maxSegments = this.resolveMaxSegments(config?.maxLength, baseSegmentLength, maxSegmentLength);\n\n\t\t\tthis.firstBlock = memory.allocUI32(SharedStack.BASE_ALLOCATE_COUNT + maxSegments);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\t\t\tthis.ownsFirstBlock = true;\n\n\t\t\tthis.initialize(config);\n\t\t}\n\n\t\tthis.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + GROW_LOCK_INDEX * Uint32Array.BYTES_PER_ELEMENT, 1);\n\t\tthis.cachedBaseSegmentLength = this.baseSegmentLength;\n\t\tthis.cachedMaxSegmentLength = this.maxSegmentLength;\n\t\t// Segments 0..(K-1) double geometrically up to the cap, where K = log2(cap / base) + 1\n\t\tthis.cachedCapSegmentCount = 32 - Math.clz32(((this.cachedMaxSegmentLength / this.cachedBaseSegmentLength) | 0));\n\t\tthis.cachedGeometricCapacity = this.cachedBaseSegmentLength * ((1 << this.cachedCapSegmentCount) - 1);\n\t\tthis.maxLength = SharedStack.getCapacity(this.maxSegments, this.cachedBaseSegmentLength, this.cachedMaxSegmentLength);\n\t\tif(this.maxLength > MAX_COUNT) {\n\t\t\t// The length word reserves LEN_BITS for the count and the rest for the ABA version tag\n\t\t\tthrow new Error(`SharedStack maxLength ${this.maxLength} exceeds the ${MAX_COUNT} the versioned length counter can address`);\n\t\t}\n\t}\n\n\t// Cap on segment length: the largest a single buffer can hold, optionally lowered to a caller-requested value (floored\n\t// to a power-of-two multiple of the base). Beyond this segments stop doubling and stay flat so each still fits a buffer.\n\tprivate resolveMaxSegmentLength(baseSegmentLength: number, requested?: number): number {\n\t\tlet cap = SharedStack.getMaxSegmentLength(this.memory.maxAllocationLength, baseSegmentLength);\n\t\tif(requested) {\n\t\t\tlet floored = baseSegmentLength;\n\t\t\twhile(floored * 2 <= requested) {\n\t\t\t\tfloored *= 2;\n\t\t\t}\n\t\t\tcap = Math.min(cap, floored);\n\t\t}\n\t\treturn cap;\n\t}\n\n\t// Segments needed to cover maxLength (or the default budget when none is given), but never more than the segment-pointer\n\t// spine can hold in a single buffer. Once capped, segments no longer double, so a huge maxLength on a small buffer is\n\t// honestly bounded here instead of blowing up later when the spine (or a segment) can't be allocated.\n\tprivate resolveMaxSegments(maxLength: number | undefined, baseSegmentLength: number, maxSegmentLength: number): number {\n\t\tlet needed = maxLength === undefined\n\t\t\t? DEFAULT_MAX_SEGMENTS\n\t\t\t: SharedStack.getMaxSegments(maxLength, baseSegmentLength, maxSegmentLength);\n\t\tlet spineLimit = this.memory.maxAllocationLength - SharedStack.BASE_ALLOCATE_COUNT;\n\t\treturn Math.min(needed, spineLimit);\n\t}\n\n\tprivate initialize(config?: SharedStackConfig<T>) {\n\t\tlet baseSegmentLength = config?.baseSegmentLength ?? DEFAULT_BASE_SEGMENT_LENGTH;\n\t\tlet maxSegmentLength = this.resolveMaxSegmentLength(baseSegmentLength, config?.maxSegmentLength);\n\t\t// Each segment block holds the data slots followed by an equal-length region of publish sequences\n\t\tlet firstSegmentBlock = this.memory.allocUI32(baseSegmentLength * 2);\n\t\tstorePointer(this.firstBlock.data, SEGMENT_START_INDEX, firstSegmentBlock.bufferPosition, firstSegmentBlock.bufferByteOffset);\n\t\tthis.baseSegmentLength = baseSegmentLength;\n\t\tthis.maxSegmentLength = maxSegmentLength;\n\t\tthis.maxSegments = this.resolveMaxSegments(config?.maxLength, baseSegmentLength, maxSegmentLength);\n\t\tthis.segmentCount = 1;\n\n\t\tconst type = config?.type ?? Uint32Array;\n\t\tif(type === Uint32Array) {\n\t\t\tthis.type = TYPE.uint32;\n\t\t// @ts-expect-error\n\t\t} else if(type === Int32Array) {\n\t\t\tthis.type = TYPE.int32;\n\t\t// @ts-expect-error\n\t\t} else if(type === Float32Array) {\n\t\t\tthis.type = TYPE.float32;\n\t\t}\n\t}\n\n\tat(index: number): number {\n\t\tlet length = this.length;\n\t\tif(index >= length || index < 0) {\n\t\t\tthrow new Error(`${index} is out of bounds ${length}`);\n\t\t}\n\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\tlet segmentIndex: number;\n\t\tlet localIndex: number;\n\t\tif(index < geometricCapacity) {\n\t\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t\tsegmentIndex = 31 - Math.clz32(((index / baseSegmentLength) | 0) + 1);\n\t\t\tlocalIndex = index - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t} else {\n\t\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\t\tlet offset = index - geometricCapacity;\n\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t}\n\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\treturn segment[localIndex];\n\t}\n\n\tpush(value: number): number {\n\t\tlet data = this.firstBlock.data;\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\t// Plain (relaxed) read is enough: it only seeds the versioned CAS below, which rejects any stale value\n\t\t\tlet packed = data[LENGTH_INDEX];\n\t\t\tlet newIndex = packed & LEN_MASK;\n\t\t\tif(newIndex >= this.maxLength) {\n\t\t\t\tthrow new Error(`${newIndex + 1} is out of bounds ${this.maxLength}`);\n\t\t\t}\n\n\t\t\tlet segmentIndex: number;\n\t\t\tlet localIndex: number;\n\t\t\tif(newIndex < geometricCapacity) {\n\t\t\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t\t\tsegmentIndex = 31 - Math.clz32(((newIndex / baseSegmentLength) | 0) + 1);\n\t\t\t\tlocalIndex = newIndex - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\t} else {\n\t\t\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\t\t\tlet offset = newIndex - geometricCapacity;\n\t\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t\t}\n\t\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\t\tlet sequences = this.cachedSequences[segmentIndex];\n\n\t\t\t// The slot must be empty (even sequence) to fill. Odd means a popper hasn't finished emptying it yet - retry\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) !== 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// Take the slot only if the whole versioned length word is unchanged. The version bump makes this fail on any\n\t\t\t// intervening op, so a win guarantees nothing touched the top - and our sequence read above is still valid\n\t\t\tif(Atomics.compareExchange(data, LENGTH_INDEX, packed, packed + VERSION_STEP + 1) !== packed) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tsegment[localIndex] = value;\n\t\t\t// Publish: bump to odd (full). Safe as a plain store - no other op can claim this slot until the length comes\n\t\t\t// back down to it, which can't happen (its popper is gated on an odd sequence) until this store lands\n\t\t\tAtomics.store(sequences, localIndex, sequence + 1);\n\t\t\treturn newIndex;\n\t\t}\n\t}\n\n\tpop(): number | undefined {\n\t\tlet data = this.firstBlock.data;\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\t// Plain (relaxed) read is enough: it only seeds the versioned CAS below, which rejects any stale value\n\t\t\tlet packed = data[LENGTH_INDEX];\n\t\t\tlet length = packed & LEN_MASK;\n\t\t\tif(length <= 0) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tlet oldIndex = length - 1;\n\n\t\t\tlet segmentIndex: number;\n\t\t\tlet localIndex: number;\n\t\t\tif(oldIndex < geometricCapacity) {\n\t\t\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t\t\tsegmentIndex = 31 - Math.clz32(((oldIndex / baseSegmentLength) | 0) + 1);\n\t\t\t\tlocalIndex = oldIndex - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\t} else {\n\t\t\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\t\t\tlet offset = oldIndex - geometricCapacity;\n\t\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t\t}\n\t\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\t\tlet sequences = this.cachedSequences[segmentIndex];\n\n\t\t\t// The slot must be full (odd sequence) to read. Even means a pusher hasn't finished publishing it yet - retry\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) === 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(Atomics.compareExchange(data, LENGTH_INDEX, packed, packed + VERSION_STEP - 1) !== packed) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet value = segment[localIndex];\n\t\t\t// Release the slot: bump to even (empty) so a later push may reuse it\n\t\t\tAtomics.store(sequences, localIndex, sequence + 1);\n\t\t\treturn value;\n\t\t}\n\t}\n\n\tclear() {\n\t\t// Mark every occupied slot empty (bump its odd sequence to the next even) before dropping the length, otherwise a\n\t\t// later push reusing the slot would spin forever on a stale full sequence. Not safe to call concurrently with push/pop.\n\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\tlet oldLength = Atomics.load(this.firstBlock.data, LENGTH_INDEX) & LEN_MASK;\n\t\tfor(let i = 0; i < oldLength; i++) {\n\t\t\tlet segmentIndex: number;\n\t\t\tlet localIndex: number;\n\t\t\tif(i < geometricCapacity) {\n\t\t\t\tsegmentIndex = 31 - Math.clz32(((i / baseSegmentLength) | 0) + 1);\n\t\t\t\tlocalIndex = i - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\t} else {\n\t\t\t\tlet offset = i - geometricCapacity;\n\t\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t\t}\n\t\t\tlet sequences = this.cachedSequences[segmentIndex] ?? (this.getSegment(segmentIndex), this.cachedSequences[segmentIndex]);\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) !== 0) {\n\t\t\t\tAtomics.store(sequences, localIndex, sequence + 1);\n\t\t\t}\n\t\t}\n\t\tAtomics.store(this.firstBlock.data, LENGTH_INDEX, 0);\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet totalDataEntries = this.length;\n\n\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\tlet segmentLength = this.baseSegmentLength;\n\t\tlet segmentIndex = 0;\n\t\tlet segment = this.getSegment(segmentIndex);\n\t\tlet segmentDataIndex = 0;\n\t\tfor(let i = 0; i < totalDataEntries; i++) {\n\t\t\tyield segment[segmentDataIndex];\n\t\t\tsegmentDataIndex++;\n\t\t\tif(segmentDataIndex >= segmentLength) {\n\t\t\t\tsegmentIndex++;\n\t\t\t\tsegment = this.getSegment(segmentIndex);\n\t\t\t\tsegmentDataIndex = 0;\n\t\t\t\tsegmentLength = Math.min(segmentLength * 2, maxSegmentLength);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getSegment(segmentIndex: number): T {\n\t\tif(this.cachedSegments[segmentIndex]) {\n\t\t\treturn this.cachedSegments[segmentIndex];\n\t\t}\n\n\t\t// Geometric while below the cap, then flat so the block always fits a single buffer\n\t\tlet segmentLength = segmentIndex < this.cachedCapSegmentCount\n\t\t\t? this.cachedBaseSegmentLength << segmentIndex\n\t\t\t: this.cachedMaxSegmentLength;\n\t\tlet segmentDataBlock: AllocatedMemory | undefined;\n\t\tif(segmentIndex >= this.segmentCount) {\n\t\t\t// Lock when growing so another thread doesn't also setup the same segment\n\t\t\tlock(this.growLock);\n\t\t\ttry {\n\t\t\t\t// Re-check under the lock: another thread may have already appended the segment while we were waiting\n\t\t\t\tif(segmentIndex >= this.segmentCount) {\n\t\t\t\t\t// Double length: the data slots followed by their publish sequences (calloc-zeroed = 0 = empty)\n\t\t\t\t\tsegmentDataBlock = this.memory.allocUI32(segmentLength * 2);\n\t\t\t\t\tstorePointer(this.firstBlock.data, SEGMENT_START_INDEX + segmentIndex, segmentDataBlock.bufferPosition, segmentDataBlock.bufferByteOffset);\n\t\t\t\t\tAtomics.add(this.firstBlock.data, SEGMENT_COUNT_INDEX, 1);\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tunlock(this.growLock);\n\t\t\t}\n\t\t}\n\n\t\tif(!segmentDataBlock) {\n\t\t\tlet pointerNumber = Atomics.load(this.firstBlock.data, SEGMENT_START_INDEX + segmentIndex);\n\t\t\tlet pointer = getPointer(pointerNumber);\n\t\t\tsegmentDataBlock = new AllocatedMemory(this.memory, pointer);\n\t\t}\n\n\t\tlet data: T;\n\t\tswitch(this.type) {\n\t\t\tcase TYPE.int32:\n\t\t\t\tdata = new Int32Array(segmentDataBlock.data.buffer, segmentDataBlock.bufferByteOffset, segmentLength) as T;\n\t\t\t\tbreak;\n\t\t\tcase TYPE.uint32:\n\t\t\t\tdata = new Uint32Array(segmentDataBlock.data.buffer, segmentDataBlock.bufferByteOffset, segmentLength) as T;\n\t\t\t\tbreak;\n\t\t\tcase TYPE.float32:\n\t\t\t\tdata = new Float32Array(segmentDataBlock.data.buffer, segmentDataBlock.bufferByteOffset, segmentLength) as T;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unknown data block type ${this.type}`);\n\t\t}\n\t\t// Publish sequences live in the second half of the block, always as Int32 regardless of the data type\n\t\tlet sequenceByteOffset = segmentDataBlock.bufferByteOffset + segmentLength * Uint32Array.BYTES_PER_ELEMENT;\n\t\tthis.cachedSequences[segmentIndex] = new Int32Array(segmentDataBlock.data.buffer, sequenceByteOffset, segmentLength);\n\t\tthis.cachedSegments[segmentIndex] = data;\n\n\t\treturn data;\n\t}\n\n\tfree() {\n\t\tfor(let i = 0; i < this.segmentCount; i++) {\n\t\t\tlet pointerNumber = Atomics.load(this.firstBlock.data, SEGMENT_START_INDEX + i);\n\t\t\tlet pointer = getPointer(pointerNumber);\n\t\t\tlet rawData = new AllocatedMemory(this.memory, pointer);\n\t\t\trawData.free();\n\t\t}\n\t\tif(this.ownsFirstBlock) {\n\t\t\tthis.firstBlock.free();\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedStackMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.firstBlock.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedStackConfig<T extends Uint32Array | Int32Array | Float32Array> {\n\ttype?: TypedArrayConstructor<T>\n\n\tbaseSegmentLength?: number\n\tmaxLength?: number\n\t// Cap on how large a single segment may grow. Defaults to the largest a buffer can hold; a smaller value is floored to\n\t// a power-of-two multiple of baseSegmentLength. Beyond the cap segments stay flat instead of doubling.\n\tmaxSegmentLength?: number\n}\ninterface SharedStackMemory {\n\tfirstBlock: SharedAllocatedMemory | AllocatedMemory\n}\n\nexport type { SharedStackConfig, SharedStackMemory };"],"mappings":";;;;AAaA,IAAM,IAAe,GACf,IAAa,GACb,IAA4B,GAC5B,IAAqB,GACrB,IAA2B,GAC3B,IAAsB,GACtB,IAAkB,GAClB,IAAsB,GAEtB,IAA8B,GAI9B,IAAuB,IAMvB,IAAW,IACX,KAAY,KAAK,KAAY,GAC7B,IAAY,SAIZ,IAAe,KAAK,GAOL,IAArB,MAAqB,EAAyG;CAC7H,OAAgB,sBAAsB;CACtC,OAAgB,yBAAyB,KAAK,sBAAsB;CAKpE,OAAO,oBAAoB,GAA6B,IAAoB,GAAqC;E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1D,UAAU;IACT,EAAO,KAAK,QAAQ;GACrB;EACD;EAEA,IAAG,CAAC,GAAkB;GACrB,IAAI,IAAgB,QAAQ,KAAK,KAAK,WAAW,MAAM,IAAsB,CAAY,GACrF,IAAU,EAAW,CAAa;GACtC,IAAmB,IAAI,EAAgB,KAAK,QAAQ,CAAO;EAC5D;EAEA,IAAI;EACJ,QAAO,KAAK,MAAZ;GACC,KAAA;IACC,IAAO,IAAI,WAAW,EAAiB,KAAK,QAAQ,EAAiB,kBAAkB,CAAa;IACpG;GACD,KAAA;IACC,IAAO,IAAI,YAAY,EAAiB,KAAK,QAAQ,EAAiB,kBAAkB,CAAa;IACrG;GACD,KAAA;IACC,IAAO,IAAI,aAAa,EAAiB,KAAK,QAAQ,EAAiB,kBAAkB,CAAa;IACtG;GACD,SACC,MAAU,MAAM,2BAA2B,KAAK,MAAM;EACxD;EAEA,IAAI,IAAqB,EAAiB,mBAAmB,IAAgB,YAAY;EAIzF,OAHA,KAAK,gBAAgB,KAAgB,IAAI,WAAW,EAAiB,KAAK,QAAQ,GAAoB,CAAa,GACnH,KAAK,eAAe,KAAgB,GAE7B;CACR;CAEA,OAAO;EACN,KAAI,IAAI,IAAI,GAAG,IAAI,KAAK,cAAc,KAAK;GAC1C,IAAI,IAAgB,QAAQ,KAAK,KAAK,WAAW,MAAM,IAAsB,CAAC,GAC1E,IAAU,EAAW,CAAa;GAEtC,IADkB,EAAgB,KAAK,QAAQ,CAC/C,CAAA,CAAQ,KAAK;EACd;EACA,AAAG,KAAK,kBACP,KAAK,WAAW,KAAK;CAEvB;CAEA,kBAAqC;EACpC,OAAO,EACN,YAAY,KAAK,WAAW,gBAAgB,EAC7C;CACD;AACD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { SharedAllocatedMemory } from './allocated-memory';
|
|
2
|
-
import
|
|
1
|
+
import type { SharedAllocatedMemory } from './allocated-memory';
|
|
2
|
+
import type MemoryHeap from './memory-heap';
|
|
3
3
|
export default class SharedString {
|
|
4
4
|
static readonly ALLOCATE_COUNT = 4;
|
|
5
5
|
private memory;
|
package/dist/shared-string.js
CHANGED
|
@@ -1,79 +1,73 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
free() {
|
|
64
|
-
let { bufferPosition: t, bufferByteOffset: e } = d(this.allocatedMemory.data, c);
|
|
65
|
-
e && this.memory.buffers[t].free(e), this.allocatedMemory.free();
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
function C(l) {
|
|
69
|
-
if (l.length <= 8192)
|
|
70
|
-
return String.fromCharCode.apply(null, l);
|
|
71
|
-
let e = "";
|
|
72
|
-
for (let r = 0; r < l.length; r += 8192)
|
|
73
|
-
e += String.fromCharCode.apply(null, l.subarray(r, r + 8192));
|
|
74
|
-
return e;
|
|
75
|
-
}
|
|
76
|
-
export {
|
|
77
|
-
M as default
|
|
1
|
+
import { getPointer as e, loadPointer as t, loadRawPointer as n, storeRawPointer as r } from "./utils/pointer.js";
|
|
2
|
+
import i from "./allocated-memory.js";
|
|
3
|
+
import { lock as a, unlock as o } from "./lock/simple-lock.js";
|
|
4
|
+
//#region src/shared-string.ts
|
|
5
|
+
var s = {
|
|
6
|
+
1: Uint8Array,
|
|
7
|
+
2: Uint16Array
|
|
8
|
+
}, c = 0, l = 1, u = 2, d = 3, f = class f {
|
|
9
|
+
static ALLOCATE_COUNT = 4;
|
|
10
|
+
memory;
|
|
11
|
+
allocatedMemory;
|
|
12
|
+
lock;
|
|
13
|
+
cachedPointer;
|
|
14
|
+
cachedString;
|
|
15
|
+
constructor(e, t) {
|
|
16
|
+
this.memory = e, typeof t == "string" ? (this.allocatedMemory = this.memory.allocUI32(f.ALLOCATE_COUNT), this.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + d * this.allocatedMemory.data.BYTES_PER_ELEMENT), this.updateString(t)) : (this.allocatedMemory = new i(e, t), this.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + d * this.allocatedMemory.data.BYTES_PER_ELEMENT), "value" in t && this.updateString(t.value));
|
|
17
|
+
}
|
|
18
|
+
updateString(e) {
|
|
19
|
+
let t = this.createString(e);
|
|
20
|
+
a(this.lock), r(this.allocatedMemory.data, c, t.pointer), Atomics.store(this.allocatedMemory.data, l, e.length), Atomics.store(this.allocatedMemory.data, u, t.charType), o(this.lock), this.cachedPointer = t.pointer, this.cachedString = e;
|
|
21
|
+
}
|
|
22
|
+
createString(e) {
|
|
23
|
+
if (e === "") return {
|
|
24
|
+
pointer: 0,
|
|
25
|
+
charType: 1
|
|
26
|
+
};
|
|
27
|
+
let t = 0;
|
|
28
|
+
for (let n = 0; n < e.length; n++) {
|
|
29
|
+
let r = e.charCodeAt(n);
|
|
30
|
+
r > t && (t = r);
|
|
31
|
+
}
|
|
32
|
+
let n = t > 255 ? 2 : 1, r = s[n], i = this.memory.allocUI32(Math.ceil(e.length / (4 / r.BYTES_PER_ELEMENT))), a = new r(i.data.buffer, i.data.byteOffset, e.length);
|
|
33
|
+
for (let t = 0; t < e.length; t++) a[t] = e.charCodeAt(t);
|
|
34
|
+
return {
|
|
35
|
+
pointer: i.pointer,
|
|
36
|
+
charType: n
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
get value() {
|
|
40
|
+
let t = n(this.allocatedMemory.data, c);
|
|
41
|
+
if (this.cachedPointer === t && this.cachedString !== void 0) return this.cachedString;
|
|
42
|
+
if (t === 0) return "";
|
|
43
|
+
let { bufferPosition: r, bufferByteOffset: i } = e(t);
|
|
44
|
+
a(this.lock);
|
|
45
|
+
let d = s[Atomics.load(this.allocatedMemory.data, u)], f = Atomics.load(this.allocatedMemory.data, l), m = p(new d(this.memory.buffers[r].buf, i, f));
|
|
46
|
+
return o(this.lock), this.cachedPointer = t, this.cachedString = m, m;
|
|
47
|
+
}
|
|
48
|
+
set value(e) {
|
|
49
|
+
let { bufferPosition: n, bufferByteOffset: r } = t(this.allocatedMemory.data, c);
|
|
50
|
+
this.updateString(e), r && this.memory.buffers[n].free(r);
|
|
51
|
+
}
|
|
52
|
+
getSharedMemory() {
|
|
53
|
+
return this.allocatedMemory.getSharedMemory();
|
|
54
|
+
}
|
|
55
|
+
get pointer() {
|
|
56
|
+
return this.allocatedMemory.pointer;
|
|
57
|
+
}
|
|
58
|
+
free() {
|
|
59
|
+
let { bufferPosition: e, bufferByteOffset: n } = t(this.allocatedMemory.data, c);
|
|
60
|
+
n && this.memory.buffers[e].free(n), this.allocatedMemory.free();
|
|
61
|
+
}
|
|
78
62
|
};
|
|
79
|
-
|
|
63
|
+
function p(e) {
|
|
64
|
+
let t = 8192;
|
|
65
|
+
if (e.length <= t) return String.fromCharCode.apply(null, e);
|
|
66
|
+
let n = "";
|
|
67
|
+
for (let r = 0; r < e.length; r += t) n += String.fromCharCode.apply(null, e.subarray(r, r + t));
|
|
68
|
+
return n;
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
export { f as default };
|
|
72
|
+
|
|
73
|
+
//# sourceMappingURL=shared-string.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-string.js","sources":["../src/shared-string.ts"],"sourcesContent":["import AllocatedMemory from './allocated-memory';\nimport type { SharedAllocatedMemory } from './allocated-memory';\nimport { lock, unlock } from './lock/simple-lock';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, loadPointer, loadRawPointer, storeRawPointer } from './utils/pointer';\n\nenum CHAR_TYPE {\n\tUNDEFINED,\n\tASCII,\n\tUTF16
|
|
1
|
+
{"version":3,"file":"shared-string.js","names":[],"sources":["../src/shared-string.ts"],"sourcesContent":["import AllocatedMemory from './allocated-memory';\nimport type { SharedAllocatedMemory } from './allocated-memory';\nimport { lock, unlock } from './lock/simple-lock';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, loadPointer, loadRawPointer, storeRawPointer } from './utils/pointer';\n\nenum CHAR_TYPE {\n\tUNDEFINED,\n\tASCII,\n\tUTF16,\n}\nconst TYPED_ARRAY_MAP = {\n\t[CHAR_TYPE.ASCII]: Uint8Array,\n\t[CHAR_TYPE.UTF16]: Uint16Array,\n};\n\nconst POINTER_INDEX = 0;\nconst LENGTH_INDEX = 1;\nconst TYPE_INDEX = 2;\nconst LOCK_INDEX = 3;\nexport default class SharedString {\n\tstatic readonly ALLOCATE_COUNT = 4;\n\n\tprivate memory: MemoryHeap;\n\tprivate allocatedMemory: AllocatedMemory;\n\tprivate lock: Int32Array;\n\n\tprivate cachedPointer?: number;\n\tprivate cachedString?: string;\n\t\n\tconstructor(memory: MemoryHeap, value: string | SharedStringConfig | SharedStringMemory) {\n\t\tthis.memory = memory;\n\n\t\tif(typeof value === 'string') {\n\t\t\tthis.allocatedMemory = this.memory.allocUI32(SharedString.ALLOCATE_COUNT);\n\t\t\tthis.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + LOCK_INDEX * this.allocatedMemory.data.BYTES_PER_ELEMENT);\n\t\t\tthis.updateString(value);\n\t\t} else {\n\t\t\tthis.allocatedMemory = new AllocatedMemory(memory, value);\n\t\t\tthis.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + LOCK_INDEX * this.allocatedMemory.data.BYTES_PER_ELEMENT);\n\n\t\t\t// We only allocated memory but didn't initialize the string yet\n\t\t\tif('value' in value) {\n\t\t\t\tthis.updateString(value.value);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tprivate updateString(value: string) {\n\t\tlet stringMemory = this.createString(value);\n\n\t\tlock(this.lock);\n\t\tstoreRawPointer(this.allocatedMemory.data, POINTER_INDEX, stringMemory.pointer);\n\t\tAtomics.store(this.allocatedMemory.data, LENGTH_INDEX, value.length);\n\t\tAtomics.store(this.allocatedMemory.data, TYPE_INDEX, stringMemory.charType);\n\t\tunlock(this.lock);\n\n\t\tthis.cachedPointer = stringMemory.pointer;\n\t\tthis.cachedString = value;\n\t}\n\tprivate createString(value: string) {\n\t\tif(value === '') {\n\t\t\treturn {\n\t\t\t\tpointer: 0,\n\t\t\t\tcharType: CHAR_TYPE.ASCII,\n\t\t\t};\n\t\t}\n\n\t\t// NOTE: Scan with a loop instead of Math.max(...charCodes) which blows the stack for large strings\n\t\tlet maxCharCode = 0;\n\t\tfor(let i = 0; i < value.length; i++) {\n\t\t\tlet charCode = value.charCodeAt(i);\n\t\t\tif(charCode > maxCharCode) {\n\t\t\t\tmaxCharCode = charCode;\n\t\t\t}\n\t\t}\n\t\tlet charType = maxCharCode > 255 ? CHAR_TYPE.UTF16 : CHAR_TYPE.ASCII;\n\n\t\tlet typedArray = TYPED_ARRAY_MAP[charType];\n\t\tlet allocatedMemory = this.memory.allocUI32(Math.ceil(value.length / (4 / typedArray.BYTES_PER_ELEMENT)));\n\t\tlet data = new typedArray(allocatedMemory.data.buffer as ArrayBuffer, allocatedMemory.data.byteOffset, value.length);\n\t\tfor(let i = 0; i < value.length; i++) {\n\t\t\tdata[i] = value.charCodeAt(i);\n\t\t}\n\n\t\treturn {\n\t\t\tpointer: allocatedMemory.pointer,\n\t\t\tcharType,\n\t\t};\n\t}\n\n\tget value(): string {\n\t\tlet pointer = loadRawPointer(this.allocatedMemory.data, POINTER_INDEX);\n\t\tif(this.cachedPointer === pointer && this.cachedString !== undefined) {\n\t\t\treturn this.cachedString;\n\t\t} else if(pointer === 0) {\n\t\t\treturn '';\n\t\t}\n\t\tlet { bufferPosition, bufferByteOffset } = getPointer(pointer);\n\n\t\tlock(this.lock);\n\t\tlet charType = Atomics.load(this.allocatedMemory.data, TYPE_INDEX);\n\t\t// @ts-expect-error\n\t\tlet typedArray = TYPED_ARRAY_MAP[charType];\n\t\tlet bufferLength = Atomics.load(this.allocatedMemory.data, LENGTH_INDEX);\n\n\t\tlet data = new typedArray(this.memory.buffers[bufferPosition].buf, bufferByteOffset, bufferLength);\n\t\tlet string = fromCharCodes(data);\n\t\t// NOTE: Do not unlock until after transforming the data since the second this is done it can free that memory block\n\t\tunlock(this.lock);\n\n\t\tthis.cachedPointer = pointer;\n\t\tthis.cachedString = string;\n\n\t\treturn string;\n\t}\n\tset value(value: string) {\n\t\tlet { bufferPosition: oldBufferPosition, bufferByteOffset: oldBufferByteOffset } = loadPointer(this.allocatedMemory.data, POINTER_INDEX);\n\t\tthis.updateString(value);\n\n\t\tif(oldBufferByteOffset) {\n\t\t\tthis.memory.buffers[oldBufferPosition].free(oldBufferByteOffset);\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedStringMemory {\n\t\treturn this.allocatedMemory.getSharedMemory();\n\t}\n\n\tget pointer() {\n\t\treturn this.allocatedMemory.pointer;\n\t}\n\n\tfree() {\n\t\tlet { bufferPosition, bufferByteOffset } = loadPointer(this.allocatedMemory.data, POINTER_INDEX);\n\t\tif(bufferByteOffset) {\n\t\t\tthis.memory.buffers[bufferPosition].free(bufferByteOffset);\n\t\t}\n\t\tthis.allocatedMemory.free();\n\t}\n}\n\n// String.fromCharCode.apply throws a RangeError (Maximum call stack size exceeded) when handed too many\n// arguments at once, so decode large buffers in chunks to support arbitrarily long strings\nfunction fromCharCodes(data: Uint8Array | Uint16Array): string {\n\tconst CHUNK_SIZE = 8192;\n\tif(data.length <= CHUNK_SIZE) {\n\t\treturn String.fromCharCode.apply(null, data as unknown as Array<number>);\n\t}\n\n\tlet string = '';\n\tfor(let i = 0; i < data.length; i += CHUNK_SIZE) {\n\t\tstring += String.fromCharCode.apply(null, data.subarray(i, i + CHUNK_SIZE) as unknown as Array<number>);\n\t}\n\n\treturn string;\n}\n\ninterface SharedStringConfig extends SharedStringMemory {\n\tvalue: string\n}\ntype SharedStringMemory = SharedAllocatedMemory;"],"mappings":";;;;AAWA,IAAM,IAAkB;CACJ,GAAA;CACA,GAAA;AACpB,GAEM,IAAgB,GAChB,IAAe,GACf,IAAa,GACb,IAAa,GACE,IAArB,MAAqB,EAAa;CACjC,OAAgB,iBAAiB;CAEjC;CACA;CACA;CAEA;CACA;CAEA,YAAY,GAAoB,GAAyD;EAGxF,AAFA,KAAK,SAAS,GAEX,OAAO,KAAU,YACnB,KAAK,kBAAkB,KAAK,OAAO,UAAU,EAAa,cAAc,GACxE,KAAK,OAAO,IAAI,WAAW,KAAK,gBAAgB,KAAK,QAAQ,KAAK,gBAAgB,mBAAmB,IAAa,KAAK,gBAAgB,KAAK,iBAAiB,GAC7J,KAAK,aAAa,CAAK,MAEvB,KAAK,kBAAkB,IAAI,EAAgB,GAAQ,CAAK,GACxD,KAAK,OAAO,IAAI,WAAW,KAAK,gBAAgB,KAAK,QAAQ,KAAK,gBAAgB,mBAAmB,IAAa,KAAK,gBAAgB,KAAK,iBAAiB,GAG1J,WAAW,KACb,KAAK,aAAa,EAAM,KAAK;CAGhC;CAEA,aAAqB,GAAe;EACnC,IAAI,IAAe,KAAK,aAAa,CAAK;EAS1C,AAPA,EAAK,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,EAAe,CAAC,KAAK,GAAkB,CAC1D,CAAI;EAO/B,OALA,EAAO,KAAK,IAAI,GAEhB,KAAK,gBAAgB,GACrB,KAAK,eAAe,GAEb;CACR;CACA,IAAI,MAAM,GAAe;EACxB,IAAI,EAAE,gBAAgB,GAAmB,kBAAkB,MAAwB,EAAY,KAAK,gBAAgB,MAAM,CAAa;EAGvI,AAFA,KAAK,aAAa,CAAK,GAEpB,KACF,KAAK,OAAO,QAAQ,EAAkB,CAAC,KAAK,CAAmB;CAEjE;CAEA,kBAAsC;EACrC,OAAO,KAAK,gBAAgB,gBAAgB;CAC7C;CAEA,IAAI,UAAU;EACb,OAAO,KAAK,gBAAgB;CAC7B;CAEA,OAAO;EACN,IAAI,EAAE,mBAAgB,wBAAqB,EAAY,KAAK,gBAAgB,MAAM,CAAa;EAI/F,AAHG,KACF,KAAK,OAAO,QAAQ,EAAe,CAAC,KAAK,CAAgB,GAE1D,KAAK,gBAAgB,KAAK;CAC3B;AACD;AAIA,SAAS,EAAc,GAAwC;CAC9D,IAAM,IAAa;CACnB,IAAG,EAAK,UAAU,GACjB,OAAO,OAAO,aAAa,MAAM,MAAM,CAAgC;CAGxE,IAAI,IAAS;CACb,KAAI,IAAI,IAAI,GAAG,IAAI,EAAK,QAAQ,KAAK,GACpC,KAAU,OAAO,aAAa,MAAM,MAAM,EAAK,SAAS,GAAG,IAAI,CAAU,CAA6B;CAGvG,OAAO;AACR"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { SharedAllocatedMemory } from './allocated-memory';
|
|
2
|
-
import { TypedArrayConstructor } from './interfaces/typed-array-constructor';
|
|
3
|
-
import
|
|
1
|
+
import type { SharedAllocatedMemory } from './allocated-memory';
|
|
2
|
+
import type { TypedArrayConstructor } from './interfaces/typed-array-constructor';
|
|
3
|
+
import type MemoryHeap from './memory-heap';
|
|
4
4
|
export default class SharedVector<T extends Uint32Array | Int32Array | Float32Array = Uint32Array> implements Iterable<T> {
|
|
5
5
|
static readonly ALLOCATE_COUNT = 4;
|
|
6
6
|
private memory;
|
|
@@ -14,9 +14,10 @@ export default class SharedVector<T extends Uint32Array | Int32Array | Float32Ar
|
|
|
14
14
|
get bufferLength(): number;
|
|
15
15
|
private set bufferLength(value);
|
|
16
16
|
get pointer(): number;
|
|
17
|
+
get usedMemory(): number;
|
|
17
18
|
private cachedFullDataBlock?;
|
|
18
19
|
private cachedPointer;
|
|
19
|
-
constructor(memory: MemoryHeap, config?: SharedVectorConfig<T> | SharedVectorMemory | SharedVectorMemory & SharedVectorConfig<T>);
|
|
20
|
+
constructor(memory: MemoryHeap, config?: SharedVectorConfig<T> | SharedVectorMemory | (SharedVectorMemory & SharedVectorConfig<T>));
|
|
20
21
|
at(index: number): T;
|
|
21
22
|
get(index: number, dataIndex?: number): number;
|
|
22
23
|
push(values: number | Array<number>): number;
|