@daneren2005/shared-memory-objects 1.0.0 → 1.2.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.
Files changed (83) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +78 -55
  3. package/dist/{src/allocated-memory.d.ts → allocated-memory.d.ts} +5 -4
  4. package/dist/allocated-memory.js +45 -0
  5. package/dist/allocated-memory.js.map +1 -0
  6. package/dist/{src/cached-item-list.d.ts → cached-item-list.d.ts} +3 -3
  7. package/dist/cached-item-list.js +71 -0
  8. package/dist/cached-item-list.js.map +1 -0
  9. package/dist/constant-string.d.ts +15 -0
  10. package/dist/constant-string.js +67 -0
  11. package/dist/constant-string.js.map +1 -0
  12. package/dist/interfaces/pow2.js +0 -0
  13. package/dist/interfaces/typed-array-constructor.js +0 -0
  14. package/dist/interfaces/typed-array.js +0 -0
  15. package/dist/{src/local-pool.d.ts → local-pool.d.ts} +2 -2
  16. package/dist/local-pool.js +90 -0
  17. package/dist/local-pool.js.map +1 -0
  18. package/dist/lock/read-write-lock.js +19 -0
  19. package/dist/lock/read-write-lock.js.map +1 -0
  20. package/dist/lock/simple-lock.js +11 -0
  21. package/dist/lock/simple-lock.js.map +1 -0
  22. package/dist/{src/memory-buffer.d.ts → memory-buffer.d.ts} +4 -4
  23. package/dist/memory-buffer.js +267 -0
  24. package/dist/memory-buffer.js.map +1 -0
  25. package/dist/{src/memory-heap.d.ts → memory-heap.d.ts} +2 -2
  26. package/dist/memory-heap.js +96 -0
  27. package/dist/memory-heap.js.map +1 -0
  28. package/dist/{src/serialize-object.d.ts → serialize-object.d.ts} +2 -2
  29. package/dist/serialize-object.js +47 -0
  30. package/dist/serialize-object.js.map +1 -0
  31. package/dist/{src/shared-list.d.ts → shared-list.d.ts} +3 -3
  32. package/dist/shared-list.js +125 -0
  33. package/dist/shared-list.js.map +1 -0
  34. package/dist/{src/shared-map.d.ts → shared-map.d.ts} +2 -2
  35. package/dist/shared-map.js +104 -0
  36. package/dist/shared-map.js.map +1 -0
  37. package/dist/{src/shared-pointer-list.d.ts → shared-pointer-list.d.ts} +3 -3
  38. package/dist/shared-pointer-list.js +47 -0
  39. package/dist/shared-pointer-list.js.map +1 -0
  40. package/dist/{src/shared-pool.d.ts → shared-pool.d.ts} +11 -8
  41. package/dist/shared-pool.js +145 -0
  42. package/dist/shared-pool.js.map +1 -0
  43. package/dist/shared-stack.d.ts +46 -0
  44. package/dist/shared-stack.js +144 -0
  45. package/dist/shared-stack.js.map +1 -0
  46. package/dist/{src/shared-string.d.ts → shared-string.d.ts} +2 -2
  47. package/dist/shared-string.js +73 -0
  48. package/dist/shared-string.js.map +1 -0
  49. package/dist/{src/shared-vector.d.ts → shared-vector.d.ts} +4 -4
  50. package/dist/shared-vector.js +147 -0
  51. package/dist/shared-vector.js.map +1 -0
  52. package/dist/utils/16-from-32-array.js +18 -0
  53. package/dist/utils/16-from-32-array.js.map +1 -0
  54. package/dist/utils/16-from-64-array.js +17 -0
  55. package/dist/utils/16-from-64-array.js.map +1 -0
  56. package/dist/utils/atomic-math.js +34 -0
  57. package/dist/utils/atomic-math.js.map +1 -0
  58. package/dist/utils/float32-atomics.js +24 -0
  59. package/dist/utils/float32-atomics.js.map +1 -0
  60. package/dist/utils/float64-atomics.js +24 -0
  61. package/dist/utils/float64-atomics.js.map +1 -0
  62. package/dist/utils/pointer.js +34 -0
  63. package/dist/utils/pointer.js.map +1 -0
  64. package/dist/{src/utils → utils}/typedarray.d.ts +47 -47
  65. package/dist/utils/typedarray.js +39 -0
  66. package/dist/utils/typedarray.js.map +1 -0
  67. package/package.json +26 -31
  68. package/dist/shared-memory-objects.js +0 -1578
  69. package/dist/shared-memory-objects.js.map +0 -1
  70. package/dist/shared-memory-objects.umd.cjs +0 -2
  71. package/dist/shared-memory-objects.umd.cjs.map +0 -1
  72. package/dist/src/main.d.ts +0 -22
  73. /package/dist/{src/interfaces → interfaces}/pow2.d.ts +0 -0
  74. /package/dist/{src/interfaces → interfaces}/typed-array-constructor.d.ts +0 -0
  75. /package/dist/{src/interfaces → interfaces}/typed-array.d.ts +0 -0
  76. /package/dist/{src/lock → lock}/read-write-lock.d.ts +0 -0
  77. /package/dist/{src/lock → lock}/simple-lock.d.ts +0 -0
  78. /package/dist/{src/utils → utils}/16-from-32-array.d.ts +0 -0
  79. /package/dist/{src/utils → utils}/16-from-64-array.d.ts +0 -0
  80. /package/dist/{src/utils → utils}/atomic-math.d.ts +0 -0
  81. /package/dist/{src/utils → utils}/float32-atomics.d.ts +0 -0
  82. /package/dist/{src/utils → utils}/float64-atomics.d.ts +0 -0
  83. /package/dist/{src/utils → utils}/pointer.d.ts +0 -0
