@daneren2005/shared-memory-objects 1.5.0 → 1.5.2
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/dist/allocated-memory.d.ts +7 -2
- package/dist/allocated-memory.js +10 -3
- package/dist/allocated-memory.js.map +1 -1
- package/dist/cached-item-list.d.ts +1 -1
- package/dist/lock/read-write-lock.d.ts +1 -0
- package/dist/lock/read-write-lock.js +6 -3
- package/dist/lock/read-write-lock.js.map +1 -1
- package/dist/memory-buffer.d.ts +11 -5
- package/dist/memory-buffer.js +70 -74
- package/dist/memory-buffer.js.map +1 -1
- package/dist/memory-heap.d.ts +1 -0
- package/dist/memory-heap.js +28 -25
- package/dist/memory-heap.js.map +1 -1
- package/dist/shared-list.d.ts +5 -1
- package/dist/shared-list.js +91 -47
- package/dist/shared-list.js.map +1 -1
- package/dist/shared-map.js +4 -1
- package/dist/shared-map.js.map +1 -1
- package/dist/shared-pool.js +4 -1
- package/dist/shared-pool.js.map +1 -1
- package/dist/shared-stack.js +11 -2
- package/dist/shared-stack.js.map +1 -1
- package/dist/shared-vector.js +4 -1
- package/dist/shared-vector.js.map +1 -1
- package/dist/spatial/shared-spatial-map.js +134 -134
- package/package.json +1 -1
package/dist/shared-map.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-map.js","names":[],"sources":["../src/shared-map.ts"],"sourcesContent":["import AllocatedMemory, { type SharedAllocatedMemory } from './allocated-memory';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, storeRawPointer } from './utils/pointer';\nimport { get32BitHash, mix32 } from './utils/hash';\nimport { lock, unlock, SIMPLE_LOCK_ALLOCATE_COUNT } from './lock/simple-lock';\nimport { getArrayTypeCode, getByteMultipler, makeArrayView, type NumericArray, type NumericArrayIO, type TypedArrayConstructor } from './utils/array-type';\n\n// Open-addressing hash map with linear probing, held in one contiguous table (no per-bucket lists, no per-operation\n// allocation). A single exclusive lock guards the table so slot reads/writes inside the critical section can be plain\n// memory access instead of per-slot Atomics; the lock's acquire/release provides the cross-thread happens-before.\n//\n// The table uses a Structure-of-Arrays layout: three contiguous regions - states | keys | values - so probing touches\n// the dense states and keys regions (more candidates per cache line) and iteration walks values densely. capacity is\n// always a power of two, so slot = mix(key) & (capacity - 1). state distinguishes EMPTY/FULL/TOMBSTONE, so key 0 and\n// value 0 need no sentinel.\n//\n// Keys are always stored as the 32-bit hash (a numeric key is its own hash); values are stored in the typed array the\n// map was created with (Uint32Array by default, or Int32Array / Float32Array). Iteration is a best-effort scan that is\n// not lock-guarded, so it must not run concurrently with mutation from another thread. A single allocation can't span\n// buffers, so the table is bounded by MemoryHeap.maxAllocationLength.\n\nconst EMPTY = 0;\nconst FULL = 1;\nconst TOMBSTONE = 2;\n\nconst DEFAULT_CAPACITY = 16;\n// Grow when (occupied + tombstones) / capacity exceeds this. Kept as a fraction to avoid floating point in the hot path.\nconst LOAD_NUMERATOR = 3;\nconst LOAD_DENOMINATOR = 4;\n\n// Metadata block layout (pointerMemory)\nconst TABLE_POINTER_INDEX = 0;\nconst LOCK_INDEX = 1;\nconst LENGTH_INDEX = 2;\nconst TOMBSTONE_INDEX = 3;\nconst CAPACITY_INDEX = 4;\nconst TYPE_INDEX = 5;\n\nexport default class SharedMap<K extends string | number, V extends NumericArray = Uint32Array> {\n\tstatic readonly ALLOCATE_COUNT = 6;\n\n\tprivate memory: MemoryHeap;\n\tprivate pointerMemory: AllocatedMemory;\n\t// False when pointerMemory is a slice of another structure's memory (e.g. inlined in SharedQuadtree); that owner frees it\n\tprivate ownsFirstBlock = true;\n\tprivate lock: Int32Array;\n\t// Value type is written once at creation and never changes, so resolve it once instead of reading metadata per op\n\tprivate cachedType: number;\n\t// u32 units per value slot: 1 for 32-bit types, 2 for 64-bit (float64/int64/uint64). The values region and every\n\t// table allocation scale by this.\n\tprivate cachedValueUnits = 1;\n\n\t// Cache the resolved views so most operations avoid rebuilding them. Rebuild whenever the table pointer OR the capacity\n\t// changes: the pointer alone is not enough because the allocator recycles freed addresses (split:false), so a rehash can\n\t// land a differently-sized table at the same address (ABA). The cached views are plain windows into shared memory, so\n\t// they always reflect the current bytes at that address; only the derived capacity (mask/offsets) can go stale, and the\n\t// capacity check catches exactly that.\n\tprivate cachedTablePointer = 0;\n\tprivate cachedTableMemory?: AllocatedMemory;\n\tprivate cachedSlots?: Uint32Array; // whole block as u32: states region, then keys region\n\tprivate cachedValues?: V; // typed view over the values region\n\tprivate cachedCapacity = 0;\n\t// Callers hold the lock, so the acquire fence already published the latest pointer/capacity - plain reads are enough\n\t// and avoid two Atomics.load per operation on the hot path.\n\tprivate ensureTable() {\n\t\tlet data = this.pointerMemory.data;\n\t\tlet pointer = data[TABLE_POINTER_INDEX];\n\t\tlet capacity = data[CAPACITY_INDEX];\n\t\tif(pointer === this.cachedTablePointer && capacity === this.cachedCapacity && this.cachedSlots) {\n\t\t\treturn;\n\t\t}\n\n\t\tlet table = new AllocatedMemory(this.memory, getPointer(pointer, this.memory.positionBits));\n\t\tthis.cachedTableMemory = table;\n\t\tthis.cachedSlots = table.data;\n\t\tthis.cachedValues = this.makeValuesView(table, capacity);\n\t\tthis.cachedCapacity = capacity;\n\t\tthis.cachedTablePointer = pointer;\n\t}\n\tprivate makeValuesView(table: AllocatedMemory, capacity: number): V {\n\t\t// states and keys each take one u32 per slot, so the values region starts after 2 * capacity u32s\n\t\tlet byteOffset = table.bufferByteOffset + capacity * 2 * Uint32Array.BYTES_PER_ELEMENT;\n\t\treturn makeArrayView(this.cachedType, table.data.buffer, byteOffset, capacity) as V;\n\t}\n\t// u32s a table of this capacity needs: states + keys (one each per slot) plus the values region, which is\n\t// cachedValueUnits u32s per slot.\n\tprivate tableLength(capacity: number): number {\n\t\treturn capacity * (2 + this.cachedValueUnits);\n\t}\n\n\tget length(): number {\n\t\treturn Atomics.load(this.pointerMemory.data, LENGTH_INDEX);\n\t}\n\tget capacity(): number {\n\t\treturn Atomics.load(this.pointerMemory.data, CAPACITY_INDEX);\n\t}\n\tget maxHash(): number {\n\t\treturn this.capacity;\n\t}\n\tget type(): number {\n\t\treturn this.cachedType;\n\t}\n\t// Metadata block plus the single table allocation the map currently points at.\n\tget usedMemory(): number {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tthis.ensureTable();\n\t\t\t// When inlined, the owner already counts pointerMemory as part of its own firstBlock\n\t\t\tlet ownMemory = this.ownsFirstBlock ? this.pointerMemory.usedMemory : 0;\n\t\t\treturn ownMemory + this.cachedTableMemory!.usedMemory;\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedMapConfig<V> | SharedMapMemory | SharedMapMemory & SharedMapConfig<V>) {\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 SharedQuadtree) for us to initialize in\n\t\t\t// place; a plain pointer is the serialized clone path, whose table already exists and is read from metadata.\n\t\t\tif(config.firstBlock instanceof AllocatedMemory) {\n\t\t\t\tthis.pointerMemory = config.firstBlock;\n\t\t\t\tthis.initialize(config);\n\t\t\t\tthis.ownsFirstBlock = false;\n\t\t\t} else {\n\t\t\t\tthis.pointerMemory = new AllocatedMemory(memory, config.firstBlock);\n\t\t\t}\n\t\t} else {\n\t\t\tthis.pointerMemory = memory.allocUI32(SharedMap.ALLOCATE_COUNT);\n\t\t\tthis.initialize(config);\n\t\t}\n\t\tthis.lock = new Int32Array(this.pointerMemory.data.buffer, this.pointerMemory.bufferByteOffset + LOCK_INDEX * Uint32Array.BYTES_PER_ELEMENT, SIMPLE_LOCK_ALLOCATE_COUNT);\n\t\tthis.cachedType = Atomics.load(this.pointerMemory.data, TYPE_INDEX);\n\t\tthis.cachedValueUnits = getByteMultipler(this.cachedType);\n\t}\n\n\t// Allocate the initial table and write the metadata. pointerMemory must already be set. Used by both the fresh-alloc\n\t// path and the inline-in-place path (where pointerMemory is a slice reserved by the owner).\n\tprivate initialize(config?: SharedMapConfig<V> | SharedMapMemory) {\n\t\tlet type = config && 'type' in config ? config.type : undefined;\n\t\tlet typeCode = getArrayTypeCode(type ?? Uint32Array);\n\t\tthis.cachedValueUnits = getByteMultipler(typeCode);\n\t\tlet requestedCapacity = config && 'capacity' in config ? config.capacity : undefined;\n\t\tlet capacity = DEFAULT_CAPACITY;\n\t\twhile(capacity < (requestedCapacity ?? DEFAULT_CAPACITY)) {\n\t\t\tcapacity *= 2;\n\t\t}\n\t\tif(capacity * (2 + this.cachedValueUnits) > this.memory.maxAllocationLength) {\n\t\t\tthrow new Error(`SharedMap capacity ${capacity} exceeds the largest contiguous allocation`);\n\t\t}\n\t\tlet table = this.memory.allocUI32(capacity * (2 + this.cachedValueUnits));\n\t\tstoreRawPointer(this.pointerMemory.data, TABLE_POINTER_INDEX, table.pointer);\n\t\tAtomics.store(this.pointerMemory.data, CAPACITY_INDEX, capacity);\n\t\tAtomics.store(this.pointerMemory.data, TYPE_INDEX, typeCode);\n\t}\n\n\tset(key: K, value: V[number]) {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tthis.setNoLock(key, value);\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tsetAll(entries: Iterable<readonly [K, V[number]]>) {\n\t\tlet batch = Array.from(entries);\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tlet data = this.pointerMemory.data;\n\t\t\tlet projected = data[LENGTH_INDEX] + data[TOMBSTONE_INDEX] + batch.length;\n\t\t\tif(projected * LOAD_DENOMINATOR > data[CAPACITY_INDEX] * LOAD_NUMERATOR) {\n\t\t\t\tlet capacity = data[CAPACITY_INDEX];\n\t\t\t\twhile(projected * LOAD_DENOMINATOR > capacity * LOAD_NUMERATOR) {\n\t\t\t\t\tcapacity *= 2;\n\t\t\t\t}\n\t\t\t\tthis.resize(capacity);\n\t\t\t}\n\t\t\tfor(let [key, value] of batch) {\n\t\t\t\tthis.setNoLock(key, value);\n\t\t\t}\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tprivate setNoLock(key: K, value: V[number]) {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tlet data = this.pointerMemory.data;\n\t\tlet length = data[LENGTH_INDEX];\n\t\tlet tombstones = data[TOMBSTONE_INDEX];\n\t\tif((length + tombstones + 1) * LOAD_DENOMINATOR > data[CAPACITY_INDEX] * LOAD_NUMERATOR) {\n\t\t\tthis.resize();\n\t\t}\n\n\t\tthis.ensureTable();\n\t\tlet slots = this.cachedSlots!;\n\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\tlet capacity = this.cachedCapacity;\n\t\tlet mask = capacity - 1;\n\t\tlet keysBase = capacity;\n\t\tlet idx = mix32(fullHashKey) & mask;\n\t\tlet firstTomb = -1;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\tlet state = slots[idx];\n\t\t\tif(state === EMPTY) {\n\t\t\t\tlet target = firstTomb >= 0 ? firstTomb : idx;\n\t\t\t\tslots[target] = FULL;\n\t\t\t\tslots[keysBase + target] = fullHashKey;\n\t\t\t\tvalues[target] = value;\n\t\t\t\tdata[LENGTH_INDEX]++;\n\t\t\t\tif(firstTomb >= 0) {\n\t\t\t\t\tdata[TOMBSTONE_INDEX]--;\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t} else if(state === TOMBSTONE) {\n\t\t\t\tif(firstTomb < 0) {\n\t\t\t\t\tfirstTomb = idx;\n\t\t\t\t}\n\t\t\t} else if(slots[keysBase + idx] === fullHashKey) {\n\t\t\t\tvalues[idx] = value;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tidx = (idx + 1) & mask;\n\t\t}\n\t}\n\n\t// Atomic read-modify-write under the table lock: resolves the current value (undefined if the key is\n\t// absent), calls updater, and stores the result - so two threads mutating the same key never lose an\n\t// update the way a separate get() + set() can. updater runs inside the lock and must be synchronous and\n\t// must not touch this same map (the lock is not reentrant).\n\tupdate(key: K, updater: (current: V[number] | undefined) => V[number]): V[number] {\n\t\tlet fullHashKey = get32BitHash(key);\n\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tlet data = this.pointerMemory.data;\n\t\t\tlet length = data[LENGTH_INDEX];\n\t\t\tlet tombstones = data[TOMBSTONE_INDEX];\n\t\t\tif((length + tombstones + 1) * LOAD_DENOMINATOR > data[CAPACITY_INDEX] * LOAD_NUMERATOR) {\n\t\t\t\tthis.resize();\n\t\t\t}\n\n\t\t\tthis.ensureTable();\n\t\t\tlet slots = this.cachedSlots!;\n\t\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\t\tlet capacity = this.cachedCapacity;\n\t\t\tlet mask = capacity - 1;\n\t\t\tlet keysBase = capacity;\n\t\t\tlet idx = mix32(fullHashKey) & mask;\n\t\t\tlet firstTomb = -1;\n\t\t\t// eslint-disable-next-line no-constant-condition\n\t\t\twhile(true) {\n\t\t\t\tlet state = slots[idx];\n\t\t\t\tif(state === EMPTY) {\n\t\t\t\t\tlet newValue = updater(undefined);\n\t\t\t\t\tlet target = firstTomb >= 0 ? firstTomb : idx;\n\t\t\t\t\tslots[target] = FULL;\n\t\t\t\t\tslots[keysBase + target] = fullHashKey;\n\t\t\t\t\tvalues[target] = newValue;\n\t\t\t\t\tdata[LENGTH_INDEX]++;\n\t\t\t\t\tif(firstTomb >= 0) {\n\t\t\t\t\t\tdata[TOMBSTONE_INDEX]--;\n\t\t\t\t\t}\n\t\t\t\t\treturn newValue;\n\t\t\t\t} else if(state === TOMBSTONE) {\n\t\t\t\t\tif(firstTomb < 0) {\n\t\t\t\t\t\tfirstTomb = idx;\n\t\t\t\t\t}\n\t\t\t\t} else if(slots[keysBase + idx] === fullHashKey) {\n\t\t\t\t\tlet newValue = updater(values[idx]);\n\t\t\t\t\tvalues[idx] = newValue;\n\t\t\t\t\treturn newValue;\n\t\t\t\t}\n\n\t\t\t\tidx = (idx + 1) & mask;\n\t\t\t}\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tget(key: K): V[number] | undefined {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\treturn this.getNoLock(key);\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tgetAll(keys: Iterable<K>): Array<V[number] | undefined> {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tlet values: Array<V[number] | undefined> = [];\n\t\t\tfor(let key of keys) {\n\t\t\t\tvalues.push(this.getNoLock(key));\n\t\t\t}\n\t\t\treturn values;\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tprivate getNoLock(key: K): V[number] | undefined {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tthis.ensureTable();\n\t\tlet slots = this.cachedSlots!;\n\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\tlet capacity = this.cachedCapacity;\n\t\tlet mask = capacity - 1;\n\t\tlet keysBase = capacity;\n\t\tlet idx = mix32(fullHashKey) & mask;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\tlet state = slots[idx];\n\t\t\tif(state === EMPTY) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tif(state === FULL && slots[keysBase + idx] === fullHashKey) {\n\t\t\t\treturn values[idx];\n\t\t\t}\n\t\t\tidx = (idx + 1) & mask;\n\t\t}\n\t}\n\thas(key: K): boolean {\n\t\treturn this.get(key) !== undefined;\n\t}\n\n\tdelete(key: K): boolean {\n\t\tlet fullHashKey = get32BitHash(key);\n\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tthis.ensureTable();\n\t\t\tlet data = this.pointerMemory.data;\n\t\t\tlet slots = this.cachedSlots!;\n\t\t\tlet capacity = this.cachedCapacity;\n\t\t\tlet mask = capacity - 1;\n\t\t\tlet keysBase = capacity;\n\t\t\tlet idx = mix32(fullHashKey) & mask;\n\t\t\t// eslint-disable-next-line no-constant-condition\n\t\t\twhile(true) {\n\t\t\t\tlet state = slots[idx];\n\t\t\t\tif(state === EMPTY) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\tif(state === FULL && slots[keysBase + idx] === fullHashKey) {\n\t\t\t\t\tslots[idx] = TOMBSTONE;\n\t\t\t\t\tdata[LENGTH_INDEX]--;\n\t\t\t\t\tdata[TOMBSTONE_INDEX]++;\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tidx = (idx + 1) & mask;\n\t\t\t}\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\t// Best-effort scan (see class note): not lock-guarded, so must not run concurrently with writes from another thread.\n\t*[Symbol.iterator](): IterableIterator<[number, V[number]]> {\n\t\tthis.ensureTable();\n\t\tlet slots = this.cachedSlots!;\n\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\tlet capacity = this.cachedCapacity;\n\t\tlet keysBase = capacity;\n\t\tfor(let i = 0; i < capacity; i++) {\n\t\t\tif(slots[i] === FULL) {\n\t\t\t\tyield [slots[keysBase + i], values[i]];\n\t\t\t}\n\t\t}\n\t}\n\n\t// Rehash into a fresh table. Doubles capacity when live entries are crowding the table; when tombstones are the cause,\n\t// a same-size table already reclaims them. Caller must hold the lock.\n\tprivate resize(requestedCapacity?: number) {\n\t\tthis.ensureTable();\n\t\tlet oldTable = this.cachedTableMemory!;\n\t\tlet oldSlots = this.cachedSlots!;\n\t\tlet oldValues = this.cachedValues! as NumericArrayIO;\n\t\tlet oldCapacity = this.cachedCapacity;\n\t\tlet oldKeysBase = oldCapacity;\n\t\tlet length = Atomics.load(this.pointerMemory.data, LENGTH_INDEX);\n\n\t\tlet newCapacity = requestedCapacity ?? oldCapacity;\n\t\tif(requestedCapacity === undefined && (length + 1) * LOAD_DENOMINATOR > oldCapacity * LOAD_NUMERATOR) {\n\t\t\tnewCapacity = oldCapacity * 2;\n\t\t}\n\t\t// A table is one contiguous allocation, so it can never exceed what a single buffer can hand out. Fail here with an\n\t\t// actionable message instead of letting allocUI32 throw a cryptic \"Unable to allocate N numbers\" from deep inside the\n\t\t// heap. Mirrors the same guard in initialize().\n\t\tif(this.tableLength(newCapacity) > this.memory.maxAllocationLength) {\n\t\t\tlet maxEntries = Math.floor(this.memory.maxAllocationLength / (2 + this.cachedValueUnits) * LOAD_NUMERATOR / LOAD_DENOMINATOR);\n\t\t\tthrow new Error(`SharedMap can't grow to capacity ${newCapacity}: its table would exceed the largest contiguous `\n\t\t\t\t+ `allocation (${this.memory.maxAllocationLength} u32s, ~${maxEntries} entries). Use larger buffers or shard across multiple maps.`);\n\t\t}\n\t\tlet newTable = this.memory.allocUI32(this.tableLength(newCapacity));\n\t\tlet newSlots = newTable.data;\n\t\tlet newValues = this.makeValuesView(newTable, newCapacity) as NumericArrayIO;\n\t\tlet mask = newCapacity - 1;\n\t\tlet newKeysBase = newCapacity;\n\n\t\tfor(let i = 0; i < oldCapacity; i++) {\n\t\t\tif(oldSlots[i] !== FULL) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet key = oldSlots[oldKeysBase + i];\n\t\t\tlet value = oldValues[i];\n\t\t\tlet idx = mix32(key) & mask;\n\t\t\t// Fresh table has no tombstones, so probe straight to the first empty slot\n\t\t\twhile(newSlots[idx] !== EMPTY) {\n\t\t\t\tidx = (idx + 1) & mask;\n\t\t\t}\n\t\t\tnewSlots[idx] = FULL;\n\t\t\tnewSlots[newKeysBase + idx] = key;\n\t\t\tnewValues[idx] = value;\n\t\t}\n\n\t\tstoreRawPointer(this.pointerMemory.data, TABLE_POINTER_INDEX, newTable.pointer);\n\t\tAtomics.store(this.pointerMemory.data, CAPACITY_INDEX, newCapacity);\n\t\tAtomics.store(this.pointerMemory.data, TOMBSTONE_INDEX, 0);\n\t\t// Invalidate the cached views so the next access rebuilds against the new table\n\t\tthis.cachedTablePointer = 0;\n\t\tthis.cachedSlots = undefined;\n\t\toldTable.free();\n\t}\n\n\tfree() {\n\t\tthis.ensureTable();\n\t\tthis.cachedTableMemory!.free();\n\t\tif(this.ownsFirstBlock) {\n\t\t\tthis.pointerMemory.free();\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedMapMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.pointerMemory.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedMapConfig<V extends NumericArray> {\n\ttype?: TypedArrayConstructor<V>\n\tcapacity?: number\n}\ninterface SharedMapMemory {\n\tfirstBlock: SharedAllocatedMemory | AllocatedMemory\n}\n\nexport type { SharedMapConfig, SharedMapMemory };\n"],"mappings":";;;;;;AAqBA,IAAM,IAAQ,GACR,IAAO,GACP,IAAY,GAEZ,IAAmB,IAEnB,IAAiB,GACjB,IAAmB,GAGnB,IAAsB,GACtB,IAAa,GACb,IAAe,GACf,IAAkB,GAClB,IAAiB,GACjB,IAAa,GAEE,IAArB,MAAqB,EAA2E;CAC/F,OAAgB,iBAAiB;CAEjC;CACA;CAEA,iBAAyB;CACzB;CAEA;CAGA,mBAA2B;CAO3B,qBAA6B;CAC7B;CACA;CACA;CACA,iBAAyB;CAGzB,cAAsB;EACrB,IAAI,IAAO,KAAK,cAAc,MAC1B,IAAU,EAAK,IACf,IAAW,EAAK;EACpB,IAAG,MAAY,KAAK,sBAAsB,MAAa,KAAK,kBAAkB,KAAK,aAClF;EAGD,IAAI,IAAQ,IAAI,EAAgB,KAAK,QAAQ,EAAW,GAAS,KAAK,OAAO,YAAY,CAAC;EAK1F,AAJA,KAAK,oBAAoB,GACzB,KAAK,cAAc,EAAM,MACzB,KAAK,eAAe,KAAK,eAAe,GAAO,CAAQ,GACvD,KAAK,iBAAiB,GACtB,KAAK,qBAAqB;CAC3B;CACA,eAAuB,GAAwB,GAAqB;EAEnE,IAAI,IAAa,EAAM,mBAAmB,IAAW,IAAI,YAAY;EACrE,OAAO,EAAc,KAAK,YAAY,EAAM,KAAK,QAAQ,GAAY,CAAQ;CAC9E;CAGA,YAAoB,GAA0B;EAC7C,OAAO,KAAY,IAAI,KAAK;CAC7B;CAEA,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAA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,KAAK,YAAY,GACjB,KAAK,kBAAmB,KAAK,GAC1B,KAAK,kBACP,KAAK,cAAc,KAAK;CAE1B;CAEA,kBAAmC;EAClC,OAAO,EACN,YAAY,KAAK,cAAc,gBAAgB,EAChD;CACD;AACD"}
|
|
1
|
+
{"version":3,"file":"shared-map.js","names":[],"sources":["../src/shared-map.ts"],"sourcesContent":["import AllocatedMemory, { type SharedAllocatedMemory } from './allocated-memory';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, storeRawPointer } from './utils/pointer';\nimport { get32BitHash, mix32 } from './utils/hash';\nimport { lock, unlock, SIMPLE_LOCK_ALLOCATE_COUNT } from './lock/simple-lock';\nimport { getArrayTypeCode, getByteMultipler, makeArrayView, type NumericArray, type NumericArrayIO, type TypedArrayConstructor } from './utils/array-type';\n\n// Open-addressing hash map with linear probing, held in one contiguous table (no per-bucket lists, no per-operation\n// allocation). A single exclusive lock guards the table so slot reads/writes inside the critical section can be plain\n// memory access instead of per-slot Atomics; the lock's acquire/release provides the cross-thread happens-before.\n//\n// The table uses a Structure-of-Arrays layout: three contiguous regions - states | keys | values - so probing touches\n// the dense states and keys regions (more candidates per cache line) and iteration walks values densely. capacity is\n// always a power of two, so slot = mix(key) & (capacity - 1). state distinguishes EMPTY/FULL/TOMBSTONE, so key 0 and\n// value 0 need no sentinel.\n//\n// Keys are always stored as the 32-bit hash (a numeric key is its own hash); values are stored in the typed array the\n// map was created with (Uint32Array by default, or Int32Array / Float32Array). Iteration is a best-effort scan that is\n// not lock-guarded, so it must not run concurrently with mutation from another thread. A single allocation can't span\n// buffers, so the table is bounded by MemoryHeap.maxAllocationLength.\n\nconst EMPTY = 0;\nconst FULL = 1;\nconst TOMBSTONE = 2;\n\nconst DEFAULT_CAPACITY = 16;\n// Grow when (occupied + tombstones) / capacity exceeds this. Kept as a fraction to avoid floating point in the hot path.\nconst LOAD_NUMERATOR = 3;\nconst LOAD_DENOMINATOR = 4;\n\n// Metadata block layout (pointerMemory)\nconst TABLE_POINTER_INDEX = 0;\nconst LOCK_INDEX = 1;\nconst LENGTH_INDEX = 2;\nconst TOMBSTONE_INDEX = 3;\nconst CAPACITY_INDEX = 4;\nconst TYPE_INDEX = 5;\n\nexport default class SharedMap<K extends string | number, V extends NumericArray = Uint32Array> {\n\tstatic readonly ALLOCATE_COUNT = 6;\n\n\tprivate memory: MemoryHeap;\n\tprivate pointerMemory: AllocatedMemory;\n\t// False when pointerMemory is a slice of another structure's memory (e.g. inlined in SharedQuadtree); that owner frees it\n\tprivate ownsFirstBlock = true;\n\tprivate lock: Int32Array;\n\t// Value type is written once at creation and never changes, so resolve it once instead of reading metadata per op\n\tprivate cachedType: number;\n\t// u32 units per value slot: 1 for 32-bit types, 2 for 64-bit (float64/int64/uint64). The values region and every\n\t// table allocation scale by this.\n\tprivate cachedValueUnits = 1;\n\n\t// Cache the resolved views so most operations avoid rebuilding them. Rebuild whenever the table pointer OR the capacity\n\t// changes: the pointer alone is not enough because the allocator recycles freed addresses (split:false), so a rehash can\n\t// land a differently-sized table at the same address (ABA). The cached views are plain windows into shared memory, so\n\t// they always reflect the current bytes at that address; only the derived capacity (mask/offsets) can go stale, and the\n\t// capacity check catches exactly that.\n\tprivate cachedTablePointer = 0;\n\tprivate cachedTableMemory?: AllocatedMemory;\n\tprivate cachedSlots?: Uint32Array; // whole block as u32: states region, then keys region\n\tprivate cachedValues?: V; // typed view over the values region\n\tprivate cachedCapacity = 0;\n\t// Callers hold the lock, so the acquire fence already published the latest pointer/capacity - plain reads are enough\n\t// and avoid two Atomics.load per operation on the hot path.\n\tprivate ensureTable() {\n\t\tlet data = this.pointerMemory.data;\n\t\tlet pointer = data[TABLE_POINTER_INDEX];\n\t\tlet capacity = data[CAPACITY_INDEX];\n\t\tif(pointer === this.cachedTablePointer && capacity === this.cachedCapacity && this.cachedSlots) {\n\t\t\treturn;\n\t\t}\n\n\t\tlet table = new AllocatedMemory(this.memory, getPointer(pointer, this.memory.positionBits));\n\t\tthis.cachedTableMemory = table;\n\t\tthis.cachedSlots = table.data;\n\t\tthis.cachedValues = this.makeValuesView(table, capacity);\n\t\tthis.cachedCapacity = capacity;\n\t\tthis.cachedTablePointer = pointer;\n\t}\n\tprivate makeValuesView(table: AllocatedMemory, capacity: number): V {\n\t\t// states and keys each take one u32 per slot, so the values region starts after 2 * capacity u32s\n\t\tlet byteOffset = table.bufferByteOffset + capacity * 2 * Uint32Array.BYTES_PER_ELEMENT;\n\t\treturn makeArrayView(this.cachedType, table.data.buffer, byteOffset, capacity) as V;\n\t}\n\t// u32s a table of this capacity needs: states + keys (one each per slot) plus the values region, which is\n\t// cachedValueUnits u32s per slot.\n\tprivate tableLength(capacity: number): number {\n\t\treturn capacity * (2 + this.cachedValueUnits);\n\t}\n\n\tget length(): number {\n\t\treturn Atomics.load(this.pointerMemory.data, LENGTH_INDEX);\n\t}\n\tget capacity(): number {\n\t\treturn Atomics.load(this.pointerMemory.data, CAPACITY_INDEX);\n\t}\n\tget maxHash(): number {\n\t\treturn this.capacity;\n\t}\n\tget type(): number {\n\t\treturn this.cachedType;\n\t}\n\t// Metadata block plus the single table allocation the map currently points at.\n\tget usedMemory(): number {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tthis.ensureTable();\n\t\t\t// When inlined, the owner already counts pointerMemory as part of its own firstBlock\n\t\t\tlet ownMemory = this.ownsFirstBlock ? this.pointerMemory.usedMemory : 0;\n\t\t\treturn ownMemory + this.cachedTableMemory!.usedMemory;\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedMapConfig<V> | SharedMapMemory | SharedMapMemory & SharedMapConfig<V>) {\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 SharedQuadtree) for us to initialize in\n\t\t\t// place; a plain pointer is the serialized clone path, whose table already exists and is read from metadata.\n\t\t\tif(config.firstBlock instanceof AllocatedMemory) {\n\t\t\t\tthis.pointerMemory = config.firstBlock;\n\t\t\t\tthis.initialize(config);\n\t\t\t\tthis.ownsFirstBlock = false;\n\t\t\t} else {\n\t\t\t\tthis.pointerMemory = new AllocatedMemory(memory, { length: SharedMap.ALLOCATE_COUNT, ...config.firstBlock });\n\t\t\t}\n\t\t} else {\n\t\t\tthis.pointerMemory = memory.allocUI32(SharedMap.ALLOCATE_COUNT);\n\t\t\tthis.initialize(config);\n\t\t}\n\t\tthis.lock = new Int32Array(this.pointerMemory.data.buffer, this.pointerMemory.bufferByteOffset + LOCK_INDEX * Uint32Array.BYTES_PER_ELEMENT, SIMPLE_LOCK_ALLOCATE_COUNT);\n\t\tthis.cachedType = Atomics.load(this.pointerMemory.data, TYPE_INDEX);\n\t\tthis.cachedValueUnits = getByteMultipler(this.cachedType);\n\t}\n\n\t// Allocate the initial table and write the metadata. pointerMemory must already be set. Used by both the fresh-alloc\n\t// path and the inline-in-place path (where pointerMemory is a slice reserved by the owner).\n\tprivate initialize(config?: SharedMapConfig<V> | SharedMapMemory) {\n\t\tlet type = config && 'type' in config ? config.type : undefined;\n\t\tlet typeCode = getArrayTypeCode(type ?? Uint32Array);\n\t\tthis.cachedValueUnits = getByteMultipler(typeCode);\n\t\tlet requestedCapacity = config && 'capacity' in config ? config.capacity : undefined;\n\t\tlet capacity = DEFAULT_CAPACITY;\n\t\twhile(capacity < (requestedCapacity ?? DEFAULT_CAPACITY)) {\n\t\t\tcapacity *= 2;\n\t\t}\n\t\tif(capacity * (2 + this.cachedValueUnits) > this.memory.maxAllocationLength) {\n\t\t\tthrow new Error(`SharedMap capacity ${capacity} exceeds the largest contiguous allocation`);\n\t\t}\n\t\tlet table = this.memory.allocUI32(capacity * (2 + this.cachedValueUnits));\n\t\tstoreRawPointer(this.pointerMemory.data, TABLE_POINTER_INDEX, table.pointer);\n\t\tAtomics.store(this.pointerMemory.data, CAPACITY_INDEX, capacity);\n\t\tAtomics.store(this.pointerMemory.data, TYPE_INDEX, typeCode);\n\t}\n\n\tset(key: K, value: V[number]) {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tthis.setNoLock(key, value);\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tsetAll(entries: Iterable<readonly [K, V[number]]>) {\n\t\tlet batch = Array.from(entries);\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tlet data = this.pointerMemory.data;\n\t\t\tlet projected = data[LENGTH_INDEX] + data[TOMBSTONE_INDEX] + batch.length;\n\t\t\tif(projected * LOAD_DENOMINATOR > data[CAPACITY_INDEX] * LOAD_NUMERATOR) {\n\t\t\t\tlet capacity = data[CAPACITY_INDEX];\n\t\t\t\twhile(projected * LOAD_DENOMINATOR > capacity * LOAD_NUMERATOR) {\n\t\t\t\t\tcapacity *= 2;\n\t\t\t\t}\n\t\t\t\tthis.resize(capacity);\n\t\t\t}\n\t\t\tfor(let [key, value] of batch) {\n\t\t\t\tthis.setNoLock(key, value);\n\t\t\t}\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tprivate setNoLock(key: K, value: V[number]) {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tlet data = this.pointerMemory.data;\n\t\tlet length = data[LENGTH_INDEX];\n\t\tlet tombstones = data[TOMBSTONE_INDEX];\n\t\tif((length + tombstones + 1) * LOAD_DENOMINATOR > data[CAPACITY_INDEX] * LOAD_NUMERATOR) {\n\t\t\tthis.resize();\n\t\t}\n\n\t\tthis.ensureTable();\n\t\tlet slots = this.cachedSlots!;\n\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\tlet capacity = this.cachedCapacity;\n\t\tlet mask = capacity - 1;\n\t\tlet keysBase = capacity;\n\t\tlet idx = mix32(fullHashKey) & mask;\n\t\tlet firstTomb = -1;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\tlet state = slots[idx];\n\t\t\tif(state === EMPTY) {\n\t\t\t\tlet target = firstTomb >= 0 ? firstTomb : idx;\n\t\t\t\tslots[target] = FULL;\n\t\t\t\tslots[keysBase + target] = fullHashKey;\n\t\t\t\tvalues[target] = value;\n\t\t\t\tdata[LENGTH_INDEX]++;\n\t\t\t\tif(firstTomb >= 0) {\n\t\t\t\t\tdata[TOMBSTONE_INDEX]--;\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t} else if(state === TOMBSTONE) {\n\t\t\t\tif(firstTomb < 0) {\n\t\t\t\t\tfirstTomb = idx;\n\t\t\t\t}\n\t\t\t} else if(slots[keysBase + idx] === fullHashKey) {\n\t\t\t\tvalues[idx] = value;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tidx = (idx + 1) & mask;\n\t\t}\n\t}\n\n\t// Atomic read-modify-write under the table lock: resolves the current value (undefined if the key is\n\t// absent), calls updater, and stores the result - so two threads mutating the same key never lose an\n\t// update the way a separate get() + set() can. updater runs inside the lock and must be synchronous and\n\t// must not touch this same map (the lock is not reentrant).\n\tupdate(key: K, updater: (current: V[number] | undefined) => V[number]): V[number] {\n\t\tlet fullHashKey = get32BitHash(key);\n\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tlet data = this.pointerMemory.data;\n\t\t\tlet length = data[LENGTH_INDEX];\n\t\t\tlet tombstones = data[TOMBSTONE_INDEX];\n\t\t\tif((length + tombstones + 1) * LOAD_DENOMINATOR > data[CAPACITY_INDEX] * LOAD_NUMERATOR) {\n\t\t\t\tthis.resize();\n\t\t\t}\n\n\t\t\tthis.ensureTable();\n\t\t\tlet slots = this.cachedSlots!;\n\t\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\t\tlet capacity = this.cachedCapacity;\n\t\t\tlet mask = capacity - 1;\n\t\t\tlet keysBase = capacity;\n\t\t\tlet idx = mix32(fullHashKey) & mask;\n\t\t\tlet firstTomb = -1;\n\t\t\t// eslint-disable-next-line no-constant-condition\n\t\t\twhile(true) {\n\t\t\t\tlet state = slots[idx];\n\t\t\t\tif(state === EMPTY) {\n\t\t\t\t\tlet newValue = updater(undefined);\n\t\t\t\t\tlet target = firstTomb >= 0 ? firstTomb : idx;\n\t\t\t\t\tslots[target] = FULL;\n\t\t\t\t\tslots[keysBase + target] = fullHashKey;\n\t\t\t\t\tvalues[target] = newValue;\n\t\t\t\t\tdata[LENGTH_INDEX]++;\n\t\t\t\t\tif(firstTomb >= 0) {\n\t\t\t\t\t\tdata[TOMBSTONE_INDEX]--;\n\t\t\t\t\t}\n\t\t\t\t\treturn newValue;\n\t\t\t\t} else if(state === TOMBSTONE) {\n\t\t\t\t\tif(firstTomb < 0) {\n\t\t\t\t\t\tfirstTomb = idx;\n\t\t\t\t\t}\n\t\t\t\t} else if(slots[keysBase + idx] === fullHashKey) {\n\t\t\t\t\tlet newValue = updater(values[idx]);\n\t\t\t\t\tvalues[idx] = newValue;\n\t\t\t\t\treturn newValue;\n\t\t\t\t}\n\n\t\t\t\tidx = (idx + 1) & mask;\n\t\t\t}\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tget(key: K): V[number] | undefined {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\treturn this.getNoLock(key);\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tgetAll(keys: Iterable<K>): Array<V[number] | undefined> {\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tlet values: Array<V[number] | undefined> = [];\n\t\t\tfor(let key of keys) {\n\t\t\t\tvalues.push(this.getNoLock(key));\n\t\t\t}\n\t\t\treturn values;\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\tprivate getNoLock(key: K): V[number] | undefined {\n\t\tlet fullHashKey = get32BitHash(key);\n\t\tthis.ensureTable();\n\t\tlet slots = this.cachedSlots!;\n\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\tlet capacity = this.cachedCapacity;\n\t\tlet mask = capacity - 1;\n\t\tlet keysBase = capacity;\n\t\tlet idx = mix32(fullHashKey) & mask;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\tlet state = slots[idx];\n\t\t\tif(state === EMPTY) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tif(state === FULL && slots[keysBase + idx] === fullHashKey) {\n\t\t\t\treturn values[idx];\n\t\t\t}\n\t\t\tidx = (idx + 1) & mask;\n\t\t}\n\t}\n\thas(key: K): boolean {\n\t\treturn this.get(key) !== undefined;\n\t}\n\n\tdelete(key: K): boolean {\n\t\tlet fullHashKey = get32BitHash(key);\n\n\t\tlock(this.lock);\n\t\ttry {\n\t\t\tthis.ensureTable();\n\t\t\tlet data = this.pointerMemory.data;\n\t\t\tlet slots = this.cachedSlots!;\n\t\t\tlet capacity = this.cachedCapacity;\n\t\t\tlet mask = capacity - 1;\n\t\t\tlet keysBase = capacity;\n\t\t\tlet idx = mix32(fullHashKey) & mask;\n\t\t\t// eslint-disable-next-line no-constant-condition\n\t\t\twhile(true) {\n\t\t\t\tlet state = slots[idx];\n\t\t\t\tif(state === EMPTY) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\tif(state === FULL && slots[keysBase + idx] === fullHashKey) {\n\t\t\t\t\tslots[idx] = TOMBSTONE;\n\t\t\t\t\tdata[LENGTH_INDEX]--;\n\t\t\t\t\tdata[TOMBSTONE_INDEX]++;\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tidx = (idx + 1) & mask;\n\t\t\t}\n\t\t} finally {\n\t\t\tunlock(this.lock);\n\t\t}\n\t}\n\n\t// Best-effort scan (see class note): not lock-guarded, so must not run concurrently with writes from another thread.\n\t*[Symbol.iterator](): IterableIterator<[number, V[number]]> {\n\t\tthis.ensureTable();\n\t\tlet slots = this.cachedSlots!;\n\t\tlet values = this.cachedValues! as NumericArrayIO;\n\t\tlet capacity = this.cachedCapacity;\n\t\tlet keysBase = capacity;\n\t\tfor(let i = 0; i < capacity; i++) {\n\t\t\tif(slots[i] === FULL) {\n\t\t\t\tyield [slots[keysBase + i], values[i]];\n\t\t\t}\n\t\t}\n\t}\n\n\t// Rehash into a fresh table. Doubles capacity when live entries are crowding the table; when tombstones are the cause,\n\t// a same-size table already reclaims them. Caller must hold the lock.\n\tprivate resize(requestedCapacity?: number) {\n\t\tthis.ensureTable();\n\t\tlet oldTable = this.cachedTableMemory!;\n\t\tlet oldSlots = this.cachedSlots!;\n\t\tlet oldValues = this.cachedValues! as NumericArrayIO;\n\t\tlet oldCapacity = this.cachedCapacity;\n\t\tlet oldKeysBase = oldCapacity;\n\t\tlet length = Atomics.load(this.pointerMemory.data, LENGTH_INDEX);\n\n\t\tlet newCapacity = requestedCapacity ?? oldCapacity;\n\t\tif(requestedCapacity === undefined && (length + 1) * LOAD_DENOMINATOR > oldCapacity * LOAD_NUMERATOR) {\n\t\t\tnewCapacity = oldCapacity * 2;\n\t\t}\n\t\t// A table is one contiguous allocation, so it can never exceed what a single buffer can hand out. Fail here with an\n\t\t// actionable message instead of letting allocUI32 throw a cryptic \"Unable to allocate N numbers\" from deep inside the\n\t\t// heap. Mirrors the same guard in initialize().\n\t\tif(this.tableLength(newCapacity) > this.memory.maxAllocationLength) {\n\t\t\tlet maxEntries = Math.floor(this.memory.maxAllocationLength / (2 + this.cachedValueUnits) * LOAD_NUMERATOR / LOAD_DENOMINATOR);\n\t\t\tthrow new Error(`SharedMap can't grow to capacity ${newCapacity}: its table would exceed the largest contiguous `\n\t\t\t\t+ `allocation (${this.memory.maxAllocationLength} u32s, ~${maxEntries} entries). Use larger buffers or shard across multiple maps.`);\n\t\t}\n\t\tlet newTable = this.memory.allocUI32(this.tableLength(newCapacity));\n\t\tlet newSlots = newTable.data;\n\t\tlet newValues = this.makeValuesView(newTable, newCapacity) as NumericArrayIO;\n\t\tlet mask = newCapacity - 1;\n\t\tlet newKeysBase = newCapacity;\n\n\t\tfor(let i = 0; i < oldCapacity; i++) {\n\t\t\tif(oldSlots[i] !== FULL) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet key = oldSlots[oldKeysBase + i];\n\t\t\tlet value = oldValues[i];\n\t\t\tlet idx = mix32(key) & mask;\n\t\t\t// Fresh table has no tombstones, so probe straight to the first empty slot\n\t\t\twhile(newSlots[idx] !== EMPTY) {\n\t\t\t\tidx = (idx + 1) & mask;\n\t\t\t}\n\t\t\tnewSlots[idx] = FULL;\n\t\t\tnewSlots[newKeysBase + idx] = key;\n\t\t\tnewValues[idx] = value;\n\t\t}\n\n\t\tstoreRawPointer(this.pointerMemory.data, TABLE_POINTER_INDEX, newTable.pointer);\n\t\tAtomics.store(this.pointerMemory.data, CAPACITY_INDEX, newCapacity);\n\t\tAtomics.store(this.pointerMemory.data, TOMBSTONE_INDEX, 0);\n\t\t// Invalidate the cached views so the next access rebuilds against the new table\n\t\tthis.cachedTablePointer = 0;\n\t\tthis.cachedSlots = undefined;\n\t\toldTable.free();\n\t}\n\n\tfree() {\n\t\tthis.ensureTable();\n\t\tthis.cachedTableMemory!.free();\n\t\tif(this.ownsFirstBlock) {\n\t\t\tthis.pointerMemory.free();\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedMapMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.pointerMemory.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedMapConfig<V extends NumericArray> {\n\ttype?: TypedArrayConstructor<V>\n\tcapacity?: number\n}\ninterface SharedMapMemory {\n\tfirstBlock: SharedAllocatedMemory | AllocatedMemory\n}\n\nexport type { SharedMapConfig, SharedMapMemory };\n"],"mappings":";;;;;;AAqBA,IAAM,IAAQ,GACR,IAAO,GACP,IAAY,GAEZ,IAAmB,IAEnB,IAAiB,GACjB,IAAmB,GAGnB,IAAsB,GACtB,IAAa,GACb,IAAe,GACf,IAAkB,GAClB,IAAiB,GACjB,IAAa,GAEE,IAArB,MAAqB,EAA2E;CAC/F,OAAgB,iBAAiB;CAEjC;CACA;CAEA,iBAAyB;CACzB;CAEA;CAGA,mBAA2B;CAO3B,qBAA6B;CAC7B;CACA;CACA;CACA,iBAAyB;CAGzB,cAAsB;E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,KAAK,YAAY,CAAW,CAAC,GAC9D,IAAW,EAAS,MACpB,IAAY,KAAK,eAAe,GAAU,CAAW,GACrD,IAAO,IAAc,GACrB,IAAc;EAElB,KAAI,IAAI,IAAI,GAAG,IAAI,GAAa,KAAK;GACpC,IAAG,EAAS,OAAO,GAClB;GAGD,IAAI,IAAM,EAAS,IAAc,IAC7B,IAAQ,EAAU,IAClB,IAAM,EAAM,CAAG,IAAI;GAEvB,OAAM,EAAS,OAAS,IACvB,IAAO,IAAM,IAAK;GAInB,AAFA,EAAS,KAAO,GAChB,EAAS,IAAc,KAAO,GAC9B,EAAU,KAAO;EAClB;EAQA,AANA,EAAgB,KAAK,cAAc,MAAM,GAAqB,EAAS,OAAO,GAC9E,QAAQ,MAAM,KAAK,cAAc,MAAM,GAAgB,CAAW,GAClE,QAAQ,MAAM,KAAK,cAAc,MAAM,GAAiB,CAAC,GAEzD,KAAK,qBAAqB,GAC1B,KAAK,cAAc,KAAA,GACnB,EAAS,KAAK;CACf;CAEA,OAAO;EAGN,AAFA,KAAK,YAAY,GACjB,KAAK,kBAAmB,KAAK,GAC1B,KAAK,kBACP,KAAK,cAAc,KAAK;CAE1B;CAEA,kBAAmC;EAClC,OAAO,EACN,YAAY,KAAK,cAAc,gBAAgB,EAChD;CACD;AACD"}
|
package/dist/shared-pool.js
CHANGED
|
@@ -56,7 +56,10 @@ var u = 0, d = 1, f = 2, p = 3, m = 4, h = 5, g = 100, _ = 1e6, v = 1e5, y = cla
|
|
|
56
56
|
return t + this.pointerStack.usedMemory + this.recycleStack.usedMemory;
|
|
57
57
|
}
|
|
58
58
|
constructor(n, i) {
|
|
59
|
-
this.memory = n, i && "firstBlock" in i ? (this.firstBlock = i.firstBlock instanceof r ? i.firstBlock : new r(n,
|
|
59
|
+
this.memory = n, i && "firstBlock" in i ? (this.firstBlock = i.firstBlock instanceof r ? i.firstBlock : new r(n, {
|
|
60
|
+
length: y.ALLOCATE_COUNT,
|
|
61
|
+
...i.firstBlock
|
|
62
|
+
}), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + d * Uint32Array.BYTES_PER_ELEMENT, 2), "type" in i || "dataLength" in i || "maxLength" in i || "maxChunkSize" in i || "maxRecycledLength" in i ? (this.initialize(i), this.ownsFirstBlock = !1) : (this.pointerStack = new l(n, { firstBlock: e(t(this.firstBlock.data, m), n.positionBits) }), this.recycleStack = new l(n, { firstBlock: e(t(this.firstBlock.data, h), n.positionBits) }), this.ownsFirstBlock = !0)) : (this.firstBlock = n.allocUI32(y.getAllocateCount(n, i)), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + d * Uint32Array.BYTES_PER_ELEMENT, 2), this.initialize(i), this.ownsFirstBlock = !0), this.cachedType = this.uint16Array[0], this.cachedDataLength = Math.max(1, this.uint16Array[1]), this.cachedMaxChunkSize = this.firstBlock.data[f], this.cachedByteMultipler = a(this.cachedType), this.maxLength = this.cachedMaxChunkSize * this.pointerStack.maxLength, this.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + p * Uint32Array.BYTES_PER_ELEMENT, 1);
|
|
60
63
|
}
|
|
61
64
|
initialize(e) {
|
|
62
65
|
let t = e?.dataLength ?? 1, r = e?.maxChunkSize ?? g, o = e?.maxLength ?? _, s = e?.maxRecycledLength ?? v, c = l.getMaxSegmentLength(this.memory.maxAllocationLength), u = o / r, d = l.getAllocateCount(u, void 0, c), f = l.getAllocateCount(s, void 0, c);
|
package/dist/shared-pool.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-pool.js","names":[],"sources":["../src/shared-pool.ts"],"sourcesContent":["import type { SharedAllocatedMemory } from './allocated-memory';\nimport AllocatedMemory from './allocated-memory';\nimport { lock, unlock } from './lock/simple-lock';\nimport type MemoryHeap from './memory-heap';\nimport SharedStack from './shared-stack';\nimport { getPointer, loadRawPointer, storeRawPointer } from './utils/pointer';\nimport { getArrayTypeCode, getByteMultipler, makeArrayView, type NumericArray, type NumericArrayIO, type TypedArrayConstructor } from './utils/array-type';\n\nconst LENGTH_INDEX = 0;\nconst TYPE_INDEX = 1;\nconst MAX_CHUNK_SIZE_INDEX = 2;\n// Guards appending a chunk pointer so only one thread grows the pointer stack at a time\nconst GROW_LOCK_INDEX = 3;\n// Pointers to the two backing SharedStacks (their own firstBlocks): the chunk pointers and the recycled indexes\nconst POINTER_STACK_INDEX = 4;\nconst RECYCLE_STACK_INDEX = 5;\n\nconst DEFAULT_MAX_CHUNK_SIZE = 100;\nconst DEFAULT_MAX_LENGTH = 1_000_000;\nconst DEFAULT_MAX_RECYCLED_LENGTH = 100_000;\n\n// Array with stable indexes and maximum contiguous memory sizes (necessary to fit large data sets into max 1MB buffers).\n// The chunk pointers and the recycled indexes both live in SharedStacks, whose segments are never moved or freed once\n// published, so concurrent readers never touch memory that is being reclaimed.\n// https://plflib.org/colony.htm for future enhancements - it seems to be an optimized version of what we were aiming for with this\nexport default class SharedPool<T extends NumericArray = Uint32Array> implements Iterable<T> {\n\tstatic readonly ALLOCATE_COUNT = 6;\n\n\t// u32s a pool with this config needs for its firstBlock, including both inlined stacks. Lets an owner (e.g.\n\t// SharedQuadtree) reserve one contiguous slice and hand it back through the config's firstBlock to inline the pool.\n\tstatic getAllocateCount(memory: MemoryHeap, config?: SharedPoolConfig<NumericArray>): number {\n\t\tlet maxChunkSize = config?.maxChunkSize ?? DEFAULT_MAX_CHUNK_SIZE;\n\t\tlet maxLength = config?.maxLength ?? DEFAULT_MAX_LENGTH;\n\t\tlet maxRecycledLength = config?.maxRecycledLength ?? DEFAULT_MAX_RECYCLED_LENGTH;\n\t\tlet maxSegmentLength = SharedStack.getMaxSegmentLength(memory.maxAllocationLength);\n\t\tlet pointerStackAllocateCount = SharedStack.getAllocateCount(maxLength / maxChunkSize, undefined, maxSegmentLength);\n\t\tlet recycleStackAllocateCount = SharedStack.getAllocateCount(maxRecycledLength, undefined, maxSegmentLength);\n\t\treturn SharedPool.ALLOCATE_COUNT + pointerStackAllocateCount + recycleStackAllocateCount;\n\t}\n\n\tprivate memory: MemoryHeap;\n\n\t// Current Length, Type/DataLength, MaxChunkLength, Grow lock, Pointer stack, Recycle stack\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\tprivate growLock: Int32Array;\n\tprivate pointerStack!: SharedStack;\n\tprivate recycleStack!: SharedStack;\n\treadonly maxLength: number;\n\n\t// False when firstBlock is a slice of another structure's memory (e.g. inlined in SharedQuadtree); that owner frees it\n\tprivate ownsFirstBlock: boolean;\n\n\t// Chunk views cached by pointer index. An array (dense, small integer keys) resolves faster than an object on the hot\n\t// get/set path and never needs rehashing.\n\tprivate cachedFullDataBlock: Array<T | undefined> = [];\n\n\t// Config is written once at construction and never mutated, so it is cached in plain fields\n\tprivate readonly cachedMaxChunkSize: number;\n\tprivate readonly cachedDataLength: number;\n\tprivate readonly cachedType: number;\n\tprivate readonly cachedByteMultipler: number;\n\n\tget length(): number {\n\t\treturn Atomics.load(this.firstBlock.data, LENGTH_INDEX) - this.recycleStack.length;\n\t}\n\tget maxChunkSize(): number {\n\t\treturn this.cachedMaxChunkSize;\n\t}\n\tprivate set maxChunkSize(value: number) {\n\t\tAtomics.store(this.firstBlock.data, MAX_CHUNK_SIZE_INDEX, value);\n\t}\n\n\tget type(): number {\n\t\treturn this.cachedType;\n\t}\n\tprivate set type(value: number) {\n\t\tAtomics.store(this.uint16Array, 0, value);\n\t}\n\tget dataLength(): number {\n\t\treturn this.cachedDataLength;\n\t}\n\tprivate set dataLength(value: number) {\n\t\tAtomics.store(this.uint16Array, 1, value);\n\t}\n\n\tget bufferLength(): number {\n\t\treturn this.cachedMaxChunkSize * this.pointerStack.length;\n\t}\n\tget byteMultipler(): number {\n\t\treturn this.cachedByteMultipler;\n\t}\n\n\t// Own internal memory (which already includes both stacks' inlined firstBlocks) plus every chunk buffer and the\n\t// segments each stack has grown. The stacks' usedMemory excludes their inlined firstBlocks, so they aren't double-counted.\n\tget usedMemory(): number {\n\t\tlet total = this.ownsFirstBlock ? this.firstBlock.usedMemory : 0;\n\t\tfor(let pointer of this.pointerStack) {\n\t\t\ttotal += new AllocatedMemory(this.memory, getPointer(pointer, this.memory.positionBits)).usedMemory;\n\t\t}\n\t\treturn total + this.pointerStack.usedMemory + this.recycleStack.usedMemory;\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedPoolConfig<T> | SharedPoolMemory | SharedPoolMemory & SharedPoolConfig<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 SharedQuadtree); 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 || 'dataLength' in config || 'maxLength' in config || 'maxChunkSize' in config || 'maxRecycledLength' 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.pointerStack = new SharedStack(memory, {\n\t\t\t\t\tfirstBlock: getPointer(loadRawPointer(this.firstBlock.data, POINTER_STACK_INDEX), memory.positionBits),\n\t\t\t\t});\n\t\t\t\tthis.recycleStack = new SharedStack(memory, {\n\t\t\t\t\tfirstBlock: getPointer(loadRawPointer(this.firstBlock.data, RECYCLE_STACK_INDEX), memory.positionBits),\n\t\t\t\t});\n\t\t\t\tthis.ownsFirstBlock = true;\n\t\t\t}\n\t\t} else {\n\t\t\tthis.firstBlock = memory.allocUI32(SharedPool.getAllocateCount(memory, config));\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.initialize(config);\n\t\t\tthis.ownsFirstBlock = true;\n\t\t}\n\n\t\tthis.cachedType = this.uint16Array[0];\n\t\tthis.cachedDataLength = Math.max(1, this.uint16Array[1]);\n\t\tthis.cachedMaxChunkSize = this.firstBlock.data[MAX_CHUNK_SIZE_INDEX];\n\t\tthis.cachedByteMultipler = getByteMultipler(this.cachedType);\n\t\tthis.maxLength = this.cachedMaxChunkSize * this.pointerStack.maxLength;\n\n\t\tthis.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + GROW_LOCK_INDEX * Uint32Array.BYTES_PER_ELEMENT, 1);\n\t}\n\n\t// Lay out the two inlined stacks (their firstBlocks sliced from ours), record their pointers, and seed the first chunk.\n\t// firstBlock and uint16Array must already be set. Used by both the fresh-alloc path and the inline-in-place path.\n\tprivate initialize(config?: SharedPoolConfig<T>) {\n\t\tlet dataLength = config?.dataLength ?? 1;\n\t\tlet maxChunkSize = config?.maxChunkSize ?? DEFAULT_MAX_CHUNK_SIZE;\n\t\tlet maxLength = config?.maxLength ?? DEFAULT_MAX_LENGTH;\n\t\tlet maxRecycledLength = config?.maxRecycledLength ?? DEFAULT_MAX_RECYCLED_LENGTH;\n\n\t\t// Each stack sizes its inlined firstBlock from its own maxLength, so compute both up front to lay them out. The\n\t\t// stacks cap their segments at what a buffer can hold, so size the inlined blocks against that same cap.\n\t\tlet maxSegmentLength = SharedStack.getMaxSegmentLength(this.memory.maxAllocationLength);\n\t\tlet pointerStackMaxLength = maxLength / maxChunkSize;\n\t\tlet pointerStackAllocateCount = SharedStack.getAllocateCount(pointerStackMaxLength, undefined, maxSegmentLength);\n\t\tlet recycleStackAllocateCount = SharedStack.getAllocateCount(maxRecycledLength, undefined, maxSegmentLength);\n\n\t\t// Inline both stacks' firstBlocks right after the pool header so the whole spine is one contiguous allocation\n\t\tthis.pointerStack = new SharedStack(this.memory, {\n\t\t\tfirstBlock: this.firstBlock.getSubAllocation(SharedPool.ALLOCATE_COUNT, pointerStackAllocateCount),\n\t\t\ttype: Uint32Array,\n\t\t\tmaxLength: pointerStackMaxLength,\n\t\t});\n\t\tstoreRawPointer(this.firstBlock.data, POINTER_STACK_INDEX, this.pointerStack.pointer);\n\t\tthis.recycleStack = new SharedStack(this.memory, {\n\t\t\tfirstBlock: this.firstBlock.getSubAllocation(SharedPool.ALLOCATE_COUNT + pointerStackAllocateCount, recycleStackAllocateCount),\n\t\t\ttype: Uint32Array,\n\t\t\tmaxLength: maxRecycledLength,\n\t\t});\n\t\tstoreRawPointer(this.firstBlock.data, RECYCLE_STACK_INDEX, this.recycleStack.pointer);\n\n\t\tlet typeCode = getArrayTypeCode(config?.type ?? Uint32Array);\n\t\tthis.type = typeCode;\n\t\tthis.dataLength = dataLength;\n\t\tthis.maxChunkSize = maxChunkSize;\n\n\t\tlet byteMultipler = getByteMultipler(typeCode);\n\t\tlet firstArray = this.memory.allocUI32(maxChunkSize * dataLength * byteMultipler);\n\t\tthis.pointerStack.push(firstArray.pointer);\n\t}\n\n\tat(index: number): T {\n\t\tlet dataBlock = this.getFullDataBlock(index);\n\t\treturn this.getDataBlock(dataBlock, index % this.cachedMaxChunkSize);\n\t}\n\tget(index: number, dataIndex = 0): T[number] {\n\t\tconst dataLength = this.cachedDataLength;\n\t\tif(dataIndex >= dataLength) {\n\t\t\tthrow new Error(`${dataIndex} is out of dataLength bounds ${dataLength}`);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock(index) as NumericArrayIO;\n\t\treturn dataBlock[(index % this.cachedMaxChunkSize) * dataLength + dataIndex];\n\t}\n\tset(index: number, dataIndex: number, value: T[number]) {\n\t\tconst dataLength = this.cachedDataLength;\n\t\tif(dataIndex >= dataLength) {\n\t\t\tthrow new Error(`${dataIndex} is out of dataLength bounds ${dataLength}`);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock(index) as NumericArrayIO;\n\t\tdataBlock[(index % this.cachedMaxChunkSize) * dataLength + dataIndex] = value;\n\t}\n\n\t// Resolve the chunk view holding an index once. Callers touching several fields of the same record read/write straight\n\t// into this view at blockOffset(index) + fieldIndex, avoiding a repeated chunk lookup per field.\n\tblockFor(index: number): T {\n\t\treturn this.getFullDataBlock(index);\n\t}\n\t// Base slot of a record within the view blockFor(index) returns.\n\tblockOffset(index: number): number {\n\t\treturn (index % this.cachedMaxChunkSize) * this.cachedDataLength;\n\t}\n\n\tpush(values: T[number] | Array<T[number]>): number {\n\t\tlet dataLength = this.cachedDataLength;\n\t\tconst isSingleValue = typeof values !== 'object';\n\t\tif(!isSingleValue && values.length > dataLength) {\n\t\t\tthrow new Error(`Can't insert ${values.length} array into shared list of ${dataLength} dataLength`);\n\t\t}\n\n\t\t// Reuse a recycled index if one is available. pop() can come back undefined when another thread drained the last\n\t\t// slot between our two calls, so fall through to a fresh index rather than trusting a length() read\n\t\tlet newIndex = this.recycleStack.length === 0 ? undefined : this.recycleStack.pop();\n\t\tif(newIndex === undefined) {\n\t\t\tnewIndex = Atomics.add(this.firstBlock.data, LENGTH_INDEX, 1);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock(newIndex) as NumericArrayIO;\n\t\tlet startIndex = dataLength * (newIndex % this.cachedMaxChunkSize);\n\t\tif(isSingleValue) {\n\t\t\tdataBlock[startIndex] = values;\n\t\t} else {\n\t\t\tfor(let i = 0; i < values.length; i++) {\n\t\t\t\tdataBlock[startIndex + i] = values[i];\n\t\t\t}\n\t\t}\n\n\t\treturn newIndex;\n\t}\n\n\treserveContiguous(count: number): number {\n\t\tif(!Number.isInteger(count) || count < 0) {\n\t\t\tthrow new Error(`Can't reserve ${count} pool entries`);\n\t\t}\n\t\treturn Atomics.add(this.firstBlock.data, LENGTH_INDEX, count);\n\t}\n\n\tdeleteIndex(index: number) {\n\t\tthis.recycleStack.push(index);\n\t}\n\n\tclear() {\n\t\tthis.firstBlock.data[LENGTH_INDEX] = 0;\n\t\tthis.recycleStack.clear();\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tconst recycledValues: { [key: number]: true } = {};\n\t\tfor(let value of this.recycleStack) {\n\t\t\trecycledValues[value] = true;\n\t\t}\n\n\t\tconst maxChunkSize = this.cachedMaxChunkSize;\n\t\t// Snapshot the length once - iteration over a concurrently mutated pool is a snapshot anyway, and re-loading the\n\t\t// atomic every step is pure overhead\n\t\tconst length = Atomics.load(this.firstBlock.data, LENGTH_INDEX);\n\t\tlet dataBlock = this.getFullDataBlock(0);\n\t\tlet dataBlockIndex = 0;\n\t\tfor(let i = 0; i < length; i++) {\n\t\t\tif(!recycledValues[i]) {\n\t\t\t\tlet newDataBlockIndex = Math.floor(i / maxChunkSize);\n\t\t\t\tif(newDataBlockIndex !== dataBlockIndex) {\n\t\t\t\t\tdataBlock = this.getFullDataBlock(i);\n\t\t\t\t\tdataBlockIndex = newDataBlockIndex;\n\t\t\t\t}\n\t\t\t\tyield this.getDataBlock(dataBlock, i % maxChunkSize);\n\t\t\t}\n\t\t}\n\t}\n\n\t*entries(): IterableIterator<[number, T]> {\n\t\tconst recycledValues: { [key: number]: true } = {};\n\t\tfor(let value of this.recycleStack) {\n\t\t\trecycledValues[value] = true;\n\t\t}\n\n\t\tconst maxChunkSize = this.cachedMaxChunkSize;\n\t\tconst length = Atomics.load(this.firstBlock.data, LENGTH_INDEX);\n\t\tlet dataBlock = this.getFullDataBlock(0);\n\t\tlet dataBlockIndex = 0;\n\t\tfor(let i = 0; i < length; i++) {\n\t\t\tif(!recycledValues[i]) {\n\t\t\t\tlet newDataBlockIndex = Math.floor(i / maxChunkSize);\n\t\t\t\tif(newDataBlockIndex !== dataBlockIndex) {\n\t\t\t\t\tdataBlock = this.getFullDataBlock(i);\n\t\t\t\t\tdataBlockIndex = newDataBlockIndex;\n\t\t\t\t}\n\t\t\t\tyield [i, this.getDataBlock(dataBlock, i % maxChunkSize)];\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getFullDataBlock(index: number) {\n\t\tlet pointerIndex = Math.floor(index / this.cachedMaxChunkSize);\n\t\tlet cachedDataBlock = this.cachedFullDataBlock[pointerIndex];\n\t\tif(cachedDataBlock) {\n\t\t\treturn cachedDataBlock;\n\t\t}\n\t\tif(pointerIndex >= this.pointerStack.length) {\n\t\t\t// Only let a single thread append a new chunk at a time - otherwise two concurrent inserts landing in a new\n\t\t\t// chunk would each allocate + push a buffer, orphaning one and leaking memory\n\t\t\tlock(this.growLock);\n\t\t\ttry {\n\t\t\t\t// Re-check under the lock: another thread may have already appended the chunk while we were waiting\n\t\t\t\twhile(pointerIndex >= this.pointerStack.length) {\n\t\t\t\t\tlet newArray = this.memory.allocUI32(this.cachedMaxChunkSize * this.cachedDataLength * this.cachedByteMultipler);\n\t\t\t\t\tthis.pointerStack.push(newArray.pointer);\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tunlock(this.growLock);\n\t\t\t}\n\t\t}\n\n\t\tlet array = new AllocatedMemory(this.memory, getPointer(this.pointerStack.at(pointerIndex), this.memory.positionBits));\n\n\t\tlet blockLength = this.cachedDataLength * this.cachedMaxChunkSize;\n\t\tlet data = makeArrayView(this.cachedType, array.data.buffer, array.bufferByteOffset, blockLength) as T;\n\n\t\tthis.cachedFullDataBlock[pointerIndex] = data;\n\t\treturn data;\n\t}\n\n\tprivate getDataBlock(rawData: T, index: number): T {\n\t\tconst dataLength = this.cachedDataLength;\n\t\tconst start = index * dataLength;\n\t\treturn rawData.subarray(start, start + dataLength) as T;\n\t}\n\n\tfree() {\n\t\tthis.recycleStack.free();\n\n\t\tfor(let pointer of this.pointerStack) {\n\t\t\tlet memory = new AllocatedMemory(this.memory, getPointer(pointer, this.memory.positionBits));\n\t\t\tmemory.free();\n\t\t}\n\t\tthis.pointerStack.free();\n\t\tif(this.ownsFirstBlock) {\n\t\t\tthis.firstBlock.free();\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedPoolMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.firstBlock.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedPoolConfig<T extends NumericArray> {\n\tmaxChunkSize?: number\n\tmaxLength?: number\n\tmaxRecycledLength?: number\n\ttype?: TypedArrayConstructor<T>\n\tdataLength?: number\n}\ninterface SharedPoolMemory {\n\tfirstBlock: SharedAllocatedMemory | AllocatedMemory\n}\n\nexport type { SharedPoolConfig, SharedPoolMemory };\n"],"mappings":";;;;;;AAQA,IAAM,IAAe,GACf,IAAa,GACb,IAAuB,GAEvB,IAAkB,GAElB,IAAsB,GACtB,IAAsB,GAEtB,IAAyB,KACzB,IAAqB,KACrB,IAA8B,KAMf,IAArB,MAAqB,EAAwE;CAC5F,OAAgB,iBAAiB;CAIjC,OAAO,iBAAiB,GAAoB,GAAiD;EAC5F,IAAI,IAAe,GAAQ,gBAAgB,GACvC,IAAY,GAAQ,aAAa,GACjC,IAAoB,GAAQ,qBAAqB,GACjD,IAAmB,EAAY,oBAAoB,EAAO,mBAAmB,GAC7E,IAA4B,EAAY,iBAAiB,IAAY,GAAc,KAAA,GAAW,CAAgB,GAC9G,IAA4B,EAAY,iBAAiB,GAAmB,KAAA,GAAW,CAAgB;EAC3G,OAAO,EAAW,iBAAiB,IAA4B;CAChE;CAEA;CAGA;CACA;CACA;CACA;CACA;CACA;CAGA;CAIA,sBAAoD,CAAC;CAGrD;CACA;CACA;CACA;CAEA,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAAK,KAAK,WAAW,MAAM,CAAY,IAAI,KAAK,aAAa;CAC7E;CACA,IAAI,eAAuB;EAC1B,OAAO,KAAK;CACb;CACA,IAAY,aAAa,GAAe;EACvC,QAAQ,MAAM,KAAK,WAAW,MAAM,GAAsB,CAAK;CAChE;CAEA,IAAI,OAAe;EAClB,OAAO,KAAK;CACb;CACA,IAAY,KAAK,GAAe;EAC/B,QAAQ,MAAM,KAAK,aAAa,GAAG,CAAK;CACzC;CACA,IAAI,aAAqB;EAC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,IAAI,CAAY,CAAC;EACzD;CAEF;CAEA,iBAAyB,GAAe;EACvC,IAAI,IAAe,KAAK,MAAM,IAAQ,KAAK,kBAAkB,GACzD,IAAkB,KAAK,oBAAoB;EAC/C,IAAG,GACF,OAAO;EAER,IAAG,KAAgB,KAAK,aAAa,QAAQ;GAG5C,EAAK,KAAK,QAAQ;GAClB,IAAI;IAEH,OAAM,KAAgB,KAAK,aAAa,SAAQ;KAC/C,IAAI,IAAW,KAAK,OAAO,UAAU,KAAK,qBAAqB,KAAK,mBAAmB,KAAK,mBAAmB;KAC/G,KAAK,aAAa,KAAK,EAAS,OAAO;IACxC;GACD,UAAU;IACT,EAAO,KAAK,QAAQ;GACrB;EACD;EAEA,IAAI,IAAQ,IAAI,EAAgB,KAAK,QAAQ,EAAW,KAAK,aAAa,GAAG,CAAY,GAAG,KAAK,OAAO,YAAY,CAAC,GAEjH,IAAc,KAAK,mBAAmB,KAAK,oBAC3C,IAAO,EAAc,KAAK,YAAY,EAAM,KAAK,QAAQ,EAAM,kBAAkB,CAAW;EAGhG,OADA,KAAK,oBAAoB,KAAgB,GAClC;CACR;CAEA,aAAqB,GAAY,GAAkB;EAClD,IAAM,IAAa,KAAK,kBAClB,IAAQ,IAAQ;EACtB,OAAO,EAAQ,SAAS,GAAO,IAAQ,CAAU;CAClD;CAEA,OAAO;EACN,KAAK,aAAa,KAAK;EAEvB,KAAI,IAAI,KAAW,KAAK,cAEvB,IADiB,EAAgB,KAAK,QAAQ,EAAW,GAAS,KAAK,OAAO,YAAY,CAC1F,CAAA,CAAO,KAAK;EAGb,AADA,KAAK,aAAa,KAAK,GACpB,KAAK,kBACP,KAAK,WAAW,KAAK;CAEvB;CAEA,kBAAoC;EACnC,OAAO,EACN,YAAY,KAAK,WAAW,gBAAgB,EAC7C;CACD;AACD"}
|
|
1
|
+
{"version":3,"file":"shared-pool.js","names":[],"sources":["../src/shared-pool.ts"],"sourcesContent":["import type { SharedAllocatedMemory } from './allocated-memory';\nimport AllocatedMemory from './allocated-memory';\nimport { lock, unlock } from './lock/simple-lock';\nimport type MemoryHeap from './memory-heap';\nimport SharedStack from './shared-stack';\nimport { getPointer, loadRawPointer, storeRawPointer } from './utils/pointer';\nimport { getArrayTypeCode, getByteMultipler, makeArrayView, type NumericArray, type NumericArrayIO, type TypedArrayConstructor } from './utils/array-type';\n\nconst LENGTH_INDEX = 0;\nconst TYPE_INDEX = 1;\nconst MAX_CHUNK_SIZE_INDEX = 2;\n// Guards appending a chunk pointer so only one thread grows the pointer stack at a time\nconst GROW_LOCK_INDEX = 3;\n// Pointers to the two backing SharedStacks (their own firstBlocks): the chunk pointers and the recycled indexes\nconst POINTER_STACK_INDEX = 4;\nconst RECYCLE_STACK_INDEX = 5;\n\nconst DEFAULT_MAX_CHUNK_SIZE = 100;\nconst DEFAULT_MAX_LENGTH = 1_000_000;\nconst DEFAULT_MAX_RECYCLED_LENGTH = 100_000;\n\n// Array with stable indexes and maximum contiguous memory sizes (necessary to fit large data sets into max 1MB buffers).\n// The chunk pointers and the recycled indexes both live in SharedStacks, whose segments are never moved or freed once\n// published, so concurrent readers never touch memory that is being reclaimed.\n// https://plflib.org/colony.htm for future enhancements - it seems to be an optimized version of what we were aiming for with this\nexport default class SharedPool<T extends NumericArray = Uint32Array> implements Iterable<T> {\n\tstatic readonly ALLOCATE_COUNT = 6;\n\n\t// u32s a pool with this config needs for its firstBlock, including both inlined stacks. Lets an owner (e.g.\n\t// SharedQuadtree) reserve one contiguous slice and hand it back through the config's firstBlock to inline the pool.\n\tstatic getAllocateCount(memory: MemoryHeap, config?: SharedPoolConfig<NumericArray>): number {\n\t\tlet maxChunkSize = config?.maxChunkSize ?? DEFAULT_MAX_CHUNK_SIZE;\n\t\tlet maxLength = config?.maxLength ?? DEFAULT_MAX_LENGTH;\n\t\tlet maxRecycledLength = config?.maxRecycledLength ?? DEFAULT_MAX_RECYCLED_LENGTH;\n\t\tlet maxSegmentLength = SharedStack.getMaxSegmentLength(memory.maxAllocationLength);\n\t\tlet pointerStackAllocateCount = SharedStack.getAllocateCount(maxLength / maxChunkSize, undefined, maxSegmentLength);\n\t\tlet recycleStackAllocateCount = SharedStack.getAllocateCount(maxRecycledLength, undefined, maxSegmentLength);\n\t\treturn SharedPool.ALLOCATE_COUNT + pointerStackAllocateCount + recycleStackAllocateCount;\n\t}\n\n\tprivate memory: MemoryHeap;\n\n\t// Current Length, Type/DataLength, MaxChunkLength, Grow lock, Pointer stack, Recycle stack\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\tprivate growLock: Int32Array;\n\tprivate pointerStack!: SharedStack;\n\tprivate recycleStack!: SharedStack;\n\treadonly maxLength: number;\n\n\t// False when firstBlock is a slice of another structure's memory (e.g. inlined in SharedQuadtree); that owner frees it\n\tprivate ownsFirstBlock: boolean;\n\n\t// Chunk views cached by pointer index. An array (dense, small integer keys) resolves faster than an object on the hot\n\t// get/set path and never needs rehashing.\n\tprivate cachedFullDataBlock: Array<T | undefined> = [];\n\n\t// Config is written once at construction and never mutated, so it is cached in plain fields\n\tprivate readonly cachedMaxChunkSize: number;\n\tprivate readonly cachedDataLength: number;\n\tprivate readonly cachedType: number;\n\tprivate readonly cachedByteMultipler: number;\n\n\tget length(): number {\n\t\treturn Atomics.load(this.firstBlock.data, LENGTH_INDEX) - this.recycleStack.length;\n\t}\n\tget maxChunkSize(): number {\n\t\treturn this.cachedMaxChunkSize;\n\t}\n\tprivate set maxChunkSize(value: number) {\n\t\tAtomics.store(this.firstBlock.data, MAX_CHUNK_SIZE_INDEX, value);\n\t}\n\n\tget type(): number {\n\t\treturn this.cachedType;\n\t}\n\tprivate set type(value: number) {\n\t\tAtomics.store(this.uint16Array, 0, value);\n\t}\n\tget dataLength(): number {\n\t\treturn this.cachedDataLength;\n\t}\n\tprivate set dataLength(value: number) {\n\t\tAtomics.store(this.uint16Array, 1, value);\n\t}\n\n\tget bufferLength(): number {\n\t\treturn this.cachedMaxChunkSize * this.pointerStack.length;\n\t}\n\tget byteMultipler(): number {\n\t\treturn this.cachedByteMultipler;\n\t}\n\n\t// Own internal memory (which already includes both stacks' inlined firstBlocks) plus every chunk buffer and the\n\t// segments each stack has grown. The stacks' usedMemory excludes their inlined firstBlocks, so they aren't double-counted.\n\tget usedMemory(): number {\n\t\tlet total = this.ownsFirstBlock ? this.firstBlock.usedMemory : 0;\n\t\tfor(let pointer of this.pointerStack) {\n\t\t\ttotal += new AllocatedMemory(this.memory, getPointer(pointer, this.memory.positionBits)).usedMemory;\n\t\t}\n\t\treturn total + this.pointerStack.usedMemory + this.recycleStack.usedMemory;\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedPoolConfig<T> | SharedPoolMemory | SharedPoolMemory & SharedPoolConfig<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 SharedQuadtree); 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\n\t\t\t\t? config.firstBlock\n\t\t\t\t: new AllocatedMemory(memory, { length: SharedPool.ALLOCATE_COUNT, ...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 || 'dataLength' in config || 'maxLength' in config || 'maxChunkSize' in config || 'maxRecycledLength' 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.pointerStack = new SharedStack(memory, {\n\t\t\t\t\tfirstBlock: getPointer(loadRawPointer(this.firstBlock.data, POINTER_STACK_INDEX), memory.positionBits),\n\t\t\t\t});\n\t\t\t\tthis.recycleStack = new SharedStack(memory, {\n\t\t\t\t\tfirstBlock: getPointer(loadRawPointer(this.firstBlock.data, RECYCLE_STACK_INDEX), memory.positionBits),\n\t\t\t\t});\n\t\t\t\tthis.ownsFirstBlock = true;\n\t\t\t}\n\t\t} else {\n\t\t\tthis.firstBlock = memory.allocUI32(SharedPool.getAllocateCount(memory, config));\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.initialize(config);\n\t\t\tthis.ownsFirstBlock = true;\n\t\t}\n\n\t\tthis.cachedType = this.uint16Array[0];\n\t\tthis.cachedDataLength = Math.max(1, this.uint16Array[1]);\n\t\tthis.cachedMaxChunkSize = this.firstBlock.data[MAX_CHUNK_SIZE_INDEX];\n\t\tthis.cachedByteMultipler = getByteMultipler(this.cachedType);\n\t\tthis.maxLength = this.cachedMaxChunkSize * this.pointerStack.maxLength;\n\n\t\tthis.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + GROW_LOCK_INDEX * Uint32Array.BYTES_PER_ELEMENT, 1);\n\t}\n\n\t// Lay out the two inlined stacks (their firstBlocks sliced from ours), record their pointers, and seed the first chunk.\n\t// firstBlock and uint16Array must already be set. Used by both the fresh-alloc path and the inline-in-place path.\n\tprivate initialize(config?: SharedPoolConfig<T>) {\n\t\tlet dataLength = config?.dataLength ?? 1;\n\t\tlet maxChunkSize = config?.maxChunkSize ?? DEFAULT_MAX_CHUNK_SIZE;\n\t\tlet maxLength = config?.maxLength ?? DEFAULT_MAX_LENGTH;\n\t\tlet maxRecycledLength = config?.maxRecycledLength ?? DEFAULT_MAX_RECYCLED_LENGTH;\n\n\t\t// Each stack sizes its inlined firstBlock from its own maxLength, so compute both up front to lay them out. The\n\t\t// stacks cap their segments at what a buffer can hold, so size the inlined blocks against that same cap.\n\t\tlet maxSegmentLength = SharedStack.getMaxSegmentLength(this.memory.maxAllocationLength);\n\t\tlet pointerStackMaxLength = maxLength / maxChunkSize;\n\t\tlet pointerStackAllocateCount = SharedStack.getAllocateCount(pointerStackMaxLength, undefined, maxSegmentLength);\n\t\tlet recycleStackAllocateCount = SharedStack.getAllocateCount(maxRecycledLength, undefined, maxSegmentLength);\n\n\t\t// Inline both stacks' firstBlocks right after the pool header so the whole spine is one contiguous allocation\n\t\tthis.pointerStack = new SharedStack(this.memory, {\n\t\t\tfirstBlock: this.firstBlock.getSubAllocation(SharedPool.ALLOCATE_COUNT, pointerStackAllocateCount),\n\t\t\ttype: Uint32Array,\n\t\t\tmaxLength: pointerStackMaxLength,\n\t\t});\n\t\tstoreRawPointer(this.firstBlock.data, POINTER_STACK_INDEX, this.pointerStack.pointer);\n\t\tthis.recycleStack = new SharedStack(this.memory, {\n\t\t\tfirstBlock: this.firstBlock.getSubAllocation(SharedPool.ALLOCATE_COUNT + pointerStackAllocateCount, recycleStackAllocateCount),\n\t\t\ttype: Uint32Array,\n\t\t\tmaxLength: maxRecycledLength,\n\t\t});\n\t\tstoreRawPointer(this.firstBlock.data, RECYCLE_STACK_INDEX, this.recycleStack.pointer);\n\n\t\tlet typeCode = getArrayTypeCode(config?.type ?? Uint32Array);\n\t\tthis.type = typeCode;\n\t\tthis.dataLength = dataLength;\n\t\tthis.maxChunkSize = maxChunkSize;\n\n\t\tlet byteMultipler = getByteMultipler(typeCode);\n\t\tlet firstArray = this.memory.allocUI32(maxChunkSize * dataLength * byteMultipler);\n\t\tthis.pointerStack.push(firstArray.pointer);\n\t}\n\n\tat(index: number): T {\n\t\tlet dataBlock = this.getFullDataBlock(index);\n\t\treturn this.getDataBlock(dataBlock, index % this.cachedMaxChunkSize);\n\t}\n\tget(index: number, dataIndex = 0): T[number] {\n\t\tconst dataLength = this.cachedDataLength;\n\t\tif(dataIndex >= dataLength) {\n\t\t\tthrow new Error(`${dataIndex} is out of dataLength bounds ${dataLength}`);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock(index) as NumericArrayIO;\n\t\treturn dataBlock[(index % this.cachedMaxChunkSize) * dataLength + dataIndex];\n\t}\n\tset(index: number, dataIndex: number, value: T[number]) {\n\t\tconst dataLength = this.cachedDataLength;\n\t\tif(dataIndex >= dataLength) {\n\t\t\tthrow new Error(`${dataIndex} is out of dataLength bounds ${dataLength}`);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock(index) as NumericArrayIO;\n\t\tdataBlock[(index % this.cachedMaxChunkSize) * dataLength + dataIndex] = value;\n\t}\n\n\t// Resolve the chunk view holding an index once. Callers touching several fields of the same record read/write straight\n\t// into this view at blockOffset(index) + fieldIndex, avoiding a repeated chunk lookup per field.\n\tblockFor(index: number): T {\n\t\treturn this.getFullDataBlock(index);\n\t}\n\t// Base slot of a record within the view blockFor(index) returns.\n\tblockOffset(index: number): number {\n\t\treturn (index % this.cachedMaxChunkSize) * this.cachedDataLength;\n\t}\n\n\tpush(values: T[number] | Array<T[number]>): number {\n\t\tlet dataLength = this.cachedDataLength;\n\t\tconst isSingleValue = typeof values !== 'object';\n\t\tif(!isSingleValue && values.length > dataLength) {\n\t\t\tthrow new Error(`Can't insert ${values.length} array into shared list of ${dataLength} dataLength`);\n\t\t}\n\n\t\t// Reuse a recycled index if one is available. pop() can come back undefined when another thread drained the last\n\t\t// slot between our two calls, so fall through to a fresh index rather than trusting a length() read\n\t\tlet newIndex = this.recycleStack.length === 0 ? undefined : this.recycleStack.pop();\n\t\tif(newIndex === undefined) {\n\t\t\tnewIndex = Atomics.add(this.firstBlock.data, LENGTH_INDEX, 1);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock(newIndex) as NumericArrayIO;\n\t\tlet startIndex = dataLength * (newIndex % this.cachedMaxChunkSize);\n\t\tif(isSingleValue) {\n\t\t\tdataBlock[startIndex] = values;\n\t\t} else {\n\t\t\tfor(let i = 0; i < values.length; i++) {\n\t\t\t\tdataBlock[startIndex + i] = values[i];\n\t\t\t}\n\t\t}\n\n\t\treturn newIndex;\n\t}\n\n\treserveContiguous(count: number): number {\n\t\tif(!Number.isInteger(count) || count < 0) {\n\t\t\tthrow new Error(`Can't reserve ${count} pool entries`);\n\t\t}\n\t\treturn Atomics.add(this.firstBlock.data, LENGTH_INDEX, count);\n\t}\n\n\tdeleteIndex(index: number) {\n\t\tthis.recycleStack.push(index);\n\t}\n\n\tclear() {\n\t\tthis.firstBlock.data[LENGTH_INDEX] = 0;\n\t\tthis.recycleStack.clear();\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tconst recycledValues: { [key: number]: true } = {};\n\t\tfor(let value of this.recycleStack) {\n\t\t\trecycledValues[value] = true;\n\t\t}\n\n\t\tconst maxChunkSize = this.cachedMaxChunkSize;\n\t\t// Snapshot the length once - iteration over a concurrently mutated pool is a snapshot anyway, and re-loading the\n\t\t// atomic every step is pure overhead\n\t\tconst length = Atomics.load(this.firstBlock.data, LENGTH_INDEX);\n\t\tlet dataBlock = this.getFullDataBlock(0);\n\t\tlet dataBlockIndex = 0;\n\t\tfor(let i = 0; i < length; i++) {\n\t\t\tif(!recycledValues[i]) {\n\t\t\t\tlet newDataBlockIndex = Math.floor(i / maxChunkSize);\n\t\t\t\tif(newDataBlockIndex !== dataBlockIndex) {\n\t\t\t\t\tdataBlock = this.getFullDataBlock(i);\n\t\t\t\t\tdataBlockIndex = newDataBlockIndex;\n\t\t\t\t}\n\t\t\t\tyield this.getDataBlock(dataBlock, i % maxChunkSize);\n\t\t\t}\n\t\t}\n\t}\n\n\t*entries(): IterableIterator<[number, T]> {\n\t\tconst recycledValues: { [key: number]: true } = {};\n\t\tfor(let value of this.recycleStack) {\n\t\t\trecycledValues[value] = true;\n\t\t}\n\n\t\tconst maxChunkSize = this.cachedMaxChunkSize;\n\t\tconst length = Atomics.load(this.firstBlock.data, LENGTH_INDEX);\n\t\tlet dataBlock = this.getFullDataBlock(0);\n\t\tlet dataBlockIndex = 0;\n\t\tfor(let i = 0; i < length; i++) {\n\t\t\tif(!recycledValues[i]) {\n\t\t\t\tlet newDataBlockIndex = Math.floor(i / maxChunkSize);\n\t\t\t\tif(newDataBlockIndex !== dataBlockIndex) {\n\t\t\t\t\tdataBlock = this.getFullDataBlock(i);\n\t\t\t\t\tdataBlockIndex = newDataBlockIndex;\n\t\t\t\t}\n\t\t\t\tyield [i, this.getDataBlock(dataBlock, i % maxChunkSize)];\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getFullDataBlock(index: number) {\n\t\tlet pointerIndex = Math.floor(index / this.cachedMaxChunkSize);\n\t\tlet cachedDataBlock = this.cachedFullDataBlock[pointerIndex];\n\t\tif(cachedDataBlock) {\n\t\t\treturn cachedDataBlock;\n\t\t}\n\t\tif(pointerIndex >= this.pointerStack.length) {\n\t\t\t// Only let a single thread append a new chunk at a time - otherwise two concurrent inserts landing in a new\n\t\t\t// chunk would each allocate + push a buffer, orphaning one and leaking memory\n\t\t\tlock(this.growLock);\n\t\t\ttry {\n\t\t\t\t// Re-check under the lock: another thread may have already appended the chunk while we were waiting\n\t\t\t\twhile(pointerIndex >= this.pointerStack.length) {\n\t\t\t\t\tlet newArray = this.memory.allocUI32(this.cachedMaxChunkSize * this.cachedDataLength * this.cachedByteMultipler);\n\t\t\t\t\tthis.pointerStack.push(newArray.pointer);\n\t\t\t\t}\n\t\t\t} finally {\n\t\t\t\tunlock(this.growLock);\n\t\t\t}\n\t\t}\n\n\t\tlet array = new AllocatedMemory(this.memory, getPointer(this.pointerStack.at(pointerIndex), this.memory.positionBits));\n\n\t\tlet blockLength = this.cachedDataLength * this.cachedMaxChunkSize;\n\t\tlet data = makeArrayView(this.cachedType, array.data.buffer, array.bufferByteOffset, blockLength) as T;\n\n\t\tthis.cachedFullDataBlock[pointerIndex] = data;\n\t\treturn data;\n\t}\n\n\tprivate getDataBlock(rawData: T, index: number): T {\n\t\tconst dataLength = this.cachedDataLength;\n\t\tconst start = index * dataLength;\n\t\treturn rawData.subarray(start, start + dataLength) as T;\n\t}\n\n\tfree() {\n\t\tthis.recycleStack.free();\n\n\t\tfor(let pointer of this.pointerStack) {\n\t\t\tlet memory = new AllocatedMemory(this.memory, getPointer(pointer, this.memory.positionBits));\n\t\t\tmemory.free();\n\t\t}\n\t\tthis.pointerStack.free();\n\t\tif(this.ownsFirstBlock) {\n\t\t\tthis.firstBlock.free();\n\t\t}\n\t}\n\n\tgetSharedMemory(): SharedPoolMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.firstBlock.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedPoolConfig<T extends NumericArray> {\n\tmaxChunkSize?: number\n\tmaxLength?: number\n\tmaxRecycledLength?: number\n\ttype?: TypedArrayConstructor<T>\n\tdataLength?: number\n}\ninterface SharedPoolMemory {\n\tfirstBlock: SharedAllocatedMemory | AllocatedMemory\n}\n\nexport type { SharedPoolConfig, SharedPoolMemory };\n"],"mappings":";;;;;;AAQA,IAAM,IAAe,GACf,IAAa,GACb,IAAuB,GAEvB,IAAkB,GAElB,IAAsB,GACtB,IAAsB,GAEtB,IAAyB,KACzB,IAAqB,KACrB,IAA8B,KAMf,IAArB,MAAqB,EAAwE;CAC5F,OAAgB,iBAAiB;CAIjC,OAAO,iBAAiB,GAAoB,GAAiD;EAC5F,IAAI,IAAe,GAAQ,gBAAgB,GACvC,IAAY,GAAQ,aAAa,GACjC,IAAoB,GAAQ,qBAAqB,GACjD,IAAmB,EAAY,oBAAoB,EAAO,mBAAmB,GAC7E,IAA4B,EAAY,iBAAiB,IAAY,GAAc,KAAA,GAAW,CAAgB,GAC9G,IAA4B,EAAY,iBAAiB,GAAmB,KAAA,GAAW,CAAgB;EAC3G,OAAO,EAAW,iBAAiB,IAA4B;CAChE;CAEA;CAGA;CACA;CACA;CACA;CACA;CACA;CAGA;CAIA,sBAAoD,CAAC;CAGrD;CACA;CACA;CACA;CAEA,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAAK,KAAK,WAAW,MAAM,CAAY,IAAI,KAAK,aAAa;CAC7E;CACA,IAAI,eAAuB;EAC1B,OAAO,KAAK;CACb;CACA,IAAY,aAAa,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AAK,GACpB,KAAK,kBACP,KAAK,WAAW,KAAK;CAEvB;CAEA,kBAAoC;EACnC,OAAO,EACN,YAAY,KAAK,WAAW,gBAAgB,EAC7C;CACD;AACD"}
|
package/dist/shared-stack.js
CHANGED
|
@@ -82,8 +82,17 @@ var c = 0, l = 1, u = 2, d = 3, f = 4, p = 5, m = 6, h = 7, g = 4, _ = 20, v = 2
|
|
|
82
82
|
return t;
|
|
83
83
|
}
|
|
84
84
|
constructor(e, t) {
|
|
85
|
-
if (this.memory = e, t && "firstBlock" in t)
|
|
86
|
-
|
|
85
|
+
if (this.memory = e, t && "firstBlock" in t) {
|
|
86
|
+
if (t.firstBlock instanceof n) this.firstBlock = t.firstBlock;
|
|
87
|
+
else {
|
|
88
|
+
let r = new Uint32Array(e.buffers[t.firstBlock.bufferPosition].buf, t.firstBlock.bufferByteOffset + d * Uint32Array.BYTES_PER_ELEMENT, 1)[0];
|
|
89
|
+
this.firstBlock = new n(e, {
|
|
90
|
+
length: v.BASE_ALLOCATE_COUNT + r,
|
|
91
|
+
...t.firstBlock
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + l * Uint32Array.BYTES_PER_ELEMENT, 2), "type" in t || "baseSegmentLength" in t || "maxLength" in t ? (this.initialize(t), this.ownsFirstBlock = !1) : this.ownsFirstBlock = !0;
|
|
95
|
+
} else {
|
|
87
96
|
let n = i(r(t?.type ?? Uint32Array)), a = t?.baseSegmentLength ?? g, o = this.resolveMaxSegmentLength(a, t?.maxSegmentLength, n), s = this.resolveMaxSegments(t?.maxLength, a, o);
|
|
88
97
|
this.firstBlock = e.allocUI32(v.BASE_ALLOCATE_COUNT + s), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + l * Uint32Array.BYTES_PER_ELEMENT, 2), this.ownsFirstBlock = !0, this.initialize(t);
|
|
89
98
|
}
|
package/dist/shared-stack.js.map
CHANGED
|
@@ -1 +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 { lock, unlock } from './lock/simple-lock';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, storePointer } from './utils/pointer';\nimport { getArrayTypeCode, getByteMultipler, makeArrayView, type NumericArray, type NumericArrayIO, type TypedArrayConstructor } from './utils/array-type';\n\nconst LENGTH_INDEX = 0;\nconst TYPE_INDEX = 1;\nconst BASE_SEGMENT_LENGTH_INDEX = 2;\nconst MAX_SEGMENTS_INDEX = 3;\nconst MAX_SEGMENT_LENGTH_INDEX = 4;\nconst SEGMENT_COUNT_INDEX = 5;\nconst GROW_LOCK_INDEX = 6;\nconst SEGMENT_START_INDEX = 7;\n\nconst DEFAULT_BASE_SEGMENT_LENGTH = 4;\n// Default segment budget. With uncapped power-of-two growth these 20 segments spanned 4,194,300 slots, but that ceiling\n// was never reachable: past segment 14 a segment's block outgrows a 1MB buffer and allocation throws. Capping segments at\n// what a buffer holds, the honest capacity of 20 segments is getCapacity(20, base, cap) instead.\nconst DEFAULT_MAX_SEGMENTS = 20;\n\n// The LENGTH word packs an ABA version counter (high bits) with the live count (low LEN_BITS). Both push and pop bump\n// the version on every successful CAS, so the CAS fails on ANY intervening op - the count can never ABA back to the\n// same value with a slot at a different generation. 22 count bits cover the default max length (4,194,300); the\n// remaining 10 version bits (1024) only need to outlast a single thread's load->CAS window.\nconst LEN_BITS = 22;\nconst LEN_MASK = (1 << LEN_BITS) - 1;\nconst MAX_COUNT = LEN_MASK - 1;\n// Adding this to the packed word bumps the version (high bits) by one. Combined with +1/-1 for the count it repacks the\n// whole word in a single add: the count is guarded from overflowing into the version bits, and a version overflow just\n// wraps harmlessly when the value is coerced back to Uint32 on the atomic store.\nconst VERSION_STEP = 1 << LEN_BITS;\n\n// Per-slot publish handshake. Each data slot has a companion sequence number: even means the slot is empty (free to\n// fill) at some lap, odd means the value is published (safe to read). It only ever increases. push/pop read the slot's\n// sequence and bail out (retry) unless it has the parity they need, then CAS the length; because the versioned CAS\n// rejects any intervening op, the sequence read stays valid all the way through the publish store below.\n\nexport default class SharedStack<T extends NumericArray = Uint32Array> implements Iterable<T[number]> {\n\tstatic readonly BASE_ALLOCATE_COUNT = 8;\n\tstatic readonly DEFAULT_ALLOCATE_COUNT = this.BASE_ALLOCATE_COUNT + DEFAULT_MAX_SEGMENTS;\n\n\t// Largest power-of-two segment length whose block (segmentLength data slots plus an equal run of publish sequences)\n\t// still fits in a single buffer. Segments double until they reach this, then stay flat so a segment can always be\n\t// allocated in one contiguous block. maxAllocationLength is MemoryHeap.maxAllocationLength. byteMultipler is the u32\n\t// units per data slot (2 for 64-bit types), so a length-L segment's block is L * byteMultipler data u32s + L sequence u32s.\n\tstatic getMaxSegmentLength(maxAllocationLength: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, byteMultipler = 1): number {\n\t\tlet segmentLength = baseSegmentLength;\n\t\t// A length-L segment needs a block of L * (byteMultipler + 1) u32s, so the next double fits while\n\t\t// 2L * (byteMultipler + 1) <= maxAllocationLength\n\t\twhile(segmentLength * 2 * (byteMultipler + 1) <= maxAllocationLength) {\n\t\t\tsegmentLength *= 2;\n\t\t}\n\t\treturn segmentLength;\n\t}\n\n\t// Smallest segment count whose summed capacity covers maxLength. Segments double from baseSegmentLength but never grow\n\t// past maxSegmentLength, so once capped they each add a flat maxSegmentLength. Looped rather than solved in closed form\n\t// to avoid float rounding landing a segment short or over.\n\tstatic getMaxSegments(maxLength: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, maxSegmentLength = Infinity): number {\n\t\tlet segments = 0;\n\t\tlet capacity = 0;\n\t\tlet segmentLength = baseSegmentLength;\n\t\twhile(capacity < maxLength) {\n\t\t\tcapacity += segmentLength;\n\t\t\tsegments++;\n\t\t\tsegmentLength = Math.min(segmentLength * 2, maxSegmentLength);\n\t\t}\n\t\treturn segments;\n\t}\n\t// Total capacity of the first `segments` segments under the same doubling-then-capped growth as getMaxSegments\n\tstatic getCapacity(segments: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, maxSegmentLength = Infinity): number {\n\t\tlet capacity = 0;\n\t\tlet segmentLength = baseSegmentLength;\n\t\tfor(let i = 0; i < segments; i++) {\n\t\t\tcapacity += segmentLength;\n\t\t\tsegmentLength = Math.min(segmentLength * 2, maxSegmentLength);\n\t\t}\n\t\treturn capacity;\n\t}\n\tstatic getAllocateCount(maxLength: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, maxSegmentLength = Infinity): number {\n\t\treturn this.BASE_ALLOCATE_COUNT + this.getMaxSegments(maxLength, baseSegmentLength, maxSegmentLength);\n\t}\n\tprivate memory: MemoryHeap;\n\n\t// Length, Type, Base Segment Length, Max Segments, Max Segment Length, Segment Count, Grow Lock, Segments\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\tprivate growLock: Int32Array;\n\treadonly maxLength: number;\n\n\t// False when firstBlock is a slice of another structure's memory (e.g. inlined in SharedPool); that owner frees it\n\tprivate ownsFirstBlock: boolean;\n\n\tprivate cachedSegments: T[] = [];\n\t// Parallel to cachedSegments: the per-slot publish sequences for each slot of that segment. Populated together with\n\t// the data view in getSegment, so whenever cachedSegments[i] is set cachedSequences[i] is too.\n\tprivate cachedSequences: Int32Array[] = [];\n\tprivate cachedBaseSegmentLength = 0;\n\t// Segment growth caps at cachedMaxSegmentLength. Segments 0..(cachedCapSegmentCount - 1) double geometrically and cover\n\t// the first cachedGeometricCapacity slots; every segment beyond that is a flat cachedMaxSegmentLength. Indexing splits\n\t// on cachedGeometricCapacity: below it the fast log2 mapping, above it a plain linear division into capped segments.\n\tprivate cachedMaxSegmentLength = 0;\n\tprivate cachedCapSegmentCount = 0;\n\tprivate cachedGeometricCapacity = 0;\n\t// u32 units per data slot: 1 for 32-bit types, 2 for 64-bit (float64/int64/uint64). A segment block is\n\t// segmentLength * cachedByteMultipler data u32s followed by segmentLength sequence u32s.\n\tprivate cachedByteMultipler = 1;\n\n\tget length(): number {\n\t\treturn Atomics.load(this.firstBlock.data, LENGTH_INDEX) & LEN_MASK;\n\t}\n\tget segmentCount(): number {\n\t\treturn Atomics.load(this.firstBlock.data, SEGMENT_COUNT_INDEX);\n\t}\n\tprivate set segmentCount(value: number) {\n\t\tthis.firstBlock.data[SEGMENT_COUNT_INDEX] = value;\n\t}\n\t\n\tget type(): number {\n\t\treturn this.uint16Array[0];\n\t}\n\tprivate set type(value: number) {\n\t\tAtomics.store(this.uint16Array, 0, value);\n\t}\n\n\tget baseSegmentLength() {\n\t\treturn this.firstBlock.data[BASE_SEGMENT_LENGTH_INDEX];\n\t}\n\tprivate set baseSegmentLength(value: number) {\n\t\tthis.firstBlock.data[BASE_SEGMENT_LENGTH_INDEX] = value;\n\t}\n\tget maxSegments() {\n\t\treturn this.firstBlock.data[MAX_SEGMENTS_INDEX];\n\t}\n\tprivate set maxSegments(value: number) {\n\t\tthis.firstBlock.data[MAX_SEGMENTS_INDEX] = value;\n\t}\n\tget maxSegmentLength() {\n\t\treturn this.firstBlock.data[MAX_SEGMENT_LENGTH_INDEX];\n\t}\n\tprivate set maxSegmentLength(value: number) {\n\t\tthis.firstBlock.data[MAX_SEGMENT_LENGTH_INDEX] = value;\n\t}\n\n\tget pointer() {\n\t\treturn this.firstBlock.pointer;\n\t}\n\n\t// Own internal memory plus every allocated segment. The firstBlock is excluded when inlined in another structure\n\t// (e.g. SharedPool) since that owner already counts it as part of its own internal memory.\n\tget usedMemory(): number {\n\t\tlet total = this.ownsFirstBlock ? this.firstBlock.usedMemory : 0;\n\t\tfor(let i = 0; i < this.segmentCount; i++) {\n\t\t\tlet pointer = getPointer(Atomics.load(this.firstBlock.data, SEGMENT_START_INDEX + i), this.memory.positionBits);\n\t\t\ttotal += new AllocatedMemory(this.memory, pointer).usedMemory;\n\t\t}\n\t\treturn total;\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedStackConfig<T> | SharedStackMemory | SharedStackMemory & SharedStackConfig<T>) {\n\t\tthis.memory = memory;\n\n\t\tif(config && 'firstBlock' in config) {\n\t\t\t// An AllocatedMemory instance is a caller-reserved slice (e.g. inlined in SharedPool); a plain pointer is the\n\t\t\t// serialized clone path. Taking the instance directly keeps us off the pointer-resolving construction branch.\n\t\t\tthis.firstBlock = config.firstBlock instanceof AllocatedMemory ? config.firstBlock : new AllocatedMemory(memory, config.firstBlock);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\n\t\t\t// Memory passed together with config means the caller reserved the firstBlock slot for us to initialize in place\n\t\t\tif('type' in config || 'baseSegmentLength' in config || 'maxLength' in config) {\n\t\t\t\tthis.initialize(config);\n\t\t\t\tthis.ownsFirstBlock = false;\n\t\t\t} else {\n\t\t\t\tthis.ownsFirstBlock = true;\n\t\t\t}\n\t\t} else {\n\t\t\tlet byteMultipler = getByteMultipler(getArrayTypeCode(config?.type ?? Uint32Array));\n\t\t\tlet baseSegmentLength = config?.baseSegmentLength ?? DEFAULT_BASE_SEGMENT_LENGTH;\n\t\t\tlet maxSegmentLength = this.resolveMaxSegmentLength(baseSegmentLength, config?.maxSegmentLength, byteMultipler);\n\t\t\tlet maxSegments = this.resolveMaxSegments(config?.maxLength, baseSegmentLength, maxSegmentLength);\n\n\t\t\tthis.firstBlock = memory.allocUI32(SharedStack.BASE_ALLOCATE_COUNT + maxSegments);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\t\t\tthis.ownsFirstBlock = true;\n\n\t\t\tthis.initialize(config);\n\t\t}\n\n\t\tthis.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + GROW_LOCK_INDEX * Uint32Array.BYTES_PER_ELEMENT, 1);\n\t\tthis.cachedByteMultipler = getByteMultipler(this.type);\n\t\tthis.cachedBaseSegmentLength = this.baseSegmentLength;\n\t\tthis.cachedMaxSegmentLength = this.maxSegmentLength;\n\t\t// Segments 0..(K-1) double geometrically up to the cap, where K = log2(cap / base) + 1\n\t\tthis.cachedCapSegmentCount = 32 - Math.clz32(((this.cachedMaxSegmentLength / this.cachedBaseSegmentLength) | 0));\n\t\tthis.cachedGeometricCapacity = this.cachedBaseSegmentLength * ((1 << this.cachedCapSegmentCount) - 1);\n\t\tthis.maxLength = SharedStack.getCapacity(this.maxSegments, this.cachedBaseSegmentLength, this.cachedMaxSegmentLength);\n\t\tif(this.maxLength > MAX_COUNT) {\n\t\t\t// The length word reserves LEN_BITS for the count and the rest for the ABA version tag\n\t\t\tthrow new Error(`SharedStack maxLength ${this.maxLength} exceeds the ${MAX_COUNT} the versioned length counter can address`);\n\t\t}\n\t}\n\n\t// Cap on segment length: the largest a single buffer can hold, optionally lowered to a caller-requested value (floored\n\t// to a power-of-two multiple of the base). Beyond this segments stop doubling and stay flat so each still fits a buffer.\n\tprivate resolveMaxSegmentLength(baseSegmentLength: number, requested?: number, byteMultipler = 1): number {\n\t\tlet cap = SharedStack.getMaxSegmentLength(this.memory.maxAllocationLength, baseSegmentLength, byteMultipler);\n\t\tif(requested) {\n\t\t\tlet floored = baseSegmentLength;\n\t\t\twhile(floored * 2 <= requested) {\n\t\t\t\tfloored *= 2;\n\t\t\t}\n\t\t\tcap = Math.min(cap, floored);\n\t\t}\n\t\treturn cap;\n\t}\n\n\t// Segments needed to cover maxLength (or the default budget when none is given), but never more than the segment-pointer\n\t// spine can hold in a single buffer. Once capped, segments no longer double, so a huge maxLength on a small buffer is\n\t// honestly bounded here instead of blowing up later when the spine (or a segment) can't be allocated.\n\tprivate resolveMaxSegments(maxLength: number | undefined, baseSegmentLength: number, maxSegmentLength: number): number {\n\t\tlet needed = maxLength === undefined\n\t\t\t? DEFAULT_MAX_SEGMENTS\n\t\t\t: SharedStack.getMaxSegments(maxLength, baseSegmentLength, maxSegmentLength);\n\t\tlet spineLimit = this.memory.maxAllocationLength - SharedStack.BASE_ALLOCATE_COUNT;\n\t\treturn Math.min(needed, spineLimit);\n\t}\n\n\tprivate initialize(config?: SharedStackConfig<T>) {\n\t\tlet typeCode = getArrayTypeCode(config?.type ?? Uint32Array);\n\t\tlet byteMultipler = getByteMultipler(typeCode);\n\t\tlet baseSegmentLength = config?.baseSegmentLength ?? DEFAULT_BASE_SEGMENT_LENGTH;\n\t\tlet maxSegmentLength = this.resolveMaxSegmentLength(baseSegmentLength, config?.maxSegmentLength, byteMultipler);\n\t\t// Each segment block holds the data slots (byteMultipler u32s each) followed by one publish sequence u32 per slot\n\t\tlet firstSegmentBlock = this.memory.allocUI32(baseSegmentLength * (byteMultipler + 1));\n\t\tstorePointer(this.firstBlock.data, SEGMENT_START_INDEX, firstSegmentBlock.bufferPosition, firstSegmentBlock.bufferByteOffset, this.memory.positionBits);\n\t\tthis.baseSegmentLength = baseSegmentLength;\n\t\tthis.maxSegmentLength = maxSegmentLength;\n\t\tthis.maxSegments = this.resolveMaxSegments(config?.maxLength, baseSegmentLength, maxSegmentLength);\n\t\tthis.segmentCount = 1;\n\t\tthis.type = typeCode;\n\t}\n\n\tat(index: number): T[number] {\n\t\tlet length = this.length;\n\t\tif(index >= length || index < 0) {\n\t\t\tthrow new Error(`${index} is out of bounds ${length}`);\n\t\t}\n\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\tlet segmentIndex: number;\n\t\tlet localIndex: number;\n\t\tif(index < geometricCapacity) {\n\t\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t\tsegmentIndex = 31 - Math.clz32(((index / baseSegmentLength) | 0) + 1);\n\t\t\tlocalIndex = index - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t} else {\n\t\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\t\tlet offset = index - geometricCapacity;\n\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t}\n\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\t// push() bumps the length before it stores the value and publishes the slot, so an index already counted in length\n\t\t// can still be mid-publish. Wait for the slot's sequence to go odd (published) before reading, otherwise we hand\n\t\t// back a slot whose value hasn't landed yet - for the pool's chunk pointers that means decoding a zeroed pointer.\n\t\tlet sequences = this.cachedSequences[segmentIndex];\n\t\twhile((Atomics.load(sequences, localIndex) & 1) === 0) { /* spin: value is being published on another thread */ }\n\t\treturn (segment as NumericArrayIO)[localIndex];\n\t}\n\n\tpush(value: T[number]): number {\n\t\tlet data = this.firstBlock.data;\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\t// Plain (relaxed) read is enough: it only seeds the versioned CAS below, which rejects any stale value\n\t\t\tlet packed = data[LENGTH_INDEX];\n\t\t\tlet newIndex = packed & LEN_MASK;\n\t\t\tif(newIndex >= this.maxLength) {\n\t\t\t\tthrow new Error(`${newIndex + 1} is out of bounds ${this.maxLength}`);\n\t\t\t}\n\n\t\t\tlet segmentIndex: number;\n\t\t\tlet localIndex: number;\n\t\t\tif(newIndex < geometricCapacity) {\n\t\t\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t\t\tsegmentIndex = 31 - Math.clz32(((newIndex / baseSegmentLength) | 0) + 1);\n\t\t\t\tlocalIndex = newIndex - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\t} else {\n\t\t\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\t\t\tlet offset = newIndex - geometricCapacity;\n\t\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t\t}\n\t\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\t\tlet sequences = this.cachedSequences[segmentIndex];\n\n\t\t\t// The slot must be empty (even sequence) to fill. Odd means a popper hasn't finished emptying it yet - retry\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) !== 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// Take the slot only if the whole versioned length word is unchanged. The version bump makes this fail on any\n\t\t\t// intervening op, so a win guarantees nothing touched the top - and our sequence read above is still valid\n\t\t\tif(Atomics.compareExchange(data, LENGTH_INDEX, packed, packed + VERSION_STEP + 1) !== packed) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t(segment as NumericArrayIO)[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(): T[number] | undefined {\n\t\tlet data = this.firstBlock.data;\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\t// Plain (relaxed) read is enough: it only seeds the versioned CAS below, which rejects any stale value\n\t\t\tlet packed = data[LENGTH_INDEX];\n\t\t\tlet length = packed & LEN_MASK;\n\t\t\tif(length <= 0) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tlet oldIndex = length - 1;\n\n\t\t\tlet segmentIndex: number;\n\t\t\tlet localIndex: number;\n\t\t\tif(oldIndex < geometricCapacity) {\n\t\t\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t\t\tsegmentIndex = 31 - Math.clz32(((oldIndex / baseSegmentLength) | 0) + 1);\n\t\t\t\tlocalIndex = oldIndex - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\t} else {\n\t\t\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\t\t\tlet offset = oldIndex - geometricCapacity;\n\t\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t\t}\n\t\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\t\tlet sequences = this.cachedSequences[segmentIndex];\n\n\t\t\t// The slot must be full (odd sequence) to read. Even means a pusher hasn't finished publishing it yet - retry\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) === 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(Atomics.compareExchange(data, LENGTH_INDEX, packed, packed + VERSION_STEP - 1) !== packed) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet value = (segment as NumericArrayIO)[localIndex] as T[number];\n\t\t\t// Release the slot: bump to even (empty) so a later push may reuse it\n\t\t\tAtomics.store(sequences, localIndex, sequence + 1);\n\t\t\treturn value;\n\t\t}\n\t}\n\n\tclear() {\n\t\t// Mark every occupied slot empty (bump its odd sequence to the next even) before dropping the length, otherwise a\n\t\t// later push reusing the slot would spin forever on a stale full sequence. Not safe to call concurrently with push/pop.\n\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\tlet oldLength = Atomics.load(this.firstBlock.data, LENGTH_INDEX) & LEN_MASK;\n\t\tfor(let i = 0; i < oldLength; i++) {\n\t\t\tlet segmentIndex: number;\n\t\t\tlet localIndex: number;\n\t\t\tif(i < geometricCapacity) {\n\t\t\t\tsegmentIndex = 31 - Math.clz32(((i / baseSegmentLength) | 0) + 1);\n\t\t\t\tlocalIndex = i - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\t} else {\n\t\t\t\tlet offset = i - geometricCapacity;\n\t\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t\t}\n\t\t\tlet sequences = this.cachedSequences[segmentIndex] ?? (this.getSegment(segmentIndex), this.cachedSequences[segmentIndex]);\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) !== 0) {\n\t\t\t\tAtomics.store(sequences, localIndex, sequence + 1);\n\t\t\t}\n\t\t}\n\t\tAtomics.store(this.firstBlock.data, LENGTH_INDEX, 0);\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet totalDataEntries = this.length;\n\n\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\tlet segmentLength = this.baseSegmentLength;\n\t\tlet segmentIndex = 0;\n\t\tlet segment = this.getSegment(segmentIndex);\n\t\tlet segmentDataIndex = 0;\n\t\tfor(let i = 0; i < totalDataEntries; i++) {\n\t\t\tyield (segment as NumericArrayIO)[segmentDataIndex] as T[number];\n\t\t\tsegmentDataIndex++;\n\t\t\tif(segmentDataIndex >= segmentLength) {\n\t\t\t\tsegmentIndex++;\n\t\t\t\tsegment = this.getSegment(segmentIndex);\n\t\t\t\tsegmentDataIndex = 0;\n\t\t\t\tsegmentLength = Math.min(segmentLength * 2, maxSegmentLength);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getSegment(segmentIndex: number): T {\n\t\tif(this.cachedSegments[segmentIndex]) {\n\t\t\treturn this.cachedSegments[segmentIndex];\n\t\t}\n\n\t\t// Geometric while below the cap, then flat so the block always fits a single buffer\n\t\tlet segmentLength = segmentIndex < this.cachedCapSegmentCount\n\t\t\t? this.cachedBaseSegmentLength << segmentIndex\n\t\t\t: this.cachedMaxSegmentLength;\n\t\tlet segmentDataBlock: AllocatedMemory | undefined;\n\t\tif(segmentIndex >= this.segmentCount) {\n\t\t\t// Lock when growing so another thread doesn't also setup the same segment\n\t\t\tlock(this.growLock);\n\t\t\ttry {\n\t\t\t\t// Re-check under the lock: another thread may have already appended the segment while we were waiting\n\t\t\t\tif(segmentIndex >= this.segmentCount) {\n\t\t\t\t\t// Data slots (byteMultipler u32s each) followed by one publish sequence u32 per slot (calloc-zeroed = 0 = empty)\n\t\t\t\t\tsegmentDataBlock = this.memory.allocUI32(segmentLength * (this.cachedByteMultipler + 1));\n\t\t\t\t\tstorePointer(this.firstBlock.data, SEGMENT_START_INDEX + segmentIndex, segmentDataBlock.bufferPosition, segmentDataBlock.bufferByteOffset, this.memory.positionBits);\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, this.memory.positionBits);\n\t\t\tsegmentDataBlock = new AllocatedMemory(this.memory, pointer);\n\t\t}\n\n\t\tlet data = makeArrayView(this.type, segmentDataBlock.data.buffer, segmentDataBlock.bufferByteOffset, segmentLength) as T;\n\t\t// Publish sequences live after the data region (segmentLength * byteMultipler u32s), always as Int32 regardless of type\n\t\tlet sequenceByteOffset = segmentDataBlock.bufferByteOffset + segmentLength * this.cachedByteMultipler * 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, this.memory.positionBits);\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 NumericArray> {\n\ttype?: TypedArrayConstructor<T>\n\n\tbaseSegmentLength?: number\n\tmaxLength?: number\n\t// Cap on how large a single segment may grow. Defaults to the largest a buffer can hold; a smaller value is floored to\n\t// a power-of-two multiple of baseSegmentLength. Beyond the cap segments stay flat instead of doubling.\n\tmaxSegmentLength?: number\n}\ninterface SharedStackMemory {\n\tfirstBlock: SharedAllocatedMemory | AllocatedMemory\n}\n\nexport type { SharedStackConfig, SharedStackMemory };"],"mappings":";;;;;AAOA,IAAM,IAAe,GACf,IAAa,GACb,IAA4B,GAC5B,IAAqB,GACrB,IAA2B,GAC3B,IAAsB,GACtB,IAAkB,GAClB,IAAsB,GAEtB,IAA8B,GAI9B,IAAuB,IAMvB,IAAW,IACX,KAAY,KAAK,KAAY,GAC7B,IAAY,SAIZ,IAAe,KAAK,GAOL,IAArB,MAAqB,EAAiF;CACrG,OAAgB,sBAAsB;CACtC,OAAgB,yBAAyB,KAAK,sBAAsB;CAMpE,OAAO,oBAAoB,GAA6B,IAAoB,GAA6B,IAAgB,GAAW;EACnI,IAAI,IAAgB;EAGpB,OAAM,IAAgB,KAAK,IAAgB,MAAM,IAChD,KAAiB;EAElB,OAAO;CACR;CAKA,OAAO,eAAe,GAAmB,IAAoB,GAA6B,IAAmB,UAAkB;EAC9H,IAAI,IAAW,GACX,IAAW,GACX,IAAgB;EACpB,OAAM,IAAW,IAGhB,AAFA,KAAY,GACZ,KACA,IAAgB,KAAK,IAAI,IAAgB,GAAG,CAAgB;EAE7D,OAAO;CACR;CAEA,OAAO,YAAY,GAAkB,IAAoB,GAA6B,IAAmB,UAAkB;EAC1H,IAAI,I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,IAAgB,QAAQ,KAAK,KAAK,WAAW,MAAM,IAAsB,CAAY,GACrF,IAAU,EAAW,GAAe,KAAK,OAAO,YAAY;GAChE,IAAmB,IAAI,EAAgB,KAAK,QAAQ,CAAO;EAC5D;EAEA,IAAI,IAAO,EAAc,KAAK,MAAM,EAAiB,KAAK,QAAQ,EAAiB,kBAAkB,CAAa,GAE9G,IAAqB,EAAiB,mBAAmB,IAAgB,KAAK,sBAAsB,YAAY;EAIpH,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,GAAe,KAAK,OAAO,YAAY;GAEhE,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
|
+
{"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 { lock, unlock } from './lock/simple-lock';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, storePointer } from './utils/pointer';\nimport { getArrayTypeCode, getByteMultipler, makeArrayView, type NumericArray, type NumericArrayIO, type TypedArrayConstructor } from './utils/array-type';\n\nconst LENGTH_INDEX = 0;\nconst TYPE_INDEX = 1;\nconst BASE_SEGMENT_LENGTH_INDEX = 2;\nconst MAX_SEGMENTS_INDEX = 3;\nconst MAX_SEGMENT_LENGTH_INDEX = 4;\nconst SEGMENT_COUNT_INDEX = 5;\nconst GROW_LOCK_INDEX = 6;\nconst SEGMENT_START_INDEX = 7;\n\nconst DEFAULT_BASE_SEGMENT_LENGTH = 4;\n// Default segment budget. With uncapped power-of-two growth these 20 segments spanned 4,194,300 slots, but that ceiling\n// was never reachable: past segment 14 a segment's block outgrows a 1MB buffer and allocation throws. Capping segments at\n// what a buffer holds, the honest capacity of 20 segments is getCapacity(20, base, cap) instead.\nconst DEFAULT_MAX_SEGMENTS = 20;\n\n// The LENGTH word packs an ABA version counter (high bits) with the live count (low LEN_BITS). Both push and pop bump\n// the version on every successful CAS, so the CAS fails on ANY intervening op - the count can never ABA back to the\n// same value with a slot at a different generation. 22 count bits cover the default max length (4,194,300); the\n// remaining 10 version bits (1024) only need to outlast a single thread's load->CAS window.\nconst LEN_BITS = 22;\nconst LEN_MASK = (1 << LEN_BITS) - 1;\nconst MAX_COUNT = LEN_MASK - 1;\n// Adding this to the packed word bumps the version (high bits) by one. Combined with +1/-1 for the count it repacks the\n// whole word in a single add: the count is guarded from overflowing into the version bits, and a version overflow just\n// wraps harmlessly when the value is coerced back to Uint32 on the atomic store.\nconst VERSION_STEP = 1 << LEN_BITS;\n\n// Per-slot publish handshake. Each data slot has a companion sequence number: even means the slot is empty (free to\n// fill) at some lap, odd means the value is published (safe to read). It only ever increases. push/pop read the slot's\n// sequence and bail out (retry) unless it has the parity they need, then CAS the length; because the versioned CAS\n// rejects any intervening op, the sequence read stays valid all the way through the publish store below.\n\nexport default class SharedStack<T extends NumericArray = Uint32Array> implements Iterable<T[number]> {\n\tstatic readonly BASE_ALLOCATE_COUNT = 8;\n\tstatic readonly DEFAULT_ALLOCATE_COUNT = this.BASE_ALLOCATE_COUNT + DEFAULT_MAX_SEGMENTS;\n\n\t// Largest power-of-two segment length whose block (segmentLength data slots plus an equal run of publish sequences)\n\t// still fits in a single buffer. Segments double until they reach this, then stay flat so a segment can always be\n\t// allocated in one contiguous block. maxAllocationLength is MemoryHeap.maxAllocationLength. byteMultipler is the u32\n\t// units per data slot (2 for 64-bit types), so a length-L segment's block is L * byteMultipler data u32s + L sequence u32s.\n\tstatic getMaxSegmentLength(maxAllocationLength: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, byteMultipler = 1): number {\n\t\tlet segmentLength = baseSegmentLength;\n\t\t// A length-L segment needs a block of L * (byteMultipler + 1) u32s, so the next double fits while\n\t\t// 2L * (byteMultipler + 1) <= maxAllocationLength\n\t\twhile(segmentLength * 2 * (byteMultipler + 1) <= maxAllocationLength) {\n\t\t\tsegmentLength *= 2;\n\t\t}\n\t\treturn segmentLength;\n\t}\n\n\t// Smallest segment count whose summed capacity covers maxLength. Segments double from baseSegmentLength but never grow\n\t// past maxSegmentLength, so once capped they each add a flat maxSegmentLength. Looped rather than solved in closed form\n\t// to avoid float rounding landing a segment short or over.\n\tstatic getMaxSegments(maxLength: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, maxSegmentLength = Infinity): number {\n\t\tlet segments = 0;\n\t\tlet capacity = 0;\n\t\tlet segmentLength = baseSegmentLength;\n\t\twhile(capacity < maxLength) {\n\t\t\tcapacity += segmentLength;\n\t\t\tsegments++;\n\t\t\tsegmentLength = Math.min(segmentLength * 2, maxSegmentLength);\n\t\t}\n\t\treturn segments;\n\t}\n\t// Total capacity of the first `segments` segments under the same doubling-then-capped growth as getMaxSegments\n\tstatic getCapacity(segments: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, maxSegmentLength = Infinity): number {\n\t\tlet capacity = 0;\n\t\tlet segmentLength = baseSegmentLength;\n\t\tfor(let i = 0; i < segments; i++) {\n\t\t\tcapacity += segmentLength;\n\t\t\tsegmentLength = Math.min(segmentLength * 2, maxSegmentLength);\n\t\t}\n\t\treturn capacity;\n\t}\n\tstatic getAllocateCount(maxLength: number, baseSegmentLength = DEFAULT_BASE_SEGMENT_LENGTH, maxSegmentLength = Infinity): number {\n\t\treturn this.BASE_ALLOCATE_COUNT + this.getMaxSegments(maxLength, baseSegmentLength, maxSegmentLength);\n\t}\n\tprivate memory: MemoryHeap;\n\n\t// Length, Type, Base Segment Length, Max Segments, Max Segment Length, Segment Count, Grow Lock, Segments\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\tprivate growLock: Int32Array;\n\treadonly maxLength: number;\n\n\t// False when firstBlock is a slice of another structure's memory (e.g. inlined in SharedPool); that owner frees it\n\tprivate ownsFirstBlock: boolean;\n\n\tprivate cachedSegments: T[] = [];\n\t// Parallel to cachedSegments: the per-slot publish sequences for each slot of that segment. Populated together with\n\t// the data view in getSegment, so whenever cachedSegments[i] is set cachedSequences[i] is too.\n\tprivate cachedSequences: Int32Array[] = [];\n\tprivate cachedBaseSegmentLength = 0;\n\t// Segment growth caps at cachedMaxSegmentLength. Segments 0..(cachedCapSegmentCount - 1) double geometrically and cover\n\t// the first cachedGeometricCapacity slots; every segment beyond that is a flat cachedMaxSegmentLength. Indexing splits\n\t// on cachedGeometricCapacity: below it the fast log2 mapping, above it a plain linear division into capped segments.\n\tprivate cachedMaxSegmentLength = 0;\n\tprivate cachedCapSegmentCount = 0;\n\tprivate cachedGeometricCapacity = 0;\n\t// u32 units per data slot: 1 for 32-bit types, 2 for 64-bit (float64/int64/uint64). A segment block is\n\t// segmentLength * cachedByteMultipler data u32s followed by segmentLength sequence u32s.\n\tprivate cachedByteMultipler = 1;\n\n\tget length(): number {\n\t\treturn Atomics.load(this.firstBlock.data, LENGTH_INDEX) & LEN_MASK;\n\t}\n\tget segmentCount(): number {\n\t\treturn Atomics.load(this.firstBlock.data, SEGMENT_COUNT_INDEX);\n\t}\n\tprivate set segmentCount(value: number) {\n\t\tthis.firstBlock.data[SEGMENT_COUNT_INDEX] = value;\n\t}\n\t\n\tget type(): number {\n\t\treturn this.uint16Array[0];\n\t}\n\tprivate set type(value: number) {\n\t\tAtomics.store(this.uint16Array, 0, value);\n\t}\n\n\tget baseSegmentLength() {\n\t\treturn this.firstBlock.data[BASE_SEGMENT_LENGTH_INDEX];\n\t}\n\tprivate set baseSegmentLength(value: number) {\n\t\tthis.firstBlock.data[BASE_SEGMENT_LENGTH_INDEX] = value;\n\t}\n\tget maxSegments() {\n\t\treturn this.firstBlock.data[MAX_SEGMENTS_INDEX];\n\t}\n\tprivate set maxSegments(value: number) {\n\t\tthis.firstBlock.data[MAX_SEGMENTS_INDEX] = value;\n\t}\n\tget maxSegmentLength() {\n\t\treturn this.firstBlock.data[MAX_SEGMENT_LENGTH_INDEX];\n\t}\n\tprivate set maxSegmentLength(value: number) {\n\t\tthis.firstBlock.data[MAX_SEGMENT_LENGTH_INDEX] = value;\n\t}\n\n\tget pointer() {\n\t\treturn this.firstBlock.pointer;\n\t}\n\n\t// Own internal memory plus every allocated segment. The firstBlock is excluded when inlined in another structure\n\t// (e.g. SharedPool) since that owner already counts it as part of its own internal memory.\n\tget usedMemory(): number {\n\t\tlet total = this.ownsFirstBlock ? this.firstBlock.usedMemory : 0;\n\t\tfor(let i = 0; i < this.segmentCount; i++) {\n\t\t\tlet pointer = getPointer(Atomics.load(this.firstBlock.data, SEGMENT_START_INDEX + i), this.memory.positionBits);\n\t\t\ttotal += new AllocatedMemory(this.memory, pointer).usedMemory;\n\t\t}\n\t\treturn total;\n\t}\n\n\tconstructor(memory: MemoryHeap, config?: SharedStackConfig<T> | SharedStackMemory | SharedStackMemory & SharedStackConfig<T>) {\n\t\tthis.memory = memory;\n\n\t\tif(config && 'firstBlock' in config) {\n\t\t\t// An AllocatedMemory instance is a caller-reserved slice (e.g. inlined in SharedPool); a plain pointer is the\n\t\t\t// serialized clone path. Taking the instance directly keeps us off the pointer-resolving construction branch.\n\t\t\tif(config.firstBlock instanceof AllocatedMemory) {\n\t\t\t\tthis.firstBlock = config.firstBlock;\n\t\t\t} else {\n\t\t\t\tconst maxSegments = new Uint32Array(\n\t\t\t\t\tmemory.buffers[config.firstBlock.bufferPosition].buf,\n\t\t\t\t\tconfig.firstBlock.bufferByteOffset + MAX_SEGMENTS_INDEX * Uint32Array.BYTES_PER_ELEMENT,\n\t\t\t\t\t1,\n\t\t\t\t)[0];\n\t\t\t\tthis.firstBlock = new AllocatedMemory(memory, {\n\t\t\t\t\tlength: SharedStack.BASE_ALLOCATE_COUNT + maxSegments,\n\t\t\t\t\t...config.firstBlock,\n\t\t\t\t});\n\t\t\t}\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\n\t\t\t// Memory passed together with config means the caller reserved the firstBlock slot for us to initialize in place\n\t\t\tif('type' in config || 'baseSegmentLength' in config || 'maxLength' in config) {\n\t\t\t\tthis.initialize(config);\n\t\t\t\tthis.ownsFirstBlock = false;\n\t\t\t} else {\n\t\t\t\tthis.ownsFirstBlock = true;\n\t\t\t}\n\t\t} else {\n\t\t\tlet byteMultipler = getByteMultipler(getArrayTypeCode(config?.type ?? Uint32Array));\n\t\t\tlet baseSegmentLength = config?.baseSegmentLength ?? DEFAULT_BASE_SEGMENT_LENGTH;\n\t\t\tlet maxSegmentLength = this.resolveMaxSegmentLength(baseSegmentLength, config?.maxSegmentLength, byteMultipler);\n\t\t\tlet maxSegments = this.resolveMaxSegments(config?.maxLength, baseSegmentLength, maxSegmentLength);\n\n\t\t\tthis.firstBlock = memory.allocUI32(SharedStack.BASE_ALLOCATE_COUNT + maxSegments);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\t\t\tthis.ownsFirstBlock = true;\n\n\t\t\tthis.initialize(config);\n\t\t}\n\n\t\tthis.growLock = new Int32Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + GROW_LOCK_INDEX * Uint32Array.BYTES_PER_ELEMENT, 1);\n\t\tthis.cachedByteMultipler = getByteMultipler(this.type);\n\t\tthis.cachedBaseSegmentLength = this.baseSegmentLength;\n\t\tthis.cachedMaxSegmentLength = this.maxSegmentLength;\n\t\t// Segments 0..(K-1) double geometrically up to the cap, where K = log2(cap / base) + 1\n\t\tthis.cachedCapSegmentCount = 32 - Math.clz32(((this.cachedMaxSegmentLength / this.cachedBaseSegmentLength) | 0));\n\t\tthis.cachedGeometricCapacity = this.cachedBaseSegmentLength * ((1 << this.cachedCapSegmentCount) - 1);\n\t\tthis.maxLength = SharedStack.getCapacity(this.maxSegments, this.cachedBaseSegmentLength, this.cachedMaxSegmentLength);\n\t\tif(this.maxLength > MAX_COUNT) {\n\t\t\t// The length word reserves LEN_BITS for the count and the rest for the ABA version tag\n\t\t\tthrow new Error(`SharedStack maxLength ${this.maxLength} exceeds the ${MAX_COUNT} the versioned length counter can address`);\n\t\t}\n\t}\n\n\t// Cap on segment length: the largest a single buffer can hold, optionally lowered to a caller-requested value (floored\n\t// to a power-of-two multiple of the base). Beyond this segments stop doubling and stay flat so each still fits a buffer.\n\tprivate resolveMaxSegmentLength(baseSegmentLength: number, requested?: number, byteMultipler = 1): number {\n\t\tlet cap = SharedStack.getMaxSegmentLength(this.memory.maxAllocationLength, baseSegmentLength, byteMultipler);\n\t\tif(requested) {\n\t\t\tlet floored = baseSegmentLength;\n\t\t\twhile(floored * 2 <= requested) {\n\t\t\t\tfloored *= 2;\n\t\t\t}\n\t\t\tcap = Math.min(cap, floored);\n\t\t}\n\t\treturn cap;\n\t}\n\n\t// Segments needed to cover maxLength (or the default budget when none is given), but never more than the segment-pointer\n\t// spine can hold in a single buffer. Once capped, segments no longer double, so a huge maxLength on a small buffer is\n\t// honestly bounded here instead of blowing up later when the spine (or a segment) can't be allocated.\n\tprivate resolveMaxSegments(maxLength: number | undefined, baseSegmentLength: number, maxSegmentLength: number): number {\n\t\tlet needed = maxLength === undefined\n\t\t\t? DEFAULT_MAX_SEGMENTS\n\t\t\t: SharedStack.getMaxSegments(maxLength, baseSegmentLength, maxSegmentLength);\n\t\tlet spineLimit = this.memory.maxAllocationLength - SharedStack.BASE_ALLOCATE_COUNT;\n\t\treturn Math.min(needed, spineLimit);\n\t}\n\n\tprivate initialize(config?: SharedStackConfig<T>) {\n\t\tlet typeCode = getArrayTypeCode(config?.type ?? Uint32Array);\n\t\tlet byteMultipler = getByteMultipler(typeCode);\n\t\tlet baseSegmentLength = config?.baseSegmentLength ?? DEFAULT_BASE_SEGMENT_LENGTH;\n\t\tlet maxSegmentLength = this.resolveMaxSegmentLength(baseSegmentLength, config?.maxSegmentLength, byteMultipler);\n\t\t// Each segment block holds the data slots (byteMultipler u32s each) followed by one publish sequence u32 per slot\n\t\tlet firstSegmentBlock = this.memory.allocUI32(baseSegmentLength * (byteMultipler + 1));\n\t\tstorePointer(this.firstBlock.data, SEGMENT_START_INDEX, firstSegmentBlock.bufferPosition, firstSegmentBlock.bufferByteOffset, this.memory.positionBits);\n\t\tthis.baseSegmentLength = baseSegmentLength;\n\t\tthis.maxSegmentLength = maxSegmentLength;\n\t\tthis.maxSegments = this.resolveMaxSegments(config?.maxLength, baseSegmentLength, maxSegmentLength);\n\t\tthis.segmentCount = 1;\n\t\tthis.type = typeCode;\n\t}\n\n\tat(index: number): T[number] {\n\t\tlet length = this.length;\n\t\tif(index >= length || index < 0) {\n\t\t\tthrow new Error(`${index} is out of bounds ${length}`);\n\t\t}\n\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\tlet segmentIndex: number;\n\t\tlet localIndex: number;\n\t\tif(index < geometricCapacity) {\n\t\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t\tsegmentIndex = 31 - Math.clz32(((index / baseSegmentLength) | 0) + 1);\n\t\t\tlocalIndex = index - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t} else {\n\t\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\t\tlet offset = index - geometricCapacity;\n\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t}\n\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\t// push() bumps the length before it stores the value and publishes the slot, so an index already counted in length\n\t\t// can still be mid-publish. Wait for the slot's sequence to go odd (published) before reading, otherwise we hand\n\t\t// back a slot whose value hasn't landed yet - for the pool's chunk pointers that means decoding a zeroed pointer.\n\t\tlet sequences = this.cachedSequences[segmentIndex];\n\t\twhile((Atomics.load(sequences, localIndex) & 1) === 0) { /* spin: value is being published on another thread */ }\n\t\treturn (segment as NumericArrayIO)[localIndex];\n\t}\n\n\tpush(value: T[number]): number {\n\t\tlet data = this.firstBlock.data;\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\t// Plain (relaxed) read is enough: it only seeds the versioned CAS below, which rejects any stale value\n\t\t\tlet packed = data[LENGTH_INDEX];\n\t\t\tlet newIndex = packed & LEN_MASK;\n\t\t\tif(newIndex >= this.maxLength) {\n\t\t\t\tthrow new Error(`${newIndex + 1} is out of bounds ${this.maxLength}`);\n\t\t\t}\n\n\t\t\tlet segmentIndex: number;\n\t\t\tlet localIndex: number;\n\t\t\tif(newIndex < geometricCapacity) {\n\t\t\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t\t\tsegmentIndex = 31 - Math.clz32(((newIndex / baseSegmentLength) | 0) + 1);\n\t\t\t\tlocalIndex = newIndex - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\t} else {\n\t\t\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\t\t\tlet offset = newIndex - geometricCapacity;\n\t\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t\t}\n\t\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\t\tlet sequences = this.cachedSequences[segmentIndex];\n\n\t\t\t// The slot must be empty (even sequence) to fill. Odd means a popper hasn't finished emptying it yet - retry\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) !== 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// Take the slot only if the whole versioned length word is unchanged. The version bump makes this fail on any\n\t\t\t// intervening op, so a win guarantees nothing touched the top - and our sequence read above is still valid\n\t\t\tif(Atomics.compareExchange(data, LENGTH_INDEX, packed, packed + VERSION_STEP + 1) !== packed) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t(segment as NumericArrayIO)[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(): T[number] | undefined {\n\t\tlet data = this.firstBlock.data;\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\t// eslint-disable-next-line no-constant-condition\n\t\twhile(true) {\n\t\t\t// Plain (relaxed) read is enough: it only seeds the versioned CAS below, which rejects any stale value\n\t\t\tlet packed = data[LENGTH_INDEX];\n\t\t\tlet length = packed & LEN_MASK;\n\t\t\tif(length <= 0) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tlet oldIndex = length - 1;\n\n\t\t\tlet segmentIndex: number;\n\t\t\tlet localIndex: number;\n\t\t\tif(oldIndex < geometricCapacity) {\n\t\t\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\t\t\tsegmentIndex = 31 - Math.clz32(((oldIndex / baseSegmentLength) | 0) + 1);\n\t\t\t\tlocalIndex = oldIndex - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\t} else {\n\t\t\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\t\t\tlet offset = oldIndex - geometricCapacity;\n\t\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t\t}\n\t\t\tlet segment = this.cachedSegments[segmentIndex] ?? this.getSegment(segmentIndex);\n\t\t\tlet sequences = this.cachedSequences[segmentIndex];\n\n\t\t\t// The slot must be full (odd sequence) to read. Even means a pusher hasn't finished publishing it yet - retry\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) === 0) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif(Atomics.compareExchange(data, LENGTH_INDEX, packed, packed + VERSION_STEP - 1) !== packed) {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\tlet value = (segment as NumericArrayIO)[localIndex] as T[number];\n\t\t\t// Release the slot: bump to even (empty) so a later push may reuse it\n\t\t\tAtomics.store(sequences, localIndex, sequence + 1);\n\t\t\treturn value;\n\t\t}\n\t}\n\n\tclear() {\n\t\t// Mark every occupied slot empty (bump its odd sequence to the next even) before dropping the length, otherwise a\n\t\t// later push reusing the slot would spin forever on a stale full sequence. Not safe to call concurrently with push/pop.\n\t\tlet baseSegmentLength = this.cachedBaseSegmentLength;\n\t\tlet geometricCapacity = this.cachedGeometricCapacity;\n\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\tlet oldLength = Atomics.load(this.firstBlock.data, LENGTH_INDEX) & LEN_MASK;\n\t\tfor(let i = 0; i < oldLength; i++) {\n\t\t\tlet segmentIndex: number;\n\t\t\tlet localIndex: number;\n\t\t\tif(i < geometricCapacity) {\n\t\t\t\tsegmentIndex = 31 - Math.clz32(((i / baseSegmentLength) | 0) + 1);\n\t\t\t\tlocalIndex = i - baseSegmentLength * ((1 << segmentIndex) - 1);\n\t\t\t} else {\n\t\t\t\tlet offset = i - geometricCapacity;\n\t\t\t\tlet extra = (offset / maxSegmentLength) | 0;\n\t\t\t\tsegmentIndex = this.cachedCapSegmentCount + extra;\n\t\t\t\tlocalIndex = offset - extra * maxSegmentLength;\n\t\t\t}\n\t\t\tlet sequences = this.cachedSequences[segmentIndex] ?? (this.getSegment(segmentIndex), this.cachedSequences[segmentIndex]);\n\t\t\tlet sequence = Atomics.load(sequences, localIndex);\n\t\t\tif((sequence & 1) !== 0) {\n\t\t\t\tAtomics.store(sequences, localIndex, sequence + 1);\n\t\t\t}\n\t\t}\n\t\tAtomics.store(this.firstBlock.data, LENGTH_INDEX, 0);\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet totalDataEntries = this.length;\n\n\t\tlet maxSegmentLength = this.cachedMaxSegmentLength;\n\t\tlet segmentLength = this.baseSegmentLength;\n\t\tlet segmentIndex = 0;\n\t\tlet segment = this.getSegment(segmentIndex);\n\t\tlet segmentDataIndex = 0;\n\t\tfor(let i = 0; i < totalDataEntries; i++) {\n\t\t\tyield (segment as NumericArrayIO)[segmentDataIndex] as T[number];\n\t\t\tsegmentDataIndex++;\n\t\t\tif(segmentDataIndex >= segmentLength) {\n\t\t\t\tsegmentIndex++;\n\t\t\t\tsegment = this.getSegment(segmentIndex);\n\t\t\t\tsegmentDataIndex = 0;\n\t\t\t\tsegmentLength = Math.min(segmentLength * 2, maxSegmentLength);\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate getSegment(segmentIndex: number): T {\n\t\tif(this.cachedSegments[segmentIndex]) {\n\t\t\treturn this.cachedSegments[segmentIndex];\n\t\t}\n\n\t\t// Geometric while below the cap, then flat so the block always fits a single buffer\n\t\tlet segmentLength = segmentIndex < this.cachedCapSegmentCount\n\t\t\t? this.cachedBaseSegmentLength << segmentIndex\n\t\t\t: this.cachedMaxSegmentLength;\n\t\tlet segmentDataBlock: AllocatedMemory | undefined;\n\t\tif(segmentIndex >= this.segmentCount) {\n\t\t\t// Lock when growing so another thread doesn't also setup the same segment\n\t\t\tlock(this.growLock);\n\t\t\ttry {\n\t\t\t\t// Re-check under the lock: another thread may have already appended the segment while we were waiting\n\t\t\t\tif(segmentIndex >= this.segmentCount) {\n\t\t\t\t\t// Data slots (byteMultipler u32s each) followed by one publish sequence u32 per slot (calloc-zeroed = 0 = empty)\n\t\t\t\t\tsegmentDataBlock = this.memory.allocUI32(segmentLength * (this.cachedByteMultipler + 1));\n\t\t\t\t\tstorePointer(this.firstBlock.data, SEGMENT_START_INDEX + segmentIndex, segmentDataBlock.bufferPosition, segmentDataBlock.bufferByteOffset, this.memory.positionBits);\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, this.memory.positionBits);\n\t\t\tsegmentDataBlock = new AllocatedMemory(this.memory, pointer);\n\t\t}\n\n\t\tlet data = makeArrayView(this.type, segmentDataBlock.data.buffer, segmentDataBlock.bufferByteOffset, segmentLength) as T;\n\t\t// Publish sequences live after the data region (segmentLength * byteMultipler u32s), always as Int32 regardless of type\n\t\tlet sequenceByteOffset = segmentDataBlock.bufferByteOffset + segmentLength * this.cachedByteMultipler * 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, this.memory.positionBits);\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 NumericArray> {\n\ttype?: TypedArrayConstructor<T>\n\n\tbaseSegmentLength?: number\n\tmaxLength?: number\n\t// Cap on how large a single segment may grow. Defaults to the largest a buffer can hold; a smaller value is floored to\n\t// a power-of-two multiple of baseSegmentLength. Beyond the cap segments stay flat instead of doubling.\n\tmaxSegmentLength?: number\n}\ninterface SharedStackMemory {\n\tfirstBlock: SharedAllocatedMemory | AllocatedMemory\n}\n\nexport type { SharedStackConfig, SharedStackMemory };"],"mappings":";;;;;AAOA,IAAM,IAAe,GACf,IAAa,GACb,IAA4B,GAC5B,IAAqB,GACrB,IAA2B,GAC3B,IAAsB,GACtB,IAAkB,GAClB,IAAsB,GAEtB,IAA8B,GAI9B,IAAuB,IAMvB,IAAW,IACX,KAAY,KAAK,KAAY,GAC7B,IAAY,SAIZ,IAAe,KAAK,GAOL,IAArB,MAAqB,EAAiF;CACrG,OAAgB,sBAAsB;CACtC,OAAgB,yBAAyB,KAAK,sBAAsB;CAMpE,OAAO,oBAAoB,GAA6B,IAAoB,GAA6B,IAAgB,GAAW;EACnI,IAAI,IAAgB;EAGpB,OAAM,IAAgB,KAAK,IAAgB,MAAM,IAChD,KAAiB;EAElB,OAAO;CACR;CAKA,OAAO,eAAe,GAAmB,IAAoB,GAA6B,IAAmB,UAAkB;EAC9H,IAAI,IAAW,GACX,IAAW,GACX,IAAgB;EACpB,OAAM,IAAW,IAGhB,AAFA,KAAY,GACZ,KACA,IAAgB,KAAK,IAAI,IAAgB,GAAG,CAAgB;EAE7D,OAAO;CACR;CAEA,OAAO,YAAY,GAAkB,IAAoB,GAA6B,IAAmB,UAAkB;EAC1H,IAAI,IAAW,GACX,IAAgB;EACpB,KAAI,IAAI,IAAI,GAAG,IAAI,GAAU,KAE5B,AADA,KAAY,GACZ,IAAgB,KAAK,IAAI,IAAgB,GAAG,CAAgB;EAE7D,OAAO;CACR;CACA,OAAO,iBA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,KAAK,QAAQ;GACrB;EACD;EAEA,IAAG,CAAC,GAAkB;GACrB,IAAI,IAAgB,QAAQ,KAAK,KAAK,WAAW,MAAM,IAAsB,CAAY,GACrF,IAAU,EAAW,GAAe,KAAK,OAAO,YAAY;GAChE,IAAmB,IAAI,EAAgB,KAAK,QAAQ,CAAO;EAC5D;EAEA,IAAI,IAAO,EAAc,KAAK,MAAM,EAAiB,KAAK,QAAQ,EAAiB,kBAAkB,CAAa,GAE9G,IAAqB,EAAiB,mBAAmB,IAAgB,KAAK,sBAAsB,YAAY;EAIpH,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,GAAe,KAAK,OAAO,YAAY;GAEhE,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"}
|
package/dist/shared-vector.js
CHANGED
|
@@ -43,7 +43,10 @@ var c = 0, l = 1, u = 2, d = 3, f = 4, p = class p {
|
|
|
43
43
|
cachedPointer;
|
|
44
44
|
constructor(e, t) {
|
|
45
45
|
if (this.memory = e, t && "firstBlock" in t) {
|
|
46
|
-
if (this.firstBlock = new i(e,
|
|
46
|
+
if (this.firstBlock = new i(e, {
|
|
47
|
+
length: p.ALLOCATE_COUNT,
|
|
48
|
+
...t.firstBlock
|
|
49
|
+
}), this.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + d * Uint32Array.BYTES_PER_ELEMENT, 2), "type" in t || "dataLength" in t) {
|
|
47
50
|
let r = a(t.type ?? Uint32Array), i = o(r), s = t.bufferLength ?? f, l = e.allocUI32(s * (t.dataLength ?? 1) * i);
|
|
48
51
|
n(this.firstBlock.data, c, l.bufferPosition, l.bufferByteOffset, e.positionBits), this.bufferLength = s, this.dataLength = t.dataLength ?? 1, this.type = r;
|
|
49
52
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shared-vector.js","names":[],"sources":["../src/shared-vector.ts"],"sourcesContent":["import type { SharedAllocatedMemory } from './allocated-memory';\nimport AllocatedMemory from './allocated-memory';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, loadPointer, storePointer, storeRawPointer } from './utils/pointer';\nimport { getArrayTypeCode, getByteMultipler, makeArrayView, type NumericArray, type NumericArrayIO, type TypedArrayConstructor } from './utils/array-type';\n\nconst VECTOR_INDEX = 0;\nconst LENGTH_INDEX = 1;\nconst BUFFER_LENGTH_INDEX = 2;\nconst TYPE_INDEX = 3;\nconst DEFAULT_SIZE = 4;\nexport default class SharedVector<T extends NumericArray = Uint32Array> implements Iterable<T> {\n\tstatic readonly ALLOCATE_COUNT = 4;\n\tprivate memory: MemoryHeap;\n\n\t// Pointer, List Length, Buffer Length, Type/DataLength\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\t// u32 units per element: 1 for 32-bit types, 2 for 64-bit (float64/int64/uint64). Every data-block allocation scales\n\t// by this; the typed view length stays in elements.\n\tprivate cachedByteMultipler = 1;\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 this.uint16Array[0];\n\t}\n\tprivate set type(value: number) {\n\t\tAtomics.store(this.uint16Array, 0, value);\n\t}\n\tget byteMultipler(): number {\n\t\treturn this.cachedByteMultipler;\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 this.uint16Array[1] || 1;\n\t}\n\tprivate set dataLength(value: number) {\n\t\tAtomics.store(this.uint16Array, 1, value);\n\t}\n\n\tget bufferLength(): number {\n\t\treturn Atomics.load(this.firstBlock.data, BUFFER_LENGTH_INDEX);\n\t}\n\tprivate set bufferLength(value: number) {\n\t\tAtomics.store(this.firstBlock.data, BUFFER_LENGTH_INDEX, value);\n\t}\n\n\tget pointer() {\n\t\treturn this.firstBlock.pointer;\n\t}\n\n\tget usedMemory(): number {\n\t\tlet dataMemory = new AllocatedMemory(this.memory, loadPointer(this.firstBlock.data, VECTOR_INDEX, this.memory.positionBits));\n\t\treturn this.firstBlock.usedMemory + dataMemory.usedMemory;\n\t}\n\n\tprivate cachedFullDataBlock?: T;\n\tprivate cachedPointer: number;\n\n\tconstructor(memory: MemoryHeap, config?: SharedVectorConfig<T> | SharedVectorMemory | SharedVectorMemory & SharedVectorConfig<T>) {\n\t\tthis.memory = memory;\n\n\t\tif(config && 'firstBlock' in config) {\n\t\t\tthis.firstBlock = new AllocatedMemory(memory, config.firstBlock);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\n\t\t\t// Pre-allocating memory and setting up in specific memory location\n\t\t\tif('type' in config || 'dataLength' in config) {\n\t\t\t\tconst typeCode = getArrayTypeCode(config.type ?? Uint32Array);\n\t\t\t\tconst byteMultipler = getByteMultipler(typeCode);\n\t\t\t\tconst bufferLength = config.bufferLength ?? DEFAULT_SIZE;\n\t\t\t\tlet dataBlock = memory.allocUI32(bufferLength * (config.dataLength ?? 1) * byteMultipler);\n\t\t\t\tstorePointer(this.firstBlock.data, VECTOR_INDEX, dataBlock.bufferPosition, dataBlock.bufferByteOffset, memory.positionBits);\n\t\t\t\tthis.bufferLength = bufferLength;\n\t\t\t\tthis.dataLength = (config.dataLength ?? 1);\n\t\t\t\tthis.type = typeCode;\n\t\t\t}\n\t\t} else {\n\t\t\tthis.firstBlock = memory.allocUI32(SharedVector.ALLOCATE_COUNT);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\n\t\t\tlet dataLength = config?.dataLength ?? 1;\n\t\t\tlet bufferLength = config?.bufferLength ?? DEFAULT_SIZE;\n\t\t\tlet typeCode = getArrayTypeCode(config?.type ?? Uint32Array);\n\t\t\tlet dataBlock = memory.allocUI32(bufferLength * dataLength * getByteMultipler(typeCode));\n\t\t\tstorePointer(this.firstBlock.data, VECTOR_INDEX, dataBlock.bufferPosition, dataBlock.bufferByteOffset, memory.positionBits);\n\t\t\tthis.bufferLength = bufferLength;\n\t\t\tthis.type = typeCode;\n\t\t\tthis.dataLength = dataLength;\n\t\t}\n\n\t\tthis.cachedByteMultipler = getByteMultipler(this.type);\n\t\tthis.cachedPointer = this.firstBlock.data[0];\n\t\tthis.cachedFullDataBlock = this.getFullDataBlock();\n\t}\n\n\tat(index: number): T {\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 dataBlock = this.getFullDataBlock();\n\t\treturn this.getDataBlock(dataBlock, index);\n\t}\n\tget(index: number, dataIndex = 0): T[number] {\n\t\tif(dataIndex >= this.dataLength) {\n\t\t\tthrow new Error(`${dataIndex} is out of dataLength bounds ${this.dataLength}`);\n\t\t} else if(index >= this.length || index < 0) {\n\t\t\tthrow new Error(`${index} is out of bounds ${this.length}`);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock() as NumericArrayIO;\n\t\treturn dataBlock[index * this.dataLength + dataIndex];\n\t}\n\n\tpush(values: T[number] | Array<T[number]>): number {\n\t\tlet dataLength = this.dataLength;\n\t\tconst isSingleValue = typeof values !== 'object';\n\t\tif(!isSingleValue && values.length > dataLength) {\n\t\t\tthrow new Error(`Can't insert ${values.length} array into shared list of ${dataLength} dataLength`);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock() as NumericArrayIO;\n\t\tlet currentLength = this.length;\n\t\tlet startIndex = dataLength * currentLength;\n\t\tif(isSingleValue) {\n\t\t\tdataBlock[startIndex] = values;\n\t\t} else {\n\t\t\tfor(let i = 0; i < values.length; i++) {\n\t\t\t\tdataBlock[startIndex + i] = values[i];\n\t\t\t}\n\t\t}\n\n\t\tlet newLength = Atomics.add(this.firstBlock.data, LENGTH_INDEX, 1) + 1;\n\t\tif(newLength >= this.bufferLength) {\n\t\t\tthis.growBuffer();\n\t\t}\n\n\t\treturn currentLength;\n\t}\n\n\tpop(): T {\n\t\tlet oldLength = Atomics.sub(this.firstBlock.data, LENGTH_INDEX, 1);\n\n\t\tlet dataBlock = this.getFullDataBlock();\n\t\treturn this.getDataBlock(dataBlock, oldLength - 1);\n\t}\n\n\t// Returns the first number irregardless of dataLength - faster way to pop if you don't care about the rest of the data block\n\tpopNumber(): T[number] {\n\t\tconst oldLength = Atomics.sub(this.firstBlock.data, LENGTH_INDEX, 1);\n\t\tconst dataBlock = this.getFullDataBlock() as NumericArrayIO;\n\t\treturn dataBlock[(oldLength - 1) * this.dataLength];\n\t}\n\n\tdeleteIndex(index: 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 dataLength = this.dataLength;\n\t\tlet dataBlock = this.getFullDataBlock() as NumericArrayIO;\n\t\tfor(let i = index; i < length; i++) {\n\t\t\tfor(let j = 0; j < dataLength; j++) {\n\t\t\t\tdataBlock[i * dataLength + j] = dataBlock[(i + 1) * dataLength + j];\n\t\t\t}\n\t\t}\n\n\t\tAtomics.sub(this.firstBlock.data, LENGTH_INDEX, 1);\n\t}\n\n\tclear() {\n\t\tthis.firstBlock.data[LENGTH_INDEX] = 0;\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet dataBlock = this.getFullDataBlock();\n\t\tlet dataLength = this.dataLength;\n\t\tlet totalDataEntries = this.length * dataLength;\n\n\t\tfor(let i = 0; i < totalDataEntries; i += dataLength) {\n\t\t\tyield dataBlock.subarray(i, i + dataLength) as T;\n\t\t}\n\t}\n\n\t// Significantly faster but won't be thread safe if anything is removing/adding to it during iteration\n\tgetCurrentArray(): T {\n\t\tlet dataBlock = this.getFullDataBlock();\n\t\treturn dataBlock.subarray(0, this.length * this.dataLength) as T;\n\t}\n\n\tprivate getFullDataBlock(): T {\n\t\tlet pointerNumber = Atomics.load(this.firstBlock.data, VECTOR_INDEX);\n\t\tif(this.cachedPointer === pointerNumber && this.cachedFullDataBlock) {\n\t\t\treturn this.cachedFullDataBlock;\n\t\t}\n\n\t\tlet pointer = getPointer(pointerNumber, this.memory.positionBits);\n\t\tlet rawData = new AllocatedMemory(this.memory, pointer);\n\n\t\tlet data = makeArrayView(this.type, rawData.data.buffer, rawData.bufferByteOffset, this.dataLength * this.bufferLength) as T;\n\n\t\tthis.cachedPointer = pointerNumber;\n\t\tthis.cachedFullDataBlock = data;\n\n\t\treturn data;\n\t}\n\tprivate getDataBlock(rawData: T, index: number): T {\n\t\tconst start = index * this.dataLength;\n\t\treturn rawData.subarray(start, start + this.dataLength) as T;\n\t}\n\n\tprivate growBuffer() {\n\t\tlet oldBufferLength = this.bufferLength;\n\t\tlet newBufferLength = oldBufferLength * 2;\n\t\tlet dataLength = this.dataLength;\n\n\t\tlet oldPointer = loadPointer(this.firstBlock.data, VECTOR_INDEX, this.memory.positionBits);\n\t\tlet oldDataMemory = new AllocatedMemory(this.memory, oldPointer);\n\t\tlet oldDataBlock = this.getFullDataBlock();\n\t\tlet newDataBlock = this.memory.allocUI32(newBufferLength * dataLength * this.cachedByteMultipler);\n\n\t\tlet newData = makeArrayView(this.type, newDataBlock.data.buffer, newDataBlock.bufferByteOffset, dataLength * newBufferLength) as T;\n\n\t\t// newData and oldDataBlock are the same concrete view type, so a direct copy is valid; the cast only bridges the\n\t\t// union's mutually-incompatible set() overloads (number vs bigint element types)\n\t\t(newData as Float64Array).set(oldDataBlock as unknown as Float64Array);\n\n\t\tconst newPointer = newDataBlock.pointer;\n\t\tstoreRawPointer(this.firstBlock.data, VECTOR_INDEX, newPointer);\n\t\tthis.bufferLength = newBufferLength;\n\t\toldDataMemory.free();\n\n\t\tthis.cachedPointer = newPointer;\n\t\tthis.cachedFullDataBlock = newData;\n\t}\n\n\tfree() {\n\t\tlet pointer = loadPointer(this.firstBlock.data, VECTOR_INDEX, this.memory.positionBits);\n\t\tlet dataMemory = new AllocatedMemory(this.memory, pointer);\n\n\t\tdataMemory.free();\n\t\tthis.firstBlock.free();\n\t}\n\n\tgetSharedMemory(): SharedVectorMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.firstBlock.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedVectorConfig<T extends NumericArray> {\n\ttype?: TypedArrayConstructor<T>\n\tdataLength?: number\n\n\tbufferLength?: number\n}\ninterface SharedVectorMemory {\n\tfirstBlock: SharedAllocatedMemory\n}\n\nexport type { SharedVectorConfig, SharedVectorMemory };"],"mappings":";;;;AAMA,IAAM,IAAe,GACf,IAAe,GACf,IAAsB,GACtB,IAAa,GACb,IAAe,GACA,IAArB,MAAqB,EAA0E;CAC9F,OAAgB,iBAAiB;CACjC;CAGA;CACA;CAGA,sBAA8B;CAE9B,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAAK,KAAK,WAAW,MAAM,CAAY;CACvD;CAEA,IAAI,OAAe;EAClB,OAAO,KAAK,YAAY;CACzB;CACA,IAAY,KAAK,GAAe;EAC/B,QAAQ,MAAM,KAAK,aAAa,GAAG,CAAK;CACzC;CACA,IAAI,gBAAwB;EAC3B,OAAO,KAAK;CACb;CACA,IAAI,aAAqB;EAExB,OAAO,KAAK,YAAY,MAAM;CAC/B;CACA,IAAY,WAAW,GAAe;EACrC,QAAQ,MAAM,KAAK,aAAa,GAAG,CAAK;CACzC;CAEA,IAAI,eAAuB;EAC1B,OAAO,QA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BAAkB,KAAK,aAAa,KAAK,YAAY;EAKtH,OAHA,KAAK,gBAAgB,GACrB,KAAK,sBAAsB,GAEpB;CACR;CACA,aAAqB,GAAY,GAAkB;EAClD,IAAM,IAAQ,IAAQ,KAAK;EAC3B,OAAO,EAAQ,SAAS,GAAO,IAAQ,KAAK,UAAU;CACvD;CAEA,aAAqB;EAEpB,IAAI,IADkB,KAAK,eACa,GACpC,IAAa,KAAK,YAElB,IAAa,EAAY,KAAK,WAAW,MAAM,GAAc,KAAK,OAAO,YAAY,GACrF,IAAgB,IAAI,EAAgB,KAAK,QAAQ,CAAU,GAC3D,IAAe,KAAK,iBAAiB,GACrC,IAAe,KAAK,OAAO,UAAU,IAAkB,IAAa,KAAK,mBAAmB,GAE5F,IAAU,EAAc,KAAK,MAAM,EAAa,KAAK,QAAQ,EAAa,kBAAkB,IAAa,CAAe;EAI5H,EAA0B,IAAI,CAAuC;EAErE,IAAM,IAAa,EAAa;EAMhC,AALA,EAAgB,KAAK,WAAW,MAAM,GAAc,CAAU,GAC9D,KAAK,eAAe,GACpB,EAAc,KAAK,GAEnB,KAAK,gBAAgB,GACrB,KAAK,sBAAsB;CAC5B;CAEA,OAAO;EACN,IAAI,IAAU,EAAY,KAAK,WAAW,MAAM,GAAc,KAAK,OAAO,YAAY;EAItF,AADA,IAFqB,EAAgB,KAAK,QAAQ,CAElD,CAAA,CAAW,KAAK,GAChB,KAAK,WAAW,KAAK;CACtB;CAEA,kBAAsC;EACrC,OAAO,EACN,YAAY,KAAK,WAAW,gBAAgB,EAC7C;CACD;AACD"}
|
|
1
|
+
{"version":3,"file":"shared-vector.js","names":[],"sources":["../src/shared-vector.ts"],"sourcesContent":["import type { SharedAllocatedMemory } from './allocated-memory';\nimport AllocatedMemory from './allocated-memory';\nimport type MemoryHeap from './memory-heap';\nimport { getPointer, loadPointer, storePointer, storeRawPointer } from './utils/pointer';\nimport { getArrayTypeCode, getByteMultipler, makeArrayView, type NumericArray, type NumericArrayIO, type TypedArrayConstructor } from './utils/array-type';\n\nconst VECTOR_INDEX = 0;\nconst LENGTH_INDEX = 1;\nconst BUFFER_LENGTH_INDEX = 2;\nconst TYPE_INDEX = 3;\nconst DEFAULT_SIZE = 4;\nexport default class SharedVector<T extends NumericArray = Uint32Array> implements Iterable<T> {\n\tstatic readonly ALLOCATE_COUNT = 4;\n\tprivate memory: MemoryHeap;\n\n\t// Pointer, List Length, Buffer Length, Type/DataLength\n\tprivate firstBlock: AllocatedMemory;\n\tprivate uint16Array: Uint16Array;\n\t// u32 units per element: 1 for 32-bit types, 2 for 64-bit (float64/int64/uint64). Every data-block allocation scales\n\t// by this; the typed view length stays in elements.\n\tprivate cachedByteMultipler = 1;\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 this.uint16Array[0];\n\t}\n\tprivate set type(value: number) {\n\t\tAtomics.store(this.uint16Array, 0, value);\n\t}\n\tget byteMultipler(): number {\n\t\treturn this.cachedByteMultipler;\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 this.uint16Array[1] || 1;\n\t}\n\tprivate set dataLength(value: number) {\n\t\tAtomics.store(this.uint16Array, 1, value);\n\t}\n\n\tget bufferLength(): number {\n\t\treturn Atomics.load(this.firstBlock.data, BUFFER_LENGTH_INDEX);\n\t}\n\tprivate set bufferLength(value: number) {\n\t\tAtomics.store(this.firstBlock.data, BUFFER_LENGTH_INDEX, value);\n\t}\n\n\tget pointer() {\n\t\treturn this.firstBlock.pointer;\n\t}\n\n\tget usedMemory(): number {\n\t\tlet dataMemory = new AllocatedMemory(this.memory, loadPointer(this.firstBlock.data, VECTOR_INDEX, this.memory.positionBits));\n\t\treturn this.firstBlock.usedMemory + dataMemory.usedMemory;\n\t}\n\n\tprivate cachedFullDataBlock?: T;\n\tprivate cachedPointer: number;\n\n\tconstructor(memory: MemoryHeap, config?: SharedVectorConfig<T> | SharedVectorMemory | SharedVectorMemory & SharedVectorConfig<T>) {\n\t\tthis.memory = memory;\n\n\t\tif(config && 'firstBlock' in config) {\n\t\t\tthis.firstBlock = new AllocatedMemory(memory, {\n\t\t\t\tlength: SharedVector.ALLOCATE_COUNT,\n\t\t\t\t...config.firstBlock,\n\t\t\t});\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// Pre-allocating memory and setting up in specific memory location\n\t\t\tif('type' in config || 'dataLength' in config) {\n\t\t\t\tconst typeCode = getArrayTypeCode(config.type ?? Uint32Array);\n\t\t\t\tconst byteMultipler = getByteMultipler(typeCode);\n\t\t\t\tconst bufferLength = config.bufferLength ?? DEFAULT_SIZE;\n\t\t\t\tlet dataBlock = memory.allocUI32(bufferLength * (config.dataLength ?? 1) * byteMultipler);\n\t\t\t\tstorePointer(this.firstBlock.data, VECTOR_INDEX, dataBlock.bufferPosition, dataBlock.bufferByteOffset, memory.positionBits);\n\t\t\t\tthis.bufferLength = bufferLength;\n\t\t\t\tthis.dataLength = (config.dataLength ?? 1);\n\t\t\t\tthis.type = typeCode;\n\t\t\t}\n\t\t} else {\n\t\t\tthis.firstBlock = memory.allocUI32(SharedVector.ALLOCATE_COUNT);\n\t\t\tthis.uint16Array = new Uint16Array(this.firstBlock.data.buffer, this.firstBlock.bufferByteOffset + TYPE_INDEX * Uint32Array.BYTES_PER_ELEMENT, 2);\n\n\t\t\tlet dataLength = config?.dataLength ?? 1;\n\t\t\tlet bufferLength = config?.bufferLength ?? DEFAULT_SIZE;\n\t\t\tlet typeCode = getArrayTypeCode(config?.type ?? Uint32Array);\n\t\t\tlet dataBlock = memory.allocUI32(bufferLength * dataLength * getByteMultipler(typeCode));\n\t\t\tstorePointer(this.firstBlock.data, VECTOR_INDEX, dataBlock.bufferPosition, dataBlock.bufferByteOffset, memory.positionBits);\n\t\t\tthis.bufferLength = bufferLength;\n\t\t\tthis.type = typeCode;\n\t\t\tthis.dataLength = dataLength;\n\t\t}\n\n\t\tthis.cachedByteMultipler = getByteMultipler(this.type);\n\t\tthis.cachedPointer = this.firstBlock.data[0];\n\t\tthis.cachedFullDataBlock = this.getFullDataBlock();\n\t}\n\n\tat(index: number): T {\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 dataBlock = this.getFullDataBlock();\n\t\treturn this.getDataBlock(dataBlock, index);\n\t}\n\tget(index: number, dataIndex = 0): T[number] {\n\t\tif(dataIndex >= this.dataLength) {\n\t\t\tthrow new Error(`${dataIndex} is out of dataLength bounds ${this.dataLength}`);\n\t\t} else if(index >= this.length || index < 0) {\n\t\t\tthrow new Error(`${index} is out of bounds ${this.length}`);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock() as NumericArrayIO;\n\t\treturn dataBlock[index * this.dataLength + dataIndex];\n\t}\n\n\tpush(values: T[number] | Array<T[number]>): number {\n\t\tlet dataLength = this.dataLength;\n\t\tconst isSingleValue = typeof values !== 'object';\n\t\tif(!isSingleValue && values.length > dataLength) {\n\t\t\tthrow new Error(`Can't insert ${values.length} array into shared list of ${dataLength} dataLength`);\n\t\t}\n\n\t\tlet dataBlock = this.getFullDataBlock() as NumericArrayIO;\n\t\tlet currentLength = this.length;\n\t\tlet startIndex = dataLength * currentLength;\n\t\tif(isSingleValue) {\n\t\t\tdataBlock[startIndex] = values;\n\t\t} else {\n\t\t\tfor(let i = 0; i < values.length; i++) {\n\t\t\t\tdataBlock[startIndex + i] = values[i];\n\t\t\t}\n\t\t}\n\n\t\tlet newLength = Atomics.add(this.firstBlock.data, LENGTH_INDEX, 1) + 1;\n\t\tif(newLength >= this.bufferLength) {\n\t\t\tthis.growBuffer();\n\t\t}\n\n\t\treturn currentLength;\n\t}\n\n\tpop(): T {\n\t\tlet oldLength = Atomics.sub(this.firstBlock.data, LENGTH_INDEX, 1);\n\n\t\tlet dataBlock = this.getFullDataBlock();\n\t\treturn this.getDataBlock(dataBlock, oldLength - 1);\n\t}\n\n\t// Returns the first number irregardless of dataLength - faster way to pop if you don't care about the rest of the data block\n\tpopNumber(): T[number] {\n\t\tconst oldLength = Atomics.sub(this.firstBlock.data, LENGTH_INDEX, 1);\n\t\tconst dataBlock = this.getFullDataBlock() as NumericArrayIO;\n\t\treturn dataBlock[(oldLength - 1) * this.dataLength];\n\t}\n\n\tdeleteIndex(index: 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 dataLength = this.dataLength;\n\t\tlet dataBlock = this.getFullDataBlock() as NumericArrayIO;\n\t\tfor(let i = index; i < length; i++) {\n\t\t\tfor(let j = 0; j < dataLength; j++) {\n\t\t\t\tdataBlock[i * dataLength + j] = dataBlock[(i + 1) * dataLength + j];\n\t\t\t}\n\t\t}\n\n\t\tAtomics.sub(this.firstBlock.data, LENGTH_INDEX, 1);\n\t}\n\n\tclear() {\n\t\tthis.firstBlock.data[LENGTH_INDEX] = 0;\n\t}\n\n\t*[Symbol.iterator]() {\n\t\tlet dataBlock = this.getFullDataBlock();\n\t\tlet dataLength = this.dataLength;\n\t\tlet totalDataEntries = this.length * dataLength;\n\n\t\tfor(let i = 0; i < totalDataEntries; i += dataLength) {\n\t\t\tyield dataBlock.subarray(i, i + dataLength) as T;\n\t\t}\n\t}\n\n\t// Significantly faster but won't be thread safe if anything is removing/adding to it during iteration\n\tgetCurrentArray(): T {\n\t\tlet dataBlock = this.getFullDataBlock();\n\t\treturn dataBlock.subarray(0, this.length * this.dataLength) as T;\n\t}\n\n\tprivate getFullDataBlock(): T {\n\t\tlet pointerNumber = Atomics.load(this.firstBlock.data, VECTOR_INDEX);\n\t\tif(this.cachedPointer === pointerNumber && this.cachedFullDataBlock) {\n\t\t\treturn this.cachedFullDataBlock;\n\t\t}\n\n\t\tlet pointer = getPointer(pointerNumber, this.memory.positionBits);\n\t\tlet rawData = new AllocatedMemory(this.memory, pointer);\n\n\t\tlet data = makeArrayView(this.type, rawData.data.buffer, rawData.bufferByteOffset, this.dataLength * this.bufferLength) as T;\n\n\t\tthis.cachedPointer = pointerNumber;\n\t\tthis.cachedFullDataBlock = data;\n\n\t\treturn data;\n\t}\n\tprivate getDataBlock(rawData: T, index: number): T {\n\t\tconst start = index * this.dataLength;\n\t\treturn rawData.subarray(start, start + this.dataLength) as T;\n\t}\n\n\tprivate growBuffer() {\n\t\tlet oldBufferLength = this.bufferLength;\n\t\tlet newBufferLength = oldBufferLength * 2;\n\t\tlet dataLength = this.dataLength;\n\n\t\tlet oldPointer = loadPointer(this.firstBlock.data, VECTOR_INDEX, this.memory.positionBits);\n\t\tlet oldDataMemory = new AllocatedMemory(this.memory, oldPointer);\n\t\tlet oldDataBlock = this.getFullDataBlock();\n\t\tlet newDataBlock = this.memory.allocUI32(newBufferLength * dataLength * this.cachedByteMultipler);\n\n\t\tlet newData = makeArrayView(this.type, newDataBlock.data.buffer, newDataBlock.bufferByteOffset, dataLength * newBufferLength) as T;\n\n\t\t// newData and oldDataBlock are the same concrete view type, so a direct copy is valid; the cast only bridges the\n\t\t// union's mutually-incompatible set() overloads (number vs bigint element types)\n\t\t(newData as Float64Array).set(oldDataBlock as unknown as Float64Array);\n\n\t\tconst newPointer = newDataBlock.pointer;\n\t\tstoreRawPointer(this.firstBlock.data, VECTOR_INDEX, newPointer);\n\t\tthis.bufferLength = newBufferLength;\n\t\toldDataMemory.free();\n\n\t\tthis.cachedPointer = newPointer;\n\t\tthis.cachedFullDataBlock = newData;\n\t}\n\n\tfree() {\n\t\tlet pointer = loadPointer(this.firstBlock.data, VECTOR_INDEX, this.memory.positionBits);\n\t\tlet dataMemory = new AllocatedMemory(this.memory, pointer);\n\n\t\tdataMemory.free();\n\t\tthis.firstBlock.free();\n\t}\n\n\tgetSharedMemory(): SharedVectorMemory {\n\t\treturn {\n\t\t\tfirstBlock: this.firstBlock.getSharedMemory(),\n\t\t};\n\t}\n}\n\ninterface SharedVectorConfig<T extends NumericArray> {\n\ttype?: TypedArrayConstructor<T>\n\tdataLength?: number\n\n\tbufferLength?: number\n}\ninterface SharedVectorMemory {\n\tfirstBlock: SharedAllocatedMemory\n}\n\nexport type { SharedVectorConfig, SharedVectorMemory };"],"mappings":";;;;AAMA,IAAM,IAAe,GACf,IAAe,GACf,IAAsB,GACtB,IAAa,GACb,IAAe,GACA,IAArB,MAAqB,EAA0E;CAC9F,OAAgB,iBAAiB;CACjC;CAGA;CACA;CAGA,sBAA8B;CAE9B,IAAI,SAAiB;EACpB,OAAO,QAAQ,KAAK,KAAK,WAAW,MAAM,CAAY;CACvD;CAEA,IAAI,OAAe;EAClB,OAAO,KAAK,YAAY;CACzB;CACA,IAAY,KAAK,GAAe;EAC/B,QAAQ,MAAM,KAAK,aAAa,GAAG,CAAK;CACzC;CACA,IAAI,gBAAwB;EAC3B,OAAO,KAAK;CACb;CACA,IAAI,aAAqB;EAExB,OAAO,KAAK,YAAY,MAAM;CAC/B;CACA,IAAY,WAAW,GAAe;EACrC,QAAQ,MAAM,KAAK,aAAa,GAAG,CAAK;CACzC;CAEA,IAAI,eAAuB;EAC1B,OAAO,QAAQ,KAAK,KAAK,WAAW,MAAM,CAAmB;CAC9D;CACA,IAAY,aAAa,GAAe;EACvC,QAAQ,MAAM,KAAK,WAAW,MAAM,GAAqB,CAAK;CAC/D;CAEA,IAAI,UAAU;EACb,OAAO,KAAK,WAAW;CACxB;CAEA,IAAI,aAAqB;EACxB,IAAI,IAAa,IAAI,EAAgB,KAAK,QAAQ,EAAY,KAAK,WAAW,MAAM,GAAc,KAAK,OAAO,YAAY,CAAC;EAC3H,OAAO,KAAK,WAAW,aAAa,EAAW;CAChD;CAEA;CACA;CAEA,YAAY,GAAoB,GAAkG;EAGjI,IAFA,KAAK,SAAS,GAEX,KAAU,gBAAgB,GAQ5B;OAPA,KAAK,aAAa,I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