@daneren2005/shared-memory-objects 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -7
- package/dist/allocated-memory.d.ts +1 -0
- package/dist/allocated-memory.js +3 -0
- package/dist/allocated-memory.js.map +1 -1
- package/dist/cached-item-list.d.ts +2 -1
- package/dist/cached-item-list.js +6 -1
- package/dist/cached-item-list.js.map +1 -1
- package/dist/local-pool.d.ts +5 -5
- package/dist/local-pool.js +13 -10
- package/dist/local-pool.js.map +1 -1
- package/dist/lock/simple-lock.js +7 -2
- package/dist/lock/simple-lock.js.map +1 -1
- package/dist/memory-buffer.d.ts +4 -1
- package/dist/memory-buffer.js +12 -9
- package/dist/memory-buffer.js.map +1 -1
- package/dist/memory-heap.d.ts +2 -0
- package/dist/memory-heap.js +11 -1
- package/dist/memory-heap.js.map +1 -1
- package/dist/shared-list.d.ts +17 -8
- package/dist/shared-list.js +99 -68
- package/dist/shared-list.js.map +1 -1
- package/dist/shared-map.d.ts +25 -10
- package/dist/shared-map.js +141 -78
- package/dist/shared-map.js.map +1 -1
- package/dist/shared-pointer-list.d.ts +1 -0
- package/dist/shared-pointer-list.js +5 -1
- package/dist/shared-pointer-list.js.map +1 -1
- package/dist/shared-pool.d.ts +9 -5
- package/dist/shared-pool.js +43 -48
- package/dist/shared-pool.js.map +1 -1
- package/dist/shared-stack.d.ts +23 -9
- package/dist/shared-stack.js +130 -66
- package/dist/shared-stack.js.map +1 -1
- package/dist/shared-vector.d.ts +9 -6
- package/dist/shared-vector.js +37 -56
- package/dist/shared-vector.js.map +1 -1
- package/dist/utils/array-type.d.ts +20 -0
- package/dist/utils/array-type.js +36 -0
- package/dist/utils/array-type.js.map +1 -0
- package/dist/utils/hash.d.ts +3 -0
- package/dist/utils/hash.js +21 -0
- package/dist/utils/hash.js.map +1 -0
- package/package.json +3 -1
package/dist/memory-heap.js
CHANGED
|
@@ -2,7 +2,7 @@ import { MAX_BYTE_OFFSET_LENGTH as e, MAX_POSITION_LENGTH as t } from "./utils/p
|
|
|
2
2
|
import n from "./allocated-memory.js";
|
|
3
3
|
import r from "./memory-buffer.js";
|
|
4
4
|
//#region src/memory-heap.ts
|
|
5
|
-
var i =
|
|
5
|
+
var i = e, a = 0, o = 1, s = 2, c = class {
|
|
6
6
|
buffers;
|
|
7
7
|
onGrowBufferHandlers = [];
|
|
8
8
|
isClone;
|
|
@@ -10,6 +10,9 @@ var i = 8192, a = 0, o = 1, s = 2, c = class {
|
|
|
10
10
|
get bufferSize() {
|
|
11
11
|
return this.memory.data[a];
|
|
12
12
|
}
|
|
13
|
+
get maxAllocationLength() {
|
|
14
|
+
return Math.floor((this.bufferSize - 32 - 8) / Uint32Array.BYTES_PER_ELEMENT);
|
|
15
|
+
}
|
|
13
16
|
constructor(t) {
|
|
14
17
|
if (t && "buffers" in t) this.buffers = t.buffers.map((e) => new r({
|
|
15
18
|
buf: e,
|
|
@@ -58,6 +61,13 @@ var i = 8192, a = 0, o = 1, s = 2, c = class {
|
|
|
58
61
|
addOnGrowBufferHandlers(e) {
|
|
59
62
|
this.onGrowBufferHandlers.push(e);
|
|
60
63
|
}
|
|
64
|
+
ensureSpareBuffer() {
|
|
65
|
+
for (let e = 0; e < this.buffers.length; e++) {
|
|
66
|
+
let t = this.buffers[e];
|
|
67
|
+
if (t && t.isEmpty) return !1;
|
|
68
|
+
}
|
|
69
|
+
return this.growBuffer(), !0;
|
|
70
|
+
}
|
|
61
71
|
allocUI32(e) {
|
|
62
72
|
e = Math.ceil(e);
|
|
63
73
|
for (let t = 0; t < this.buffers.length; t++) {
|
package/dist/memory-heap.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory-heap.js","names":[],"sources":["../src/memory-heap.ts"],"sourcesContent":["import AllocatedMemory, { type SharedAllocatedMemory } from './allocated-memory';\nimport { MAX_BYTE_OFFSET_LENGTH, MAX_POSITION_LENGTH } from './utils/pointer';\nimport MemoryBuffer from './memory-buffer';\n\nconst DEFAULT_BUFFER_SIZE = 8_192;\nconst BUFFER_SIZE_INDEX = 0;\nconst BUFFER_COUNT_INDEX = 1;\nconst BUFFER_AUTO_GROW_INDEX = 2;\nexport default class MemoryHeap {\n\tbuffers: Array<MemoryBuffer>;\n\tprivate onGrowBufferHandlers: Array<OnGrowBuffer> = [];\n\tisClone: boolean;\n\tprivate memory: AllocatedMemory;\n\n\tget bufferSize() {\n\t\treturn this.memory.data[BUFFER_SIZE_INDEX];\n\t}\n\n\tconstructor(config?: MemoryHeapConfig | MemoryHeapMemory) {\n\t\tif(config && 'buffers' in config) {\n\t\t\tthis.buffers = config.buffers.map(buffer => {\n\t\t\t\treturn new MemoryBuffer({\n\t\t\t\t\tbuf: buffer,\n\t\t\t\t\tskipInitialization: true,\n\t\t\t\t});\n\t\t\t});\n\n\t\t\t// TODO: This should be programic instead of hoping the first allocation is always byte 40\n\t\t\tthis.memory = new AllocatedMemory(this, {\n\t\t\t\tbufferPosition: 0,\n\t\t\t\tbufferByteOffset: 40,\n\t\t\t});\n\t\t\tthis.isClone = true;\n\t\t} else {\n\t\t\tif(!('SharedArrayBuffer' in globalThis)) {\n\t\t\t\tconsole.warn('SharedArrayBuffer is not working: falling back to ArrayBuffer');\n\t\t\t}\n\n\t\t\tconst bufferSize = config?.bufferSize ?? DEFAULT_BUFFER_SIZE;\n\t\t\tif(bufferSize > MAX_BYTE_OFFSET_LENGTH) {\n\t\t\t\tthrow new Error(`Buffer size ${bufferSize} is greater than max ${MAX_BYTE_OFFSET_LENGTH} that we can reference with pointers`);\n\t\t\t}\n\t\t\t\n\t\t\tlet startBuffer = this.createBuffer(bufferSize);\n\t\t\tthis.buffers = [\n\t\t\t\tstartBuffer,\n\t\t\t];\n\t\t\tconst data = startBuffer.callocAs('u32', 3);\n\t\t\tif(data) {\n\t\t\t\tthis.memory = new AllocatedMemory(this, {\n\t\t\t\t\tbufferPosition: 0,\n\t\t\t\t\tbufferByteOffset: data.byteOffset,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tthrow new Error('Failed to initialize first byte from buffer');\n\t\t\t}\n\t\t\tthis.memory.data[BUFFER_SIZE_INDEX] = bufferSize;\n\t\t\tthis.memory.data[BUFFER_COUNT_INDEX] = 1;\n\t\t\tthis.memory.data[BUFFER_AUTO_GROW_INDEX] = config?.autoGrowSize ?? 100;\n\t\t\tthis.isClone = false;\n\n\t\t\tfor(let i = 1; i < (config?.initialBuffers ?? 1); i++) {\n\t\t\t\tthis.buffers.push(this.createBuffer(bufferSize));\n\t\t\t}\n\t\t}\n\t}\n\n\taddSharedBuffer(data: GrowBufferData) {\n\t\tthis.buffers[data.bufferPosition] = new MemoryBuffer({\n\t\t\tbuf: data.buffer,\n\t\t\tskipInitialization: true,\n\t\t});\n\t}\n\n\tprivate growBuffer() {\n\t\tconst buffer = this.createBuffer();\n\t\tlet nextBufferPosition = Atomics.add(this.memory.data, BUFFER_COUNT_INDEX, 1);\n\t\t// Setting index set by internal Atomic count so we can create new buffers from multiple threads and keep position consistent\n\t\tthis.buffers[nextBufferPosition] = buffer;\n\t\tthis.onGrowBufferHandlers.forEach(handler => handler({\n\t\t\tbufferPosition: nextBufferPosition,\n\t\t\tbuffer: buffer.buf as SharedArrayBuffer,\n\t\t}));\n\n\t\treturn buffer;\n\t}\n\tprivate createBuffer(bufferSize?: number): MemoryBuffer {\n\t\tconst usedBufferSize = bufferSize ?? this.bufferSize;\n\t\tlet buf: ArrayBuffer | SharedArrayBuffer;\n\t\tif('SharedArrayBuffer' in globalThis) {\n\t\t\tbuf = new SharedArrayBuffer(usedBufferSize);\n\t\t} else {\n\t\t\tbuf = new ArrayBuffer(usedBufferSize);\n\t\t}\n\n\t\treturn new MemoryBuffer({\n\t\t\tbuf,\n\n\t\t\t// We can't use this unless we can 100% guarantee that every thread will stop using memory the instant it is freed\n\t\t\t// ex: Allocate 16 bytes. Thread A frees that allocation and then allocates 12 bytes and 4 bytes, but Thread B is mid-execution\n\t\t\t// on the old allocation can changes the internal state of the 4-byte allocation breaking everything\n\t\t\t// After the internal state is wrong MemoryBuffer will loose track of which blocks are where and how big they are\n\t\t\tcompact: false,\n\t\t\tsplit: false,\n\t\t});\n\t}\n\n\taddOnGrowBufferHandlers(handler: OnGrowBuffer) {\n\t\tthis.onGrowBufferHandlers.push(handler);\n\t}\n\n\tallocUI32(count: number): AllocatedMemory {\n\t\tcount = Math.ceil(count);\n\t\tfor(let i = 0; i < this.buffers.length; i++) {\n\t\t\tconst buffer = this.buffers[i];\n\t\t\t// Should just mean we haven't synced this buffer from another thread yet\n\t\t\tif(!buffer) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Should be fine to initialize all values as 0s since unsigned/signed ints and floats all store 0 as all 0s\n\t\t\tconst data = buffer.callocAs('u32', count);\n\t\t\tif(data) {\n\t\t\t\t// Auto grow when nearly full when we need buffer to already be sync'd between threads BEFORE we try to use it\n\t\t\t\tif(\n\t\t\t\t\ti === (this.buffers.length - 1)\n\t\t\t\t\t&& Atomics.load(this.memory.data, BUFFER_COUNT_INDEX) === this.buffers.length\n\t\t\t\t\t&& this.memory.data[BUFFER_AUTO_GROW_INDEX] < 100\n\t\t\t\t\t&& this.memory.data[BUFFER_AUTO_GROW_INDEX] > 0\n\t\t\t\t) {\n\t\t\t\t\tconst percentFull = buffer.top / buffer.end;\n\t\t\t\t\tif(percentFull > (this.memory.data[BUFFER_AUTO_GROW_INDEX] / 100)) {\n\t\t\t\t\t\tthis.growBuffer();\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn new AllocatedMemory(this, {\n\t\t\t\t\tdata,\n\t\t\t\t\tbuffer,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif(this.buffers.length >= MAX_POSITION_LENGTH) {\n\t\t\tthrow new Error(`Can't initialize a new buffer since it would have a position greater than the max of ${MAX_POSITION_LENGTH}`);\n\t\t}\n\n\t\t// If we get here we need to grow another buffer to continue allocating new memory\n\t\tlet buffer = this.growBuffer();\n\t\tconst data = buffer.callocAs('u32', count);\n\t\tif(data) {\n\t\t\treturn new AllocatedMemory(this, {\n\t\t\t\tdata,\n\t\t\t\tbuffer,\n\t\t\t});\n\t\t} else {\n\t\t\tthrow new Error(`Unable to allocate ${count} numbers even after adding a new buffer`);\n\t\t}\n\t}\n\n\tgetSharedAlloc(shared: SharedAllocatedMemory): AllocatedMemory | undefined {\n\t\t// Should just mean it hasn't synced to this thread yet\n\t\tif(this.buffers[shared.bufferPosition] === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn new AllocatedMemory(this, shared);\n\t}\n\n\tget currentUsed() {\n\t\treturn this.totalAllocated - this.buffers.reduce((total, memPool) => total + memPool.stats().available, 0);\n\t}\n\tget totalAllocated() {\n\t\treturn this.buffers[0].buf.byteLength * this.buffers.length;\n\t}\n\n\tgetSharedMemory(): MemoryHeapMemory {\n\t\treturn {\n\t\t\tbuffers: this.buffers.map(buffer => buffer.buf as SharedArrayBuffer),\n\t\t};\n\t}\n}\n\ntype OnGrowBuffer = (newBuffer: GrowBufferData) => void;\ninterface GrowBufferData {\n\tbufferPosition: number\n\tbuffer: SharedArrayBuffer\n}\n\ninterface MemoryHeapConfig {\n\tbufferSize?: number\n\tinitialBuffers?: number\n\tautoGrowSize?: number\n}\ninterface MemoryHeapMemory {\n\tbuffers: Array<SharedArrayBuffer>\n}\n\nexport type { MemoryHeapConfig, MemoryHeapMemory, GrowBufferData };"],"mappings":";;;;AAIA,IAAM,IAAsB,MACtB,IAAoB,GACpB,IAAqB,GACrB,IAAyB,GACV,IAArB,MAAgC;CAC/B;CACA,uBAAoD,CAAC;CACrD;CACA;CAEA,IAAI,aAAa;EAChB,OAAO,KAAK,OAAO,KAAK;CACzB;CAEA,YAAY,GAA8C;EACzD,IAAG,KAAU,aAAa,GAazB,AAZA,KAAK,UAAU,EAAO,QAAQ,KAAI,MAC1B,IAAI,EAAa;GACvB,KAAK;GACL,oBAAoB;EACrB,CA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,OAAO,EACN,SAAS,KAAK,QAAQ,KAAI,MAAU,EAAO,GAAwB,EACpE;CACD;AACD"}
|
|
1
|
+
{"version":3,"file":"memory-heap.js","names":[],"sources":["../src/memory-heap.ts"],"sourcesContent":["import AllocatedMemory, { type SharedAllocatedMemory } from './allocated-memory';\nimport { MAX_BYTE_OFFSET_LENGTH, MAX_POSITION_LENGTH } from './utils/pointer';\nimport MemoryBuffer, { SIZEOF_MEM_BLOCK, SIZEOF_STATE } from './memory-buffer';\n\nconst DEFAULT_BUFFER_SIZE = MAX_BYTE_OFFSET_LENGTH;\nconst BUFFER_SIZE_INDEX = 0;\nconst BUFFER_COUNT_INDEX = 1;\nconst BUFFER_AUTO_GROW_INDEX = 2;\nexport default class MemoryHeap {\n\tbuffers: Array<MemoryBuffer>;\n\tprivate onGrowBufferHandlers: Array<OnGrowBuffer> = [];\n\tisClone: boolean;\n\tprivate memory: AllocatedMemory;\n\n\tget bufferSize() {\n\t\treturn this.memory.data[BUFFER_SIZE_INDEX];\n\t}\n\n\t// Largest u32 count a single fresh buffer can hand out in one allocation: the buffer minus the allocator state and the\n\t// block header. A structure that grows in segments must keep each segment within this or the allocation can never fit.\n\tget maxAllocationLength() {\n\t\treturn Math.floor((this.bufferSize - SIZEOF_STATE - SIZEOF_MEM_BLOCK) / Uint32Array.BYTES_PER_ELEMENT);\n\t}\n\n\tconstructor(config?: MemoryHeapConfig | MemoryHeapMemory) {\n\t\tif(config && 'buffers' in config) {\n\t\t\tthis.buffers = config.buffers.map(buffer => {\n\t\t\t\treturn new MemoryBuffer({\n\t\t\t\t\tbuf: buffer,\n\t\t\t\t\tskipInitialization: true,\n\t\t\t\t});\n\t\t\t});\n\n\t\t\t// TODO: This should be programic instead of hoping the first allocation is always byte 40\n\t\t\tthis.memory = new AllocatedMemory(this, {\n\t\t\t\tbufferPosition: 0,\n\t\t\t\tbufferByteOffset: 40,\n\t\t\t});\n\t\t\tthis.isClone = true;\n\t\t} else {\n\t\t\tif(!('SharedArrayBuffer' in globalThis)) {\n\t\t\t\tconsole.warn('SharedArrayBuffer is not working: falling back to ArrayBuffer');\n\t\t\t}\n\n\t\t\tconst bufferSize = config?.bufferSize ?? DEFAULT_BUFFER_SIZE;\n\t\t\tif(bufferSize > MAX_BYTE_OFFSET_LENGTH) {\n\t\t\t\tthrow new Error(`Buffer size ${bufferSize} is greater than max ${MAX_BYTE_OFFSET_LENGTH} that we can reference with pointers`);\n\t\t\t}\n\t\t\t\n\t\t\tlet startBuffer = this.createBuffer(bufferSize);\n\t\t\tthis.buffers = [\n\t\t\t\tstartBuffer,\n\t\t\t];\n\t\t\tconst data = startBuffer.callocAs('u32', 3);\n\t\t\tif(data) {\n\t\t\t\tthis.memory = new AllocatedMemory(this, {\n\t\t\t\t\tbufferPosition: 0,\n\t\t\t\t\tbufferByteOffset: data.byteOffset,\n\t\t\t\t});\n\t\t\t} else {\n\t\t\t\tthrow new Error('Failed to initialize first byte from buffer');\n\t\t\t}\n\t\t\tthis.memory.data[BUFFER_SIZE_INDEX] = bufferSize;\n\t\t\tthis.memory.data[BUFFER_COUNT_INDEX] = 1;\n\t\t\tthis.memory.data[BUFFER_AUTO_GROW_INDEX] = config?.autoGrowSize ?? 100;\n\t\t\tthis.isClone = false;\n\n\t\t\tfor(let i = 1; i < (config?.initialBuffers ?? 1); i++) {\n\t\t\t\tthis.buffers.push(this.createBuffer(bufferSize));\n\t\t\t}\n\t\t}\n\t}\n\n\taddSharedBuffer(data: GrowBufferData) {\n\t\tthis.buffers[data.bufferPosition] = new MemoryBuffer({\n\t\t\tbuf: data.buffer,\n\t\t\tskipInitialization: true,\n\t\t});\n\t}\n\n\tprivate growBuffer() {\n\t\tconst buffer = this.createBuffer();\n\t\tlet nextBufferPosition = Atomics.add(this.memory.data, BUFFER_COUNT_INDEX, 1);\n\t\t// Setting index set by internal Atomic count so we can create new buffers from multiple threads and keep position consistent\n\t\tthis.buffers[nextBufferPosition] = buffer;\n\t\tthis.onGrowBufferHandlers.forEach(handler => handler({\n\t\t\tbufferPosition: nextBufferPosition,\n\t\t\tbuffer: buffer.buf as SharedArrayBuffer,\n\t\t}));\n\n\t\treturn buffer;\n\t}\n\tprivate createBuffer(bufferSize?: number): MemoryBuffer {\n\t\tconst usedBufferSize = bufferSize ?? this.bufferSize;\n\t\tlet buf: ArrayBuffer | SharedArrayBuffer;\n\t\tif('SharedArrayBuffer' in globalThis) {\n\t\t\tbuf = new SharedArrayBuffer(usedBufferSize);\n\t\t} else {\n\t\t\tbuf = new ArrayBuffer(usedBufferSize);\n\t\t}\n\n\t\treturn new MemoryBuffer({\n\t\t\tbuf,\n\n\t\t\t// We can't use this unless we can 100% guarantee that every thread will stop using memory the instant it is freed\n\t\t\t// ex: Allocate 16 bytes. Thread A frees that allocation and then allocates 12 bytes and 4 bytes, but Thread B is mid-execution\n\t\t\t// on the old allocation can changes the internal state of the 4-byte allocation breaking everything\n\t\t\t// After the internal state is wrong MemoryBuffer will loose track of which blocks are where and how big they are\n\t\t\tcompact: false,\n\t\t\tsplit: false,\n\t\t});\n\t}\n\n\taddOnGrowBufferHandlers(handler: OnGrowBuffer) {\n\t\tthis.onGrowBufferHandlers.push(handler);\n\t}\n\n\t// Guarantees at least one completely-empty buffer exists and (via the grow handlers) has been fanned out to every\n\t// thread. Call this on the owning thread before dispatching work that allocates on another thread (a worker): that\n\t// allocation then lands in a buffer everyone already holds instead of forcing growBuffer on the worker, which would\n\t// create a buffer only the worker can see and leave every other thread unable to resolve pointers into it. Returns\n\t// true if it had to grow one.\n\tensureSpareBuffer(): boolean {\n\t\tfor(let i = 0; i < this.buffers.length; i++) {\n\t\t\tconst buffer = this.buffers[i];\n\t\t\tif(buffer && buffer.isEmpty) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t}\n\n\t\tthis.growBuffer();\n\t\treturn true;\n\t}\n\n\tallocUI32(count: number): AllocatedMemory {\n\t\tcount = Math.ceil(count);\n\t\tfor(let i = 0; i < this.buffers.length; i++) {\n\t\t\tconst buffer = this.buffers[i];\n\t\t\t// Should just mean we haven't synced this buffer from another thread yet\n\t\t\tif(!buffer) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Should be fine to initialize all values as 0s since unsigned/signed ints and floats all store 0 as all 0s\n\t\t\tconst data = buffer.callocAs('u32', count);\n\t\t\tif(data) {\n\t\t\t\t// Auto grow when nearly full when we need buffer to already be sync'd between threads BEFORE we try to use it\n\t\t\t\tif(\n\t\t\t\t\ti === (this.buffers.length - 1)\n\t\t\t\t\t&& Atomics.load(this.memory.data, BUFFER_COUNT_INDEX) === this.buffers.length\n\t\t\t\t\t&& this.memory.data[BUFFER_AUTO_GROW_INDEX] < 100\n\t\t\t\t\t&& this.memory.data[BUFFER_AUTO_GROW_INDEX] > 0\n\t\t\t\t) {\n\t\t\t\t\tconst percentFull = buffer.top / buffer.end;\n\t\t\t\t\tif(percentFull > (this.memory.data[BUFFER_AUTO_GROW_INDEX] / 100)) {\n\t\t\t\t\t\tthis.growBuffer();\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn new AllocatedMemory(this, {\n\t\t\t\t\tdata,\n\t\t\t\t\tbuffer,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tif(this.buffers.length >= MAX_POSITION_LENGTH) {\n\t\t\tthrow new Error(`Can't initialize a new buffer since it would have a position greater than the max of ${MAX_POSITION_LENGTH}`);\n\t\t}\n\n\t\t// If we get here we need to grow another buffer to continue allocating new memory\n\t\tlet buffer = this.growBuffer();\n\t\tconst data = buffer.callocAs('u32', count);\n\t\tif(data) {\n\t\t\treturn new AllocatedMemory(this, {\n\t\t\t\tdata,\n\t\t\t\tbuffer,\n\t\t\t});\n\t\t} else {\n\t\t\tthrow new Error(`Unable to allocate ${count} numbers even after adding a new buffer`);\n\t\t}\n\t}\n\n\tgetSharedAlloc(shared: SharedAllocatedMemory): AllocatedMemory | undefined {\n\t\t// Should just mean it hasn't synced to this thread yet\n\t\tif(this.buffers[shared.bufferPosition] === undefined) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn new AllocatedMemory(this, shared);\n\t}\n\n\tget currentUsed() {\n\t\treturn this.totalAllocated - this.buffers.reduce((total, memPool) => total + memPool.stats().available, 0);\n\t}\n\tget totalAllocated() {\n\t\treturn this.buffers[0].buf.byteLength * this.buffers.length;\n\t}\n\n\tgetSharedMemory(): MemoryHeapMemory {\n\t\treturn {\n\t\t\tbuffers: this.buffers.map(buffer => buffer.buf as SharedArrayBuffer),\n\t\t};\n\t}\n}\n\ntype OnGrowBuffer = (newBuffer: GrowBufferData) => void;\ninterface GrowBufferData {\n\tbufferPosition: number\n\tbuffer: SharedArrayBuffer\n}\n\ninterface MemoryHeapConfig {\n\tbufferSize?: number\n\tinitialBuffers?: number\n\tautoGrowSize?: number\n}\ninterface MemoryHeapMemory {\n\tbuffers: Array<SharedArrayBuffer>\n}\n\nexport type { MemoryHeapConfig, MemoryHeapMemory, GrowBufferData };"],"mappings":";;;;AAIA,IAAM,IAAsB,GACtB,IAAoB,GACpB,IAAqB,GACrB,IAAyB,GACV,IAArB,MAAgC;CAC/B;CACA,uBAAoD,CAAC;CACrD;CACA;CAEA,IAAI,aAAa;EAChB,OAAO,KAAK,OAAO,KAAK;CACzB;CAIA,IAAI,sBAAsB;EACzB,OAAO,KAAK,OAAO,KAAK,aAAA,KAAA,KAAgD,YAAY,iBAAiB;CACtG;CAEA,YAAY,GAA8C;EACzD,IAAG,KAAU,aAAa,GAazB,AAZA,KAAK,UAAU,EAAO,QAAQ,KAAI,MAC1B,IAAI,EAAa;GACvB,KAAK;GACL,oBAAoB;EACrB,CAAC,CACD,GAGD,KAAK,SAAS,IAAI,EAAgB,MAAM;GACvC,gBAAgB;GAChB,kBAAkB;EACnB,CAAC,GACD,KAAK,UAAU;OACT;GACN,AAAK,uBAAuB,cAC3B,QAAQ,KAAK,+DAA+D;GAG7E,IAAM,IAAa,GAAQ,cAAc;GACzC,IAAG,IAAa,GACf,MAAU,MAAM,eAAe,EAAW,uB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wB,EACpE;CACD;AACD"}
|
package/dist/shared-list.d.ts
CHANGED
|
@@ -1,34 +1,43 @@
|
|
|
1
1
|
import type { SharedAllocatedMemory } from './allocated-memory';
|
|
2
|
-
import type { TypedArrayConstructor } from './interfaces/typed-array-constructor';
|
|
3
2
|
import type MemoryHeap from './memory-heap';
|
|
4
|
-
|
|
3
|
+
import { type NumericArray, type TypedArrayConstructor } from './utils/array-type';
|
|
4
|
+
export default class SharedList<T extends NumericArray = Uint32Array> implements Iterable<SharedListIterable<T>> {
|
|
5
5
|
static readonly ALLOCATE_COUNT = 4;
|
|
6
6
|
private memory;
|
|
7
7
|
private firstBlock;
|
|
8
8
|
private uint16Array;
|
|
9
|
+
private cachedType;
|
|
10
|
+
private cachedDataLength;
|
|
11
|
+
private cachedByteMultipler;
|
|
12
|
+
private cachedIsBigInt;
|
|
13
|
+
private cachedIsFloat;
|
|
14
|
+
private bufferViews;
|
|
9
15
|
onDelete?: (data: T) => void;
|
|
10
16
|
get length(): number;
|
|
17
|
+
get usedMemory(): number;
|
|
11
18
|
get type(): number;
|
|
12
19
|
private set type(value);
|
|
13
20
|
get dataLength(): number;
|
|
14
21
|
private set dataLength(value);
|
|
15
22
|
constructor(memory: MemoryHeap, config?: SharedListConfig<T> | SharedListMemory);
|
|
16
|
-
insert(values: number | Array<number>): void;
|
|
23
|
+
insert(values: T[number] | Array<T[number]>): void;
|
|
17
24
|
deleteMatch(callback: (values: T, index: number) => boolean): boolean;
|
|
18
25
|
deleteIndex(deleteIndex: number): boolean;
|
|
19
|
-
deleteValue(deleteValues: number | Array<number>): boolean;
|
|
26
|
+
deleteValue(deleteValues: T[number] | Array<T[number]>): boolean;
|
|
27
|
+
compact(): void;
|
|
20
28
|
clear(): void;
|
|
21
29
|
[Symbol.iterator](): Generator<{
|
|
22
30
|
data: T;
|
|
23
31
|
index: number;
|
|
24
|
-
deleteCurrent: () =>
|
|
32
|
+
deleteCurrent: () => boolean;
|
|
25
33
|
}, void, unknown>;
|
|
26
34
|
forEach(callback: (data: T) => void): void;
|
|
27
35
|
getSharedMemory(): SharedListMemory;
|
|
36
|
+
private getBufferView;
|
|
28
37
|
private getDataBlock;
|
|
29
38
|
free(): void;
|
|
30
39
|
}
|
|
31
|
-
interface SharedListConfig<T extends
|
|
40
|
+
interface SharedListConfig<T extends NumericArray> {
|
|
32
41
|
initWithBlock?: SharedAllocatedMemory;
|
|
33
42
|
type?: TypedArrayConstructor<T>;
|
|
34
43
|
dataLength?: number;
|
|
@@ -36,9 +45,9 @@ interface SharedListConfig<T extends Uint32Array | Int32Array | Float32Array> {
|
|
|
36
45
|
interface SharedListMemory {
|
|
37
46
|
firstBlock: SharedAllocatedMemory;
|
|
38
47
|
}
|
|
39
|
-
interface SharedListIterable<T extends
|
|
48
|
+
interface SharedListIterable<T extends NumericArray> {
|
|
40
49
|
data: T;
|
|
41
50
|
index: number;
|
|
42
|
-
deleteCurrent: () =>
|
|
51
|
+
deleteCurrent: () => boolean;
|
|
43
52
|
}
|
|
44
53
|
export { type SharedListMemory };
|
package/dist/shared-list.js
CHANGED
|
@@ -1,95 +1,125 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { loadPointer as e, loadRawPointer as t, replaceRawPointer as n, storeRawPointer as r } from "./utils/pointer.js";
|
|
2
|
+
import i from "./allocated-memory.js";
|
|
3
|
+
import { getArrayTypeCode as a, getByteMultipler as o, isBigIntType as s, isFloatType as c, makeArrayView as l } from "./utils/array-type.js";
|
|
3
4
|
//#region src/shared-list.ts
|
|
4
|
-
var
|
|
5
|
-
static ALLOCATE_COUNT =
|
|
5
|
+
var u = 4, d = 2, f = d * Uint32Array.BYTES_PER_ELEMENT, p = 0, m = 1, h = 2, g = 0, _ = 1, v = 4095, y = class {
|
|
6
|
+
static ALLOCATE_COUNT = u;
|
|
6
7
|
memory;
|
|
7
8
|
firstBlock;
|
|
8
9
|
uint16Array;
|
|
10
|
+
cachedType;
|
|
11
|
+
cachedDataLength;
|
|
12
|
+
cachedByteMultipler = 1;
|
|
13
|
+
cachedIsBigInt = !1;
|
|
14
|
+
cachedIsFloat = !1;
|
|
15
|
+
bufferViews = [];
|
|
9
16
|
onDelete;
|
|
10
17
|
get length() {
|
|
11
|
-
return Atomics.load(this.firstBlock.data,
|
|
18
|
+
return Atomics.load(this.firstBlock.data, h);
|
|
19
|
+
}
|
|
20
|
+
get usedMemory() {
|
|
21
|
+
let t = this.firstBlock.usedMemory, { bufferPosition: n, bufferByteOffset: r } = e(this.firstBlock.data, p);
|
|
22
|
+
for (; r;) {
|
|
23
|
+
let a = new i(this.memory, {
|
|
24
|
+
bufferPosition: n,
|
|
25
|
+
bufferByteOffset: r
|
|
26
|
+
});
|
|
27
|
+
t += a.usedMemory, {bufferPosition: n, bufferByteOffset: r} = e(a.data, g);
|
|
28
|
+
}
|
|
29
|
+
return t;
|
|
12
30
|
}
|
|
13
31
|
get type() {
|
|
14
|
-
return
|
|
32
|
+
return this.cachedType;
|
|
15
33
|
}
|
|
16
34
|
set type(e) {
|
|
17
35
|
Atomics.store(this.uint16Array, 0, e);
|
|
18
36
|
}
|
|
19
37
|
get dataLength() {
|
|
20
|
-
return
|
|
38
|
+
return this.cachedDataLength;
|
|
21
39
|
}
|
|
22
40
|
set dataLength(e) {
|
|
23
41
|
Atomics.store(this.uint16Array, 1, e);
|
|
24
42
|
}
|
|
25
43
|
constructor(e, t) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
44
|
+
this.memory = e, t && "firstBlock" in t ? (this.firstBlock = new i(e, t.firstBlock), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + 3 * Uint32Array.BYTES_PER_ELEMENT, 2)) : (this.firstBlock = t && t.initWithBlock ? new i(e, t.initWithBlock) : e.allocUI32(u), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + 3 * Uint32Array.BYTES_PER_ELEMENT, 2), r(this.firstBlock.data, m, this.firstBlock.pointer), this.type = a(t?.type ?? Uint32Array), this.dataLength = t?.dataLength ?? 1), this.cachedType = Atomics.load(this.uint16Array, 0), this.cachedDataLength = Math.max(1, Atomics.load(this.uint16Array, 1)), this.cachedByteMultipler = o(this.cachedType), this.cachedIsBigInt = s(this.cachedType), this.cachedIsFloat = c(this.cachedType);
|
|
45
|
+
}
|
|
46
|
+
insert(e) {
|
|
47
|
+
Array.isArray(e) || (e = [e]);
|
|
48
|
+
let r = this.cachedDataLength;
|
|
49
|
+
if (e.length > r) throw Error(`Can't insert ${e.length} array into shared list of ${r} dataLength`);
|
|
50
|
+
let i = this.memory.allocUI32(d + r * this.cachedByteMultipler), a = this.getDataBlock(i.data.buffer, i.data.byteOffset), o = i.pointer, s = a;
|
|
51
|
+
for (let t = 0; t < e.length; t++) this.cachedIsFloat ? s[t] = e[t] : (this.cachedIsBigInt, Atomics.store(a, t, e[t]));
|
|
52
|
+
for (;;) {
|
|
53
|
+
let e = t(this.firstBlock.data, m), r = this.getBufferView(e & v);
|
|
54
|
+
if (!r) continue;
|
|
55
|
+
let i = (e >>> 12 >>> 2) + g, a = Atomics.load(r, i);
|
|
56
|
+
if (e === t(this.firstBlock.data, m)) {
|
|
57
|
+
if (a === 0) {
|
|
58
|
+
if (Atomics.compareExchange(r, i, 0, o) === 0) {
|
|
59
|
+
n(this.firstBlock.data, m, o, e);
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
} else n(this.firstBlock.data, m, a, e);
|
|
63
|
+
}
|
|
31
64
|
}
|
|
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);
|
|
65
|
+
Atomics.add(this.firstBlock.data, h, 1);
|
|
46
66
|
}
|
|
47
67
|
deleteMatch(e) {
|
|
48
|
-
for (let { data: t, index: n, deleteCurrent: r } of this) if (e(t, n)
|
|
68
|
+
for (let { data: t, index: n, deleteCurrent: r } of this) if (e(t, n) && r()) return !0;
|
|
49
69
|
return !1;
|
|
50
70
|
}
|
|
51
71
|
deleteIndex(e) {
|
|
52
|
-
|
|
72
|
+
if (e >= this.length || e < 0) return !1;
|
|
73
|
+
for (let t of this) if (t.index === e && t.deleteCurrent()) return !0;
|
|
74
|
+
return !1;
|
|
53
75
|
}
|
|
54
76
|
deleteValue(e) {
|
|
55
|
-
return
|
|
77
|
+
return Array.isArray(e) ? this.deleteMatch((t) => {
|
|
56
78
|
if (t.length !== e.length) return !1;
|
|
57
79
|
for (let n = 0; n < t.length; n++) if (t[n] !== e[n]) return !1;
|
|
58
80
|
return !0;
|
|
59
|
-
});
|
|
81
|
+
}) : this.deleteMatch((t) => t[0] === e);
|
|
82
|
+
}
|
|
83
|
+
compact() {
|
|
84
|
+
let e = this.firstBlock.pointer, n = this.firstBlock.data, i = g, a = t(n, i);
|
|
85
|
+
for (; a;) {
|
|
86
|
+
let t = a & v, o = this.memory.buffers[t];
|
|
87
|
+
if (!o) break;
|
|
88
|
+
let s = this.getBufferView(t), c = a >>> 12, l = c >>> 2, u = Atomics.load(s, l + g);
|
|
89
|
+
Atomics.load(s, l + _) ? (r(n, i, u), o.free(c)) : (e = a, n = s, i = l + g), a = u;
|
|
90
|
+
}
|
|
91
|
+
r(this.firstBlock.data, m, e);
|
|
60
92
|
}
|
|
61
93
|
clear() {
|
|
62
|
-
let
|
|
63
|
-
for (; !
|
|
64
|
-
if (
|
|
65
|
-
|
|
94
|
+
let e, i = !1;
|
|
95
|
+
for (; !i;) {
|
|
96
|
+
if (e = t(this.firstBlock.data, p), !e) return;
|
|
97
|
+
i = n(this.firstBlock.data, p, 0, e);
|
|
66
98
|
}
|
|
67
|
-
if (!
|
|
68
|
-
r(this.firstBlock.data,
|
|
69
|
-
let
|
|
70
|
-
for (;
|
|
71
|
-
let
|
|
72
|
-
if (!
|
|
73
|
-
let
|
|
74
|
-
|
|
99
|
+
if (!e) return;
|
|
100
|
+
r(this.firstBlock.data, m, this.firstBlock.pointer);
|
|
101
|
+
let a = 0, o = e;
|
|
102
|
+
for (; o;) {
|
|
103
|
+
let e = o & v, t = this.memory.buffers[e];
|
|
104
|
+
if (!t) break;
|
|
105
|
+
let n = this.getBufferView(e), r = o >>> 12, i = r >>> 2;
|
|
106
|
+
o = Atomics.load(n, i + g), Atomics.load(n, i + _) || (a++, this.onDelete && this.onDelete(this.getDataBlock(n.buffer, r))), t.free(r);
|
|
75
107
|
}
|
|
76
|
-
Atomics.sub(this.firstBlock.data,
|
|
108
|
+
Atomics.sub(this.firstBlock.data, h, a);
|
|
77
109
|
}
|
|
78
110
|
*[Symbol.iterator]() {
|
|
79
|
-
let e = 0,
|
|
80
|
-
for (;
|
|
81
|
-
let
|
|
82
|
-
if (!
|
|
83
|
-
let
|
|
84
|
-
(
|
|
85
|
-
let
|
|
111
|
+
let e = 0, n = t(this.firstBlock.data, p);
|
|
112
|
+
for (; n;) {
|
|
113
|
+
let t = n & v, r = this.getBufferView(t);
|
|
114
|
+
if (!r) return;
|
|
115
|
+
let i = n >>> 12, a = i >>> 2;
|
|
116
|
+
if (n = Atomics.load(r, a + g), Atomics.load(r, a + _)) continue;
|
|
117
|
+
let o = r.buffer, s = a + _;
|
|
86
118
|
yield {
|
|
87
|
-
data:
|
|
119
|
+
data: this.getDataBlock(o, i),
|
|
88
120
|
index: e,
|
|
89
|
-
deleteCurrent: () =>
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
}, m && (a = u, o = f, s = p, e++);
|
|
121
|
+
deleteCurrent: () => Atomics.compareExchange(r, s, 0, 1) === 0 && (Atomics.sub(this.firstBlock.data, h, 1), this.onDelete && this.onDelete(this.getDataBlock(o, i)), !0)
|
|
122
|
+
}, e++;
|
|
93
123
|
}
|
|
94
124
|
}
|
|
95
125
|
forEach(e) {
|
|
@@ -98,28 +128,29 @@ var s = 4, c = 1, l = 2, u = class {
|
|
|
98
128
|
getSharedMemory() {
|
|
99
129
|
return { firstBlock: this.firstBlock.getSharedMemory() };
|
|
100
130
|
}
|
|
101
|
-
|
|
102
|
-
let t =
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
131
|
+
getBufferView(e) {
|
|
132
|
+
let t = this.bufferViews[e];
|
|
133
|
+
if (t) return t;
|
|
134
|
+
let n = this.memory.buffers[e];
|
|
135
|
+
if (n) return t = new Uint32Array(n.buf), this.bufferViews[e] = t, t;
|
|
136
|
+
}
|
|
137
|
+
getDataBlock(e, t) {
|
|
138
|
+
let n = t + f;
|
|
139
|
+
return l(this.cachedType, e, n, this.cachedDataLength);
|
|
109
140
|
}
|
|
110
141
|
free() {
|
|
111
|
-
let { bufferPosition:
|
|
142
|
+
let { bufferPosition: t, bufferByteOffset: n } = e(this.firstBlock.data, p);
|
|
112
143
|
for (; n;) {
|
|
113
|
-
let r = new
|
|
114
|
-
bufferPosition:
|
|
144
|
+
let r = new i(this.memory, {
|
|
145
|
+
bufferPosition: t,
|
|
115
146
|
bufferByteOffset: n
|
|
116
147
|
});
|
|
117
|
-
({bufferPosition:
|
|
148
|
+
({bufferPosition: t, bufferByteOffset: n} = e(r.data, g)), this.onDelete && !Atomics.load(r.data, _) && this.onDelete(this.getDataBlock(r.data.buffer, r.data.byteOffset)), r.free();
|
|
118
149
|
}
|
|
119
150
|
this.firstBlock.free();
|
|
120
151
|
}
|
|
121
152
|
};
|
|
122
153
|
//#endregion
|
|
123
|
-
export {
|
|
154
|
+
export { y as default };
|
|
124
155
|
|
|
125
156
|
//# sourceMappingURL=shared-list.js.map
|
package/dist/shared-list.js.map
CHANGED
|
@@ -1 +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;EA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;CACD;CAEA,aAAqB,GAAwB;EAC5C,IAAM,IAAa,EAAO,aAAa,IAAkC,EAAO;EAEhF,QAAO,KAAK,MAAZ;GACC,KAAA,GACC,OAAO,IAAI,WAAW,EAAO,QAAQ,GAAY,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
|
+
{"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 MemoryHeap from './memory-heap';\nimport { loadPointer, loadRawPointer, POSITION_BIT_COUNT, replaceRawPointer, storeRawPointer } from './utils/pointer';\nimport { getArrayTypeCode, getByteMultipler, isBigIntType, isFloatType, makeArrayView, type NumericArray, type NumericArrayIO, type TypedArrayConstructor } from './utils/array-type';\n\n// Lock-free singly-linked list. firstBlock doubles as an immortal sentinel node whose next pointer (index 0) is the head.\n// insert appends with a Michael-Scott CAS enqueue; delete only tombstones a node (never unlinks/frees). Memory is\n// reclaimed exclusively by the quiescent compact()/clear()/free() passes. That deferral is what makes the CAS loops\n// safe without hazard pointers: a live node's address is never handed back to the allocator, so no append can ever\n// observe a recycled address (ABA) while another thread is mid-traversal.\nconst FIRST_BLOCK_RECORD_KEEPING_COUNT = 4;\nconst DATA_BLOCK_RECORD_KEEPING_COUNT = 2;\nconst DATA_BLOCK_BYTE_OFFSET = DATA_BLOCK_RECORD_KEEPING_COUNT * Uint32Array.BYTES_PER_ELEMENT;\nconst HEAD_INDEX = 0;\nconst TAIL_INDEX = 1;\nconst LENGTH_INDEX = 2;\nconst NODE_NEXT_INDEX = 0;\nconst NODE_DELETED_INDEX = 1;\n// A pointer packs the buffer position in its low bits and the byte offset in the high bits (see utils/pointer)\nconst POSITION_MASK = (1 << POSITION_BIT_COUNT) - 1;\nexport default class SharedList<T extends NumericArray = Uint32Array> implements Iterable<SharedListIterable<T>> {\n\tstatic readonly ALLOCATE_COUNT = FIRST_BLOCK_RECORD_KEEPING_COUNT;\n\n\tprivate memory: MemoryHeap;\n\t/* First block (also the sentinel node - index 0 is its next pointer = list head)\n\t\t32 index 0\n\t\tuint16 0 - head buffer position | sentinel.next\n\t\tuint16 1 - head buffer index\n\t\t32 index 1\n\t\tuint16 2 - tail hint buffer position\n\t\tuint16 3 - tail hint 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 - next buffer pointer\n\t\t32 index 1 - deleted flag (0 = live, 1 = tombstoned)\n\t\t32 index 2 => data\n\t*/\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\t// type and dataLength are written once at init and never change, so cache them instead of atomic-loading per node\n\tprivate cachedType: number;\n\tprivate cachedDataLength: number;\n\t// u32 units per element: 1 for 32-bit types, 2 for 64-bit (float64/int64/uint64). Node data blocks scale by this.\n\tprivate cachedByteMultipler = 1;\n\t// Whether the value type is a BigInt view (int64/uint64), so insert stores bigint via Atomics; cached to keep the\n\t// per-insert branch off an instanceof check\n\tprivate cachedIsBigInt = false;\n\t// Float views can't be published with Atomics, so insert falls back to a plain store for them\n\tprivate cachedIsFloat = false;\n\t// One full-buffer Uint32Array per buffer position, so traversal indexes shared memory directly instead of\n\t// allocating a fresh view (and a getPointer object) for every node it touches\n\tprivate bufferViews: Array<Uint32Array> = [];\n\tonDelete?: (data: T) => void;\n\n\tget length(): number {\n\t\treturn Atomics.load(this.firstBlock.data, LENGTH_INDEX);\n\t}\n\n\t// Own internal memory plus every allocated node still linked in the chain (tombstoned nodes included, since they stay\n\t// allocated until a compact()/clear() reclaims them).\n\tget usedMemory(): number {\n\t\tlet total = this.firstBlock.usedMemory;\n\t\tlet { bufferPosition, bufferByteOffset } = loadPointer(this.firstBlock.data, HEAD_INDEX);\n\t\twhile(bufferByteOffset) {\n\t\t\tlet node = new AllocatedMemory(this.memory, { bufferPosition, bufferByteOffset });\n\t\t\ttotal += node.usedMemory;\n\t\t\t({ bufferPosition, bufferByteOffset } = loadPointer(node.data, NODE_NEXT_INDEX));\n\t\t}\n\t\treturn total;\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\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\t// Empty list: the tail hint points at the sentinel (firstBlock) so insert never special-cases the first node\n\t\t\tstoreRawPointer(this.firstBlock.data, TAIL_INDEX, this.firstBlock.pointer);\n\n\t\t\tthis.type = getArrayTypeCode(config?.type ?? Uint32Array);\n\t\t\tthis.dataLength = config?.dataLength ?? 1;\n\t\t}\n\n\t\tthis.cachedType = Atomics.load(this.uint16Array, 0);\n\t\t// dataLength defaults to at least one even when initialized from raw memory that was never explicitly set\n\t\tthis.cachedDataLength = Math.max(1, Atomics.load(this.uint16Array, 1));\n\t\tthis.cachedByteMultipler = getByteMultipler(this.cachedType);\n\t\tthis.cachedIsBigInt = isBigIntType(this.cachedType);\n\t\tthis.cachedIsFloat = isFloatType(this.cachedType);\n\t}\n\n\tinsert(values: T[number] | Array<T[number]>) {\n\t\tif(!Array.isArray(values)) {\n\t\t\tvalues = [values];\n\t\t}\n\n\t\tlet dataLength = this.cachedDataLength;\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 * this.cachedByteMultipler);\n\t\tlet newData = this.getDataBlock(newBlock.data.buffer, newBlock.data.byteOffset);\n\t\tlet newBlockPointer = newBlock.pointer;\n\n\t\tlet io = newData as NumericArrayIO;\n\t\tfor(let i = 0; i < values.length; i++) {\n\t\t\tif(this.cachedIsFloat) {\n\t\t\t\t// Float views don't support Atomics; the enqueue CAS below still provides the cross-thread publish\n\t\t\t\tio[i] = values[i];\n\t\t\t} else if(this.cachedIsBigInt) {\n\t\t\t\tAtomics.store(newData as BigInt64Array, i, values[i] as bigint);\n\t\t\t} else {\n\t\t\t\tAtomics.store(newData as Int32Array, i, values[i] as number);\n\t\t\t}\n\t\t}\n\n\t\t// Michael-Scott enqueue: link the node onto tail.next BEFORE swinging the tail hint, so a committed node is never\n\t\t// invisible to a concurrent traversal. The tail hint may lag by one node; enqueuers help advance it.\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\tlet tailPointer = loadRawPointer(this.firstBlock.data, TAIL_INDEX);\n\t\t\tlet view = this.getBufferView(tailPointer & POSITION_MASK);\n\t\t\t// Tail node lives in a buffer this thread hasn't synced yet - retry until it appears\n\t\t\tif(!view) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tlet tailNextIndex = ((tailPointer >>> POSITION_BIT_COUNT) >>> 2) + NODE_NEXT_INDEX;\n\t\t\tlet nextPointer = Atomics.load(view, tailNextIndex);\n\n\t\t\t// Re-check the hint hasn't moved out from under us before acting on what we read\n\t\t\tif(tailPointer !== loadRawPointer(this.firstBlock.data, TAIL_INDEX)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tif(nextPointer === 0) {\n\t\t\t\tif(Atomics.compareExchange(view, tailNextIndex, 0, newBlockPointer) === 0) {\n\t\t\t\t\t// Linked. Try to swing the hint forward - failure is harmless, the next enqueuer will help.\n\t\t\t\t\treplaceRawPointer(this.firstBlock.data, TAIL_INDEX, newBlockPointer, tailPointer);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Tail hint is lagging behind a linked node - help it catch up, then retry\n\t\t\t\treplaceRawPointer(this.firstBlock.data, TAIL_INDEX, nextPointer, tailPointer);\n\t\t\t}\n\t\t}\n\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\tif(deleteCurrent()) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\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\t// Compare on index only so the payload view is never materialized for an index-based delete\n\t\tfor(let item of this) {\n\t\t\tif(item.index === deleteIndex) {\n\t\t\t\tif(item.deleteCurrent()) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn false;\n\t}\n\tdeleteValue(deleteValues: T[number] | Array<T[number]>) {\n\t\tif(!Array.isArray(deleteValues)) {\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\t// Reclaims tombstoned nodes: physically unlinks and frees them. Not safe to run concurrently with insert/delete.\n\tcompact() {\n\t\tlet prevPointer = this.firstBlock.pointer;\n\t\tlet prevView = this.firstBlock.data;\n\t\tlet prevNextIndex = NODE_NEXT_INDEX;\n\t\tlet nextPointer = loadRawPointer(prevView, prevNextIndex);\n\t\twhile(nextPointer) {\n\t\t\tlet position = nextPointer & POSITION_MASK;\n\t\t\tlet memPool = this.memory.buffers[position];\n\t\t\tif(!memPool) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tlet view = this.getBufferView(position)!;\n\t\t\tlet byteOffset = nextPointer >>> POSITION_BIT_COUNT;\n\t\t\tlet nodeIndex = byteOffset >>> 2;\n\t\t\tlet followingPointer = Atomics.load(view, nodeIndex + NODE_NEXT_INDEX);\n\n\t\t\tif(Atomics.load(view, nodeIndex + NODE_DELETED_INDEX)) {\n\t\t\t\t// Skip over the tombstone and free it. onDelete already fired when the node was logically deleted.\n\t\t\t\tstoreRawPointer(prevView, prevNextIndex, followingPointer);\n\t\t\t\tmemPool.free(byteOffset);\n\t\t\t} else {\n\t\t\t\tprevPointer = nextPointer;\n\t\t\t\tprevView = view;\n\t\t\t\tprevNextIndex = nodeIndex + NODE_NEXT_INDEX;\n\t\t\t}\n\n\t\t\tnextPointer = followingPointer;\n\t\t}\n\n\t\t// The last surviving node (or the sentinel when everything is gone) is the new tail\n\t\tstoreRawPointer(this.firstBlock.data, TAIL_INDEX, prevPointer);\n\t}\n\n\tclear() {\n\t\t// Detach the whole chain, then free it. Not safe to run concurrently with insert/delete.\n\t\tlet firstBlockPointer;\n\t\tlet updateWorked = false;\n\t\twhile(!updateWorked) {\n\t\t\tfirstBlockPointer = loadRawPointer(this.firstBlock.data, HEAD_INDEX);\n\t\t\t// Already cleared\n\t\t\tif(!firstBlockPointer) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tupdateWorked = replaceRawPointer(this.firstBlock.data, HEAD_INDEX, 0, firstBlockPointer);\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// Reset the tail hint back to the sentinel\n\t\tstoreRawPointer(this.firstBlock.data, TAIL_INDEX, this.firstBlock.pointer);\n\n\t\tlet liveItems = 0;\n\t\tlet nextBlockPointer = firstBlockPointer;\n\t\twhile(nextBlockPointer) {\n\t\t\tlet position = nextBlockPointer & POSITION_MASK;\n\t\t\tlet memPool = this.memory.buffers[position];\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 view = this.getBufferView(position)!;\n\t\t\tlet byteOffset = nextBlockPointer >>> POSITION_BIT_COUNT;\n\t\t\tlet nodeIndex = byteOffset >>> 2;\n\t\t\tnextBlockPointer = Atomics.load(view, nodeIndex + NODE_NEXT_INDEX);\n\n\t\t\tif(!Atomics.load(view, nodeIndex + NODE_DELETED_INDEX)) {\n\t\t\t\tliveItems++;\n\t\t\t\tif(this.onDelete) {\n\t\t\t\t\tthis.onDelete(this.getDataBlock(view.buffer, byteOffset));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tmemPool.free(byteOffset);\n\t\t}\n\n\t\t// Only subtract the live nodes we detached so a concurrent insert's increment stays accurate\n\t\tAtomics.sub(this.firstBlock.data, LENGTH_INDEX, liveItems);\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet currentIndex = 0;\n\t\tlet nextPointer = loadRawPointer(this.firstBlock.data, HEAD_INDEX);\n\t\twhile(nextPointer) {\n\t\t\tlet position = nextPointer & POSITION_MASK;\n\t\t\tlet view = this.getBufferView(position);\n\t\t\t// Short circuit iterations if we can't access memory\n\t\t\tif(!view) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tlet byteOffset = nextPointer >>> POSITION_BIT_COUNT;\n\t\t\tlet nodeIndex = byteOffset >>> 2;\n\t\t\tnextPointer = Atomics.load(view, nodeIndex + NODE_NEXT_INDEX);\n\n\t\t\t// Skip tombstoned nodes - they stay linked until a quiescent compact()/clear()\n\t\t\tif(Atomics.load(view, nodeIndex + NODE_DELETED_INDEX)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet buffer = view.buffer;\n\t\t\tlet deletedIndex = nodeIndex + NODE_DELETED_INDEX;\n\t\t\t// Plain object with an eager data view: V8 keeps this monomorphic and fast. A lazy `get data()` accessor is far\n\t\t\t// slower to allocate than the payload view it would save, so pay for the view up front.\n\t\t\tyield {\n\t\t\t\tdata: this.getDataBlock(buffer, byteOffset),\n\t\t\t\tindex: currentIndex,\n\t\t\t\t// Logical delete: tombstone the node and drop length. Returns false if another thread beat us to it.\n\t\t\t\tdeleteCurrent: (): boolean => {\n\t\t\t\t\tif(Atomics.compareExchange(view, deletedIndex, 0, 1) === 0) {\n\t\t\t\t\t\tAtomics.sub(this.firstBlock.data, LENGTH_INDEX, 1);\n\t\t\t\t\t\tif(this.onDelete) {\n\t\t\t\t\t\t\tthis.onDelete(this.getDataBlock(buffer, byteOffset));\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn false;\n\t\t\t\t},\n\t\t\t};\n\n\t\t\tcurrentIndex++;\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 getBufferView(position: number): Uint32Array | undefined {\n\t\tlet view = this.bufferViews[position];\n\t\tif(view) {\n\t\t\treturn view;\n\t\t}\n\n\t\tlet buffer = this.memory.buffers[position];\n\t\tif(!buffer) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\tview = new Uint32Array(buffer.buf);\n\t\tthis.bufferViews[position] = view;\n\t\treturn view;\n\t}\n\n\tprivate getDataBlock(buffer: ArrayBufferLike, nodeByteOffset: number): T {\n\t\tconst startIndex = nodeByteOffset + DATA_BLOCK_BYTE_OFFSET;\n\t\treturn makeArrayView(this.cachedType, buffer, startIndex, this.cachedDataLength) as T;\n\t}\n\n\tfree() {\n\t\tlet { bufferPosition: nextBlockPosition, bufferByteOffset: nextBlockByteOffset } = loadPointer(this.firstBlock.data, HEAD_INDEX);\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, NODE_NEXT_INDEX));\n\n\t\t\tif(this.onDelete && !Atomics.load(allocatedMemory.data, NODE_DELETED_INDEX)) {\n\t\t\t\tthis.onDelete(this.getDataBlock(allocatedMemory.data.buffer, allocatedMemory.data.byteOffset));\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 NumericArray> {\n\tinitWithBlock?: SharedAllocatedMemory\n\ttype?: TypedArrayConstructor<T>\n\tdataLength?: number\n}\ninterface SharedListMemory {\n\tfirstBlock: SharedAllocatedMemory\n}\n\ninterface SharedListIterable<T extends NumericArray> {\n\tdata: T\n\tindex: number\n\tdeleteCurrent: () => boolean\n}\n\nexport { type SharedListMemory };\n"],"mappings":";;;;AAWA,IAAM,IAAmC,GACnC,IAAkC,GAClC,IAAyB,IAAkC,YAAY,mBACvE,IAAa,GACb,IAAa,GACb,IAAe,GACf,IAAkB,GAClB,IAAqB,GAErB,IAAA,MACe,IAArB,MAAiH;CAChH,OAAgB,iBAAiB;CAEjC;CAmBA;CACA;CAEA;CACA;CAEA,sBAA8B;CAG9B,iBAAyB;CAEzB,gBAAwB;CAGxB,cAA0C,CAAC;CAC3C;CAEA,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAAK,KAAK,WAAW,MAAM,CAAY;CACvD;CAIA,IAAI,aAAqB;EACxB,IAAI,IAAQ,KAAK,WAAW,YACxB,EAAE,mBAAgB,wBAAqB,EAAY,KAAK,WAAW,MAAM,CAAU;EACvF,OAAM,IAAkB;GACvB,IAAI,IAAO,IAAI,EAAgB,KAAK,QAAQ;IAAE;IAAgB;GAAiB,CAAC;GAEhF,AADA,KAAS,EAAK,YACb,2CAAuC,EAAY,EAAK,MAAM,CAAe;EAC/E;EACA,OAAO;CACR;CAEA,IAAI,OAAe;EAClB,OAAO,KAAK;CACb;CACA,IAAY,KAAK,GAAe;EAC/B,QAAQ,MAAM,KAAK,aAAa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,MAAwB,EAAY,KAAK,WAAW,MAAM,CAAU;EAC/H,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,CAAe,IAE9H,KAAK,YAAY,CAAC,QAAQ,KAAK,EAAgB,MAAM,CAAkB,KACzE,KAAK,SAAS,KAAK,aAAa,EAAgB,KAAK,QAAQ,EAAgB,KAAK,UAAU,CAAC,GAG9F,EAAgB,KAAK;EACtB;EAEA,KAAK,WAAW,KAAK;CACtB;AACD"}
|
package/dist/shared-map.d.ts
CHANGED
|
@@ -1,25 +1,40 @@
|
|
|
1
1
|
import { type SharedAllocatedMemory } from './allocated-memory';
|
|
2
2
|
import type MemoryHeap from './memory-heap';
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import { type NumericArray, type TypedArrayConstructor } from './utils/array-type';
|
|
4
|
+
export default class SharedMap<K extends string | number, V extends NumericArray = Uint32Array> {
|
|
5
|
+
static readonly ALLOCATE_COUNT = 6;
|
|
5
6
|
private memory;
|
|
6
7
|
private pointerMemory;
|
|
7
8
|
private lock;
|
|
8
|
-
private
|
|
9
|
+
private cachedType;
|
|
10
|
+
private cachedValueUnits;
|
|
11
|
+
private cachedTablePointer;
|
|
12
|
+
private cachedTableMemory?;
|
|
13
|
+
private cachedSlots?;
|
|
14
|
+
private cachedValues?;
|
|
15
|
+
private cachedCapacity;
|
|
16
|
+
private ensureTable;
|
|
17
|
+
private makeValuesView;
|
|
18
|
+
private tableLength;
|
|
9
19
|
get length(): number;
|
|
20
|
+
get capacity(): number;
|
|
10
21
|
get maxHash(): number;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
22
|
+
get type(): number;
|
|
23
|
+
constructor(memory: MemoryHeap, config?: SharedMapConfig<V> | SharedMapMemory);
|
|
24
|
+
set(key: K, value: V[number]): void;
|
|
25
|
+
update(key: K, updater: (current: V[number] | undefined) => V[number]): V[number];
|
|
26
|
+
get(key: K): V[number] | undefined;
|
|
15
27
|
has(key: K): boolean;
|
|
16
28
|
delete(key: K): boolean;
|
|
17
|
-
|
|
18
|
-
private
|
|
29
|
+
[Symbol.iterator](): IterableIterator<[number, V[number]]>;
|
|
30
|
+
private resize;
|
|
19
31
|
free(): void;
|
|
20
32
|
getSharedMemory(): SharedMapMemory;
|
|
21
33
|
}
|
|
34
|
+
interface SharedMapConfig<V extends NumericArray> {
|
|
35
|
+
type?: TypedArrayConstructor<V>;
|
|
36
|
+
}
|
|
22
37
|
interface SharedMapMemory {
|
|
23
38
|
firstBlock: SharedAllocatedMemory;
|
|
24
39
|
}
|
|
25
|
-
export type { SharedMapMemory };
|
|
40
|
+
export type { SharedMapConfig, SharedMapMemory };
|