@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
package/dist/shared-pool.js
CHANGED
|
@@ -1,165 +1,152 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
free() {
|
|
149
|
-
this.recycleVector.free();
|
|
150
|
-
for (let t of this.pointerVector) {
|
|
151
|
-
let e = t[0];
|
|
152
|
-
new f(this.memory, n(e)).free();
|
|
153
|
-
}
|
|
154
|
-
this.pointerVector.free(), this.firstBlock.free();
|
|
155
|
-
}
|
|
156
|
-
getSharedMemory() {
|
|
157
|
-
return {
|
|
158
|
-
firstBlock: this.firstBlock.getSharedMemory()
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
export {
|
|
163
|
-
k as default
|
|
1
|
+
import { getPointer as e, loadRawPointer as t, storeRawPointer as n } from "./utils/pointer.js";
|
|
2
|
+
import r from "./allocated-memory.js";
|
|
3
|
+
import { lock as i, unlock as a } from "./lock/simple-lock.js";
|
|
4
|
+
import o from "./shared-stack.js";
|
|
5
|
+
//#region src/shared-pool.ts
|
|
6
|
+
var s = 0, c = 1, l = 2, u = 3, d = 4, f = 5, p = 100, m = 1e6, h = 1e5, g = class g {
|
|
7
|
+
static ALLOCATE_COUNT = 6;
|
|
8
|
+
memory;
|
|
9
|
+
firstBlock;
|
|
10
|
+
uint16Array;
|
|
11
|
+
growLock;
|
|
12
|
+
pointerStack;
|
|
13
|
+
recycleStack;
|
|
14
|
+
maxLength;
|
|
15
|
+
cachedFullDataBlock = {};
|
|
16
|
+
cachedMaxChunkSize;
|
|
17
|
+
cachedDataLength;
|
|
18
|
+
cachedType;
|
|
19
|
+
cachedByteMultipler;
|
|
20
|
+
get length() {
|
|
21
|
+
return Atomics.load(this.firstBlock.data, s) - this.recycleStack.length;
|
|
22
|
+
}
|
|
23
|
+
get maxChunkSize() {
|
|
24
|
+
return this.cachedMaxChunkSize;
|
|
25
|
+
}
|
|
26
|
+
set maxChunkSize(e) {
|
|
27
|
+
Atomics.store(this.firstBlock.data, l, e);
|
|
28
|
+
}
|
|
29
|
+
get type() {
|
|
30
|
+
return this.cachedType;
|
|
31
|
+
}
|
|
32
|
+
set type(e) {
|
|
33
|
+
Atomics.store(this.uint16Array, 0, e);
|
|
34
|
+
}
|
|
35
|
+
get dataLength() {
|
|
36
|
+
return this.cachedDataLength;
|
|
37
|
+
}
|
|
38
|
+
set dataLength(e) {
|
|
39
|
+
Atomics.store(this.uint16Array, 1, e);
|
|
40
|
+
}
|
|
41
|
+
get bufferLength() {
|
|
42
|
+
return this.cachedMaxChunkSize * this.pointerStack.length;
|
|
43
|
+
}
|
|
44
|
+
get byteMultipler() {
|
|
45
|
+
return this.cachedByteMultipler;
|
|
46
|
+
}
|
|
47
|
+
get usedMemory() {
|
|
48
|
+
let t = this.firstBlock.usedMemory;
|
|
49
|
+
for (let n of this.pointerStack) t += new r(this.memory, e(n)).usedMemory;
|
|
50
|
+
return t + this.pointerStack.usedMemory + this.recycleStack.usedMemory;
|
|
51
|
+
}
|
|
52
|
+
constructor(i, a) {
|
|
53
|
+
if (this.memory = i, a && "firstBlock" in a) this.firstBlock = new r(i, a.firstBlock), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + c * Uint32Array.BYTES_PER_ELEMENT, 2), this.pointerStack = new o(i, { firstBlock: e(t(this.firstBlock.data, d)) }), this.recycleStack = new o(i, { firstBlock: e(t(this.firstBlock.data, f)) });
|
|
54
|
+
else {
|
|
55
|
+
let e = a?.dataLength ?? 1, t = a?.maxChunkSize ?? p, r = a?.maxLength ?? m, s = a?.maxRecycledLength ?? h, l = o.getMaxSegmentLength(i.maxAllocationLength), u = r / t, _ = o.getAllocateCount(u, void 0, l), v = o.getAllocateCount(s, void 0, l);
|
|
56
|
+
this.firstBlock = i.allocUI32(g.ALLOCATE_COUNT + _ + v), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + c * Uint32Array.BYTES_PER_ELEMENT, 2), this.pointerStack = new o(i, {
|
|
57
|
+
firstBlock: this.firstBlock.getSubAllocation(g.ALLOCATE_COUNT, _),
|
|
58
|
+
type: Uint32Array,
|
|
59
|
+
maxLength: u
|
|
60
|
+
}), n(this.firstBlock.data, d, this.pointerStack.pointer), this.recycleStack = new o(i, {
|
|
61
|
+
firstBlock: this.firstBlock.getSubAllocation(g.ALLOCATE_COUNT + _, v),
|
|
62
|
+
type: Uint32Array,
|
|
63
|
+
maxLength: s
|
|
64
|
+
}), n(this.firstBlock.data, f, this.recycleStack.pointer);
|
|
65
|
+
let y = a?.type ?? Uint32Array;
|
|
66
|
+
y === Uint32Array ? this.type = 0 : y === Int32Array ? this.type = 1 : y === Float32Array ? this.type = 2 : y === Float64Array && (this.type = 3), this.dataLength = e, this.maxChunkSize = t;
|
|
67
|
+
let b = y === Float64Array ? 2 : 1, x = i.allocUI32(t * e * b);
|
|
68
|
+
this.pointerStack.push(x.pointer);
|
|
69
|
+
}
|
|
70
|
+
this.cachedType = this.uint16Array[0], this.cachedDataLength = Math.max(1, this.uint16Array[1]), this.cachedMaxChunkSize = this.firstBlock.data[l], this.cachedByteMultipler = this.cachedType === 3 ? 2 : 1, this.maxLength = this.cachedMaxChunkSize * this.pointerStack.maxLength, this.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + u * Uint32Array.BYTES_PER_ELEMENT, 1);
|
|
71
|
+
}
|
|
72
|
+
at(e) {
|
|
73
|
+
let t = this.getFullDataBlock(e);
|
|
74
|
+
return this.getDataBlock(t, e % this.cachedMaxChunkSize);
|
|
75
|
+
}
|
|
76
|
+
get(e, t = 0) {
|
|
77
|
+
let n = this.cachedDataLength;
|
|
78
|
+
if (t >= n) throw Error(`${t} is out of dataLength bounds ${n}`);
|
|
79
|
+
return this.getFullDataBlock(e)[e % this.cachedMaxChunkSize * n + t];
|
|
80
|
+
}
|
|
81
|
+
push(e) {
|
|
82
|
+
let t = this.cachedDataLength, n = typeof e == "number";
|
|
83
|
+
if (!n && e.length > t) throw Error(`Can't insert ${e.length} array into shared list of ${t} dataLength`);
|
|
84
|
+
let r = this.recycleStack.pop();
|
|
85
|
+
r === void 0 && (r = Atomics.add(this.firstBlock.data, s, 1));
|
|
86
|
+
let i = this.getFullDataBlock(r), a = t * (r % this.cachedMaxChunkSize);
|
|
87
|
+
return n ? i[a] = e : i.set(e, a), r;
|
|
88
|
+
}
|
|
89
|
+
deleteIndex(e) {
|
|
90
|
+
this.recycleStack.push(e);
|
|
91
|
+
}
|
|
92
|
+
clear() {
|
|
93
|
+
this.firstBlock.data[s] = 0, this.recycleStack.clear();
|
|
94
|
+
}
|
|
95
|
+
*[Symbol.iterator]() {
|
|
96
|
+
let e = {};
|
|
97
|
+
for (let t of this.recycleStack) e[t] = !0;
|
|
98
|
+
let t = this.cachedMaxChunkSize, n = Atomics.load(this.firstBlock.data, s), r = this.getFullDataBlock(0), i = 0;
|
|
99
|
+
for (let a = 0; a < n; a++) if (!e[a]) {
|
|
100
|
+
let e = Math.floor(a / t);
|
|
101
|
+
e !== i && (r = this.getFullDataBlock(a), i = e), yield this.getDataBlock(r, a % t);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
getFullDataBlock(t) {
|
|
105
|
+
let n = Math.floor(t / this.cachedMaxChunkSize), o = this.cachedFullDataBlock[n];
|
|
106
|
+
if (o) return o;
|
|
107
|
+
if (n >= this.pointerStack.length) {
|
|
108
|
+
i(this.growLock);
|
|
109
|
+
try {
|
|
110
|
+
for (; n >= this.pointerStack.length;) {
|
|
111
|
+
let e = this.memory.allocUI32(this.cachedMaxChunkSize * this.cachedDataLength * this.cachedByteMultipler);
|
|
112
|
+
this.pointerStack.push(e.pointer);
|
|
113
|
+
}
|
|
114
|
+
} finally {
|
|
115
|
+
a(this.growLock);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
let s = new r(this.memory, e(this.pointerStack.at(n))), c = this.cachedDataLength * this.cachedMaxChunkSize, l;
|
|
119
|
+
switch (this.cachedType) {
|
|
120
|
+
case 1:
|
|
121
|
+
l = new Int32Array(s.data.buffer, s.bufferByteOffset, c);
|
|
122
|
+
break;
|
|
123
|
+
case 0:
|
|
124
|
+
l = new Uint32Array(s.data.buffer, s.bufferByteOffset, c);
|
|
125
|
+
break;
|
|
126
|
+
case 2:
|
|
127
|
+
l = new Float32Array(s.data.buffer, s.bufferByteOffset, c);
|
|
128
|
+
break;
|
|
129
|
+
case 3:
|
|
130
|
+
l = new Float64Array(s.data.buffer, s.bufferByteOffset, c);
|
|
131
|
+
break;
|
|
132
|
+
default: throw Error(`Unknown data block type ${this.cachedType}`);
|
|
133
|
+
}
|
|
134
|
+
return this.cachedFullDataBlock[n] = l, l;
|
|
135
|
+
}
|
|
136
|
+
getDataBlock(e, t) {
|
|
137
|
+
let n = this.cachedDataLength, r = t * n;
|
|
138
|
+
return e.subarray(r, r + n);
|
|
139
|
+
}
|
|
140
|
+
free() {
|
|
141
|
+
this.recycleStack.free();
|
|
142
|
+
for (let t of this.pointerStack) new r(this.memory, e(t)).free();
|
|
143
|
+
this.pointerStack.free(), this.firstBlock.free();
|
|
144
|
+
}
|
|
145
|
+
getSharedMemory() {
|
|
146
|
+
return { firstBlock: this.firstBlock.getSharedMemory() };
|
|
147
|
+
}
|
|
164
148
|
};
|
|
165
|
-
//#
|
|
149
|
+
//#endregion
|
|
150
|
+
export { g as default };
|
|
151
|
+
|
|
152
|
+
//# sourceMappingURL=shared-pool.js.map
|
package/dist/shared-pool.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-pool.js","sources":["../src/shared-pool.ts"],"sourcesContent":["import type { SharedAllocatedMemory } from './allocated-memory';\nimport AllocatedMemory from './allocated-memory';\nimport type { TypedArrayConstructor } from './interfaces/typed-array-constructor';\nimport type MemoryHeap from './memory-heap';\nimport SharedVector from './shared-vector';\nimport { getPointer } from './utils/pointer';\n\nenum TYPE {\n\tuint32,\n\tint32,\n\tfloat32,\n\tfloat64\n}\n\nconst LENGTH_INDEX = 0;\nconst TYPE_INDEX = 1;\nconst MAX_CHUNK_SIZE_INDEX = 2;\nconst POINTERS_INDEX = 3;\nconst RECYCLE_INDEX = POINTERS_INDEX + SharedVector.ALLOCATE_COUNT;\n\n// Array with stable indexes and maximum contiguous memory sizes (necessary to fit large data sets into max 1MB buffers)\n// https://plflib.org/colony.htm for future enhancements - it seems to be an optimized version of what we were aiming for with this\nexport default class SharedPool<T extends Uint32Array | Int32Array | Float32Array | Float64Array = Uint32Array> implements Iterable<T> {\n\tstatic readonly ALLOCATE_COUNT = 3 + SharedVector.ALLOCATE_COUNT * 2;\n\tprivate memory: MemoryHeap;\n\n\t// Current Length, Type/DataLength, MaxChunkLength, Pointer vector, Recycle vector\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\tprivate pointerVector: SharedVector<Uint32Array>;\n\tprivate recycleVector: SharedVector<Uint32Array>;\n\n\tprivate cachedFullDataBlock: { [key: number]: T } = {};\n\n\tget length(): number {\n\t\treturn Atomics.load(this.firstBlock.data, LENGTH_INDEX) - this.recycleVector.length;\n\t}\n\tget maxChunkSize(): number {\n\t\treturn this.firstBlock.data[MAX_CHUNK_SIZE_INDEX];\n\t}\n\tprivate set maxChunkSize(value: number) {\n\t\tAtomics.store(this.firstBlock.data, MAX_CHUNK_SIZE_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\tget dataLength(): number {\n\t\treturn Math.max(1, this.uint16Array[1]);\n\t}\n\tprivate set dataLength(value: number) {\n\t\tAtomics.store(this.uint16Array, 1, value);\n\t}\n\n\tget bufferLength(): number {\n\t\treturn this.maxChunkSize * this.pointerVector.length;\n\t}\n\tget recycleBufferLength(): number {\n\t\treturn this.recycleVector.bufferLength;\n\t}\n\tget byteMultipler(): number {\n\t\tif(this.type === TYPE.float64) {\n\t\t\treturn 2;\n\t\t} else {\n\t\t\treturn 1;\n\t\t}\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedPoolConfig<T> | SharedPoolMemory) {\n\t\tthis.memory = memory;\n\n\t\tif(config && 'firstBlock' in config) {\n\t\t\tthis.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\t\t\tthis.pointerVector = new SharedVector(memory, {\n\t\t\t\tfirstBlock: {\n\t\t\t\t\tbufferPosition: this.firstBlock.bufferPosition,\n\t\t\t\t\tbufferByteOffset: this.firstBlock.bufferByteOffset + POINTERS_INDEX * Uint32Array.BYTES_PER_ELEMENT\n\t\t\t\t}\n\t\t\t});\n\t\t\tthis.recycleVector = new SharedVector(memory, {\n\t\t\t\tfirstBlock: {\n\t\t\t\t\tbufferPosition: this.firstBlock.bufferPosition,\n\t\t\t\t\tbufferByteOffset: this.firstBlock.bufferByteOffset + RECYCLE_INDEX * Uint32Array.BYTES_PER_ELEMENT\n\t\t\t\t}\n\t\t\t});\n\t\t} else {\n\t\t\tthis.firstBlock = memory.allocUI32(SharedPool.ALLOCATE_COUNT);\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\tlet dataLength = config?.dataLength ?? 1;\n\t\t\tlet maxLength = config?.maxChunkSize ?? 100;\n\n\t\t\tthis.pointerVector = new SharedVector(memory, {\n\t\t\t\ttype: Uint32Array,\n\t\t\t\tfirstBlock: {\n\t\t\t\t\tbufferPosition: this.firstBlock.bufferPosition,\n\t\t\t\t\tbufferByteOffset: this.firstBlock.bufferByteOffset + POINTERS_INDEX * Uint32Array.BYTES_PER_ELEMENT\n\t\t\t\t}\n\t\t\t});\n\t\t\tthis.recycleVector = new SharedVector(memory, {\n\t\t\t\ttype: Uint32Array,\n\t\t\t\tfirstBlock: {\n\t\t\t\t\tbufferPosition: this.firstBlock.bufferPosition,\n\t\t\t\t\tbufferByteOffset: this.firstBlock.bufferByteOffset + RECYCLE_INDEX * Uint32Array.BYTES_PER_ELEMENT\n\t\t\t\t},\n\t\t\t\tbufferLength: config?.recycleBufferLength\n\t\t\t});\n\n\t\t\tconst type = config?.type ?? Uint32Array;\n\t\t\tif(type === Uint32Array) {\n\t\t\t\tthis.type = TYPE.uint32;\n\t\t\t}\n\t\t\t// @ts-expect-error\n\t\t\telse if(type === Int32Array) {\n\t\t\t\tthis.type = TYPE.int32;\n\t\t\t}\n\t\t\t// @ts-expect-error\n\t\t\telse if(type === Float32Array) {\n\t\t\t\tthis.type = TYPE.float32;\n\t\t\t// @ts-expect-error\n\t\t\t} else if(type === Float64Array) {\n\t\t\t\tthis.type = TYPE.float64;\n\t\t\t}\n\t\t\tthis.dataLength = dataLength;\n\t\t\tthis.maxChunkSize = maxLength;\n\n\t\t\tlet firstArray = memory.allocUI32(maxLength * dataLength * this.byteMultipler);\n\t\t\tthis.pointerVector.push(firstArray.pointer);\n\t\t}\n\t}\n\n\tat(index: number): T {\n\t\tlet dataBlock = this.getFullDataBlock(index);\n\t\treturn this.getDataBlock(dataBlock, index % this.maxChunkSize);\n\t}\n\tget(index: number, dataIndex = 0): number {\n\t\tconst dataLength = this.dataLength;\n\t\tif(dataIndex >= dataLength) {\n\t\t\tthrow new Error(`${dataIndex} is out of dataLength bounds ${dataLength}`);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock(index);\n\t\treturn dataBlock[(index % this.maxChunkSize) * dataLength + dataIndex];\n\t}\n\n\tpush(values: number | Array<number>): number {\n\t\tif(typeof values === 'number') {\n\t\t\tvalues = [values];\n\t\t}\n\n\t\tlet dataLength = this.dataLength;\n\t\tif(values.length > dataLength) {\n\t\t\tthrow new Error(`Can't insert ${values.length} array into shared list of ${dataLength} dataLength`);\n\t\t}\n\n\t\tlet newIndex;\n\t\tif(this.recycleVector.length) {\n\t\t\tnewIndex = this.recycleVector.popNumber();\n\t\t} else {\n\t\t\tnewIndex = Atomics.add(this.firstBlock.data, LENGTH_INDEX, 1);\n\t\t}\n\t\t\n\t\tlet dataBlock = this.getFullDataBlock(newIndex);\n\t\tlet blockIndex = newIndex % this.maxChunkSize;\n\t\tdataBlock.set(values, dataLength * blockIndex);\n\n\t\treturn newIndex;\n\t}\n\n\tdeleteIndex(index: number) {\n\t\tthis.recycleVector.push(index);\n\t}\n\n\tclear() {\n\t\tthis.firstBlock.data[LENGTH_INDEX] = 0;\n\t\tthis.recycleVector.clear();\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tconst recycledValues: { [key: number]: true } = {};\n\t\tfor(let value of this.recycleVector) {\n\t\t\trecycledValues[value[0]] = true;\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock(0);\n\t\tlet dataBlockIndex = 0;\n\t\tfor(let i = 0; i < Atomics.load(this.firstBlock.data, LENGTH_INDEX); i++) {\n\t\t\tif(!recycledValues[i]) {\n\t\t\t\tlet newDataBlockIndex = Math.floor(i / this.maxChunkSize);\n\t\t\t\tif(newDataBlockIndex !== dataBlockIndex) {\n\t\t\t\t\tdataBlock = this.getFullDataBlock(i);\n\t\t\t\t\tdataBlockIndex = newDataBlockIndex;\n\t\t\t\t}\n\t\t\t\tyield this.getDataBlock(dataBlock, i % this.maxChunkSize);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getFullDataBlock(index: number) {\n\t\tlet pointerIndex = Math.floor(index / this.maxChunkSize);\n\t\tlet cachedDataBlock = this.cachedFullDataBlock[pointerIndex];\n\t\tif(cachedDataBlock) {\n\t\t\treturn cachedDataBlock;\n\t\t}\n\t\tif(pointerIndex >= this.pointerVector.length) {\n\t\t\tlet newArray = this.memory.allocUI32(this.maxChunkSize * this.dataLength * this.byteMultipler);\n\t\t\tthis.pointerVector.push(newArray.pointer);\n\t\t}\n\n\t\tlet array = new AllocatedMemory(this.memory, getPointer(this.pointerVector.get(pointerIndex)));\n\n\t\tlet data: T;\n\t\tswitch(this.type) {\n\t\t\tcase TYPE.int32:\n\t\t\t\tdata = new Int32Array(array.data.buffer, array.bufferByteOffset, this.dataLength * this.maxChunkSize) as T;\n\t\t\t\tbreak;\n\t\t\tcase TYPE.uint32:\n\t\t\t\tdata = new Uint32Array(array.data.buffer, array.bufferByteOffset, this.dataLength * this.maxChunkSize) as T;\n\t\t\t\tbreak;\n\t\t\tcase TYPE.float32:\n\t\t\t\tdata = new Float32Array(array.data.buffer, array.bufferByteOffset, this.dataLength * this.maxChunkSize) as T;\n\t\t\t\tbreak;\n\t\t\tcase TYPE.float64:\n\t\t\t\tdata = new Float64Array(array.data.buffer, array.bufferByteOffset, this.dataLength * this.maxChunkSize) 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\n\t\tthis.cachedFullDataBlock[pointerIndex] = data;\n\t\treturn data;\n\t}\n\n\tprivate getDataBlock(rawData: T, index: number): T {\n\t\tconst start = index * this.dataLength;\n\t\treturn rawData.subarray(start, start + this.dataLength) as T;\n\t}\n\n\tfree() {\n\t\tthis.recycleVector.free();\n\n\t\tfor(let pointerArray of this.pointerVector) {\n\t\t\tlet pointer = pointerArray[0];\n\t\t\tlet memory = new AllocatedMemory(this.memory, getPointer(pointer));\n\t\t\tmemory.free();\n\t\t}\n\t\tthis.pointerVector.free();\n\t\tthis.firstBlock.free();\n\t}\n\n\tgetSharedMemory(): SharedPoolMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.firstBlock.getSharedMemory()\n\t\t};\n\t}\n}\n\ninterface SharedPoolConfig<T extends Uint32Array | Int32Array | Float32Array | Float64Array> {\n\tmaxChunkSize?: number\n\ttype?: TypedArrayConstructor<T>\n\tdataLength?: number\n\trecycleBufferLength?: number\n}\ninterface SharedPoolMemory {\n\tfirstBlock: SharedAllocatedMemory\n}\n\nexport type { SharedPoolConfig, SharedPoolMemory };"],"names":["LENGTH_INDEX","TYPE_INDEX","MAX_CHUNK_SIZE_INDEX","POINTERS_INDEX","RECYCLE_INDEX","SharedVector","SharedPool","value","memory","config","AllocatedMemory","dataLength","maxLength","type","firstArray","index","dataBlock","dataIndex","values","newIndex","blockIndex","recycledValues","dataBlockIndex","i","newDataBlockIndex","pointerIndex","cachedDataBlock","newArray","array","getPointer","data","rawData","start","pointerArray","pointer"],"mappings":";;;AAcA,MAAMA,IAAe,GACfC,IAAa,GACbC,IAAuB,GACvBC,IAAiB,GACjBC,IAAgBD,IAAiBE,EAAa;AAIpD,MAAqBC,EAAkH;AAAA,EACtI,OAAgB,iBAAiB,IAAID,EAAa,iBAAiB;AAAA,EAC3D;AAAA;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA,sBAA4C,CAAA;AAAA,EAEpD,IAAI,SAAiB;AACpB,WAAO,QAAQ,KAAK,KAAK,WAAW,MAAML,CAAY,IAAI,KAAK,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AAK,UAAU;AAAA,EACvD;AAAA,EAEA,OAAO;AACN,SAAK,cAAc,KAAA;AAEnB,aAAQC,KAAgB,KAAK,eAAe;AAC3C,UAAIC,IAAUD,EAAa,CAAC;AAE5B,MADa,IAAIvB,EAAgB,KAAK,QAAQmB,EAAWK,CAAO,CAAC,EAC1D,KAAA;AAAA,IACR;AACA,SAAK,cAAc,KAAA,GACnB,KAAK,WAAW,KAAA;AAAA,EACjB;AAAA,EAEA,kBAAoC;AACnC,WAAO;AAAA,MACN,YAAY,KAAK,WAAW,gBAAA;AAAA,IAAgB;AAAA,EAE9C;AACD;"}
|
|
1
|
+
{"version":3,"file":"shared-pool.js","names":[],"sources":["../src/shared-pool.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 SharedStack from './shared-stack';\nimport { getPointer, loadRawPointer, storeRawPointer } from './utils/pointer';\n\nenum TYPE {\n\tuint32,\n\tint32,\n\tfloat32,\n\tfloat64,\n}\n\nconst LENGTH_INDEX = 0;\nconst TYPE_INDEX = 1;\nconst MAX_CHUNK_SIZE_INDEX = 2;\n// Guards appending a chunk pointer so only one thread grows the pointer stack at a time\nconst GROW_LOCK_INDEX = 3;\n// Pointers to the two backing SharedStacks (their own firstBlocks): the chunk pointers and the recycled indexes\nconst POINTER_STACK_INDEX = 4;\nconst RECYCLE_STACK_INDEX = 5;\n\nconst DEFAULT_MAX_CHUNK_SIZE = 100;\nconst DEFAULT_MAX_LENGTH = 1_000_000;\nconst DEFAULT_MAX_RECYCLED_LENGTH = 100_000;\n\n// Array with stable indexes and maximum contiguous memory sizes (necessary to fit large data sets into max 1MB buffers).\n// The chunk pointers and the recycled indexes both live in SharedStacks, whose segments are never moved or freed once\n// published, so concurrent readers never touch memory that is being reclaimed.\n// https://plflib.org/colony.htm for future enhancements - it seems to be an optimized version of what we were aiming for with this\nexport default class SharedPool<T extends Uint32Array | Int32Array | Float32Array | Float64Array = Uint32Array> implements Iterable<T> {\n\tstatic readonly ALLOCATE_COUNT = 6;\n\tprivate memory: MemoryHeap;\n\n\t// Current Length, Type/DataLength, MaxChunkLength, Grow lock, Pointer stack, Recycle stack\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\tprivate growLock: Int32Array;\n\tprivate pointerStack: SharedStack;\n\tprivate recycleStack: SharedStack;\n\treadonly maxLength: number;\n\n\tprivate cachedFullDataBlock: { [key: number]: T } = {};\n\n\t// Config is written once at construction and never mutated, so it is cached in plain fields\n\tprivate readonly cachedMaxChunkSize: number;\n\tprivate readonly cachedDataLength: number;\n\tprivate readonly cachedType: number;\n\tprivate readonly cachedByteMultipler: number;\n\n\tget length(): number {\n\t\treturn Atomics.load(this.firstBlock.data, LENGTH_INDEX) - this.recycleStack.length;\n\t}\n\tget maxChunkSize(): number {\n\t\treturn this.cachedMaxChunkSize;\n\t}\n\tprivate set maxChunkSize(value: number) {\n\t\tAtomics.store(this.firstBlock.data, MAX_CHUNK_SIZE_INDEX, value);\n\t}\n\n\tget type(): number {\n\t\treturn this.cachedType;\n\t}\n\tprivate set type(value: number) {\n\t\tAtomics.store(this.uint16Array, 0, value);\n\t}\n\tget dataLength(): number {\n\t\treturn this.cachedDataLength;\n\t}\n\tprivate set dataLength(value: number) {\n\t\tAtomics.store(this.uint16Array, 1, value);\n\t}\n\n\tget bufferLength(): number {\n\t\treturn this.cachedMaxChunkSize * this.pointerStack.length;\n\t}\n\tget byteMultipler(): number {\n\t\treturn this.cachedByteMultipler;\n\t}\n\n\t// Own internal memory (which already includes both stacks' inlined firstBlocks) plus every chunk buffer and the\n\t// segments each stack has grown. The stacks' usedMemory excludes their inlined firstBlocks, so they aren't double-counted.\n\tget usedMemory(): number {\n\t\tlet total = this.firstBlock.usedMemory;\n\t\tfor(let pointer of this.pointerStack) {\n\t\t\ttotal += new AllocatedMemory(this.memory, getPointer(pointer)).usedMemory;\n\t\t}\n\t\treturn total + this.pointerStack.usedMemory + this.recycleStack.usedMemory;\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedPoolConfig<T> | SharedPoolMemory) {\n\t\tthis.memory = memory;\n\n\t\tif(config && 'firstBlock' in config) {\n\t\t\tthis.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\t\t\tthis.pointerStack = new SharedStack(memory, {\n\t\t\t\tfirstBlock: getPointer(loadRawPointer(this.firstBlock.data, POINTER_STACK_INDEX)),\n\t\t\t});\n\t\t\tthis.recycleStack = new SharedStack(memory, {\n\t\t\t\tfirstBlock: getPointer(loadRawPointer(this.firstBlock.data, RECYCLE_STACK_INDEX)),\n\t\t\t});\n\t\t} else {\n\t\t\tlet dataLength = config?.dataLength ?? 1;\n\t\t\tlet maxChunkSize = config?.maxChunkSize ?? DEFAULT_MAX_CHUNK_SIZE;\n\t\t\tlet maxLength = config?.maxLength ?? DEFAULT_MAX_LENGTH;\n\t\t\tlet maxRecycledLength = config?.maxRecycledLength ?? DEFAULT_MAX_RECYCLED_LENGTH;\n\n\t\t\t// Each stack sizes its inlined firstBlock from its own maxLength, so compute both up front to lay them out. The\n\t\t\t// stacks cap their segments at what a buffer can hold, so size the inlined blocks against that same cap.\n\t\t\tlet maxSegmentLength = SharedStack.getMaxSegmentLength(memory.maxAllocationLength);\n\t\t\tlet pointerStackMaxLength = maxLength / maxChunkSize;\n\t\t\tlet pointerStackAllocateCount = SharedStack.getAllocateCount(pointerStackMaxLength, undefined, maxSegmentLength);\n\t\t\tlet recycleStackAllocateCount = SharedStack.getAllocateCount(maxRecycledLength, undefined, maxSegmentLength);\n\n\t\t\t// Inline both stacks' firstBlocks right after the pool header so the whole spine is one contiguous allocation\n\t\t\tthis.firstBlock = memory.allocUI32(SharedPool.ALLOCATE_COUNT + pointerStackAllocateCount + recycleStackAllocateCount);\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\tthis.pointerStack = new SharedStack(memory, {\n\t\t\t\tfirstBlock: this.firstBlock.getSubAllocation(SharedPool.ALLOCATE_COUNT, pointerStackAllocateCount),\n\t\t\t\ttype: Uint32Array,\n\t\t\t\tmaxLength: pointerStackMaxLength,\n\t\t\t});\n\t\t\tstoreRawPointer(this.firstBlock.data, POINTER_STACK_INDEX, this.pointerStack.pointer);\n\t\t\tthis.recycleStack = new SharedStack(memory, {\n\t\t\t\tfirstBlock: this.firstBlock.getSubAllocation(SharedPool.ALLOCATE_COUNT + pointerStackAllocateCount, recycleStackAllocateCount),\n\t\t\t\ttype: Uint32Array,\n\t\t\t\tmaxLength: maxRecycledLength,\n\t\t\t});\n\t\t\tstoreRawPointer(this.firstBlock.data, RECYCLE_STACK_INDEX, this.recycleStack.pointer);\n\n\t\t\tconst type = config?.type ?? Uint32Array;\n\t\t\tif(type === Uint32Array) {\n\t\t\t\tthis.type = TYPE.uint32;\n\t\t\t// @ts-expect-error\n\t\t\t} else if(type === Int32Array) {\n\t\t\t\tthis.type = TYPE.int32;\n\t\t\t// @ts-expect-error\n\t\t\t} else if(type === Float32Array) {\n\t\t\t\tthis.type = TYPE.float32;\n\t\t\t// @ts-expect-error\n\t\t\t} else if(type === Float64Array) {\n\t\t\t\tthis.type = TYPE.float64;\n\t\t\t}\n\t\t\tthis.dataLength = dataLength;\n\t\t\tthis.maxChunkSize = maxChunkSize;\n\n\t\t\t// @ts-expect-error\n\t\t\tlet byteMultipler = type === Float64Array ? 2 : 1;\n\t\t\tlet firstArray = memory.allocUI32(maxChunkSize * dataLength * byteMultipler);\n\t\t\tthis.pointerStack.push(firstArray.pointer);\n\t\t}\n\n\t\tthis.cachedType = this.uint16Array[0];\n\t\tthis.cachedDataLength = Math.max(1, this.uint16Array[1]);\n\t\tthis.cachedMaxChunkSize = this.firstBlock.data[MAX_CHUNK_SIZE_INDEX];\n\t\tthis.cachedByteMultipler = this.cachedType === TYPE.float64 ? 2 : 1;\n\t\tthis.maxLength = this.cachedMaxChunkSize * this.pointerStack.maxLength;\n\n\t\tthis.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + GROW_LOCK_INDEX * Uint32Array.BYTES_PER_ELEMENT, 1);\n\t}\n\n\tat(index: number): T {\n\t\tlet dataBlock = this.getFullDataBlock(index);\n\t\treturn this.getDataBlock(dataBlock, index % this.cachedMaxChunkSize);\n\t}\n\tget(index: number, dataIndex = 0): number {\n\t\tconst dataLength = this.cachedDataLength;\n\t\tif(dataIndex >= dataLength) {\n\t\t\tthrow new Error(`${dataIndex} is out of dataLength bounds ${dataLength}`);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock(index);\n\t\treturn dataBlock[(index % this.cachedMaxChunkSize) * dataLength + dataIndex];\n\t}\n\n\tpush(values: number | Array<number>): number {\n\t\tlet dataLength = this.cachedDataLength;\n\t\tconst isSingleNumber = typeof values === 'number';\n\t\tif(!isSingleNumber && values.length > dataLength) {\n\t\t\tthrow new Error(`Can't insert ${values.length} array into shared list of ${dataLength} dataLength`);\n\t\t}\n\n\t\t// Reuse a recycled index if one is available. pop() can come back undefined when another thread drained the last\n\t\t// slot between our two calls, so fall through to a fresh index rather than trusting a length() read\n\t\tlet newIndex = this.recycleStack.pop();\n\t\tif(newIndex === undefined) {\n\t\t\tnewIndex = Atomics.add(this.firstBlock.data, LENGTH_INDEX, 1);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock(newIndex);\n\t\tlet startIndex = dataLength * (newIndex % this.cachedMaxChunkSize);\n\t\tif(isSingleNumber) {\n\t\t\tdataBlock[startIndex] = values;\n\t\t} else {\n\t\t\tdataBlock.set(values, startIndex);\n\t\t}\n\n\t\treturn newIndex;\n\t}\n\n\tdeleteIndex(index: number) {\n\t\tthis.recycleStack.push(index);\n\t}\n\n\tclear() {\n\t\tthis.firstBlock.data[LENGTH_INDEX] = 0;\n\t\tthis.recycleStack.clear();\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tconst recycledValues: { [key: number]: true } = {};\n\t\tfor(let value of this.recycleStack) {\n\t\t\trecycledValues[value] = true;\n\t\t}\n\n\t\tconst maxChunkSize = this.cachedMaxChunkSize;\n\t\t// Snapshot the length once - iteration over a concurrently mutated pool is a snapshot anyway, and re-loading the\n\t\t// atomic every step is pure overhead\n\t\tconst length = Atomics.load(this.firstBlock.data, LENGTH_INDEX);\n\t\tlet dataBlock = this.getFullDataBlock(0);\n\t\tlet dataBlockIndex = 0;\n\t\tfor(let i = 0; i < length; i++) {\n\t\t\tif(!recycledValues[i]) {\n\t\t\t\tlet newDataBlockIndex = Math.floor(i / maxChunkSize);\n\t\t\t\tif(newDataBlockIndex !== dataBlockIndex) {\n\t\t\t\t\tdataBlock = this.getFullDataBlock(i);\n\t\t\t\t\tdataBlockIndex = newDataBlockIndex;\n\t\t\t\t}\n\t\t\t\tyield this.getDataBlock(dataBlock, i % maxChunkSize);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getFullDataBlock(index: number) {\n\t\tlet pointerIndex = Math.floor(index / this.cachedMaxChunkSize);\n\t\tlet cachedDataBlock = this.cachedFullDataBlock[pointerIndex];\n\t\tif(cachedDataBlock) {\n\t\t\treturn cachedDataBlock;\n\t\t}\n\t\tif(pointerIndex >= this.pointerStack.length) {\n\t\t\t// Only let a single thread append a new chunk at a time - otherwise two concurrent inserts landing in a new\n\t\t\t// chunk would each allocate + push a buffer, orphaning one and leaking memory\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 chunk while we were waiting\n\t\t\t\twhile(pointerIndex >= this.pointerStack.length) {\n\t\t\t\t\tlet newArray = this.memory.allocUI32(this.cachedMaxChunkSize * this.cachedDataLength * this.cachedByteMultipler);\n\t\t\t\t\tthis.pointerStack.push(newArray.pointer);\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\tlet array = new AllocatedMemory(this.memory, getPointer(this.pointerStack.at(pointerIndex)));\n\n\t\tlet blockLength = this.cachedDataLength * this.cachedMaxChunkSize;\n\t\tlet data: T;\n\t\tswitch(this.cachedType) {\n\t\t\tcase TYPE.int32:\n\t\t\t\tdata = new Int32Array(array.data.buffer, array.bufferByteOffset, blockLength) as T;\n\t\t\t\tbreak;\n\t\t\tcase TYPE.uint32:\n\t\t\t\tdata = new Uint32Array(array.data.buffer, array.bufferByteOffset, blockLength) as T;\n\t\t\t\tbreak;\n\t\t\tcase TYPE.float32:\n\t\t\t\tdata = new Float32Array(array.data.buffer, array.bufferByteOffset, blockLength) as T;\n\t\t\t\tbreak;\n\t\t\tcase TYPE.float64:\n\t\t\t\tdata = new Float64Array(array.data.buffer, array.bufferByteOffset, blockLength) as T;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unknown data block type ${this.cachedType}`);\n\t\t}\n\n\t\tthis.cachedFullDataBlock[pointerIndex] = data;\n\t\treturn data;\n\t}\n\n\tprivate getDataBlock(rawData: T, index: number): T {\n\t\tconst dataLength = this.cachedDataLength;\n\t\tconst start = index * dataLength;\n\t\treturn rawData.subarray(start, start + dataLength) as T;\n\t}\n\n\tfree() {\n\t\tthis.recycleStack.free();\n\n\t\tfor(let pointer of this.pointerStack) {\n\t\t\tlet memory = new AllocatedMemory(this.memory, getPointer(pointer));\n\t\t\tmemory.free();\n\t\t}\n\t\tthis.pointerStack.free();\n\t\tthis.firstBlock.free();\n\t}\n\n\tgetSharedMemory(): SharedPoolMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.firstBlock.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedPoolConfig<T extends Uint32Array | Int32Array | Float32Array | Float64Array> {\n\tmaxChunkSize?: number\n\tmaxLength?: number\n\tmaxRecycledLength?: number\n\ttype?: TypedArrayConstructor<T>\n\tdataLength?: number\n}\ninterface SharedPoolMemory {\n\tfirstBlock: SharedAllocatedMemory\n}\n\nexport type { SharedPoolConfig, SharedPoolMemory };\n"],"mappings":";;;;;AAeA,IAAM,IAAe,GACf,IAAa,GACb,IAAuB,GAEvB,IAAkB,GAElB,IAAsB,GACtB,IAAsB,GAEtB,IAAyB,KACzB,IAAqB,KACrB,IAA8B,KAMf,IAArB,MAAqB,EAAkH;CACtI,OAAgB,iBAAiB;CACjC;CAGA;CACA;CACA;CACA;CACA;CACA;CAEA,sBAAoD,CAAC;CAGrD;CACA;CACA;CACA;CAEA,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAAK,KAAK,WAAW,MAAM,CAAY,IAAI,KAAK,aAAa;CAC7E;CACA,IAAI,eAAuB;EAC1B,OAAO,KAAK;CACb;CACA,IAAY,aAAa,GAAe;EACvC,QAAQ,MAAM,KAAK,WAAW,MAAM,GAAsB,CAAK;CAChE;CAEA,IAAI,OAAe;EAClB,OAAO,KAAK;CACb;CACA,IAAY,KAAK,GAAe;EAC/B,QAAQ,MAAM,KAAK,aAAa,GAAG,CAAK;CACzC;CACA,IAAI,aAAqB;EACxB,OAAO,KAAK;CACb;CACA,IAAY,WAAW,GAAe;EACrC,QAAQ,MAAM,KAAK,aAAa,GAAG,CAAK;CACzC;CAEA,IAAI,eAAuB;EAC1B,OAAO,KAAK,qBAAqB,KAAK,aAAa;CACpD;CACA,IAAI,gBAAwB;EAC3B,OAAO,KAAK;CACb;CAIA,IAAI,aAAqB;EACxB,IAAI,IAAQ,KAAK,WAAW;EAC5B,KAAI,IAAI,KAAW,KAAK,cACvB,KAAS,IAAI,EAAgB,KAAK,QAAQ,EAAW,CAAO,CAAC,CAAC,CAAC;EAEhE,OAAO,IAAQ,KAAK,aAAa,aAAa,KAAK,aAAa;CACjE;CAEA,YAAY,GAAoB,GAA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AAQ,EAAW,KAAK,aAAa,GAAG,CAAY,CAAC,CAAC,GAEvF,IAAc,KAAK,mBAAmB,KAAK,oBAC3C;EACJ,QAAO,KAAK,YAAZ;GACC,KAAA;IACC,IAAO,IAAI,WAAW,EAAM,KAAK,QAAQ,EAAM,kBAAkB,CAAW;IAC5E;GACD,KAAA;IACC,IAAO,IAAI,YAAY,EAAM,KAAK,QAAQ,EAAM,kBAAkB,CAAW;IAC7E;GACD,KAAA;IACC,IAAO,IAAI,aAAa,EAAM,KAAK,QAAQ,EAAM,kBAAkB,CAAW;IAC9E;GACD,KAAA;IACC,IAAO,IAAI,aAAa,EAAM,KAAK,QAAQ,EAAM,kBAAkB,CAAW;IAC9E;GACD,SACC,MAAU,MAAM,2BAA2B,KAAK,YAAY;EAC9D;EAGA,OADA,KAAK,oBAAoB,KAAgB,GAClC;CACR;CAEA,aAAqB,GAAY,GAAkB;EAClD,IAAM,IAAa,KAAK,kBAClB,IAAQ,IAAQ;EACtB,OAAO,EAAQ,SAAS,GAAO,IAAQ,CAAU;CAClD;CAEA,OAAO;EACN,KAAK,aAAa,KAAK;EAEvB,KAAI,IAAI,KAAW,KAAK,cAEvB,IADiB,EAAgB,KAAK,QAAQ,EAAW,CAAO,CAChE,CAAA,CAAO,KAAK;EAGb,AADA,KAAK,aAAa,KAAK,GACvB,KAAK,WAAW,KAAK;CACtB;CAEA,kBAAoC;EACnC,OAAO,EACN,YAAY,KAAK,WAAW,gBAAgB,EAC7C;CACD;AACD"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { SharedAllocatedMemory } from './allocated-memory';
|
|
2
|
+
import AllocatedMemory from './allocated-memory';
|
|
3
|
+
import type { TypedArrayConstructor } from './interfaces/typed-array-constructor';
|
|
4
|
+
import type MemoryHeap from './memory-heap';
|
|
5
|
+
export default class SharedStack<T extends Uint32Array | Int32Array | Float32Array = Uint32Array> implements Iterable<number> {
|
|
6
|
+
static readonly BASE_ALLOCATE_COUNT = 8;
|
|
7
|
+
static readonly DEFAULT_ALLOCATE_COUNT: number;
|
|
8
|
+
static getMaxSegmentLength(maxAllocationLength: number, baseSegmentLength?: number): number;
|
|
9
|
+
static getMaxSegments(maxLength: number, baseSegmentLength?: number, maxSegmentLength?: number): number;
|
|
10
|
+
static getCapacity(segments: number, baseSegmentLength?: number, maxSegmentLength?: number): number;
|
|
11
|
+
static getAllocateCount(maxLength: number, baseSegmentLength?: number, maxSegmentLength?: number): number;
|
|
12
|
+
private memory;
|
|
13
|
+
private firstBlock;
|
|
14
|
+
private uint16Array;
|
|
15
|
+
private growLock;
|
|
16
|
+
readonly maxLength: number;
|
|
17
|
+
private ownsFirstBlock;
|
|
18
|
+
private cachedSegments;
|
|
19
|
+
private cachedSequences;
|
|
20
|
+
private cachedBaseSegmentLength;
|
|
21
|
+
private cachedMaxSegmentLength;
|
|
22
|
+
private cachedCapSegmentCount;
|
|
23
|
+
private cachedGeometricCapacity;
|
|
24
|
+
get length(): number;
|
|
25
|
+
get segmentCount(): number;
|
|
26
|
+
private set segmentCount(value);
|
|
27
|
+
get type(): number;
|
|
28
|
+
private set type(value);
|
|
29
|
+
get baseSegmentLength(): number;
|
|
30
|
+
private set baseSegmentLength(value);
|
|
31
|
+
get maxSegments(): number;
|
|
32
|
+
private set maxSegments(value);
|
|
33
|
+
get maxSegmentLength(): number;
|
|
34
|
+
private set maxSegmentLength(value);
|
|
35
|
+
get pointer(): number;
|
|
36
|
+
get usedMemory(): number;
|
|
37
|
+
constructor(memory: MemoryHeap, config?: SharedStackConfig<T> | SharedStackMemory | (SharedStackMemory & SharedStackConfig<T>));
|
|
38
|
+
private resolveMaxSegmentLength;
|
|
39
|
+
private resolveMaxSegments;
|
|
40
|
+
private initialize;
|
|
41
|
+
at(index: number): number;
|
|
42
|
+
push(value: number): number;
|
|
43
|
+
pop(): number | undefined;
|
|
44
|
+
clear(): void;
|
|
45
|
+
[Symbol.iterator](): Generator<number, void, unknown>;
|
|
46
|
+
private getSegment;
|
|
47
|
+
free(): void;
|
|
48
|
+
getSharedMemory(): SharedStackMemory;
|
|
49
|
+
}
|
|
50
|
+
interface SharedStackConfig<T extends Uint32Array | Int32Array | Float32Array> {
|
|
51
|
+
type?: TypedArrayConstructor<T>;
|
|
52
|
+
baseSegmentLength?: number;
|
|
53
|
+
maxLength?: number;
|
|
54
|
+
maxSegmentLength?: number;
|
|
55
|
+
}
|
|
56
|
+
interface SharedStackMemory {
|
|
57
|
+
firstBlock: SharedAllocatedMemory | AllocatedMemory;
|
|
58
|
+
}
|
|
59
|
+
export type { SharedStackConfig, SharedStackMemory };
|