Skip to content

perf(lexer): perform part of reads strictly - #38

Open
AlexaDeWit wants to merge 1 commit into
ondrap:masterfrom
AlexaDeWit:perf/token-reads
Open

AlexaDeWit wants to merge 1 commit into
ondrap:masterfrom
AlexaDeWit:perf/token-reads

Conversation

@AlexaDeWit

Copy link
Copy Markdown

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

  1. 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.

  2. 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.

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>
@AlexaDeWit AlexaDeWit changed the title perf(lexer): streamline reads strictly perf(lexer): platform part of reads strictly Oct 16, 2026
@AlexaDeWit AlexaDeWit changed the title perf(lexer): platform part of reads strictly perf(lexer): perform part of reads strictly Oct 24, 2026
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.

1 participant