@@ -0,0 +1,125 @@
1
+ import { getPointer as e, loadPointer as t, loadRawPointer as n, replaceRawPointer as r, storePointer as i, storeRawPointer as a } from "./utils/pointer.js";
2
+ import o from "./allocated-memory.js";
3
+ //#region src/shared-list.ts
4
+ var s = 4, c = 1, l = 2, u = class {
5
+ static ALLOCATE_COUNT = s;
6
+ memory;
7
+ firstBlock;
8
+ uint16Array;
9
+ onDelete;
10
+ get length() {
11
+ return Atomics.load(this.firstBlock.data, l);
12
+ }
13
+ get type() {
14
+ return Atomics.load(this.uint16Array, 0);
15
+ }
16
+ set type(e) {
17
+ Atomics.store(this.uint16Array, 0, e);
18
+ }
19
+ get dataLength() {
20
+ return Math.max(1, Atomics.load(this.uint16Array, 1));
21
+ }
22
+ set dataLength(e) {
23
+ Atomics.store(this.uint16Array, 1, e);
24
+ }
25
+ constructor(e, t) {
26
+ if (this.memory = e, t && "firstBlock" in t) this.firstBlock = new o(e, t.firstBlock), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + 3 * Uint32Array.BYTES_PER_ELEMENT, 2);
27
+ else {
28
+ this.firstBlock = t && t.initWithBlock ? new o(e, t.initWithBlock) : e.allocUI32(s), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + 3 * Uint32Array.BYTES_PER_ELEMENT, 2);
29
+ let n = t?.type ?? Uint32Array;
30
+ n === Uint32Array ? this.type = 0 : n === Int32Array ? this.type = 1 : n === Float32Array && (this.type = 2), this.dataLength = t?.dataLength ?? 1;
31
+ }
32
+ }
33
+ insert(t) {
34
+ typeof t == "number" && (t = [t]);
35
+ let i = this.dataLength;
36
+ if (t.length > i) throw Error(`Can't insert ${t.length} array into shared list of ${i} dataLength`);
37
+ let o = this.memory.allocUI32(c + i), s = this.getDataBlock(o.data), u = o.pointer;
38
+ for (let e = 0; e < t.length; e++) s instanceof Int32Array || s instanceof Uint32Array ? Atomics.store(s, e, t[e]) : s[e] = t[e];
39
+ let d, f = !1;
40
+ for (; !f;) d = n(this.firstBlock.data, 1), f = r(this.firstBlock.data, 1, u, d);
41
+ if (d) {
42
+ let { bufferPosition: t, bufferByteOffset: n } = e(d), r = new Uint32Array(this.memory.buffers[t].buf, n, 1);
43
+ a(r, 0, u);
44
+ } else a(this.firstBlock.data, 0, u);
45
+ Atomics.add(this.firstBlock.data, l, 1);
46
+ }
47
+ deleteMatch(e) {
48
+ for (let { data: t, index: n, deleteCurrent: r } of this) if (e(t, n)) return r(), !0;
49
+ return !1;
50
+ }
51
+ deleteIndex(e) {
52
+ return e >= this.length || e < 0 ? !1 : this.deleteMatch((t, n) => n === e);
53
+ }
54
+ deleteValue(e) {
55
+ return typeof e == "number" ? this.deleteMatch((t) => t[0] === e) : this.deleteMatch((t) => {
56
+ if (t.length !== e.length) return !1;
57
+ for (let n = 0; n < t.length; n++) if (t[n] !== e[n]) return !1;
58
+ return !0;
59
+ });
60
+ }
61
+ clear() {
62
+ let t, i, a = !1;
63
+ for (; !a;) {
64
+ if (t = n(this.firstBlock.data, 0), i = n(this.firstBlock.data, 1), !i) return;
65
+ a = r(this.firstBlock.data, 1, 0, i);
66
+ }
67
+ if (!t) return;
68
+ r(this.firstBlock.data, 0, 0, t);
69
+ let o = 0, s = t;
70
+ for (; s;) {
71
+ let { bufferPosition: t, bufferByteOffset: r } = e(s), i = this.memory.buffers[t];
72
+ if (!i) break;
73
+ let a = new Uint32Array(i.buf, r, 2);
74
+ s = n(a, 0), o++, this.onDelete && this.onDelete(this.getDataBlock(a)), i.free(a.byteOffset);
75
+ }
76
+ Atomics.sub(this.firstBlock.data, l, o);
77
+ }
78
+ *[Symbol.iterator]() {
79
+ let e = 0, { bufferPosition: n, bufferByteOffset: r } = t(this.firstBlock.data, 0), a = this.firstBlock.data, o = 0, s = 0;
80
+ for (; r;) {
81
+ let c = this.memory.buffers[n];
82
+ if (!c) return;
83
+ let u = new Uint32Array(c.buf, r, 2), d = this.getDataBlock(u), f = n, p = r;
84
+ ({bufferPosition: n, bufferByteOffset: r} = t(u, 0));
85
+ let m = !0;
86
+ yield {
87
+ data: d,
88
+ index: e,
89
+ deleteCurrent: () => {
90
+ i(a, 0, n, r), r || i(this.firstBlock.data, 1, o, s), this.onDelete && this.onDelete(this.getDataBlock(u)), c.free(u.byteOffset), Atomics.sub(this.firstBlock.data, l, 1), m = !1;
91
+ }
92
+ }, m && (a = u, o = f, s = p, e++);
93
+ }
94
+ }
95
+ forEach(e) {
96
+ for (let t of this) e(t.data);
97
+ }
98
+ getSharedMemory() {
99
+ return { firstBlock: this.firstBlock.getSharedMemory() };
100
+ }
101
+ getDataBlock(e) {
102
+ let t = e.byteOffset + c * e.BYTES_PER_ELEMENT;
103
+ switch (this.type) {
104
+ case 1: return new Int32Array(e.buffer, t, this.dataLength);
105
+ case 0: return new Uint32Array(e.buffer, t, this.dataLength);
106
+ case 2: return new Float32Array(e.buffer, t, this.dataLength);
107
+ default: throw Error(`Unknown data block type ${this.type}`);
108
+ }
109
+ }
110
+ free() {
111
+ let { bufferPosition: e, bufferByteOffset: n } = t(this.firstBlock.data, 0);
112
+ for (; n;) {
113
+ let r = new o(this.memory, {
114
+ bufferPosition: e,
115
+ bufferByteOffset: n
116
+ });
117
+ ({bufferPosition: e, bufferByteOffset: n} = t(r.data, 0)), this.onDelete && this.onDelete(this.getDataBlock(r.data)), r.free();
118
+ }
119
+ this.firstBlock.free();
120
+ }
121
+ };
122
+ //#endregion
123
+ export { u as default };
124
+
125
+ //# sourceMappingURL=shared-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-list.js","names":[],"sources":["../src/shared-list.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 { getPointer, loadPointer, loadRawPointer, replaceRawPointer, storePointer, storeRawPointer } from './utils/pointer';\n\nenum TYPE {\n\tuint32,\n\tint32,\n\tfloat32,\n}\n\n// TODO: We need some sort of locking on insert/deletes!\nconst FIRST_BLOCK_RECORD_KEEPING_COUNT = 4;\nconst DATA_BLOCK_RECORD_KEEPING_COUNT = 1;\nconst LENGTH_INDEX = 2;\nexport default class SharedList<T extends Uint32Array | Int32Array | Float32Array = Uint32Array> implements Iterable<SharedListIterable<T>> {\n\tstatic readonly ALLOCATE_COUNT = FIRST_BLOCK_RECORD_KEEPING_COUNT;\n\n\tprivate memory: MemoryHeap;\n\t/* First block\n\t\t32 index 0\n\t\tuint16 0 - next buffer position\n\t\tuint16 1 - next buffer index\n\t\t32 index 1\n\t\tuint16 2 - last buffer position\n\t\tuint16 3 - last buffer index\n\t\t32 index 2\n\t\tuint32 4 - length\n\t\t32 index 3\n\t\tuint16 6 - type\n\t\tuint16 7 - data length (defaults to 1 number per data)\n\t*/\n\t/* Other blocks\n\t\t32 index 0\n\t\tuint16 0 - next buffer position\n\t\tuint16 1 - next buffer index\n\t\t32 index 1 => data\n\t*/\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\tonDelete?: (data: T) => void;\n\n\tget length(): number {\n\t\treturn Atomics.load(this.firstBlock.data, LENGTH_INDEX);\n\t}\n\t\n\tget type(): number {\n\t\treturn Atomics.load(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\t// Can technically be initialized by passing memory without actually every being called - need to make sure dataLength is always at least one\n\t\treturn Math.max(1, Atomics.load(this.uint16Array, 1));\n\t}\n\tprivate set dataLength(value: number) {\n\t\tAtomics.store(this.uint16Array, 1, value);\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedListConfig<T> | SharedListMemory) {\n\t\tthis.memory = memory;\n\n\t\tif(config && 'firstBlock' in config) {\n\t\t\t// TODO: How to handle referencing memory we don't have access to yet because buffer not synced from worker?\n\t\t\tthis.firstBlock = new AllocatedMemory(memory, config.firstBlock);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + (LENGTH_INDEX + 1) * Uint32Array.BYTES_PER_ELEMENT, 2);\n\t\t} else {\n\t\t\tif(config && config.initWithBlock) {\n\t\t\t\tthis.firstBlock = new AllocatedMemory(memory, config.initWithBlock);\n\t\t\t} else {\n\t\t\t\tthis.firstBlock = memory.allocUI32(FIRST_BLOCK_RECORD_KEEPING_COUNT);\n\t\t\t}\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + (LENGTH_INDEX + 1) * Uint32Array.BYTES_PER_ELEMENT, 2);\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}\n\t\t\tthis.dataLength = config?.dataLength ?? 1;\n\t\t}\n\t}\n\n\tinsert(values: number | Array<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\t\tlet newBlock = this.memory.allocUI32(DATA_BLOCK_RECORD_KEEPING_COUNT + dataLength);\n\t\tlet newData = this.getDataBlock(newBlock.data);\n\t\tlet newBlockPointer = newBlock.pointer;\n\t\t\n\t\tfor(let i = 0; i < values.length; i++) {\n\t\t\tif(newData instanceof Int32Array || newData instanceof Uint32Array) {\n\t\t\t\tAtomics.store(newData, i, values[i]);\n\t\t\t} else {\n\t\t\t\t// TODO: Should we replace with pass thru float32 conversion -> store?\n\t\t\t\tnewData[i] = values[i];\n\t\t\t}\n\t\t}\n\n\t\tlet lastBlockPointer;\n\t\tlet updateWorked = false;\n\t\twhile(!updateWorked) {\n\t\t\tlastBlockPointer = loadRawPointer(this.firstBlock.data, 1);\n\t\t\tupdateWorked = replaceRawPointer(this.firstBlock.data, 1, newBlockPointer, lastBlockPointer);\n\t\t}\n\n\t\tif(lastBlockPointer) {\n\t\t\tlet { bufferPosition: lastBlockPosition, bufferByteOffset: lastBlockByteOffset } = getPointer(lastBlockPointer);\n\t\t\t// TODO: How to handle referencing memory we don't have access to yet because buffer not synced from worker?\n\t\t\tlet lastBlock = new Uint32Array(this.memory.buffers[lastBlockPosition].buf, lastBlockByteOffset, 1);\n\t\t\tstoreRawPointer(lastBlock, 0, newBlockPointer);\n\t\t} else {\n\t\t\t// First item - store on first block\n\t\t\tstoreRawPointer(this.firstBlock.data, 0, newBlockPointer);\n\t\t}\n\t\t\n\t\t// Always update new last buffer position and length\n\t\tAtomics.add(this.firstBlock.data, LENGTH_INDEX, 1);\n\t}\n\n\tdeleteMatch(callback: (values: T, index: number) => boolean): boolean {\n\t\tfor(let { data, index, deleteCurrent } of this) {\n\t\t\tif(callback(data, index)) {\n\t\t\t\tdeleteCurrent();\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\tdeleteIndex(deleteIndex: number): boolean {\n\t\tif(deleteIndex >= this.length || deleteIndex < 0) {\n\t\t\treturn false;\n\t\t}\n\n\t\treturn this.deleteMatch((values, index) => index === deleteIndex);\n\t}\n\tdeleteValue(deleteValues: number | Array<number>) {\n\t\tif(typeof deleteValues === 'number') {\n\t\t\treturn this.deleteMatch(values => values[0] === deleteValues);\n\t\t} else {\n\t\t\treturn this.deleteMatch(values => {\n\t\t\t\tif(values.length !== deleteValues.length) {\n\t\t\t\t\treturn false;\n\t\t\t\t} else {\n\t\t\t\t\tfor(let i = 0; i < values.length; i++) {\n\t\t\t\t\t\tif(values[i] !== deleteValues[i]) {\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t}\n\n\tclear() {\n\t\tlet firstBlockPointer, lastBlockPointer;\n\t\tlet updateWorked = false;\n\t\twhile(!updateWorked) {\n\t\t\tfirstBlockPointer = loadRawPointer(this.firstBlock.data, 0);\n\t\t\tlastBlockPointer = loadRawPointer(this.firstBlock.data, 1);\n\t\t\t// Already cleared\n\t\t\tif(!lastBlockPointer) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tupdateWorked = replaceRawPointer(this.firstBlock.data, 1, 0, lastBlockPointer);\n\t\t}\n\t\t\n\t\t// Shouldn't be possible to hit: making Typescript happy\n\t\tif(!firstBlockPointer) {\n\t\t\treturn;\n\t\t}\n\n\t\t// We only want to update the last block if this is ran before something new was inserted\n\t\treplaceRawPointer(this.firstBlock.data, 0, 0, firstBlockPointer);\n\n\t\t// Iterate through inaccessible nodes and delete them\n\t\tlet deletedItems = 0;\n\t\tlet nextBlockPointer = firstBlockPointer;\n\t\twhile(nextBlockPointer) {\n\t\t\tlet { bufferPosition: nextBlockPosition, bufferByteOffset: nextBlockByteOffset } = getPointer(nextBlockPointer);\n\t\t\tlet memPool = this.memory.buffers[nextBlockPosition];\n\t\t\t// Short circuit iterations if we can't access memory\n\t\t\tif(!memPool) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tlet blockRecord = new Uint32Array(memPool.buf, nextBlockByteOffset, 2);\n\t\t\tnextBlockPointer = loadRawPointer(blockRecord, 0);\n\t\t\tdeletedItems++;\n\n\t\t\tif(this.onDelete) {\n\t\t\t\tthis.onDelete(this.getDataBlock(blockRecord));\n\t\t\t}\n\n\t\t\tmemPool.free(blockRecord.byteOffset);\n\t\t}\n\t\t\n\t\t// Subtract by however many we deleted so that a insert during this operation is accurate\n\t\tAtomics.sub(this.firstBlock.data, LENGTH_INDEX, deletedItems);\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet currentIndex = 0;\n\t\tlet { bufferPosition: nextBlockPosition, bufferByteOffset: nextBlockByteOffset } = loadPointer(this.firstBlock.data, 0);\n\t\tlet lastBlockData = this.firstBlock.data;\n\t\tlet lastBlockPosition = 0;\n\t\tlet lastBlockByteOffset = 0;\n\t\twhile(nextBlockByteOffset) {\n\t\t\tlet memPool = this.memory.buffers[nextBlockPosition];\n\t\t\t// Short circuit iterations if we can't access memory\n\t\t\tif(!memPool) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet blockRecord = new Uint32Array(memPool.buf, nextBlockByteOffset, 2);\n\t\t\tlet blockData = this.getDataBlock(blockRecord);\n\n\t\t\tlet currentBlockPosition = nextBlockPosition;\n\t\t\tlet currentBlockByteOffset = nextBlockByteOffset;\n\t\t\t({ bufferPosition: nextBlockPosition, bufferByteOffset: nextBlockByteOffset } = loadPointer(blockRecord, 0));\n\n\t\t\tlet updateLastBlock = true;\n\t\t\tyield {\n\t\t\t\tdata: blockData,\n\t\t\t\tindex: currentIndex,\n\t\t\t\tdeleteCurrent: () => {\n\t\t\t\t\t// Move previous index to point to one after\n\t\t\t\t\tstorePointer(lastBlockData, 0, nextBlockPosition, nextBlockByteOffset);\n\n\t\t\t\t\t// If this is the last item, update last block to be previous location\n\t\t\t\t\tif(!nextBlockByteOffset) {\n\t\t\t\t\t\tstorePointer(this.firstBlock.data, 1, lastBlockPosition, lastBlockByteOffset);\n\t\t\t\t\t}\n\n\t\t\t\t\tif(this.onDelete) {\n\t\t\t\t\t\tthis.onDelete(this.getDataBlock(blockRecord));\n\t\t\t\t\t}\n\n\t\t\t\t\tmemPool.free(blockRecord.byteOffset);\n\t\t\t\t\tAtomics.sub(this.firstBlock.data, LENGTH_INDEX, 1);\n\t\t\t\t\tupdateLastBlock = false;\n\t\t\t\t},\n\t\t\t};\n\n\t\t\tif(updateLastBlock) {\n\t\t\t\tlastBlockData = blockRecord;\n\t\t\t\tlastBlockPosition = currentBlockPosition;\n\t\t\t\tlastBlockByteOffset = currentBlockByteOffset;\n\t\t\t\tcurrentIndex++;\n\t\t\t}\n\t\t}\n\t}\n\n\tforEach(callback: (data: T) => void) {\n\t\tfor(let value of this) {\n\t\t\tcallback(value.data);\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedListMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.firstBlock.getSharedMemory(),\n\t\t};\n\t}\n\n\tprivate getDataBlock(memory: Uint32Array): T {\n\t\tconst startIndex = memory.byteOffset + DATA_BLOCK_RECORD_KEEPING_COUNT * memory.BYTES_PER_ELEMENT;\n\n\t\tswitch(this.type) {\n\t\t\tcase TYPE.int32:\n\t\t\t\treturn new Int32Array(memory.buffer, startIndex, this.dataLength) as T;\n\t\t\tcase TYPE.uint32:\n\t\t\t\treturn new Uint32Array(memory.buffer, startIndex, this.dataLength) as T;\n\t\t\tcase TYPE.float32:\n\t\t\t\treturn new Float32Array(memory.buffer, startIndex, this.dataLength) as T;\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unknown data block type ${this.type}`);\n\t\t}\n\t}\n\n\tfree() {\n\t\tlet { bufferPosition: nextBlockPosition, bufferByteOffset: nextBlockByteOffset } = loadPointer(this.firstBlock.data, 0);\n\t\twhile(nextBlockByteOffset) {\n\t\t\tlet allocatedMemory = new AllocatedMemory(this.memory, {\n\t\t\t\tbufferPosition: nextBlockPosition,\n\t\t\t\tbufferByteOffset: nextBlockByteOffset,\n\t\t\t});\n\n\t\t\t({ bufferPosition: nextBlockPosition, bufferByteOffset: nextBlockByteOffset } = loadPointer(allocatedMemory.data, 0));\n\n\t\t\tif(this.onDelete) {\n\t\t\t\tthis.onDelete(this.getDataBlock(allocatedMemory.data));\n\t\t\t}\n\n\t\t\tallocatedMemory.free();\n\t\t}\n\n\t\tthis.firstBlock.free();\n\t}\n}\n\ninterface SharedListConfig<T extends Uint32Array | Int32Array | Float32Array> {\n\tinitWithBlock?: SharedAllocatedMemory\n\ttype?: TypedArrayConstructor<T>\n\tdataLength?: number\n}\ninterface SharedListMemory {\n\tfirstBlock: SharedAllocatedMemory\n}\n\ninterface SharedListIterable<T extends Uint32Array | Int32Array | Float32Array> {\n\tdata: T\n\tindex: number\n\tdeleteCurrent: () => void\n}\n\nexport { type SharedListMemory };"],"mappings":";;;AAaA,IAAM,IAAmC,GACnC,IAAkC,GAClC,IAAe,GACA,IAArB,MAA4I;CAC3I,OAAgB,iBAAiB;CAEjC;CAoBA;CACA;CACA;CAEA,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAAK,KAAK,WAAW,MAAM,CAAY;CACvD;CAEA,IAAI,OAAe;EAClB,OAAO,QAAQ,KAAK,KAAK,aAAa,CAAC;CACxC;CACA,IAAY,KAAK,GAAe;EAC/B,QAAQ,MAAM,KAAK,aAAa,GAAG,CAAK;CACzC;CACA,IAAI,aAAqB;EAExB,OAAO,KAAK,IAAI,GAAG,QAAQ,KAAK,KAAK,aAAa,CAAC,CAAC;CACrD;CACA,IAAY,WAAW,GAAe;EACrC,QAAQ,MAAM,KAAK,aAAa,GAAG,CAAK;CACzC;CAEA,YAAY,GAAoB,GAAiD;EAGhF,IAFA,KAAK,SAAS,GAEX,KAAU,gBAAgB,GAG5B,AADA,KAAK,aAAa,IAAI,EAAgB,GAAQ,EAAO,UAAU,GAC/D,KAAK,cAAc,IAAI,YAAY,KAAK,WAAW,KAAK,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,KAAK,UAAU;GACjE,KAAA,GACC,OAAO,IAAI,YAAY,EAAO,QAAQ,GAAY,KAAK,UAAU;GAClE,KAAA,GACC,OAAO,IAAI,aAAa,EAAO,QAAQ,GAAY,KAAK,UAAU;GACnE,SACC,MAAU,MAAM,2BAA2B,KAAK,MAAM;EACxD;CACD;CAEA,OAAO;EACN,IAAI,EAAE,gBAAgB,GAAmB,kBAAkB,MAAwB,EAAY,KAAK,WAAW,MAAM,CAAC;EACtH,OAAM,IAAqB;GAC1B,IAAI,IAAkB,IAAI,EAAgB,KAAK,QAAQ;IACtD,gBAAgB;IAChB,kBAAkB;GACnB,CAAC;GAQD,CANC,CAAE,gBAAgB,GAAmB,kBAAkB,KAAwB,EAAY,EAAgB,MAAM,CAAC,IAEhH,KAAK,YACP,KAAK,SAAS,KAAK,aAAa,EAAgB,IAAI,CAAC,GAGtD,EAAgB,KAAK;EACtB;EAEA,KAAK,WAAW,KAAK;CACtB;AACD"}
@@ -1,5 +1,5 @@
1
- import { SharedAllocatedMemory } from './allocated-memory';
2
- import { default as MemoryHeap } from './memory-heap';
1
+ import { type SharedAllocatedMemory } from './allocated-memory';
2
+ import type MemoryHeap from './memory-heap';
3
3
  export default class SharedMap<K extends string | number> {
4
4
  static readonly ALLOCATE_COUNT = 4;
5
5
  private memory;
@@ -0,0 +1,104 @@
1
+ import { loadPointer as e, storePointer as t } from "./utils/pointer.js";
2
+ import n from "./allocated-memory.js";
3
+ import r from "./shared-list.js";
4
+ //#region src/shared-map.ts
5
+ var i = 10, a = class a {
6
+ static ALLOCATE_COUNT = 4;
7
+ memory;
8
+ pointerMemory;
9
+ lock;
10
+ get hashMemory() {
11
+ return new n(this.memory, e(this.pointerMemory.data, 0));
12
+ }
13
+ get length() {
14
+ return Atomics.load(this.pointerMemory.data, 2);
15
+ }
16
+ get maxHash() {
17
+ return Atomics.load(this.pointerMemory.data, 3);
18
+ }
19
+ constructor(e, r) {
20
+ if (this.memory = e, r) this.pointerMemory = new n(e, r.firstBlock);
21
+ else {
22
+ this.pointerMemory = e.allocUI32(a.ALLOCATE_COUNT);
23
+ let n = e.allocUI32(i);
24
+ t(this.pointerMemory.data, 0, n.bufferPosition, n.bufferByteOffset), Atomics.store(this.pointerMemory.data, 3, i);
25
+ }
26
+ this.lock = new Int32Array(this.pointerMemory.data.buffer, this.pointerMemory.bufferByteOffset + Uint32Array.BYTES_PER_ELEMENT, 1);
27
+ }
28
+ set(e, t) {
29
+ this.length >= this.maxHash * 2 && this.growHashTable();
30
+ let n = o(e);
31
+ this.setHashKey(this.hashMemory, this.maxHash, n, t) && Atomics.add(this.pointerMemory.data, 2, 1);
32
+ }
33
+ setHashKey(n, i, a, o) {
34
+ let s = this.hash(a, i), c, l = e(n.data, s);
35
+ if (l.bufferByteOffset === 0) {
36
+ c = new r(this.memory, { dataLength: 2 });
37
+ let e = c.getSharedMemory();
38
+ t(n.data, s, e.firstBlock.bufferPosition, e.firstBlock.bufferByteOffset);
39
+ } else c = new r(this.memory, { firstBlock: l });
40
+ let u = !0;
41
+ return c.deleteValue(a) && (u = !1), c.insert([a, o]), u;
42
+ }
43
+ get(t) {
44
+ let n = o(t), i = this.hash(n, this.maxHash), a = e(this.hashMemory.data, i);
45
+ if (a.bufferByteOffset === 0) return;
46
+ let s = new r(this.memory, { firstBlock: a });
47
+ for (let { data: e } of s) if (e[0] === n) return e[1];
48
+ }
49
+ has(t) {
50
+ let n = o(t), i = this.hash(n, this.maxHash), a = e(this.hashMemory.data, i);
51
+ if (a.bufferByteOffset === 0) return !1;
52
+ let s = new r(this.memory, { firstBlock: a });
53
+ for (let { data: e } of s) if (e[0] === n) return !0;
54
+ return !1;
55
+ }
56
+ delete(t) {
57
+ let n = o(t), i = this.hash(n, this.maxHash), a = e(this.hashMemory.data, i);
58
+ if (a.bufferByteOffset === 0) return !1;
59
+ let s = new r(this.memory, { firstBlock: a });
60
+ for (let { data: e, deleteCurrent: t } of s) if (e[0] === n) return t(), Atomics.sub(this.pointerMemory.data, 2, 1), !0;
61
+ return !1;
62
+ }
63
+ growHashTable() {
64
+ let n = this.maxHash, i = n * 2, a = this.memory.allocUI32(i), o = this.hashMemory;
65
+ for (let t = 0; t < n; t++) {
66
+ let n = e(o.data, t);
67
+ if (n.bufferByteOffset === 0) continue;
68
+ let s = new r(this.memory, { firstBlock: n });
69
+ for (let { data: e } of s) this.setHashKey(a, i, e[0], e[1]);
70
+ s.free();
71
+ }
72
+ t(this.pointerMemory.data, 0, a.bufferPosition, a.bufferByteOffset), Atomics.store(this.pointerMemory.data, 3, i), o.free();
73
+ }
74
+ hash(e, t) {
75
+ return e % t;
76
+ }
77
+ free() {
78
+ for (let t = 0; t < this.maxHash; t++) {
79
+ let n = e(this.hashMemory.data, t);
80
+ n.bufferByteOffset !== 0 && new r(this.memory, { firstBlock: n }).free();
81
+ }
82
+ this.hashMemory.free(), this.pointerMemory.free();
83
+ }
84
+ getSharedMemory() {
85
+ return { firstBlock: this.pointerMemory.getSharedMemory() };
86
+ }
87
+ };
88
+ function o(e) {
89
+ return typeof e == "number" ? e : typeof e == "string" ? s(e) : e;
90
+ }
91
+ function s(e) {
92
+ let t = e.length, n = 17 ^ t, r = 0, i;
93
+ for (; t >= 4;) i = e.charCodeAt(r) & 255 | (e.charCodeAt(++r) & 255) << 8 | (e.charCodeAt(++r) & 255) << 16 | (e.charCodeAt(++r) & 255) << 14, i = (i & 65535) * 1540483477 + (((i >>> 16) * 1540483477 & 65535) << 16), i ^= i >>> 14, i = (i & 65535) * 1540483477 + (((i >>> 16) * 1540483477 & 65535) << 16), n = (n & 65535) * 1540483477 + (((n >>> 16) * 1540483477 & 65535) << 16) ^ i, t -= 4, ++r;
94
+ switch (t) {
95
+ case 3: n ^= (e.charCodeAt(r + 2) & 255) << 16;
96
+ case 2: n ^= (e.charCodeAt(r + 1) & 255) << 8;
97
+ case 1: n ^= e.charCodeAt(r) & 255, n = (n & 65535) * 1540483477 + (((n >>> 16) * 1540483477 & 65535) << 16);
98
+ }
99
+ return n ^= n >>> 13, n = (n & 65535) * 1540483477 + (((n >>> 16) * 1540483477 & 65535) << 16), n ^= n >>> 15, n >>>= 0, n;
100
+ }
101
+ //#endregion
102
+ export { a as default };
103
+
104
+ //# sourceMappingURL=shared-map.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-map.js","names":[],"sources":["../src/shared-map.ts"],"sourcesContent":["import AllocatedMemory, { type SharedAllocatedMemory } from './allocated-memory';\nimport type MemoryHeap from './memory-heap';\nimport SharedList from './shared-list';\nimport { loadPointer, storePointer } from './utils/pointer';\n\n// TODO: Grow hashMemory\n// TODO: Add iterator\n// TODO: Add read/write locks\nconst DEFAULT_HASH_SIZE = 10;\nexport default class SharedMap<K extends string | number> {\n\tstatic readonly ALLOCATE_COUNT = 4;\n\n\tprivate memory: MemoryHeap;\n\t// Memory order: Pointer, Lock, Length, MaxHash\n\tprivate pointerMemory: AllocatedMemory;\n\tprivate lock: Int32Array;\n\tprivate get hashMemory(): AllocatedMemory {\n\t\treturn new AllocatedMemory(this.memory, loadPointer(this.pointerMemory.data, 0));\n\t}\n\n\tget length(): number {\n\t\treturn Atomics.load(this.pointerMemory.data, 2);\n\t}\n\tget maxHash(): number {\n\t\treturn Atomics.load(this.pointerMemory.data, 3);\n\t}\n\n\tconstructor(memory: MemoryHeap, clone?: SharedMapMemory) {\n\t\tthis.memory = memory;\n\n\t\tif(clone) {\n\t\t\tthis.pointerMemory = new AllocatedMemory(memory, clone.firstBlock);\n\t\t} else {\n\t\t\tthis.pointerMemory = memory.allocUI32(SharedMap.ALLOCATE_COUNT);\n\t\t\tlet hashMemory = memory.allocUI32(DEFAULT_HASH_SIZE);\n\t\t\tstorePointer(this.pointerMemory.data, 0, hashMemory.bufferPosition, hashMemory.bufferByteOffset);\n\t\t\tAtomics.store(this.pointerMemory.data, 3, DEFAULT_HASH_SIZE);\n\t\t}\n\t\tthis.lock = new Int32Array(this.pointerMemory.data.buffer, this.pointerMemory.bufferByteOffset + Uint32Array.BYTES_PER_ELEMENT, 1);\n\t}\n\n\tset(key: K, value: number) {\n\t\tif(this.length >= this.maxHash * 2) {\n\t\t\tthis.growHashTable();\n\t\t}\n\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tif(this.setHashKey(this.hashMemory, this.maxHash, fullHashKey, value)) {\n\t\t\tAtomics.add(this.pointerMemory.data, 2, 1);\n\t\t}\n\t}\n\tprivate setHashKey(hashMemory: AllocatedMemory, maxHash: number, fullHashKey: number, value: number) {\n\t\tlet hashKey = this.hash(fullHashKey, maxHash);\n\t\t\n\t\tlet list: SharedList;\n\t\tlet pointer = loadPointer(hashMemory.data, hashKey);\n\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\t// Initialize a list here\n\t\t\tlist = new SharedList(this.memory, {\n\t\t\t\tdataLength: 2,\n\t\t\t});\n\n\t\t\tlet listMemory = list.getSharedMemory();\n\t\t\tstorePointer(hashMemory.data, hashKey, listMemory.firstBlock.bufferPosition, listMemory.firstBlock.bufferByteOffset);\n\t\t} else {\n\t\t\tlist = new SharedList(this.memory, {\n\t\t\t\tfirstBlock: pointer,\n\t\t\t});\n\t\t}\n\n\t\t// Check if any other items in list have the same key and delete them\n\t\tlet inserted = true;\n\t\tif(list.deleteValue(fullHashKey)) {\n\t\t\tinserted = false;\n\t\t}\n\t\tlist.insert([fullHashKey, value]);\n\t\t\n\t\treturn inserted;\n\t}\n\n\tget(key: K): number | undefined {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tlet hashKey = this.hash(fullHashKey, this.maxHash);\n\n\t\tlet pointer = loadPointer(this.hashMemory.data, hashKey);\n\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tlet list = new SharedList(this.memory, {\n\t\t\tfirstBlock: pointer,\n\t\t});\n\n\t\tfor(let { data } of list) {\n\t\t\tif(data[0] === fullHashKey) {\n\t\t\t\treturn data[1];\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n\thas(key: K): boolean {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tlet hashKey = this.hash(fullHashKey, this.maxHash);\n\n\t\tlet pointer = loadPointer(this.hashMemory.data, hashKey);\n\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\treturn false;\n\t\t}\n\n\t\tlet list = new SharedList(this.memory, {\n\t\t\tfirstBlock: pointer,\n\t\t});\n\n\t\tfor(let { data } of list) {\n\t\t\tif(data[0] === fullHashKey) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tdelete(key: K): boolean {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tlet hashKey = this.hash(fullHashKey, this.maxHash);\n\n\t\tlet pointer = loadPointer(this.hashMemory.data, hashKey);\n\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\treturn false;\n\t\t}\n\n\t\tlet list = new SharedList(this.memory, {\n\t\t\tfirstBlock: pointer,\n\t\t});\n\n\t\tfor(let { data, deleteCurrent } of list) {\n\t\t\tif(data[0] === fullHashKey) {\n\t\t\t\tdeleteCurrent();\n\n\t\t\t\tAtomics.sub(this.pointerMemory.data, 2, 1);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\n\tprivate growHashTable() {\n\t\tlet oldMaxHash = this.maxHash;\n\t\tlet newMaxHash = oldMaxHash * 2;\n\t\tlet newHashMemory = this.memory.allocUI32(newMaxHash);\n\t\tlet oldHashMemory = this.hashMemory;\n\n\t\t// Copy each old hash value into new hash memory\n\t\tfor(let i = 0; i < oldMaxHash; i++) {\n\t\t\tlet pointer = loadPointer(oldHashMemory.data, i);\n\t\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet list = new SharedList(this.memory, {\n\t\t\t\tfirstBlock: pointer,\n\t\t\t});\n\t\t\tfor(let { data } of list) {\n\t\t\t\tthis.setHashKey(newHashMemory, newMaxHash, data[0], data[1]);\n\t\t\t}\n\n\t\t\t// The entries have been copied into freshly allocated lists in the new hash table so the old list can be freed\n\t\t\tlist.free();\n\t\t}\n\n\t\tstorePointer(this.pointerMemory.data, 0, newHashMemory.bufferPosition, newHashMemory.bufferByteOffset);\n\t\tAtomics.store(this.pointerMemory.data, 3, newMaxHash);\n\n\t\t// pointerMemory now references newHashMemory so the old hash table memory can be freed\n\t\toldHashMemory.free();\n\t}\n\n\tprivate hash(key: number, maxHash: number) {\n\t\treturn key % maxHash;\n\t}\n\n\tfree() {\n\t\t// Loop through and free lists in hash table first\n\t\tfor(let i = 0; i < this.maxHash; i++) {\n\t\t\tlet pointer = loadPointer(this.hashMemory.data, i);\n\t\t\tif(pointer.bufferByteOffset === 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet list = new SharedList(this.memory, {\n\t\t\t\tfirstBlock: pointer,\n\t\t\t});\n\t\t\tlist.free();\n\t\t}\n\n\t\tthis.hashMemory.free();\n\t\tthis.pointerMemory.free();\n\t}\n\n\tgetSharedMemory(): SharedMapMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.pointerMemory.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedMapMemory {\n\tfirstBlock: SharedAllocatedMemory\n}\n\nfunction get32BitHash<K extends string | number>(key: K): number {\n\tif(typeof key === 'number') {\n\t\treturn key;\n\t} else if(typeof key === 'string') {\n\t\treturn hashString(key);\n\t} else {\n\t\treturn key;\n\t}\n}\n\n// Copied from https://github.com/mmomtchev/SharedMap/blob/master/index.js - MurmurHash2\nfunction hashString(str: string): number {\n\tlet\n\t\tl = str.length,\n\t\th = 17 ^ l,\n\t\ti = 0,\n\t\tk;\n\twhile(l >= 4) {\n\t\tk =\n\t\t\t((str.charCodeAt(i) & 0xff)) |\n\t\t\t((str.charCodeAt(++i) & 0xff) << 8) |\n\t\t\t((str.charCodeAt(++i) & 0xff) << 16) |\n\t\t\t((str.charCodeAt(++i) & 0xff) << 14);\n\t\tk = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));\n\t\tk ^= k >>> 14;\n\t\tk = (((k & 0xffff) * 0x5bd1e995) + ((((k >>> 16) * 0x5bd1e995) & 0xffff) << 16));\n\t\th = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16)) ^ k;\n\t\tl -= 4;\n\t\t++i;\n\t}\n\t/* eslint-disable no-fallthrough */\n\tswitch(l) {\n\t\tcase 3: h ^= (str.charCodeAt(i + 2) & 0xff) << 16;\n\t\tcase 2: h ^= (str.charCodeAt(i + 1) & 0xff) << 8;\n\t\tcase 1: h ^= (str.charCodeAt(i) & 0xff);\n\t\t\th = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));\n\t}\n\t/* eslint-enable no-fallthrough */\n\th ^= h >>> 13;\n\th = (((h & 0xffff) * 0x5bd1e995) + ((((h >>> 16) * 0x5bd1e995) & 0xffff) << 16));\n\th ^= h >>> 15;\n\th = h >>> 0;\n\treturn h;\n}\n\nexport type { SharedMapMemory };"],"mappings":";;;;AAQA,IAAM,IAAoB,IACL,IAArB,MAAqB,EAAqC;CACzD,OAAgB,iBAAiB;CAEjC;CAEA;CACA;CACA,IAAY,aAA8B;EACzC,OAAO,IAAI,EAAgB,KAAK,QAAQ,EAAY,KAAK,cAAc,MAAM,CAAC,CAAC;CAChF;CAEA,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAAK,KAAK,cAAc,MAAM,CAAC;CAC/C;CACA,IAAI,UAAkB;EACrB,OAAO,QAAQ,KAAK,KAAK,cAAc,MAAM,CAAC;CAC/C;CAEA,YAAY,GAAoB,GAAyB;EAGxD,IAFA,KAAK,SAAS,GAEX,GACF,KAAK,gBAAgB,IAAI,EAAgB,GAAQ,EAAM,UAAU;OAC3D;GACN,KAAK,gBAAgB,EAAO,UAAU,EAAU,cAAc;GAC9D,IAAI,IAAa,EAAO,UAAU,CAAiB;GAEnD,AADA,EAAa,KAAK,c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IAAI,KACjC,KAAO,IAAI,SAAU,gBAAkB,MAAM,MAAM,aAAc,UAAW;CAC9E;CAMA,OAJA,KAAK,MAAM,IACX,KAAO,IAAI,SAAU,gBAAkB,MAAM,MAAM,aAAc,UAAW,KAC5E,KAAK,MAAM,IACX,OAAU,GACH;AACR"}
@@ -1,6 +1,6 @@
1
- import { default as AllocatedMemory } from './allocated-memory';
2
- import { default as MemoryHeap } from './memory-heap';
3
- import { SharedListMemory } from './shared-list';
1
+ import AllocatedMemory from './allocated-memory';
2
+ import type MemoryHeap from './memory-heap';
3
+ import type { SharedListMemory } from './shared-list';
4
4
  export default abstract class SharedPointerList<T extends PointerItem> implements Iterable<T> {
5
5
  private memory;
6
6
  private list;
@@ -0,0 +1,47 @@
1
+ import { createPointer as e, loadPointer as t } from "./utils/pointer.js";
2
+ import n from "./allocated-memory.js";
3
+ import r from "./shared-list.js";
4
+ //#region src/shared-pointer-list.ts
5
+ var i = class {
6
+ memory;
7
+ list;
8
+ constructor(e, t) {
9
+ this.memory = e, this.list = t ? new r(e, t) : new r(e);
10
+ }
11
+ get length() {
12
+ return this.list.length;
13
+ }
14
+ insert(t) {
15
+ this.list.insert(e(t.memory.bufferPosition, t.memory.bufferByteOffset));
16
+ }
17
+ delete(t) {
18
+ return this.list.deleteValue(e(t.memory.bufferPosition, t.memory.bufferByteOffset));
19
+ }
20
+ *[Symbol.iterator]() {
21
+ let e = this.list[Symbol.iterator]();
22
+ for (let { data: r } of e) {
23
+ let { bufferPosition: e, bufferByteOffset: i } = t(r, 0), a = new n(this.memory, {
24
+ bufferPosition: e,
25
+ bufferByteOffset: i
26
+ });
27
+ yield this.createItem(a);
28
+ }
29
+ }
30
+ forEach(e) {
31
+ for (let t of this) e(t);
32
+ }
33
+ find(e) {
34
+ for (let t of this) if (e(t)) return t;
35
+ }
36
+ getSharedMemory() {
37
+ return this.list.getSharedMemory();
38
+ }
39
+ free() {
40
+ for (let e of this) "free" in e && typeof e.free == "function" ? e.free() : e.memory.free();
41
+ this.list.free();
42
+ }
43
+ };
44
+ //#endregion
45
+ export { i as default };
46
+
47
+ //# sourceMappingURL=shared-pointer-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-pointer-list.js","names":[],"sources":["../src/shared-pointer-list.ts"],"sourcesContent":["import AllocatedMemory from './allocated-memory';\nimport type MemoryHeap from './memory-heap';\nimport type { SharedListMemory } from './shared-list';\nimport SharedList from './shared-list';\nimport { createPointer, loadPointer } from './utils/pointer';\n\nexport default abstract class SharedPointerList<T extends PointerItem> implements Iterable<T> {\n\tprivate memory: MemoryHeap;\n\tprivate list: SharedList;\n\n\tconstructor(heap: MemoryHeap, memory?: SharedListMemory) {\n\t\tthis.memory = heap;\n\n\t\tif(memory) {\n\t\t\tthis.list = new SharedList(heap, memory);\n\t\t} else {\n\t\t\tthis.list = new SharedList(heap);\n\t\t}\n\t}\n\n\tget length() {\n\t\treturn this.list.length;\n\t}\n\n\tinsert(item: T) {\n\t\tthis.list.insert(createPointer(item.memory.bufferPosition, item.memory.bufferByteOffset));\n\t}\n\tdelete(item: T) {\n\t\treturn this.list.deleteValue(createPointer(item.memory.bufferPosition, item.memory.bufferByteOffset));\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet iterator = this.list[Symbol.iterator]();\n\n\t\tfor(let { data: pointerData } of iterator) {\n\t\t\tlet { bufferPosition, bufferByteOffset } = loadPointer(pointerData, 0);\n\t\t\tlet allocatedMemory = new AllocatedMemory(this.memory, {\n\t\t\t\tbufferPosition,\n\t\t\t\tbufferByteOffset,\n\t\t\t});\n\t\t\tyield this.createItem(allocatedMemory);\n\t\t}\n\t}\n\tforEach(callback: (item: T) => void) {\n\t\tfor(let value of this) {\n\t\t\tcallback(value);\n\t\t}\n\t}\n\n\tfind(callback: (item: T) => boolean): T | undefined {\n\t\tfor(let value of this) {\n\t\t\tif(callback(value)) {\n\t\t\t\treturn value;\n\t\t\t}\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\tgetSharedMemory() {\n\t\treturn this.list.getSharedMemory();\n\t}\n\n\tprotected abstract createItem(allocatedMemory: AllocatedMemory): T;\n\n\tfree() {\n\t\tfor(let item of this) {\n\t\t\t// NOTE: Anything that allocates it's own memory (ie: type for Item) needs to call free for that class so we can clear all memory recursively\n\t\t\tif('free' in item && typeof item.free === 'function') {\n\t\t\t\titem.free();\n\t\t\t} else {\n\t\t\t\titem.memory.free();\n\t\t\t}\n\t\t}\n\n\t\tthis.list.free();\n\t}\n}\n\ninterface PointerItem {\n\treadonly memory: AllocatedMemory\n}"],"mappings":";;;;AAMA,IAA8B,IAA9B,MAA8F;CAC7F;CACA;CAEA,YAAY,GAAkB,GAA2B;EAGxD,AAFA,KAAK,SAAS,GAEd,AAGC,KAAK,OAHH,IACU,IAAI,EAAW,GAAM,CAAM,IAE3B,IAAI,EAAW,CAAI;CAEjC;CAEA,IAAI,SAAS;EACZ,OAAO,KAAK,KAAK;CAClB;CAEA,OAAO,GAAS;EACf,KAAK,KAAK,OAAO,EAAc,EAAK,OAAO,gBAAgB,EAAK,OAAO,gBAAgB,CAAC;CACzF;CACA,OAAO,GAAS;EACf,OAAO,KAAK,KAAK,YAAY,EAAc,EAAK,OAAO,gBAAgB,EAAK,OAAO,gBAAgB,CAAC;CACrG;CAEA,EAAE,OAAO,YAAY;EACpB,IAAI,IAAW,KAAK,KAAK,OAAO,SAAS,CAAC;EAE1C,KAAI,IAAI,EAAE,MAAM,OAAiB,GAAU;GAC1C,IAAI,EAAE,mBAAgB,wBAAqB,EAAY,GAAa,CAAC,GACjE,IAAkB,IAAI,EAAgB,KAAK,QAAQ;IACtD;IACA;GACD,CAAC;GACD,MAAM,KAAK,WAAW,CAAe;EACtC;CACD;CACA,QAAQ,GAA6B;EACpC,KAAI,IAAI,KAAS,MAChB,EAAS,CAAK;CAEhB;CAEA,KAAK,GAA+C;EACnD,KAAI,IAAI,KAAS,MAChB,IAAG,EAAS,CAAK,GAChB,OAAO;CAKV;CAEA,kBAAkB;EACjB,OAAO,KAAK,KAAK,gBAAgB;CAClC;CAIA,OAAO;EACN,KAAI,IAAI,KAAQ,MAEf,AAAG,UAAU,KAAQ,OAAO,EAAK,QAAS,aACzC,EAAK,KAAK,IAEV,EAAK,OAAO,KAAK;EAInB,KAAK,KAAK,KAAK;CAChB;AACD"}
@@ -1,14 +1,19 @@
1
- import { SharedAllocatedMemory } from './allocated-memory';
2
- import { TypedArrayConstructor } from './interfaces/typed-array-constructor';
3
- import { default as MemoryHeap } from './memory-heap';
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 SharedPool<T extends Uint32Array | Int32Array | Float32Array | Float64Array = Uint32Array> implements Iterable<T> {
5
- static readonly ALLOCATE_COUNT: number;
5
+ static readonly ALLOCATE_COUNT = 6;
6
6
  private memory;
7
7
  private firstBlock;
8
8
  private uint16Array;
9
- private pointerVector;
10
- private recycleVector;
9
+ private growLock;
10
+ private pointerStack;
11
+ private recycleStack;
11
12
  private cachedFullDataBlock;
13
+ private readonly cachedMaxChunkSize;
14
+ private readonly cachedDataLength;
15
+ private readonly cachedType;
16
+ private readonly cachedByteMultipler;
12
17
  get length(): number;
13
18
  get maxChunkSize(): number;
14
19
  private set maxChunkSize(value);
@@ -17,7 +22,6 @@ export default class SharedPool<T extends Uint32Array | Int32Array | Float32Arra
17
22
  get dataLength(): number;
18
23
  private set dataLength(value);
19
24
  get bufferLength(): number;
20
- get recycleBufferLength(): number;
21
25
  get byteMultipler(): number;
22
26
  constructor(memory: MemoryHeap, config?: SharedPoolConfig<T> | SharedPoolMemory);
23
27
  at(index: number): T;
@@ -35,7 +39,6 @@ interface SharedPoolConfig<T extends Uint32Array | Int32Array | Float32Array | F
35
39
  maxChunkSize?: number;
36
40
  type?: TypedArrayConstructor<T>;
37
41
  dataLength?: number;
38
- recycleBufferLength?: number;
39
42
  }
40
43
  interface SharedPoolMemory {
41
44
  firstBlock: SharedAllocatedMemory;
@@ -0,0 +1,145 @@
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 = class p {
7
+ static ALLOCATE_COUNT = 6;
8
+ memory;
9
+ firstBlock;
10
+ uint16Array;
11
+ growLock;
12
+ pointerStack;
13
+ recycleStack;
14
+ cachedFullDataBlock = {};
15
+ cachedMaxChunkSize;
16
+ cachedDataLength;
17
+ cachedType;
18
+ cachedByteMultipler;
19
+ get length() {
20
+ return Atomics.load(this.firstBlock.data, s) - this.recycleStack.length;
21
+ }
22
+ get maxChunkSize() {
23
+ return this.cachedMaxChunkSize;
24
+ }
25
+ set maxChunkSize(e) {
26
+ Atomics.store(this.firstBlock.data, l, e);
27
+ }
28
+ get type() {
29
+ return this.cachedType;
30
+ }
31
+ set type(e) {
32
+ Atomics.store(this.uint16Array, 0, e);
33
+ }
34
+ get dataLength() {
35
+ return this.cachedDataLength;
36
+ }
37
+ set dataLength(e) {
38
+ Atomics.store(this.uint16Array, 1, e);
39
+ }
40
+ get bufferLength() {
41
+ return this.cachedMaxChunkSize * this.pointerStack.length;
42
+ }
43
+ get byteMultipler() {
44
+ return this.cachedByteMultipler;
45
+ }
46
+ constructor(i, a) {
47
+ 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)) });
48
+ else {
49
+ this.firstBlock = i.allocUI32(p.ALLOCATE_COUNT + o.DEFAULT_ALLOCATE_COUNT * 2), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + c * Uint32Array.BYTES_PER_ELEMENT, 2);
50
+ let e = a?.dataLength ?? 1, t = a?.maxChunkSize ?? 100;
51
+ this.pointerStack = new o(i, {
52
+ firstBlock: this.firstBlock.getSubAllocation(p.ALLOCATE_COUNT, o.DEFAULT_ALLOCATE_COUNT),
53
+ type: Uint32Array
54
+ }), n(this.firstBlock.data, d, this.pointerStack.pointer), this.recycleStack = new o(i, {
55
+ firstBlock: this.firstBlock.getSubAllocation(p.ALLOCATE_COUNT + o.DEFAULT_ALLOCATE_COUNT, o.DEFAULT_ALLOCATE_COUNT),
56
+ type: Uint32Array
57
+ }), n(this.firstBlock.data, f, this.recycleStack.pointer);
58
+ let r = a?.type ?? Uint32Array;
59
+ r === Uint32Array ? this.type = 0 : r === Int32Array ? this.type = 1 : r === Float32Array ? this.type = 2 : r === Float64Array && (this.type = 3), this.dataLength = e, this.maxChunkSize = t;
60
+ let s = r === Float64Array ? 2 : 1, l = i.allocUI32(t * e * s);
61
+ this.pointerStack.push(l.pointer);
62
+ }
63
+ 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.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + u * Uint32Array.BYTES_PER_ELEMENT, 1);
64
+ }
65
+ at(e) {
66
+ let t = this.getFullDataBlock(e);
67
+ return this.getDataBlock(t, e % this.cachedMaxChunkSize);
68
+ }
69
+ get(e, t = 0) {
70
+ let n = this.cachedDataLength;
71
+ if (t >= n) throw Error(`${t} is out of dataLength bounds ${n}`);
72
+ return this.getFullDataBlock(e)[e % this.cachedMaxChunkSize * n + t];
73
+ }
74
+ push(e) {
75
+ let t = this.cachedDataLength, n = typeof e == "number";
76
+ if (!n && e.length > t) throw Error(`Can't insert ${e.length} array into shared list of ${t} dataLength`);
77
+ let r = this.recycleStack.pop();
78
+ r === void 0 && (r = Atomics.add(this.firstBlock.data, s, 1));
79
+ let i = this.getFullDataBlock(r), a = t * (r % this.cachedMaxChunkSize);
80
+ return n ? i[a] = e : i.set(e, a), r;
81
+ }
82
+ deleteIndex(e) {
83
+ this.recycleStack.push(e);
84
+ }
85
+ clear() {
86
+ this.firstBlock.data[s] = 0, this.recycleStack.clear();
87
+ }
88
+ *[Symbol.iterator]() {
89
+ let e = {};
90
+ for (let t of this.recycleStack) e[t] = !0;
91
+ let t = this.cachedMaxChunkSize, n = Atomics.load(this.firstBlock.data, s), r = this.getFullDataBlock(0), i = 0;
92
+ for (let a = 0; a < n; a++) if (!e[a]) {
93
+ let e = Math.floor(a / t);
94
+ e !== i && (r = this.getFullDataBlock(a), i = e), yield this.getDataBlock(r, a % t);
95
+ }
96
+ }
97
+ getFullDataBlock(t) {
98
+ let n = Math.floor(t / this.cachedMaxChunkSize), o = this.cachedFullDataBlock[n];
99
+ if (o) return o;
100
+ if (n >= this.pointerStack.length) {
101
+ i(this.growLock);
102
+ try {
103
+ for (; n >= this.pointerStack.length;) {
104
+ let e = this.memory.allocUI32(this.cachedMaxChunkSize * this.cachedDataLength * this.cachedByteMultipler);
105
+ this.pointerStack.push(e.pointer);
106
+ }
107
+ } finally {
108
+ a(this.growLock);
109
+ }
110
+ }
111
+ let s = new r(this.memory, e(this.pointerStack.at(n))), c = this.cachedDataLength * this.cachedMaxChunkSize, l;
112
+ switch (this.cachedType) {
113
+ case 1:
114
+ l = new Int32Array(s.data.buffer, s.bufferByteOffset, c);
115
+ break;
116
+ case 0:
117
+ l = new Uint32Array(s.data.buffer, s.bufferByteOffset, c);
118
+ break;
119
+ case 2:
120
+ l = new Float32Array(s.data.buffer, s.bufferByteOffset, c);
121
+ break;
122
+ case 3:
123
+ l = new Float64Array(s.data.buffer, s.bufferByteOffset, c);
124
+ break;
125
+ default: throw Error(`Unknown data block type ${this.cachedType}`);
126
+ }
127
+ return this.cachedFullDataBlock[n] = l, l;
128
+ }
129
+ getDataBlock(e, t) {
130
+ let n = this.cachedDataLength, r = t * n;
131
+ return e.subarray(r, r + n);
132
+ }
133
+ free() {
134
+ this.recycleStack.free();
135
+ for (let t of this.pointerStack) new r(this.memory, e(t)).free();
136
+ this.pointerStack.free(), this.firstBlock.free();
137
+ }
138
+ getSharedMemory() {
139
+ return { firstBlock: this.firstBlock.getSharedMemory() };
140
+ }
141
+ };
142
+ //#endregion
143
+ export { p as default };
144
+
145
+ //# sourceMappingURL=shared-pool.js.map
@@ -0,0 +1 @@
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\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\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\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\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 + SharedStack.DEFAULT_ALLOCATE_COUNT * 2);\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.pointerStack = new SharedStack(memory, {\n\t\t\t\tfirstBlock: this.firstBlock.getSubAllocation(SharedPool.ALLOCATE_COUNT, SharedStack.DEFAULT_ALLOCATE_COUNT),\n\t\t\t\ttype: Uint32Array,\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 + SharedStack.DEFAULT_ALLOCATE_COUNT, SharedStack.DEFAULT_ALLOCATE_COUNT),\n\t\t\t\ttype: Uint32Array,\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 = maxLength;\n\n\t\t\t// @ts-expect-error\n\t\t\tlet byteMultipler = type === Float64Array ? 2 : 1;\n\t\t\tlet firstArray = memory.allocUI32(maxLength * 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\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\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,GAMP,IAArB,MAAqB,EAAkH;CACtI,OAAgB,iBAAiB;CACjC;CAGA;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;CAEA,YAAY,GAAoB,GAAiD;EAGhF,IAFA,KAAK,SAAS,GAEX,KAAU,gBAAgB,GAM5B,AALA,KAAK,aA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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,46 @@
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 = 7;
7
+ static readonly DEFAULT_ALLOCATE_COUNT: number;
8
+ private memory;
9
+ private firstBlock;
10
+ private uint16Array;
11
+ private growLock;
12
+ readonly maxLength: number;
13
+ private ownsFirstBlock;
14
+ private cachedSegments;
15
+ private cachedSequences;
16
+ private cachedBaseSegmentLength;
17
+ get length(): number;
18
+ get segmentCount(): number;
19
+ private set segmentCount(value);
20
+ get type(): number;
21
+ private set type(value);
22
+ get baseSegmentLength(): number;
23
+ private set baseSegmentLength(value);
24
+ get maxSegments(): number;
25
+ private set maxSegments(value);
26
+ get pointer(): number;
27
+ constructor(memory: MemoryHeap, config?: SharedStackConfig<T> | SharedStackMemory | (SharedStackMemory & SharedStackConfig<T>));
28
+ private initialize;
29
+ at(index: number): number;
30
+ push(value: number): number;
31
+ pop(): number | undefined;
32
+ clear(): void;
33
+ [Symbol.iterator](): Generator<number, void, unknown>;
34
+ private getSegment;
35
+ free(): void;
36
+ getSharedMemory(): SharedStackMemory;
37
+ }
38
+ interface SharedStackConfig<T extends Uint32Array | Int32Array | Float32Array> {
39
+ type?: TypedArrayConstructor<T>;
40
+ baseSegmentLength?: number;
41
+ segmentCount?: number;
42
+ }
43
+ interface SharedStackMemory {
44
+ firstBlock: SharedAllocatedMemory | AllocatedMemory;
45
+ }
46
+ export type { SharedStackConfig, SharedStackMemory };