From 94cc0931cae1622fe4b6835e520aa31652b0b8ab Mon Sep 17 00:00:00 2001 From: Pix3lPirat3 Date: Tue, 3 Nov 2026 00:06:54 -0800 Subject: [PATCH 1/2] Serialize without a separate sizeOf pass createPacketBuffer walked the whole structure once for sizeOf and again for write (re-encoding every string length). Write once into a reusable scratch buffer, use the returned end offset, and copy out the exact bytes. If the packet overflows the scratch the exact sizeOf+alloc path is used and the scratch grows. Bench: ~20-24% faster serialize on real-sized packets (3.5KB and 73KB); full test suite (503) green. --- src/compiler.js | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/compiler.js b/src/compiler.js index 9c8f057..6f64440 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -93,18 +93,36 @@ class CompiledProtodef { } createPacketBuffer (type, packet) { - const length = tryCatch(() => this.sizeOf(packet, type), - (e) => { - e.message = `SizeOf error for ${e.field} : ${e.message}` - throw e - }) - const buffer = Buffer.allocUnsafe(length) - tryCatch(() => this.write(packet, buffer, 0, type), - (e) => { + // Fast path: write straight into a reusable scratch buffer and copy out the exact bytes, avoiding + // the separate sizeOf traversal (which re-walks the whole structure and re-encodes string lengths). + // Falls back to the exact sizeOf+alloc path if the packet overflows the scratch (then grows it). + let scratch = this._scratch + if (scratch === undefined) scratch = this._scratch = Buffer.allocUnsafe(65536) + try { + const end = this.write(packet, scratch, 0, type) + const buffer = Buffer.allocUnsafe(end) + scratch.copy(buffer, 0, 0, end) + return buffer + } catch (e) { + if (e.code !== 'ERR_BUFFER_OUT_OF_BOUNDS' && e.code !== 'ERR_OUT_OF_RANGE') { e.message = `Write error for ${e.field} : ${e.message}` throw e - }) - return buffer + } + // Scratch too small (or a value that didn't fit): take the exact path and grow the scratch. + const length = tryCatch(() => this.sizeOf(packet, type), + (er) => { + er.message = `SizeOf error for ${er.field} : ${er.message}` + throw er + }) + const buffer = Buffer.allocUnsafe(length) + tryCatch(() => this.write(packet, buffer, 0, type), + (er) => { + er.message = `Write error for ${er.field} : ${er.message}` + throw er + }) + if (length > this._scratch.length) this._scratch = Buffer.allocUnsafe(length) + return buffer + } } parsePacketBuffer (type, buffer, offset = 0) { From 63719f2c803a2017dd641f5b80efca279d0456a1 Mon Sep 17 00:00:00 2001 From: Pix3lPirat3 Date: Tue, 3 Nov 2026 01:08:54 -0800 Subject: [PATCH 2/2] Test large packets that overflow the write scratch buffer Covers P2's grow-on-overflow fallback: a packet beyond the 64 KiB scratch must round-trip (fallback path), the scratch must then grow so a second large packet does not re-trigger it, and small packets must still work. --- test/misc.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/misc.js b/test/misc.js index 3ae1325..67380ec 100644 --- a/test/misc.js +++ b/test/misc.js @@ -77,6 +77,31 @@ describe('bitflags with a signed underlying type', () => { } }) +describe('large packets exceeding the write scratch buffer', () => { + // createPacketBuffer writes into a reused 64 KiB scratch and only falls back to a sizeOf pass when that + // overflows. A packet larger than the scratch must still serialize correctly (fallback path), the scratch + // must then grow so a second large packet does not re-trigger the fallback, and small packets must keep working. + const type = ['array', { type: 'i32', countType: 'varint' }] + const compiler = new ProtoDefCompiler() + compiler.addTypesToCompile({ ints: type }) + const compiled = compiler.compileProtoDefSync() + + const big = Array.from({ length: 20000 }, (_, i) => i - 10000) // 20000 * 4 B = 80 KB > 64 KiB scratch + const bigger = Array.from({ length: 40000 }, (_, i) => i) // 160 KB, must also round-trip + + it('round-trips a packet larger than the scratch (fallback path)', () => { + const buf = compiled.createPacketBuffer('ints', big) + assert.strictEqual(buf.length, 3 + big.length * 4) // 3-byte varint count + payload + assert.deepStrictEqual(compiled.parsePacketBuffer('ints', buf).data, big) + }) + + it('round-trips a second, larger packet and a small one after growing the scratch', () => { + assert.deepStrictEqual(compiled.parsePacketBuffer('ints', compiled.createPacketBuffer('ints', bigger)).data, bigger) + const small = [1, 2, 3] + assert.deepStrictEqual(compiled.parsePacketBuffer('ints', compiled.createPacketBuffer('ints', small)).data, small) + }) +}) + describe('FullPacketParser', () => { const packet = ['container', [{ name: 'a', type: 'i32' }]] const proto = new ProtoDef()