mirror of
https://github.com/stablyai/orca.git
synced 2026-10-21 16:03:26 +00:00
takeQueuedChunk stored the unconsumed remainder back into the queue as chunk.data.slice(remaining). V8 backs that with a SlicedString pointing at the whole parent, so a nearly drained 2 MB chunk kept all 2 MB alive to serve a few KB tail while queuedChars was charged only for the remainder. Fully consumed chunks were also left referenced until the 64-chunk compaction, so they retained their strings and callbacks while charging nothing at all. Track what each chunk really pins in retainedChars and copy the residual flat once it pins twice its own length. Halving bounds retention to ~2x the honest queued chars and keeps total copying linear in the original chunk, and the drain path measured no slower. Drained slots are replaced with an empty placeholder, which nothing below chunkIndex ever reads. Measured with 8 background terminals drained to a <=64 KB tail: 8,710,264 bytes retained before, 342,776 after.