Repository navigation
perf(lexer): perform part of reads strictly - #38
Open
AlexaDeWit wants to merge 1 commit into
Open
AlexaDeWit wants to merge 1 commit into
AlexaDeWit wants to merge 1 commit into
Conversation
parseResults now reads each result record in one withForeignPtr call instead of four lazy peeks. Nothing writes the buffer after the lexer returns, so eager reads are safe, and the laziness only cost an allocation per field while keeping the buffer alive. handle_string keeps its position and escape flag in locals rather than writing through the lexer struct on every byte. This also stops it reading one byte past the input.
AlexaDeWit
added a commit
to AlexaDeWit/Ecluse
that referenced
this pull request
Sep 28, 2026
…ackument walkers (#1541) ## What npm and PyPI metadata reads walk the tokens of json-stream's lexer, now vendored under `vendor/json-stream/`, instead of running json-stream's parser combinators. Each read finds every key and string it keeps in its own table by the bytes the lexer read, and builds each retained object once, so the separate intern pass goes. Only the releases and files the projection keeps enter the table, and within an object only the first member under each key, which is the one json-stream keeps. The table hashes with SipHash-1-3 under a key drawn for each read. The walk accepts and refuses the same input as the combinators it replaces, and the vendored files stay upstream's own, with their BSD-3-Clause licence recorded in REUSE.toml. ## Why The combinator layer, the intern pass and the map rebuilds were about half of a cold full read's allocation, and interning code does not belong in json-stream, a constant-memory streaming library. Owning the reader also lets each read key its table, so upstream text cannot choose which strings collide. ## Consequences On CI's cold-read benchmark, full reads allocate 15 to 31% less (typescript 1,137 to 787 MiB) and take 4 to 12% less time, selected npm reads allocate about 5% less, and served bytes, ETags and the 291 recorded corpus outputs do not change. In a 4cpu-1gib load comparison, a cold npm merge allocates 14% less per success and heavy private reads 31% less, while the concurrent cold scenarios' memory peak holds or falls and single-connection pattern scans peak up to 9% higher. Threat 118, the hash-flooding risk the threat model had accepted, is now mitigated, because each read keys its table's hash. Three vendored files differ from upstream commit 537a43a7: CLexer.hs reads each lexer result strictly and lexer.c's string loop no longer reads a byte past its input (both offered upstream as ondrap/json-stream#38), and Parser.hs drops an import the Prelude now provides, so it builds under `-Werror`. The owner approved three lint exceptions for `vendor/` on 2026-09-28: fourmolu and hlint skip it, weeder roots its modules, and stan removes its directory, while a pin list that CI's static job checks catches any change to the tree's paths, modes or contents. `vendor/json-stream/json-stream.freeze` keeps json-stream 0.4.6.1 in the OSV and HSEC scan, and the package licence stays MIT with the vendored code's licence recorded in the repository source. AI-assisted: `Assisted-by: Claude Code (Anthropic)` trailer on every commit. Part of #1520 --------- Signed-off-by: Alexandra DeWit <alexa.dewit@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Went down a rabbit hole and found a decent performance benefit that's easily available.
FYI I did work with an AI agent if you have any zero-AI policy or anything, I can keep the changes isolated to my fork I'm going to use, since I need the benefits when dealing with NPM Packuments for my project.
Changes
parseResults now reads each result record in one withForeignPtr call instead of four lazy peeks. Nothing writes the buffer after the lexer returns, so eager reads are safe, and the laziness only cost an allocation per field while keeping the buffer alive.
handle_string keeps its position and escape flag in locals rather than writing through the lexer struct on every byte. This also stops it reading one byte past the input.