SharedMemory unstable
Typed arrays in shared memory, for passing data to workers without copying.
Use make to allocate shared arrays and schema for worker RPC payloads. Unlike transferred buffers, shared buffers remain usable by the sender and multiple workers.
With RPC, provide RpcClient.layerProtocolWorker on the client and
RpcServer.layerProtocolWorkerRunner on the server, with platform worker layers.
Gotchas
- Shared memory works only between threads of one process, such as worker threads in Node, Bun and Deno, and web workers. A child process receives a copy, so the schema rejects it.
- In browsers,
SharedArrayBufferneeds a cross-origin isolated page, served withCross-Origin-Opener-Policy: same-originandCross-Origin-Embedder-Policy: require-corp. Check isAvailable. - Threads that write to the same memory race. Let each worker write its own
range, or use
Atomics. - A field declared as
Schema.Uint8Arrayis encoded as base64 by worker codecs, so the worker gets a private copy and its writes are lost. Use schema for shared fields.
Example Workers that write into one shared array
import { Effect, Schema } from "effect"import { Rpc, RpcClient, RpcGroup } from "effect/rpc"import { SharedMemory } from "effect/workers"
// shared.tsexport const Fill = Rpc.make("Fill", { payload: { data: SharedMemory.schema(Int32Array), from: Schema.Number, to: Schema.Number }})export const Compute = RpcGroup.make(Fill)
// worker.tsexport const ComputeLive = Compute.toLayer({ Fill: ({ data, from, to }) => Effect.sync(() => { for (let i = from; i < to; i++) data[i] = i * 2 })})
// main.tsexport const fillOnWorkers = Effect.gen(function*() { const data = SharedMemory.make(Int32Array, 1_000_000) const client = yield* RpcClient.make(Compute) yield* Effect.forEach( [[0, 500_000], [500_000, 1_000_000]], ([from, to]) => client.Fill({ data, from, to }), { concurrency: "unbounded" } ) return data})Constructors
Allocates a zero-filled typed array of length elements in shared memory.
Gotchas
Throws SharedMemoryError when shared memory is unavailable.
Signature
declare function make<C extends TypedArrayConstructor>(constructor: C, length: number): Shared<InstanceType<C>>Example Copying existing values into shared memory
import { SharedMemory } from "effect/workers"
const values = Int32Array.of(1, 2, 3)const shared = SharedMemory.make(Int32Array, values.length)shared.set(values)values[0] = 9shared[0] // => 1Errors
Guards
isAvailable
unstableReturns whether shared memory is available in this environment.
Details
True in Node, Bun and Deno. In browsers, true only on a cross-origin
isolated page (crossOriginIsolated).
Signature
declare function isAvailable(): booleanModels
The typed arrays that can live in shared memory.
Signature
type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64ArrayThe constructors of TypedArrays.
Signature
type TypedArrayConstructor = Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | BigInt64ArrayConstructor | BigUint64ArrayConstructorSchemas
Creates a worker-message schema for a typed array backed by shared memory.
Details
- Decoding and encoding accept only instances of
constructorbacked by aSharedArrayBuffer. Serialized copies fail validation. Schema.toCodecJsonpasses the array through unchanged forpostMessage.
Signature
declare function schema<C extends TypedArrayConstructor>(constructor: C): declare<Shared<InstanceType<C>>>