Skip to content

Frame packets without throwing on partial reads - #1552

Open
Pix3lPirat3 wants to merge 1 commit into
PrismarineJS:masterfrom
Pix3lPirat3:perf/splitter-no-throw
Open

Pix3lPirat3 wants to merge 1 commit into
PrismarineJS:masterfrom
Pix3lPirat3:perf/splitter-no-throw

Conversation

@Pix3lPirat3

Copy link
Copy Markdown
Contributor

The Splitter reads each packet-length varint with protodef's readVarInt, which throws a PartialReadError when the buffer doesn't yet hold a complete varint. On a TCP stream that happens on nearly every segment (a packet split across reads), and PartialReadError's constructor captures a stack trace each time.

Profiling a chunk-streaming session, that error construction was a top CPU cost: ExtendableError/PartialReadError was the #1 non-GC function (~7-8%), and all of it originated from readVarInt inside Splitter._transform (never the deserializer, which only ever receives complete frames). Measured throw+catch cost ~9.8us each, almost entirely the stack capture.

This decodes the length varint inline and returns null when more bytes are needed, so the hot framing loop no longer throws. Framing is byte-for-byte identical to before (verified against the old splitter across whole-stream, one-byte-at-a-time, fixed and random chunkings, incl. boundaries landing mid-length-varint); added test/splitterTest.js covering those. With the change, PartialReadError disappears from the profile and protodef's CPU share on a chunk-load session dropped from ~11% to ~4%. The packet deserializer still uses protodef's throwing reads as before.

The splitter read each packet-length varint with protodef's readVarInt, which throws a PartialReadError when the buffer does not yet hold a complete varint. That happens on nearly every TCP segment (a packet split across reads), and the error's constructor captures a stack trace each time, which profiled as a top CPU cost under load (a chunk-streaming session spent ~8% there). Decode the length inline instead, returning null when more bytes are needed. Framing is byte-identical (added a splitter test covering byte-at-a-time and mid-varint boundaries); the deserializer still uses protodef normally.

@rom1504 rom1504 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Astra agent review — AI-generated, not manually written by the maintainer.

Reviewed 49a5d57 using the review skills prismarine-protocol-data-review (stream fragmentation and framing semantics), prismarine-architecture-review (the allocation/error-path change), and prismarine-review (test discovery and existing feedback).

The four submitted fragmentation tests pass locally, and I found no blocker in their valid-frame behavior. There is one validation repair before calling this ready: none of these tests is selected by the current CI version filter, as detailed inline. Running the same file with -g 26.1v executes zero tests. I did not independently reproduce the performance benchmark.

Comment thread test/splitterTest.js

// The splitter reassembles length-prefixed packets from an arbitrarily chunked TCP stream. It must
// frame identically no matter how the bytes are delivered, including when a chunk boundary falls in
// the middle of a packet's length varint (the case the splitter must wait on rather than mis-frame).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Astra agent review — AI-generated, not manually written by the maintainer.

[P2] Include the version-independent splitter suite in CI

.github/workflows/ci.yml runs every test job as npm run mochaTest -- -g ${{ matrix.mcVersion }}v; the other job only runs lint. This suite's title has no version suffix, so its four new framing cases never run in any CI job. I confirmed 4 passing when executing this file normally and 0 passing with -g 26.1v.

Add an explicit unfiltered invocation of test/splitterTest.js to a required job (or a small version-independent test job), keeping the existing version matrix. These are appropriate focused tests of the real stream transform; they just need to be part of the maintained gate.

Skills used: prismarine-review and its repository testing standards caught the runner/filter mismatch; prismarine-protocol-data-review checked that the assertions exercise actual framing across chunk boundaries.

@Pix3lPirat3

Copy link
Copy Markdown
Contributor Author

More numbers for this, from a load with many bots at once. A bot service ran N mineflayer bots on
one vanilla 26.3 server (ViaVersion; mineflayer 4.39.0, minecraft-protocol 1.68.0, protodef 1.19.0,
Node 24.15.0, Windows 11), each bot in a worker thread of its own, each at work: quarrying,
farming, chopping, walking 150-block lanes, carrying stacks between two chests, fighting husks.
A 20 s CPU profile of one bot thread of each kind, after 90 s of work:

bots on the server ExtendableError (PartialReadError), share of the thread's CPU protodef in all
8 16 to 27% (3% for the farmer, whose own work is heavier) 20 to 29%
16 26 to 31% (11%) 29 to 34%
24 29 to 33% (10%) 31 to 37%

Every sample of it comes from the splitter:

26.8%  PartialReadError utils.js:69 < readVarInt datatypes/varint.js:13 < _transform transforms/framing.js:34 < Transform._write

It grows with the number of players, since each bot gets more small packets (other players'
moves) and the splitter throws once per TCP read that ends on a packet boundary. For a bot that
is not computing a path, the stack capture of that error is the largest single cost it has.

With this PR applied as a patch, the same load with 64 bots: PartialReadError is gone from every
bot thread's profile, and the whole process went from 7.3 to 6.4 cores (all 64 bots at 20 ticks a
second both times).

With all bots in one thread the share is smaller (2.2% at 48 bots): a busy thread reads larger
pieces at a time, so it throws less often per packet.

@Pix3lPirat3
Pix3lPirat3 force-pushed the perf/splitter-no-throw branch from 49a5d57 to 07cd1d1 Compare November 25, 2026 05:38

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants