Skip to content
// EFFECT DAYS 2026

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, SharedArrayBuffer needs a cross-origin isolated page, served with Cross-Origin-Opener-Policy: same-origin and Cross-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.Uint8Array is 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.ts
export const Fill = Rpc.make("Fill", {
payload: {
data: SharedMemory.schema(Int32Array),
from: Schema.Number,
to: Schema.Number
}
})
export const Compute = RpcGroup.make(Fill)
// worker.ts
export const ComputeLive = Compute.toLayer({
Fill: ({ data, from, to }) =>
Effect.sync(() => {
for (let i = from; i < to; i++) data[i] = i * 2
})
})
// main.ts
export 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
})
8 exports Since v4.1.5 Source

Constructors

make

unstable
Since v4.1.5

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] = 9
shared[0] // => 1

Errors

Since v4.1.5

Thrown by make when shared memory is unavailable.

See

Signature

declare class SharedMemoryError extends YieldableError<this> & {
readonly _tag: "SharedMemoryError";
} & Readonly<{
readonly message: string;
}> {
constructor(args: {
readonly message: string;
});
}

Guards

isAvailable

unstable
Since v4.1.5

Returns 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(): boolean

isShared

unstable
Since v4.1.5

Returns whether the view's memory is a SharedArrayBuffer.

Signature

declare function isShared(view: ArrayBufferView): boolean

Models

Shared

type unstable
Since v4.1.5

The kind of typed array A, backed by a SharedArrayBuffer.

Signature

type Shared<A extends TypedArray> = SharedByName[A[typeof Symbol.toStringTag]]

TypedArray

type unstable
Since v4.1.5

The typed arrays that can live in shared memory.

Signature

type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array
Since v4.1.5

The constructors of TypedArrays.

Signature

type TypedArrayConstructor = Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | BigInt64ArrayConstructor | BigUint64ArrayConstructor

Schemas

schema

unstable
Since v4.1.5

Creates a worker-message schema for a typed array backed by shared memory.

Details

  • Decoding and encoding accept only instances of constructor backed by a SharedArrayBuffer. Serialized copies fail validation.
  • Schema.toCodecJson passes the array through unchanged for postMessage.

Signature

declare function schema<C extends TypedArrayConstructor>(constructor: C): declare<Shared<InstanceType<C>>>