JavaScript · Arenas

Arenas

import { Arena } from 'uform';import { ArenaView } from 'uform'; 4.0.0-rc.1 · 3ee5e534

Every output whose length scales with the call lands in an arena, and the verb answers views into it rather than copies. Each call rewinds the arena it is given, detaching the buffers under the previous call’s views, so a stale view refuses its reads with invalid-argument instead of reading another call’s memory. To keep one result past the next call, give that call another arena. A deferred call holds its arena until final, and a second call naming it finishes contended.

const first = model.textEncode(["first"], arena);
const second = model.textEncode(["second"], arena);
first.embeddings.data; // throws: the arena was rewound

API

ClassArenaMemory the library writes every output whose length scales with a call into, reused across calls.4
types.ts:361 ↗
class Arena implements Disposable

Every verb with such an output takes one and answers a view into it rather than a copy, so a steady loop allocates once. Each call rewinds the arena it is handed and detaches every buffer over the previous call's outputs: a view refuses its reads with Status.InvalidArgument from then on. To keep one result past the next call, give that call another arena. A deferred call holds it until final: another call naming it before then finishes Status.Contended and leaves the first call's outputs alone, and free waits for it.

ArenaMakes an arena where executor computes, or host pages for calls on the calling thread.
types.ts:367 ↗
new Arena(executor?: Executor)

Calls uform_arena_init.

freeReleases the memory now, once every deferred call writing it is final, detaching every view into it.
types.ts:379 ↗
free(): void

Calls uform_arena_free.

compiledThe backend handle every verb is handed.
types.ts:363 ↗
readonly compiled: CompiledArena
residency"ram" or "vram", where the executor that made it computes; a VRAM arena's views refuse host reads.
types.ts:373 ↗
readonly residency: string
ClassArenaViewOutputs a call wrote into an arena, readable until the arena's next call rewinds it.
types.ts:395 ↗
abstract class ArenaView

The rewind detaches the buffer under every array, so each accessor refuses once the arena moved on, and refuses device memory with Status.Device, so a view never reads memory its call did not write.