From 1242d08118f304b48d048baf5c073a9021a932bf Mon Sep 17 00:00:00 2001 From: ightevenmckane187 <214069160+ightevenmckane187@users.noreply.github.com> Date: Sun, 23 Aug 2026 15:13:14 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20optimize=20PersistenceLayer?= =?UTF-8?q?.save=20buffer=20allocations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed syntax/reference errors in `PersistenceLayer.save` and optimized serialization performance. Pre-allocates a single output buffer using `Buffer.allocUnsafe`, writes string payload directly into destination buffer via `out.write()`, and calculates HMAC signature directly from string payload to avoid intermediate payload Buffer allocations and double buffer copying. --- src/os/persistence/PersistenceLayer.ts | 28 ++++++++++---------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/os/persistence/PersistenceLayer.ts b/src/os/persistence/PersistenceLayer.ts index b60a349..4108039 100644 --- a/src/os/persistence/PersistenceLayer.ts +++ b/src/os/persistence/PersistenceLayer.ts @@ -13,31 +13,25 @@ export class PersistenceLayer { */ save(data: any, key: string): Buffer { const payload = JSON.stringify(data); - const payloadBuf = Buffer.from(payload, 'utf8'); - - // Bolt Optimization: Allocate unsafe buffer for exact combined size to avoid Buffer.concat and intermediate payload allocations - const outBuf = Buffer.allocUnsafe(38 + payloadBuf.length); - - // Copy pre-allocated header and payload - outBuf.set(HEADER_BUF, 0); - outBuf.set(payloadBuf, 38); - - // Compute and write HMAC signature directly - const hmac = crypto.createHmac('sha256',. key); - hmac.update(payloadBuf); - const signature = hmac.digest(); - outBuf.set(signature, 6); - const payloadByteLength = Buffer.byteLength(payload, 'utf8'); + + // Bolt Optimization: Allocate pre-sized buffer to eliminate Buffer.concat and intermediate allocations const out = Buffer.allocUnsafe(HEADER_LENGTH + SIGNATURE_LENGTH + payloadByteLength); // Zero-copy set of pre-allocated header out.set(HEADER_MAGIC, 0); - // Zero-copy set of hmac signature - out.set(signature, HEADER_LENGTH); + // Direct UTF-8 write of the payload to avoid intermediate Buffer allocation out.write(payload, HEADER_LENGTH + SIGNATURE_LENGTH, payloadByteLength, 'utf8'); + // Compute HMAC signature directly from string payload without intermediate buffer + const hmac = crypto.createHmac('sha256', key); + hmac.update(payload); + const signature = hmac.digest(); + + // Zero-copy set of hmac signature + out.set(signature, HEADER_LENGTH); + return out; }