From a99a00c8f4764cdde7e2bf8dde07e0b50941163f Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 21 Aug 2026 21:21:02 -0400 Subject: [PATCH] refactor: allocate attached opsBuf eagerly, drop definite-assignment guard Review follow-up: 'let opsBuf!: number' plus a runtime '=== undefined' check sent conflicting signals. Allocating the fixed-size attached buffer once at declaration makes opsBuf always a number and removes the guard from layout(). --- term-native.ts | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/term-native.ts b/term-native.ts index d81097d..6f2eafa 100644 --- a/term-native.ts +++ b/term-native.ts @@ -141,7 +141,9 @@ export async function createTermNative( CLAY_DEFAULT_MAX_ELEMENT_COUNT * MAX_FIXED_ELEMENT_WIRE_BYTES; let statePtr!: number; - let opsBuf!: number; + // opsBuf is fixed-size: attached mode allocates it exactly once here; + // standalone mode recomputes it in layout() as the state region resizes. + let opsBuf = attach ? attach.alloc(transferBytes, 4) : 0; let arena: number | undefined; let arenaCapacity = 0; @@ -150,15 +152,11 @@ export async function createTermNative( if (attach) { // Bump-allocated memory is never reclaimed: reuse the arena when // it still fits, otherwise allocate a larger one and abandon the - // old (growth-only, renderer-spec 7.7). opsBuf is fixed-size and - // allocated once. + // old (growth-only, renderer-spec 7.7). if (arena === undefined || size > arenaCapacity) { arena = attach.alloc(size); arenaCapacity = size; } - if (opsBuf === undefined) { - opsBuf = attach.alloc(transferBytes, 4); - } statePtr = ct.init(arena, lw, lh, attach.structPtr); } else { // Standalone layout is [heap: state][opsBuf]; opsBuf moves when -- 2.51.2