@daneren2005/shared-memory-objects 1.1.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 (80) hide show
  1. package/README.md +78 -55
  2. package/dist/{src/allocated-memory.d.ts → allocated-memory.d.ts} +5 -4
  3. package/dist/allocated-memory.js +44 -37
  4. package/dist/allocated-memory.js.map +1 -1
  5. package/dist/{src/cached-item-list.d.ts → cached-item-list.d.ts} +3 -3
  6. package/dist/cached-item-list.js +70 -75
  7. package/dist/cached-item-list.js.map +1 -1
  8. package/dist/constant-string.d.ts +15 -0
  9. package/dist/constant-string.js +67 -0
  10. package/dist/constant-string.js.map +1 -0
  11. package/dist/interfaces/pow2.js +0 -2
  12. package/dist/interfaces/typed-array-constructor.js +0 -2
  13. package/dist/interfaces/typed-array.js +0 -2
  14. package/dist/{src/local-pool.d.ts → local-pool.d.ts} +2 -2
  15. package/dist/local-pool.js +89 -94
  16. package/dist/local-pool.js.map +1 -1
  17. package/dist/lock/read-write-lock.js +15 -21
  18. package/dist/lock/read-write-lock.js.map +1 -1
  19. package/dist/lock/simple-lock.js +9 -12
  20. package/dist/lock/simple-lock.js.map +1 -1
  21. package/dist/{src/memory-buffer.d.ts → memory-buffer.d.ts} +4 -4
  22. package/dist/memory-buffer.js +266 -326
  23. package/dist/memory-buffer.js.map +1 -1
  24. package/dist/{src/memory-heap.d.ts → memory-heap.d.ts} +2 -2
  25. package/dist/memory-heap.js +95 -115
  26. package/dist/memory-heap.js.map +1 -1
  27. package/dist/{src/serialize-object.d.ts → serialize-object.d.ts} +2 -2
  28. package/dist/serialize-object.js +43 -55
  29. package/dist/serialize-object.js.map +1 -1
  30. package/dist/{src/shared-list.d.ts → shared-list.d.ts} +3 -3
  31. package/dist/shared-list.js +124 -164
  32. package/dist/shared-list.js.map +1 -1
  33. package/dist/{src/shared-map.d.ts → shared-map.d.ts} +2 -2
  34. package/dist/shared-map.js +102 -138
  35. package/dist/shared-map.js.map +1 -1
  36. package/dist/{src/shared-pointer-list.d.ts → shared-pointer-list.d.ts} +3 -3
  37. package/dist/shared-pointer-list.js +45 -48
  38. package/dist/shared-pointer-list.js.map +1 -1
  39. package/dist/{src/shared-pool.d.ts → shared-pool.d.ts} +11 -8
  40. package/dist/shared-pool.js +144 -164
  41. package/dist/shared-pool.js.map +1 -1
  42. package/dist/shared-stack.d.ts +46 -0
  43. package/dist/shared-stack.js +144 -0
  44. package/dist/shared-stack.js.map +1 -0
  45. package/dist/{src/shared-string.d.ts → shared-string.d.ts} +2 -2
  46. package/dist/shared-string.js +72 -78
  47. package/dist/shared-string.js.map +1 -1
  48. package/dist/{src/shared-vector.d.ts → shared-vector.d.ts} +4 -4
  49. package/dist/shared-vector.js +146 -164
  50. package/dist/shared-vector.js.map +1 -1
  51. package/dist/utils/16-from-32-array.js +14 -16
  52. package/dist/utils/16-from-32-array.js.map +1 -1
  53. package/dist/utils/16-from-64-array.js +15 -10
  54. package/dist/utils/16-from-64-array.js.map +1 -1
  55. package/dist/utils/atomic-math.js +30 -32
  56. package/dist/utils/atomic-math.js.map +1 -1
  57. package/dist/utils/float32-atomics.js +18 -22
  58. package/dist/utils/float32-atomics.js.map +1 -1
  59. package/dist/utils/float64-atomics.js +18 -22
  60. package/dist/utils/float64-atomics.js.map +1 -1
  61. package/dist/utils/pointer.js +34 -44
  62. package/dist/utils/pointer.js.map +1 -1
  63. package/dist/{src/utils → utils}/typedarray.d.ts +47 -47
  64. package/dist/utils/typedarray.js +36 -43
  65. package/dist/utils/typedarray.js.map +1 -1
  66. package/package.json +22 -22
  67. package/dist/interfaces/pow2.js.map +0 -1
  68. package/dist/interfaces/typed-array-constructor.js.map +0 -1
  69. package/dist/interfaces/typed-array.js.map +0 -1
  70. /package/dist/{src/interfaces → interfaces}/pow2.d.ts +0 -0
  71. /package/dist/{src/interfaces → interfaces}/typed-array-constructor.d.ts +0 -0
  72. /package/dist/{src/interfaces → interfaces}/typed-array.d.ts +0 -0
  73. /package/dist/{src/lock → lock}/read-write-lock.d.ts +0 -0
  74. /package/dist/{src/lock → lock}/simple-lock.d.ts +0 -0
  75. /package/dist/{src/utils → utils}/16-from-32-array.d.ts +0 -0
  76. /package/dist/{src/utils → utils}/16-from-64-array.d.ts +0 -0
  77. /package/dist/{src/utils → utils}/atomic-math.d.ts +0 -0
  78. /package/dist/{src/utils → utils}/float32-atomics.d.ts +0 -0
  79. /package/dist/{src/utils → utils}/float64-atomics.d.ts +0 -0
  80. /package/dist/{src/utils → utils}/pointer.d.ts +0 -0
