From e2541a8ece0ede5d4038e7f367a1cdfb45edf715 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 10 Oct 2026 01:14:33 -0700 Subject: [PATCH] Refactor array capture to set elements directly. --- src/lib/primordials.ts | 16 ++++++++++------ src/lib/verify.ts | 6 +++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/lib/primordials.ts b/src/lib/primordials.ts index 37b8da1..32737f7 100644 --- a/src/lib/primordials.ts +++ b/src/lib/primordials.ts @@ -38,19 +38,18 @@ * bundler will order that dependency first, and any call it makes into here * during its own evaluation will read captures that are still unassigned. */ +const ObjectCreate = Object.create +const ObjectDefineProperty = Object.defineProperty /** Type aliases for legibility. */ -export type List = Array export type Bytes = Uint8Array export type Words = Uint32Array /** Constructors bound before anything can replace the globals. */ -const List = Array const Bytes = Uint8Array const Words = Uint32Array const TypedArray = Object.getPrototypeOf(Bytes.prototype) -const arrayFill = Function.prototype.call.bind(List.prototype.fill) const bytesSet = Function.prototype.call.bind(Bytes.prototype.set) const bytesFill = Function.prototype.call.bind(Bytes.prototype.fill) @@ -129,9 +128,14 @@ export function isBytes (a: unknown): a is Bytes { /** Checks system endianness. Returns true if little-endian, else false. */ export const isLE = new Bytes(getBuffer(new Words([1])))[0] === 1 -/** Creates a non-sparse array of the specified length */ -export function list (length: number): List { - return arrayFill(new List(length), undefined) +/** Sets `array[index] = value`. */ +export function slot (array: T[], index: number, value: T): void { + const descriptor = ObjectCreate(null) + descriptor.value = value + descriptor.writable = true + descriptor.enumerable = true + descriptor.configurable = true + ObjectDefineProperty(array, index, descriptor) } /** diff --git a/src/lib/verify.ts b/src/lib/verify.ts index ca513be..f7e89a3 100644 --- a/src/lib/verify.ts +++ b/src/lib/verify.ts @@ -1,7 +1,7 @@ //! SPDX-FileCopyrightText: 2026 Chris Duncan //! SPDX-License-Identifier: GPL-3.0-or-later -import { allocate, byteLength, copy, isArray, list, normalize, } from './primordials' +import { allocate, byteLength, copy, isArray, normalize, slot } from './primordials' import { Mutex, constants, getOutput, setInput, verify as wasm_verify, verify_blocks as wasm_verify_blocks, } from './wasm' const { BLOCKHASH_BYTELENGTH, KEY_BYTELENGTH, MAX_MESSAGE_BYTELENGTH, MAX_VERIFY_BLOCKS, SIGNATURE_BYTELENGTH, } = constants @@ -32,7 +32,7 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] { if (MAX_VERIFY_BLOCKS < count) { throw new RangeError(`Bulk Nano block verification must be no more than ${MAX_VERIFY_BLOCKS} blocks`, { cause: count }) } - const verified: boolean[] = list(count) + const verified: boolean[] = [] Mutex.lock() try { const publicKey = normalize('public key', KEY_BYTELENGTH, KEY_BYTELENGTH, pub) @@ -58,7 +58,7 @@ export function verify_blocks (pub: unknown, data: unknown): boolean[] { setInput('msg', message) wasm_verify_blocks(count) for (let i = 0; i < count; i++) { - verified[i] = getOutput('verify', i) === 1 + slot(verified, i, getOutput('verify', i) === 1) } return verified } finally { -- 2.52.0