Repository navigation
Serialize without a separate sizeOf pass - #184
Pix3lPirat3 wants to merge 2 commits into
Conversation
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.
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.
rom1504
left a comment
There was a problem hiding this comment.
Astra agent review — AI-generated, not manually written by the maintainer.
Reviewed 63719f2 using the review skills prismarine-protocol-data-review (compiled write/read/size parity), prismarine-architecture-review (generic codec contracts and buffer ownership), and prismarine-review (existing discussion and test coverage).
This needs the overflow repair below before merging. The optimization is useful, but not every supported writer signals insufficient space by throwing. I reproduced corruption with an 80,000-byte buffer field; the exact-size allocation control writes it correctly. The candidate's 14 test/misc.js tests pass, including its integer-array overflow tests, which miss this non-throwing writer case. I have not validated the performance claim or run a downstream Minecraft integration matrix.
| if (scratch === undefined) scratch = this._scratch = Buffer.allocUnsafe(65536) | ||
| try { | ||
| const end = this.write(packet, scratch, 0, type) | ||
| const buffer = Buffer.allocUnsafe(end) |
There was a problem hiding this comment.
Astra agent review — AI-generated, not manually written by the maintainer.
[P1] Detect writers that overrun the scratch without throwing
The compiled buffer writer uses value.copy(buffer, offset) and returns offset + value.length; Buffer.copy silently truncates when the destination is full. With payload: ['buffer', { countType: 'varint' }] and Buffer.alloc(80000, 0x5a), this call returns end = 80003 without entering the catch. The next allocation is 80,003 bytes, but only 65,536 bytes are copied from the scratch, leaving the suffix uninitialized. I executed the candidate: the round-trip first differs at payload byte 65,533, while restoring the exact-size allocation/write path preserves every byte. Large payloads can therefore be corrupted and include unrelated memory in returned packets.
Check the returned cursor against the scratch capacity before accepting the fast path and fall back/rewrite when it exceeds it. Cover a large buffer field (and other non-throwing writes, such as indexed bitfield writes), not just integer arrays whose Buffer methods throw.
Skills used: prismarine-protocol-data-review exposed the write/size mismatch; prismarine-architecture-review checked the shared codec's native-writer contract; prismarine-review checked the regression's actual coverage.
Write into a reused scratch buffer and use
write()'s return cursor to copy out the exact bytes, instead of doing a fullsizeOftraversal before every write. Falls back to the old sizeOf+alloc path only when the scratch overflows, then grows it so later large packets skip the fallback. This removes the double traversal (strings were being encoded twice) on the compiled path that node-minecraft-protocol uses by default.Measured ~20-25% faster serialize on non-tiny packets (3.5KB ~24%, 73KB ~20%), byte-identical round-trip; tiny packets unchanged within noise. Added a test for the overflow fallback (>64KB packet) and scratch growth. Full suite green (510).