@@ -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 };
@@ -0,0 +1,144 @@
1
+ import { getPointer as e, storePointer as t } from "./utils/pointer.js";
2
+ import n from "./allocated-memory.js";
3
+ import { lock as r, unlock as i } from "./lock/simple-lock.js";
4
+ //#region src/shared-stack.ts
5
+ var a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = 6, f = 4, p = 20, m = 22, h = (1 << m) - 1, g = 4194302, _ = 1 << m, v = class m {
6
+ static BASE_ALLOCATE_COUNT = 7;
7
+ static DEFAULT_ALLOCATE_COUNT = this.BASE_ALLOCATE_COUNT + p;
8
+ memory;
9
+ firstBlock;
10
+ uint16Array;
11
+ growLock;
12
+ maxLength;
13
+ ownsFirstBlock;
14
+ cachedSegments = [];
15
+ cachedSequences = [];
16
+ cachedBaseSegmentLength = 0;
17
+ get length() {
18
+ return Atomics.load(this.firstBlock.data, a) & h;
19
+ }
20
+ get segmentCount() {
21
+ return Atomics.load(this.firstBlock.data, l);
22
+ }
23
+ set segmentCount(e) {
24
+ this.firstBlock.data[l] = e;
25
+ }
26
+ get type() {
27
+ return this.uint16Array[0];
28
+ }
29
+ set type(e) {
30
+ Atomics.store(this.uint16Array, 0, e);
31
+ }
32
+ get baseSegmentLength() {
33
+ return this.firstBlock.data[s];
34
+ }
35
+ set baseSegmentLength(e) {
36
+ this.firstBlock.data[s] = e;
37
+ }
38
+ get maxSegments() {
39
+ return this.firstBlock.data[c];
40
+ }
41
+ set maxSegments(e) {
42
+ this.firstBlock.data[c] = e;
43
+ }
44
+ get pointer() {
45
+ return this.firstBlock.pointer;
46
+ }
47
+ constructor(e, t) {
48
+ if (this.memory = e, t && "firstBlock" in t) this.firstBlock = t.firstBlock instanceof n ? t.firstBlock : new n(e, t.firstBlock), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + o * Uint32Array.BYTES_PER_ELEMENT, 2), "type" in t || "baseSegmentLength" in t || "segmentCount" in t ? (this.initialize(t), this.ownsFirstBlock = !1) : this.ownsFirstBlock = !0;
49
+ else {
50
+ let n = t?.segmentCount ?? p;
51
+ this.firstBlock = e.allocUI32(m.BASE_ALLOCATE_COUNT + n), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + o * Uint32Array.BYTES_PER_ELEMENT, 2), this.ownsFirstBlock = !0, this.initialize(t);
52
+ }
53
+ if (this.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + u * Uint32Array.BYTES_PER_ELEMENT, 1), this.cachedBaseSegmentLength = this.baseSegmentLength, this.maxLength = this.cachedBaseSegmentLength * (2 ** this.maxSegments - 1), this.maxLength > g) throw Error(`SharedStack maxLength ${this.maxLength} exceeds the ${g} the versioned length counter can address`);
54
+ }
55
+ initialize(e) {
56
+ let n = e?.baseSegmentLength ?? f, r = this.memory.allocUI32(n * 2);
57
+ t(this.firstBlock.data, d, r.bufferPosition, r.bufferByteOffset), this.baseSegmentLength = n, this.maxSegments = e?.segmentCount ?? p, this.segmentCount = 1;
58
+ let i = e?.type ?? Uint32Array;
59
+ i === Uint32Array ? this.type = 0 : i === Int32Array ? this.type = 1 : i === Float32Array && (this.type = 2);
60
+ }
61
+ at(e) {
62
+ let t = this.length;
63
+ if (e >= t || e < 0) throw Error(`${e} is out of bounds ${t}`);
64
+ let n = this.cachedBaseSegmentLength, r = 31 - Math.clz32((e / n | 0) + 1);
65
+ return (this.cachedSegments[r] ?? this.getSegment(r))[e - n * ((1 << r) - 1)];
66
+ }
67
+ push(e) {
68
+ let t = this.firstBlock.data, n = this.cachedBaseSegmentLength;
69
+ for (;;) {
70
+ let r = t[a], i = r & h;
71
+ if (i >= this.maxLength) throw Error(`${i + 1} is out of bounds ${this.maxLength}`);
72
+ let o = 31 - Math.clz32((i / n | 0) + 1), s = i - n * ((1 << o) - 1), c = this.cachedSegments[o] ?? this.getSegment(o), l = this.cachedSequences[o], u = Atomics.load(l, s);
73
+ if (!(u & 1) && Atomics.compareExchange(t, a, r, r + _ + 1) === r) return c[s] = e, Atomics.store(l, s, u + 1), i;
74
+ }
75
+ }
76
+ pop() {
77
+ let e = this.firstBlock.data, t = this.cachedBaseSegmentLength;
78
+ for (;;) {
79
+ let n = e[a], r = n & h;
80
+ if (r <= 0) return;
81
+ let i = r - 1, o = 31 - Math.clz32((i / t | 0) + 1), s = i - t * ((1 << o) - 1), c = this.cachedSegments[o] ?? this.getSegment(o), l = this.cachedSequences[o], u = Atomics.load(l, s);
82
+ if (!(u & 1) || Atomics.compareExchange(e, a, n, n + _ - 1) !== n) continue;
83
+ let d = c[s];
84
+ return Atomics.store(l, s, u + 1), d;
85
+ }
86
+ }
87
+ clear() {
88
+ let e = this.cachedBaseSegmentLength, t = Atomics.load(this.firstBlock.data, a) & h;
89
+ for (let n = 0; n < t; n++) {
90
+ let t = 31 - Math.clz32((n / e | 0) + 1), r = this.cachedSequences[t] ?? (this.getSegment(t), this.cachedSequences[t]), i = n - e * ((1 << t) - 1), a = Atomics.load(r, i);
91
+ a & 1 && Atomics.store(r, i, a + 1);
92
+ }
93
+ Atomics.store(this.firstBlock.data, a, 0);
94
+ }
95
+ *[Symbol.iterator]() {
96
+ let e = this.length, t = this.baseSegmentLength, n = 0, r = this.getSegment(n), i = 0;
97
+ for (let a = 0; a < e; a++) yield r[i], i++, i >= t && (n++, r = this.getSegment(n), i = 0, t *= 2);
98
+ }
99
+ getSegment(a) {
100
+ if (this.cachedSegments[a]) return this.cachedSegments[a];
101
+ let o = this.cachedBaseSegmentLength * 2 ** a, s;
102
+ if (a >= this.segmentCount) {
103
+ r(this.growLock);
104
+ try {
105
+ a >= this.segmentCount && (s = this.memory.allocUI32(o * 2), t(this.firstBlock.data, d + a, s.bufferPosition, s.bufferByteOffset), Atomics.add(this.firstBlock.data, l, 1));
106
+ } finally {
107
+ i(this.growLock);
108
+ }
109
+ }
110
+ if (!s) {
111
+ let t = Atomics.load(this.firstBlock.data, d + a), r = e(t);
112
+ s = new n(this.memory, r);
113
+ }
114
+ let c;
115
+ switch (this.type) {
116
+ case 1:
117
+ c = new Int32Array(s.data.buffer, s.bufferByteOffset, o);
118
+ break;
119
+ case 0:
120
+ c = new Uint32Array(s.data.buffer, s.bufferByteOffset, o);
121
+ break;
122
+ case 2:
123
+ c = new Float32Array(s.data.buffer, s.bufferByteOffset, o);
124
+ break;
125
+ default: throw Error(`Unknown data block type ${this.type}`);
126
+ }
127
+ let u = s.bufferByteOffset + o * Uint32Array.BYTES_PER_ELEMENT;
128
+ return this.cachedSequences[a] = new Int32Array(s.data.buffer, u, o), this.cachedSegments[a] = c, c;
129
+ }
130
+ free() {
131
+ for (let t = 0; t < this.segmentCount; t++) {
132
+ let r = Atomics.load(this.firstBlock.data, d + t), i = e(r);
133
+ new n(this.memory, i).free();
134
+ }
135
+ this.ownsFirstBlock && this.firstBlock.free();
136
+ }
137
+ getSharedMemory() {
138
+ return { firstBlock: this.firstBlock.getSharedMemory() };
139
+ }
140
+ };
141
+ //#endregion
142
+ export { v as default };
143
+
144
+ //# sourceMappingURL=shared-stack.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-stack.js","names":[],"sources":["../src/shared-stack.ts"],"sourcesContent":["import type { SharedAllocatedMemory } from './allocated-memory';\nimport AllocatedMemory from './allocated-memory';\nimport type { TypedArrayConstructor } from './interfaces/typed-array-constructor';\nimport { lock, unlock } from './lock/simple-lock';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, storePointer } from './utils/pointer';\n\nenum TYPE {\n\tuint32,\n\tint32,\n\tfloat32,\n}\n\nconst LENGTH_INDEX = 0;\nconst TYPE_INDEX = 1;\nconst BASE_SEGMENT_LENGTH_INDEX = 2;\nconst MAX_SEGMENTS_INDEX = 3;\nconst SEGMENT_COUNT_INDEX = 4;\nconst GROW_LOCK_INDEX = 5;\nconst SEGMENT_START_INDEX = 6;\n\nconst DEFAULT_BASE_SEGMENT_LENGTH = 4;\nconst DEFAULT_MAX_SEGMENTS = 20;\n\n// The LENGTH word packs an ABA version counter (high bits) with the live count (low LEN_BITS). Both push and pop bump\n// the version on every successful CAS, so the CAS fails on ANY intervening op - the count can never ABA back to the\n// same value with a slot at a different generation. 22 count bits cover the default max length (4,194,300); the\n// remaining 10 version bits (1024) only need to outlast a single thread's load->CAS window.\nconst LEN_BITS = 22;\nconst LEN_MASK = (1 << LEN_BITS) - 1;\nconst MAX_COUNT = LEN_MASK - 1;\n// Adding this to the packed word bumps the version (high bits) by one. Combined with +1/-1 for the count it repacks the\n// whole word in a single add: the count is guarded from overflowing into the version bits, and a version overflow just\n// wraps harmlessly when the value is coerced back to Uint32 on the atomic store.\nconst VERSION_STEP = 1 << LEN_BITS;\n\n// Per-slot publish handshake. Each data slot has a companion sequence number: even means the slot is empty (free to\n// fill) at some lap, odd means the value is published (safe to read). It only ever increases. push/pop read the slot's\n// sequence and bail out (retry) unless it has the parity they need, then CAS the length; because the versioned CAS\n// rejects any intervening op, the sequence read stays valid all the way through the publish store below.\n\nexport default class SharedStack<T extends Uint32Array | Int32Array | Float32Array = Uint32Array> implements Iterable<number> {\n\tstatic readonly BASE_ALLOCATE_COUNT = 7;\n\tstatic readonly DEFAULT_ALLOCATE_COUNT = this.BASE_ALLOCATE_COUNT + DEFAULT_MAX_SEGMENTS;\n\tprivate memory: MemoryHeap;\n\n\t// Length, Type, Base Segment Length, Max Segments, Segment Count, Grow Lock, Segments\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\tprivate growLock: Int32Array;\n\treadonly maxLength: number;\n\n\t// False when firstBlock is a slice of another structure's memory (e.g. inlined in SharedPool); that owner frees it\n\tprivate ownsFirstBlock: boolean;\n\n\tprivate cachedSegments: T[] = [];\n\t// Parallel to cachedSegments: the per-slot publish sequences for each slot of that segment. Populated together with\n\t// the data view in getSegment, so whenever cachedSegments[i] is set cachedSequences[i] is too.\n\tprivate cachedSequences: Int32Array[] = [];\n\tprivate cachedBaseSegmentLength = 0;\n\n\tget length(): number {\n\t\treturn Atomics.load(this.firstBlock.data, LENGTH_INDEX) & LEN_MASK;\n\t}\n\tget segmentCount(): number {\n\t\treturn Atomics.load(this.firstBlock.data, SEGMENT_COUNT_INDEX);\n\t}\n\tprivate set segmentCount(value: number) {\n\t\tthis.firstBlock.data[SEGMENT_COUNT_INDEX] = value;\n\t}\n\t\n\tget type(): number {\n\t\treturn this.uint16Array[0];\n\t}\n\tprivate set type(value: number) {\n\t\tAtomics.store(this.uint16Array, 0, value);\n\t}\n\n\tget baseSegmentLength() {\n\t\treturn this.firstBlock.data[BASE_SEGMENT_LENGTH_INDEX];\n\t}\n\tprivate set baseSegmentLength(value: number) {\n\t\tthis.firstBlock.data[BASE_SEGMENT_LENGTH_INDEX] = value;\n\t}\n\tget maxSegments() {\n\t\treturn this.firstBlock.data[MAX_SEGMENTS_INDEX];\n\t}\n\tprivate set maxSegments(value: number) {\n\t\tthis.firstBlock.data[MAX_SEGMENTS_INDEX] = value;\n\t}\n\n\tget pointer() {\n\t\treturn this.firstBlock.pointer;\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedStackConfig<T> | SharedStackMemory | SharedStackMemory & SharedStackConfig<T>) {\n\t\tthis.memory = memory;\n\n\t\tif(config && 'firstBlock' in config) {\n\t\t\t// An AllocatedMemory instance is a caller-reserved slice (e.g. inlined in SharedPool); a plain pointer is the\n\t\t\t// serialized clone path. Taking the instance directly keeps us off the pointer-resolving construction branch.\n\t\t\tthis.firstBlock = config.firstBlock instanceof AllocatedMemory ? config.firstBlock : new AllocatedMemory(memory, config.firstBlock);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\n\t\t\t// Memory passed together with config means the caller reserved the firstBlock slot for us to initialize in place\n\t\t\tif('type' in config || 'baseSegmentLength' in config || 'segmentCount' in config) {\n\t\t\t\tthis.initialize(config);\n\t\t\t\tthis.ownsFirstBlock = false;\n\t\t\t} else {\n\t\t\t\tthis.ownsFirstBlock = true;\n\t\t\t}\n\t\t} else {\n\t\t\tlet maxSegments = config?.segmentCount ?? DEFAULT_MAX_SEGMENTS;\n\n\t\t\tthis.firstBlock = memory.allocUI32(SharedStack.BASE_ALLOCATE_COUNT + maxSegments);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\t\t\tthis.ownsFirstBlock = true;\n\n\t\t\tthis.initialize(config);\n\t\t}\n\n\t\tthis.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + GROW_LOCK_INDEX * Uint32Array.BYTES_PER_ELEMENT, 1);\n\t\tthis.cachedBaseSegmentLength = this.baseSegmentLength;\n\t\tthis.maxLength = this.cachedBaseSegmentLength * (2 ** this.maxSegments - 1);\n\t\tif(this.maxLength > MAX_COUNT) {\n\t\t\t// The length word reserves LEN_BITS for the count and the rest for the ABA version tag\n\t\t\tthrow new Error(`SharedStack maxLength ${this.maxLength} exceeds the ${MAX_COUNT} the versioned length counter can address`);\n\t\t}\n\t}\n\n\tprivate initialize(config?: SharedStackConfig<T>) {\n\t\tlet baseSegmentLength = config?.baseSegmentLength ?? DEFAULT_BASE_SEGMENT_LENGTH;\n\t\t// Each segment block holds the data slots followed by an equal-length region of publish sequences\n\t\tlet firstSegmentBlock = this.memory.allocUI32(baseSegmentLength * 2);\n\t\tstorePointer(this.firstBlock.data, SEGMENT_START_INDEX, firstSegmentBlock.bufferPosition, firstSegmentBlock.bufferByteOffset);\n\t\tthis.baseSegmentLength = baseSegmentLength;\n\t\tthis.maxSegments = config?.segmentCount ?? DEFAULT_MAX_SEGMENTS;\n\t\tthis.segmentCount = 1;\n\n\t\tconst type = config?.type ?? Uint32Array;\n\t\tif(type === Uint32Array) {\n\t\t\tthis.type = TYPE.uint32;\n\t\t// @ts-expect-error\n\t\t} else if(type === Int32Array) {\n\t\t\tthis.type = TYPE.int32;\n\t\t// @ts-expect-error\n\t\t} else if(type === Float32Array) {\n\t\t\tthis.type = TYPE.float32;\n\t\t}\n\t}\n\n\tat(index: number): number {\n\t\tlet length = this.length;\n\t\tif(index >= length || index < 0) {\n\t\t\tthrow new Error(`${index} is out of bounds ${length}`);\n\t\t}\n\n\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\tlet segmentIndex = 31 - Math.clz32(((index / baseSegmentLength) | 0) + 1);\n\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\treturn segment[index - baseSegmentLength * ((1 << segmentIndex) - 1)];\n\t}\n\n\tpush(value: number): number {\n\t\tlet data = this.firstBlock.data;\n\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\t// Plain (relaxed) read is enough: it only seeds the versioned CAS below, which rejects any stale value\n\t\t\tlet packed = data[LENGTH_INDEX];\n\t\t\tlet newIndex = packed & LEN_MASK;\n\t\t\tif(newIndex >= this.maxLength) {\n\t\t\t\tthrow new Error(`${newIndex + 1} is out of bounds ${this.maxLength}`);\n\t\t\t}\n\n\t\t\tlet segmentIndex = 31 - Math.clz32(((newIndex / baseSegmentLength) | 0) + 1);\n\t\t\tlet localIndex = newIndex - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\t\tlet sequences = this.cachedSequences[segmentIndex];\n\n\t\t\t// The slot must be empty (even sequence) to fill. Odd means a popper hasn't finished emptying it yet - retry\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) !== 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// Take the slot only if the whole versioned length word is unchanged. The version bump makes this fail on any\n\t\t\t// intervening op, so a win guarantees nothing touched the top - and our sequence read above is still valid\n\t\t\tif(Atomics.compareExchange(data, LENGTH_INDEX, packed, packed + VERSION_STEP + 1) !== packed) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tsegment[localIndex] = value;\n\t\t\t// Publish: bump to odd (full). Safe as a plain store - no other op can claim this slot until the length comes\n\t\t\t// back down to it, which can't happen (its popper is gated on an odd sequence) until this store lands\n\t\t\tAtomics.store(sequences, localIndex, sequence + 1);\n\t\t\treturn newIndex;\n\t\t}\n\t}\n\n\tpop(): number | undefined {\n\t\tlet data = this.firstBlock.data;\n\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\t// Plain (relaxed) read is enough: it only seeds the versioned CAS below, which rejects any stale value\n\t\t\tlet packed = data[LENGTH_INDEX];\n\t\t\tlet length = packed & LEN_MASK;\n\t\t\tif(length <= 0) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tlet oldIndex = length - 1;\n\n\t\t\tlet segmentIndex = 31 - Math.clz32(((oldIndex / baseSegmentLength) | 0) + 1);\n\t\t\tlet localIndex = oldIndex - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\t\tlet sequences = this.cachedSequences[segmentIndex];\n\n\t\t\t// The slot must be full (odd sequence) to read. Even means a pusher hasn't finished publishing it yet - retry\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) === 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(Atomics.compareExchange(data, LENGTH_INDEX, packed, packed + VERSION_STEP - 1) !== packed) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet value = segment[localIndex];\n\t\t\t// Release the slot: bump to even (empty) so a later push may reuse it\n\t\t\tAtomics.store(sequences, localIndex, sequence + 1);\n\t\t\treturn value;\n\t\t}\n\t}\n\n\tclear() {\n\t\t// Mark every occupied slot empty (bump its odd sequence to the next even) before dropping the length, otherwise a\n\t\t// later push reusing the slot would spin forever on a stale full sequence. Not safe to call concurrently with push/pop.\n\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\tlet oldLength = Atomics.load(this.firstBlock.data, LENGTH_INDEX) & LEN_MASK;\n\t\tfor(let i = 0; i < oldLength; i++) {\n\t\t\tlet segmentIndex = 31 - Math.clz32(((i / baseSegmentLength) | 0) + 1);\n\t\t\tlet sequences = this.cachedSequences[segmentIndex] ?? (this.getSegment(segmentIndex), this.cachedSequences[segmentIndex]);\n\t\t\tlet localIndex = i - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) !== 0) {\n\t\t\t\tAtomics.store(sequences, localIndex, sequence + 1);\n\t\t\t}\n\t\t}\n\t\tAtomics.store(this.firstBlock.data, LENGTH_INDEX, 0);\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet totalDataEntries = this.length;\n\n\t\tlet segmentLength = this.baseSegmentLength;\n\t\tlet segmentIndex = 0;\n\t\tlet segment = this.getSegment(segmentIndex);\n\t\tlet segmentDataIndex = 0;\n\t\tfor(let i = 0; i < totalDataEntries; i++) {\n\t\t\tyield segment[segmentDataIndex];\n\t\t\tsegmentDataIndex++;\n\t\t\tif(segmentDataIndex >= segmentLength) {\n\t\t\t\tsegmentIndex++;\n\t\t\t\tsegment = this.getSegment(segmentIndex);\n\t\t\t\tsegmentDataIndex = 0;\n\t\t\t\tsegmentLength = segmentLength * 2;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getSegment(segmentIndex: number): T {\n\t\tif(this.cachedSegments[segmentIndex]) {\n\t\t\treturn this.cachedSegments[segmentIndex];\n\t\t}\n\n\t\tlet segmentLength = this.cachedBaseSegmentLength * 2 ** segmentIndex;\n\t\tlet segmentDataBlock: AllocatedMemory | undefined;\n\t\tif(segmentIndex >= this.segmentCount) {\n\t\t\t// Lock when growing so another thread doesn't also setup the same segment\n\t\t\tlock(this.growLock);\n\t\t\ttry {\n\t\t\t\t// Re-check under the lock: another thread may have already appended the segment while we were waiting\n\t\t\t\tif(segmentIndex >= this.segmentCount) {\n\t\t\t\t\t// Double length: the data slots followed by their publish sequences (calloc-zeroed = 0 = empty)\n\t\t\t\t\tsegmentDataBlock = this.memory.allocUI32(segmentLength * 2);\n\t\t\t\t\tstorePointer(this.firstBlock.data, SEGMENT_START_INDEX + segmentIndex, segmentDataBlock.bufferPosition, segmentDataBlock.bufferByteOffset);\n\t\t\t\t\tAtomics.add(this.firstBlock.data, SEGMENT_COUNT_INDEX, 1);\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tunlock(this.growLock);\n\t\t\t}\n\t\t}\n\n\t\tif(!segmentDataBlock) {\n\t\t\tlet pointerNumber = Atomics.load(this.firstBlock.data, SEGMENT_START_INDEX + segmentIndex);\n\t\t\tlet pointer = getPointer(pointerNumber);\n\t\t\tsegmentDataBlock = new AllocatedMemory(this.memory, pointer);\n\t\t}\n\n\t\tlet data: T;\n\t\tswitch(this.type) {\n\t\t\tcase TYPE.int32:\n\t\t\t\tdata = new Int32Array(segmentDataBlock.data.buffer, segmentDataBlock.bufferByteOffset, segmentLength) as T;\n\t\t\t\tbreak;\n\t\t\tcase TYPE.uint32:\n\t\t\t\tdata = new Uint32Array(segmentDataBlock.data.buffer, segmentDataBlock.bufferByteOffset, segmentLength) as T;\n\t\t\t\tbreak;\n\t\t\tcase TYPE.float32:\n\t\t\t\tdata = new Float32Array(segmentDataBlock.data.buffer, segmentDataBlock.bufferByteOffset, segmentLength) as T;\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tthrow new Error(`Unknown data block type ${this.type}`);\n\t\t}\n\t\t// Publish sequences live in the second half of the block, always as Int32 regardless of the data type\n\t\tlet sequenceByteOffset = segmentDataBlock.bufferByteOffset + segmentLength * Uint32Array.BYTES_PER_ELEMENT;\n\t\tthis.cachedSequences[segmentIndex] = new Int32Array(segmentDataBlock.data.buffer, sequenceByteOffset, segmentLength);\n\t\tthis.cachedSegments[segmentIndex] = data;\n\n\t\treturn data;\n\t}\n\n\tfree() {\n\t\tfor(let i = 0; i < this.segmentCount; i++) {\n\t\t\tlet pointerNumber = Atomics.load(this.firstBlock.data, SEGMENT_START_INDEX + i);\n\t\t\tlet pointer = getPointer(pointerNumber);\n\t\t\tlet rawData = new AllocatedMemory(this.memory, pointer);\n\t\t\trawData.free();\n\t\t}\n\t\tif(this.ownsFirstBlock) {\n\t\t\tthis.firstBlock.free();\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedStackMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.firstBlock.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedStackConfig<T extends Uint32Array | Int32Array | Float32Array> {\n\ttype?: TypedArrayConstructor<T>\n\n\tbaseSegmentLength?: number\n\tsegmentCount?: number\n}\ninterface SharedStackMemory {\n\tfirstBlock: SharedAllocatedMemory | AllocatedMemory\n}\n\nexport type { SharedStackConfig, SharedStackMemory };"],"mappings":";;;;AAaA,IAAM,IAAe,GACf,IAAa,GACb,IAA4B,GAC5B,IAAqB,GACrB,IAAsB,GACtB,IAAkB,GAClB,IAAsB,GAEtB,IAA8B,GAC9B,IAAuB,IAMvB,IAAW,IACX,KAAY,KAAK,KAAY,GAC7B,IAAY,SAIZ,IAAe,KAAK,GAOL,IAArB,MAAqB,EAAyG;CAC7H,OAAgB,sBAAsB;CACtC,OAAgB,yBAAyB,KAAK,sBAAsB;CACpE;CAGA;CACA;CACA;CACA;CAGA;CAEA,iBAA8B,CAAC;CAG/B,kBAAwC,CAAC;CACzC,0BAAkC;CAElC,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAAK,KAAK,WAAW,MAAM,CAAY,IAAI;CAC3D;CACA,IAAI,eAAuB;EAC1B,OAAO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K,MAAM;EACxD;EAEA,IAAI,IAAqB,EAAiB,mBAAmB,IAAgB,YAAY;EAIzF,OAHA,KAAK,gBAAgB,KAAgB,IAAI,WAAW,EAAiB,KAAK,QAAQ,GAAoB,CAAa,GACnH,KAAK,eAAe,KAAgB,GAE7B;CACR;CAEA,OAAO;EACN,KAAI,IAAI,IAAI,GAAG,IAAI,KAAK,cAAc,KAAK;GAC1C,IAAI,IAAgB,QAAQ,KAAK,KAAK,WAAW,MAAM,IAAsB,CAAC,GAC1E,IAAU,EAAW,CAAa;GAEtC,IADkB,EAAgB,KAAK,QAAQ,CAC/C,CAAA,CAAQ,KAAK;EACd;EACA,AAAG,KAAK,kBACP,KAAK,WAAW,KAAK;CAEvB;CAEA,kBAAqC;EACpC,OAAO,EACN,YAAY,KAAK,WAAW,gBAAgB,EAC7C;CACD;AACD"}
@@ -1,5 +1,5 @@
1
- import { SharedAllocatedMemory } from './allocated-memory';
2
- import { 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 SharedString {
4
4
  static readonly ALLOCATE_COUNT = 4;
5
5
  private memory;
@@ -1,79 +1,73 @@
1
- import E from "./allocated-memory.js";
2
- import { lock as f, unlock as h } from "./lock/simple-lock.js";
3
- import { storeRawPointer as p, loadRawPointer as S, getPointer as A, loadPointer as d } from "./utils/pointer.js";
4
- const y = {
5
- 1: Uint8Array,
6
- 2: Uint16Array
7
- }, c = 0, m = 1, g = 2, u = 3;
8
- class M {
9
- static ALLOCATE_COUNT = 4;
10
- memory;
11
- allocatedMemory;
12
- lock;
13
- cachedPointer;
14
- cachedString;
15
- constructor(t, e) {
16
- this.memory = t, typeof e == "string" ? (this.allocatedMemory = this.memory.allocUI32(M.ALLOCATE_COUNT), this.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + u * this.allocatedMemory.data.BYTES_PER_ELEMENT), this.updateString(e)) : (this.allocatedMemory = new E(t, e), this.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + u * this.allocatedMemory.data.BYTES_PER_ELEMENT), "value" in e && this.updateString(e.value));
17
- }
18
- updateString(t) {
19
- let e = this.createString(t);
20
- f(this.lock), p(this.allocatedMemory.data, c, e.pointer), Atomics.store(this.allocatedMemory.data, m, t.length), Atomics.store(this.allocatedMemory.data, g, e.charType), h(this.lock), this.cachedPointer = e.pointer, this.cachedString = t;
21
- }
22
- createString(t) {
23
- if (t === "")
24
- return {
25
- pointer: 0,
26
- charType: 1
27
- /* ASCII */
28
- };
29
- let e = 0;
30
- for (let o = 0; o < t.length; o++) {
31
- let i = t.charCodeAt(o);
32
- i > e && (e = i);
33
- }
34
- let r = e > 255 ? 2 : 1, s = y[r], a = this.memory.allocUI32(Math.ceil(t.length / (4 / s.BYTES_PER_ELEMENT))), n = new s(a.data.buffer, a.data.byteOffset, t.length);
35
- for (let o = 0; o < t.length; o++)
36
- n[o] = t.charCodeAt(o);
37
- return {
38
- pointer: a.pointer,
39
- charType: r
40
- };
41
- }
42
- get value() {
43
- let t = S(this.allocatedMemory.data, c);
44
- if (this.cachedPointer === t && this.cachedString !== void 0)
45
- return this.cachedString;
46
- if (t === 0)
47
- return "";
48
- let { bufferPosition: e, bufferByteOffset: r } = A(t);
49
- f(this.lock);
50
- let s = Atomics.load(this.allocatedMemory.data, g), a = y[s], n = Atomics.load(this.allocatedMemory.data, m), o = new a(this.memory.buffers[e].buf, r, n), i = C(o);
51
- return h(this.lock), this.cachedPointer = t, this.cachedString = i, i;
52
- }
53
- set value(t) {
54
- let { bufferPosition: e, bufferByteOffset: r } = d(this.allocatedMemory.data, c);
55
- this.updateString(t), r && this.memory.buffers[e].free(r);
56
- }
57
- getSharedMemory() {
58
- return this.allocatedMemory.getSharedMemory();
59
- }
60
- get pointer() {
61
- return this.allocatedMemory.pointer;
62
- }
63
- free() {
64
- let { bufferPosition: t, bufferByteOffset: e } = d(this.allocatedMemory.data, c);
65
- e && this.memory.buffers[t].free(e), this.allocatedMemory.free();
66
- }
67
- }
68
- function C(l) {
69
- if (l.length <= 8192)
70
- return String.fromCharCode.apply(null, l);
71
- let e = "";
72
- for (let r = 0; r < l.length; r += 8192)
73
- e += String.fromCharCode.apply(null, l.subarray(r, r + 8192));
74
- return e;
75
- }
76
- export {
77
- M as default
1
+ import { getPointer as e, loadPointer as t, loadRawPointer as n, storeRawPointer as r } from "./utils/pointer.js";
2
+ import i from "./allocated-memory.js";
3
+ import { lock as a, unlock as o } from "./lock/simple-lock.js";
4
+ //#region src/shared-string.ts
5
+ var s = {
6
+ 1: Uint8Array,
7
+ 2: Uint16Array
8
+ }, c = 0, l = 1, u = 2, d = 3, f = class f {
9
+ static ALLOCATE_COUNT = 4;
10
+ memory;
11
+ allocatedMemory;
12
+ lock;
13
+ cachedPointer;
14
+ cachedString;
15
+ constructor(e, t) {
16
+ this.memory = e, typeof t == "string" ? (this.allocatedMemory = this.memory.allocUI32(f.ALLOCATE_COUNT), this.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + d * this.allocatedMemory.data.BYTES_PER_ELEMENT), this.updateString(t)) : (this.allocatedMemory = new i(e, t), this.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + d * this.allocatedMemory.data.BYTES_PER_ELEMENT), "value" in t && this.updateString(t.value));
17
+ }
18
+ updateString(e) {
19
+ let t = this.createString(e);
20
+ a(this.lock), r(this.allocatedMemory.data, c, t.pointer), Atomics.store(this.allocatedMemory.data, l, e.length), Atomics.store(this.allocatedMemory.data, u, t.charType), o(this.lock), this.cachedPointer = t.pointer, this.cachedString = e;
21
+ }
22
+ createString(e) {
23
+ if (e === "") return {
24
+ pointer: 0,
25
+ charType: 1
26
+ };
27
+ let t = 0;
28
+ for (let n = 0; n < e.length; n++) {
29
+ let r = e.charCodeAt(n);
30
+ r > t && (t = r);
31
+ }
32
+ let n = t > 255 ? 2 : 1, r = s[n], i = this.memory.allocUI32(Math.ceil(e.length / (4 / r.BYTES_PER_ELEMENT))), a = new r(i.data.buffer, i.data.byteOffset, e.length);
33
+ for (let t = 0; t < e.length; t++) a[t] = e.charCodeAt(t);
34
+ return {
35
+ pointer: i.pointer,
36
+ charType: n
37
+ };
38
+ }
39
+ get value() {
40
+ let t = n(this.allocatedMemory.data, c);
41
+ if (this.cachedPointer === t && this.cachedString !== void 0) return this.cachedString;
42
+ if (t === 0) return "";
43
+ let { bufferPosition: r, bufferByteOffset: i } = e(t);
44
+ a(this.lock);
45
+ let d = s[Atomics.load(this.allocatedMemory.data, u)], f = Atomics.load(this.allocatedMemory.data, l), m = p(new d(this.memory.buffers[r].buf, i, f));
46
+ return o(this.lock), this.cachedPointer = t, this.cachedString = m, m;
47
+ }
48
+ set value(e) {
49
+ let { bufferPosition: n, bufferByteOffset: r } = t(this.allocatedMemory.data, c);
50
+ this.updateString(e), r && this.memory.buffers[n].free(r);
51
+ }
52
+ getSharedMemory() {
53
+ return this.allocatedMemory.getSharedMemory();
54
+ }
55
+ get pointer() {
56
+ return this.allocatedMemory.pointer;
57
+ }
58
+ free() {
59
+ let { bufferPosition: e, bufferByteOffset: n } = t(this.allocatedMemory.data, c);
60
+ n && this.memory.buffers[e].free(n), this.allocatedMemory.free();
61
+ }
78
62
  };
