Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

at e46aca05dbf9a2204886d23f7ae3b34883f4e5c6 28 lines 875 B view raw
1interface Slabbable<T> { 2 readonly byteSize: number; 3 readonly byteAlignment: number; 4 new (buffer: SharedArrayBuffer, byteOffset: number): T; 5} 6 7type SlabInstances<T extends Slabbable<any>[]> = { 8 [K in keyof T]: T[K] extends Slabbable<infer I> ? I : never; 9}; 10 11function align(offset: number, alignment: number): number { 12 const remainder = offset % alignment; 13 return remainder === 0 ? offset : offset + (alignment - remainder); 14} 15 16export function slab<T extends Slabbable<any>[]>(...classes: T): SlabInstances<T> { 17 const offsets: number[] = []; 18 let cursor = 0; 19 for (const cls of classes) { 20 cursor = align(cursor, cls.byteAlignment); 21 offsets.push(cursor); 22 cursor += cls.byteSize; 23 } 24 25 const buffer = new SharedArrayBuffer(cursor); 26 const instances = classes.map((cls, i) => new cls(buffer, offsets[i])); 27 return instances as SlabInstances<T>; 28}