79
- //# sourceMappingURL=shared-string.js.map
63
+ function p(e) {
64
+ let t = 8192;
65
+ if (e.length <= t) return String.fromCharCode.apply(null, e);
66
+ let n = "";
67
+ for (let r = 0; r < e.length; r += t) n += String.fromCharCode.apply(null, e.subarray(r, r + t));
68
+ return n;
69
+ }
70
+ //#endregion
71
+ export { f as default };
72
+
73
+ //# sourceMappingURL=shared-string.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"shared-string.js","sources":["../src/shared-string.ts"],"sourcesContent":["import AllocatedMemory from './allocated-memory';\nimport type { SharedAllocatedMemory } from './allocated-memory';\nimport { lock, unlock } from './lock/simple-lock';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, loadPointer, loadRawPointer, storeRawPointer } from './utils/pointer';\n\nenum CHAR_TYPE {\n\tUNDEFINED,\n\tASCII,\n\tUTF16\n}\nconst TYPED_ARRAY_MAP = {\n\t[CHAR_TYPE.ASCII]: Uint8Array,\n\t[CHAR_TYPE.UTF16]: Uint16Array\n};\n\nconst POINTER_INDEX = 0;\nconst LENGTH_INDEX = 1;\nconst TYPE_INDEX = 2;\nconst LOCK_INDEX = 3;\nexport default class SharedString {\n\tstatic readonly ALLOCATE_COUNT = 4;\n\n\tprivate memory: MemoryHeap;\n\tprivate allocatedMemory: AllocatedMemory;\n\tprivate lock: Int32Array;\n\n\tprivate cachedPointer?: number;\n\tprivate cachedString?: string;\n\t\n\tconstructor(memory: MemoryHeap, value: string | SharedStringConfig | SharedStringMemory) {\n\t\tthis.memory = memory;\n\n\t\tif(typeof value === 'string') {\n\t\t\tthis.allocatedMemory = this.memory.allocUI32(SharedString.ALLOCATE_COUNT);\n\t\t\tthis.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + LOCK_INDEX * this.allocatedMemory.data.BYTES_PER_ELEMENT);\n\t\t\tthis.updateString(value);\n\t\t} else {\n\t\t\tthis.allocatedMemory = new AllocatedMemory(memory, value);\n\t\t\tthis.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + LOCK_INDEX * this.allocatedMemory.data.BYTES_PER_ELEMENT);\n\n\t\t\t// We only allocated memory but didn't initialize the string yet\n\t\t\tif('value' in value) {\n\t\t\t\tthis.updateString(value.value);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tprivate updateString(value: string) {\n\t\tlet stringMemory = this.createString(value);\n\n\t\tlock(this.lock);\n\t\tstoreRawPointer(this.allocatedMemory.data, POINTER_INDEX, stringMemory.pointer);\n\t\tAtomics.store(this.allocatedMemory.data, LENGTH_INDEX, value.length);\n\t\tAtomics.store(this.allocatedMemory.data, TYPE_INDEX, stringMemory.charType);\n\t\tunlock(this.lock);\n\n\t\tthis.cachedPointer = stringMemory.pointer;\n\t\tthis.cachedString = value;\n\t}\n\tprivate createString(value: string) {\n\t\tif(value === '') {\n\t\t\treturn {\n\t\t\t\tpointer: 0,\n\t\t\t\tcharType: CHAR_TYPE.ASCII\n\t\t\t};\n\t\t}\n\n\t\t// NOTE: Scan with a loop instead of Math.max(...charCodes) which blows the stack for large strings\n\t\tlet maxCharCode = 0;\n\t\tfor(let i = 0; i < value.length; i++) {\n\t\t\tlet charCode = value.charCodeAt(i);\n\t\t\tif(charCode > maxCharCode) {\n\t\t\t\tmaxCharCode = charCode;\n\t\t\t}\n\t\t}\n\t\tlet charType = maxCharCode > 255 ? CHAR_TYPE.UTF16 : CHAR_TYPE.ASCII;\n\n\t\tlet typedArray = TYPED_ARRAY_MAP[charType];\n\t\tlet allocatedMemory = this.memory.allocUI32(Math.ceil(value.length / (4 / typedArray.BYTES_PER_ELEMENT)));\n\t\tlet data = new typedArray(allocatedMemory.data.buffer as ArrayBuffer, allocatedMemory.data.byteOffset, value.length);\n\t\tfor(let i = 0; i < value.length; i++) {\n\t\t\tdata[i] = value.charCodeAt(i);\n\t\t}\n\n\t\treturn {\n\t\t\tpointer: allocatedMemory.pointer,\n\t\t\tcharType\n\t\t};\n\t}\n\n\tget value(): string {\n\t\tlet pointer = loadRawPointer(this.allocatedMemory.data, POINTER_INDEX);\n\t\tif(this.cachedPointer === pointer && this.cachedString !== undefined) {\n\t\t\treturn this.cachedString;\n\t\t} else if(pointer === 0) {\n\t\t\treturn '';\n\t\t}\n\t\tlet { bufferPosition, bufferByteOffset } = getPointer(pointer);\n\n\t\tlock(this.lock);\n\t\tlet charType = Atomics.load(this.allocatedMemory.data, TYPE_INDEX) as number;\n\t\t// @ts-expect-error\n\t\tlet typedArray = TYPED_ARRAY_MAP[charType];\n\t\tlet bufferLength = Atomics.load(this.allocatedMemory.data, LENGTH_INDEX);\n\n\t\tlet data = new typedArray(this.memory.buffers[bufferPosition].buf, bufferByteOffset, bufferLength);\n\t\tlet string = fromCharCodes(data);\n\t\t// NOTE: Do not unlock until after transforming the data since the second this is done it can free that memory block\n\t\tunlock(this.lock);\n\n\t\tthis.cachedPointer = pointer;\n\t\tthis.cachedString = string;\n\n\t\treturn string;\n\t}\n\tset value(value: string) {\n\t\tlet { bufferPosition: oldBufferPosition, bufferByteOffset: oldBufferByteOffset } = loadPointer(this.allocatedMemory.data, POINTER_INDEX);\n\t\tthis.updateString(value);\n\n\t\tif(oldBufferByteOffset) {\n\t\t\tthis.memory.buffers[oldBufferPosition].free(oldBufferByteOffset);\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedStringMemory {\n\t\treturn this.allocatedMemory.getSharedMemory();\n\t}\n\n\tget pointer() {\n\t\treturn this.allocatedMemory.pointer;\n\t}\n\n\tfree() {\n\t\tlet { bufferPosition, bufferByteOffset } = loadPointer(this.allocatedMemory.data, POINTER_INDEX);\n\t\tif(bufferByteOffset) {\n\t\t\tthis.memory.buffers[bufferPosition].free(bufferByteOffset);\n\t\t}\n\t\tthis.allocatedMemory.free();\n\t}\n}\n\n// String.fromCharCode.apply throws a RangeError (Maximum call stack size exceeded) when handed too many\n// arguments at once, so decode large buffers in chunks to support arbitrarily long strings\nfunction fromCharCodes(data: Uint8Array | Uint16Array): string {\n\tconst CHUNK_SIZE = 8192;\n\tif(data.length <= CHUNK_SIZE) {\n\t\treturn String.fromCharCode.apply(null, data as unknown as Array<number>);\n\t}\n\n\tlet string = '';\n\tfor(let i = 0; i < data.length; i += CHUNK_SIZE) {\n\t\tstring += String.fromCharCode.apply(null, data.subarray(i, i + CHUNK_SIZE) as unknown as Array<number>);\n\t}\n\n\treturn string;\n}\n\ninterface SharedStringConfig extends SharedStringMemory {\n\tvalue: string\n}\ntype SharedStringMemory = SharedAllocatedMemory;"],"names":["TYPED_ARRAY_MAP","POINTER_INDEX","LENGTH_INDEX","TYPE_INDEX","LOCK_INDEX","SharedString","memory","value","AllocatedMemory","stringMemory","lock","storeRawPointer","unlock","maxCharCode","i","charCode","charType","typedArray","allocatedMemory","data","pointer","loadRawPointer","bufferPosition","bufferByteOffset","getPointer","bufferLength","string","fromCharCodes","oldBufferPosition","oldBufferByteOffset","loadPointer"],"mappings":";;;AAWA,MAAMA,IAAkB;AAAA,EACtB,GAAkB;AAAA,EAClB,GAAkB;AACpB,GAEMC,IAAgB,GAChBC,IAAe,GACfC,IAAa,GACbC,IAAa;AACnB,MAAqBC,EAAa;AAAA,EACjC,OAAgB,iBAAiB;AAAA,EAEzB;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,EAER,YAAYC,GAAoBC,GAAyD;AACxF,SAAK,SAASD,GAEX,OAAOC,KAAU,YACnB,KAAK,kBAAkB,KAAK,OAAO,UAAUF,EAAa,cAAc,GACxE,KAAK,OAAO,IAAI,WAAW,KAAK,gBAAgB,KAAK,QAAQ,KAAK,gBAAgB,mBAAmBD,IAAa,KAAK,gBAAgB,KAAK,iBAAiB,GAC7J,KAAK,aAAaG,CAAK,MAEvB,KAAK,kBAAkB,IAAIC,EAAgBF,GAAQC,CAAK,GACxD,KAAK,OAAO,IAAI,WAAW,KAAK,gBAAgB,KAAK,QAAQ,KAAK,gBAAgB,mBAAmBH,IAAa,KAAK,gBAAgB,KAAK,iBAAiB,GAG1J,WAAWG,KACb,KAAK,aAAaA,EAAM,KAAK;AAAA,EAGhC;AAAA,EAEQ,aAAaA,GAAe;AACnC,QAAIE,IAAe,KAAK,aAAaF,CAAK;AAE1C,IAAAG,EAAK,KAAK,IAAI,GACdC,EAAgB,KAAK,gBAAgB,MAAMV,GAAeQ,EAAa,OAAO,GAC9E,QAAQ,MAAM,KAAK,gBAAgB,MAAMP,GAAcK,EAAM,MAAM,GACnE,QAAQ,MAAM,KAAK,gBAAgB,MAAMJ,GAAYM,EAAa,QAAQ,GAC1EG,EAAO,KAAK,IAAI,GAEhB,KAAK,gBAAgBH,EAAa,SAClC,KAAK,eAAeF;AAAA,EACrB;AAAA,E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KAAK,IAAI,GAEhB,KAAK,gBAAgBQ,GACrB,KAAK,eAAeM,GAEbA;AAAA,EACR;AAAA,EACA,IAAI,MAAMnB,GAAe;AACxB,QAAI,EAAE,gBAAgBqB,GAAmB,kBAAkBC,EAAA,IAAwBC,EAAY,KAAK,gBAAgB,MAAM7B,CAAa;AACvI,SAAK,aAAaM,CAAK,GAEpBsB,KACF,KAAK,OAAO,QAAQD,CAAiB,EAAE,KAAKC,CAAmB;AAAA,EAEjE;AAAA,EAEA,kBAAsC;AACrC,WAAO,KAAK,gBAAgB,gBAAA;AAAA,EAC7B;AAAA,EAEA,IAAI,UAAU;AACb,WAAO,KAAK,gBAAgB;AAAA,EAC7B;AAAA,EAEA,OAAO;AACN,QAAI,EAAE,gBAAAP,GAAgB,kBAAAC,MAAqBO,EAAY,KAAK,gBAAgB,MAAM7B,CAAa;AAC/F,IAAGsB,KACF,KAAK,OAAO,QAAQD,CAAc,EAAE,KAAKC,CAAgB,GAE1D,KAAK,gBAAgB,KAAA;AAAA,EACtB;AACD;AAIA,SAASI,EAAcR,GAAwC;AAE9D,MAAGA,EAAK,UAAU;AACjB,WAAO,OAAO,aAAa,MAAM,MAAMA,CAAgC;AAGxE,MAAIO,IAAS;AACb,WAAQZ,IAAI,GAAGA,IAAIK,EAAK,QAAQL,KAAK;AACpC,IAAAY,KAAU,OAAO,aAAa,MAAM,MAAMP,EAAK,SAASL,GAAGA,IAAI,IAAU,CAA6B;AAGvG,SAAOY;AACR;"}
1
+ {"version":3,"file":"shared-string.js","names":[],"sources":["../src/shared-string.ts"],"sourcesContent":["import AllocatedMemory from './allocated-memory';\nimport type { SharedAllocatedMemory } from './allocated-memory';\nimport { lock, unlock } from './lock/simple-lock';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, loadPointer, loadRawPointer, storeRawPointer } from './utils/pointer';\n\nenum CHAR_TYPE {\n\tUNDEFINED,\n\tASCII,\n\tUTF16,\n}\nconst TYPED_ARRAY_MAP = {\n\t[CHAR_TYPE.ASCII]: Uint8Array,\n\t[CHAR_TYPE.UTF16]: Uint16Array,\n};\n\nconst POINTER_INDEX = 0;\nconst LENGTH_INDEX = 1;\nconst TYPE_INDEX = 2;\nconst LOCK_INDEX = 3;\nexport default class SharedString {\n\tstatic readonly ALLOCATE_COUNT = 4;\n\n\tprivate memory: MemoryHeap;\n\tprivate allocatedMemory: AllocatedMemory;\n\tprivate lock: Int32Array;\n\n\tprivate cachedPointer?: number;\n\tprivate cachedString?: string;\n\t\n\tconstructor(memory: MemoryHeap, value: string | SharedStringConfig | SharedStringMemory) {\n\t\tthis.memory = memory;\n\n\t\tif(typeof value === 'string') {\n\t\t\tthis.allocatedMemory = this.memory.allocUI32(SharedString.ALLOCATE_COUNT);\n\t\t\tthis.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + LOCK_INDEX * this.allocatedMemory.data.BYTES_PER_ELEMENT);\n\t\t\tthis.updateString(value);\n\t\t} else {\n\t\t\tthis.allocatedMemory = new AllocatedMemory(memory, value);\n\t\t\tthis.lock = new Int32Array(this.allocatedMemory.data.buffer, this.allocatedMemory.bufferByteOffset + LOCK_INDEX * this.allocatedMemory.data.BYTES_PER_ELEMENT);\n\n\t\t\t// We only allocated memory but didn't initialize the string yet\n\t\t\tif('value' in value) {\n\t\t\t\tthis.updateString(value.value);\n\t\t\t}\n\t\t}\n\t}\n\t\n\tprivate updateString(value: string) {\n\t\tlet stringMemory = this.createString(value);\n\n\t\tlock(this.lock);\n\t\tstoreRawPointer(this.allocatedMemory.data, POINTER_INDEX, stringMemory.pointer);\n\t\tAtomics.store(this.allocatedMemory.data, LENGTH_INDEX, value.length);\n\t\tAtomics.store(this.allocatedMemory.data, TYPE_INDEX, stringMemory.charType);\n\t\tunlock(this.lock);\n\n\t\tthis.cachedPointer = stringMemory.pointer;\n\t\tthis.cachedString = value;\n\t}\n\tprivate createString(value: string) {\n\t\tif(value === '') {\n\t\t\treturn {\n\t\t\t\tpointer: 0,\n\t\t\t\tcharType: CHAR_TYPE.ASCII,\n\t\t\t};\n\t\t}\n\n\t\t// NOTE: Scan with a loop instead of Math.max(...charCodes) which blows the stack for large strings\n\t\tlet maxCharCode = 0;\n\t\tfor(let i = 0; i < value.length; i++) {\n\t\t\tlet charCode = value.charCodeAt(i);\n\t\t\tif(charCode > maxCharCode) {\n\t\t\t\tmaxCharCode = charCode;\n\t\t\t}\n\t\t}\n\t\tlet charType = maxCharCode > 255 ? CHAR_TYPE.UTF16 : CHAR_TYPE.ASCII;\n\n\t\tlet typedArray = TYPED_ARRAY_MAP[charType];\n\t\tlet allocatedMemory = this.memory.allocUI32(Math.ceil(value.length / (4 / typedArray.BYTES_PER_ELEMENT)));\n\t\tlet data = new typedArray(allocatedMemory.data.buffer as ArrayBuffer, allocatedMemory.data.byteOffset, value.length);\n\t\tfor(let i = 0; i < value.length; i++) {\n\t\t\tdata[i] = value.charCodeAt(i);\n\t\t}\n\n\t\treturn {\n\t\t\tpointer: allocatedMemory.pointer,\n\t\t\tcharType,\n\t\t};\n\t}\n\n\tget value(): string {\n\t\tlet pointer = loadRawPointer(this.allocatedMemory.data, POINTER_INDEX);\n\t\tif(this.cachedPointer === pointer && this.cachedString !== undefined) {\n\t\t\treturn this.cachedString;\n\t\t} else if(pointer === 0) {\n\t\t\treturn '';\n\t\t}\n\t\tlet { bufferPosition, bufferByteOffset } = getPointer(pointer);\n\n\t\tlock(this.lock);\n\t\tlet charType = Atomics.load(this.allocatedMemory.data, TYPE_INDEX);\n\t\t// @ts-expect-error\n\t\tlet typedArray = TYPED_ARRAY_MAP[charType];\n\t\tlet bufferLength = Atomics.load(this.allocatedMemory.data, LENGTH_INDEX);\n\n\t\tlet data = new typedArray(this.memory.buffers[bufferPosition].buf, bufferByteOffset, bufferLength);\n\t\tlet string = fromCharCodes(data);\n\t\t// NOTE: Do not unlock until after transforming the data since the second this is done it can free that memory block\n\t\tunlock(this.lock);\n\n\t\tthis.cachedPointer = pointer;\n\t\tthis.cachedString = string;\n\n\t\treturn string;\n\t}\n\tset value(value: string) {\n\t\tlet { bufferPosition: oldBufferPosition, bufferByteOffset: oldBufferByteOffset } = loadPointer(this.allocatedMemory.data, POINTER_INDEX);\n\t\tthis.updateString(value);\n\n\t\tif(oldBufferByteOffset) {\n\t\t\tthis.memory.buffers[oldBufferPosition].free(oldBufferByteOffset);\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedStringMemory {\n\t\treturn this.allocatedMemory.getSharedMemory();\n\t}\n\n\tget pointer() {\n\t\treturn this.allocatedMemory.pointer;\n\t}\n\n\tfree() {\n\t\tlet { bufferPosition, bufferByteOffset } = loadPointer(this.allocatedMemory.data, POINTER_INDEX);\n\t\tif(bufferByteOffset) {\n\t\t\tthis.memory.buffers[bufferPosition].free(bufferByteOffset);\n\t\t}\n\t\tthis.allocatedMemory.free();\n\t}\n}\n\n// String.fromCharCode.apply throws a RangeError (Maximum call stack size exceeded) when handed too many\n// arguments at once, so decode large buffers in chunks to support arbitrarily long strings\nfunction fromCharCodes(data: Uint8Array | Uint16Array): string {\n\tconst CHUNK_SIZE = 8192;\n\tif(data.length <= CHUNK_SIZE) {\n\t\treturn String.fromCharCode.apply(null, data as unknown as Array<number>);\n\t}\n\n\tlet string = '';\n\tfor(let i = 0; i < data.length; i += CHUNK_SIZE) {\n\t\tstring += String.fromCharCode.apply(null, data.subarray(i, i + CHUNK_SIZE) as unknown as Array<number>);\n\t}\n\n\treturn string;\n}\n\ninterface SharedStringConfig extends SharedStringMemory {\n\tvalue: string\n}\ntype SharedStringMemory = SharedAllocatedMemory;"],"mappings":";;;;AAWA,IAAM,IAAkB;CACJ,GAAA;CACA,GAAA;AACpB,GAEM,IAAgB,GAChB,IAAe,GACf,IAAa,GACb,IAAa,GACE,IAArB,MAAqB,EAAa;CACjC,OAAgB,iBAAiB;CAEjC;CACA;CACA;CAEA;CACA;CAEA,YAAY,GAAoB,GAAyD;EAGxF,AAFA,KAAK,SAAS,GAEX,OAAO,KAAU,YACnB,KA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,GAAG,IAAI,EAAK,QAAQ,KAAK,GACpC,KAAU,OAAO,aAAa,MAAM,MAAM,EAAK,SAAS,GAAG,IAAI,CAAU,CAA6B;CAGvG,OAAO;AACR"}
@@ -1,6 +1,6 @@
1
- import { SharedAllocatedMemory } from './allocated-memory';
2
- import { TypedArrayConstructor } from './interfaces/typed-array-constructor';
3
- import { 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 SharedVector<T extends Uint32Array | Int32Array | Float32Array = Uint32Array> implements Iterable<T> {
5
5
  static readonly ALLOCATE_COUNT = 4;
6
6
  private memory;
@@ -16,7 +16,7 @@ export default class SharedVector<T extends Uint32Array | Int32Array | Float32Ar
16
16
  get pointer(): number;
17
17
  private cachedFullDataBlock?;
18
18
  private cachedPointer;
19
- constructor(memory: MemoryHeap, config?: SharedVectorConfig<T> | SharedVectorMemory | SharedVectorMemory & SharedVectorConfig<T>);
19
+ constructor(memory: MemoryHeap, config?: SharedVectorConfig<T> | SharedVectorMemory | (SharedVectorMemory & SharedVectorConfig<T>));
20
20
  at(index: number): T;
21
21
  get(index: number, dataIndex?: number): number;
22
22
  push(values: number | Array<number>): number;