Skip to content

Read input characters without a dynamic call per character - #286

Merged
quinnj-bot[bot] merged 1 commit into
masterfrom
claude/faster-input-reads
Nov 19, 2026
Merged

quinnj-bot[bot] merged 1 commit into
masterfrom
claude/faster-input-reads

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Why

YAML.load spent about a quarter of its time on two dynamic calls per input character, because the scanner's input buffer holds its stream as an abstract IO. With this change, loading a 3 MB document takes 501 ms instead of 704 ms (1.4x faster), and YAML.load_file on the same file takes 598 ms instead of 1036 ms (1.7x). A 71 KB stream of 40 Kubernetes manifests loads in 16.7 ms instead of 21.3 ms. Results are unchanged.

What changed

  • buffer! branches on the stream types that load and load_file create (IOBuffer and IOStream), so reading a character is a static call. Other streams, such as the decoder for UTF-16 and UTF-32 input, make one dynamic call per buffer fill instead of two per character.
  • prefix builds its string from a view of the buffer instead of a copy. This removes about 700 k of the 4.5 M allocations on the 3 MB document.
  • forwardchars! compares the line-break characters directly instead of searching a string for them.

Scope

  • The load path only. Loaded values, errors, and error positions are the same.
  • The parser, composer, and constructor are unchanged. They now take most of the load time.

Tradeoffs

Making BufferedInput and TokenStream parametric on the stream type would remove the dynamic calls for every stream type, but it compiles the whole scanner once per stream type and changes the shape of two structs. The branch covers the stream types the package itself creates.

Blast Radius

Every load reads its input through this code. The same eof and read methods run as before; only how Julia dispatches to them changes. When a decoder throws partway through a buffer fill, avail is not advanced, as before. buffer! is called only from peek and prefix.

Verification

  • Differential run against master on 110,332 records, all identical: token streams with every mark, load_all values, and full error messages. Inputs are test/yaml and four benchmark documents (each also through load_file, load_all_file, UTF-16LE, UTF-32BE, and CRLF line ends), 100,000 inputs from the fuzz generators used in earlier review rounds (with load_file on every 10th), and the 20,000- and 30,000-value writer corpora. Both trees had the local fetch_block_entry hang patch, as in earlier rounds.
  • Pkg.test() passes on Julia 1.12 and on Julia 1.0 (Docker).
  • Load of a 3 MB generated document: 704 ms → 501 ms (Chairmarks, both trees in one process, interleaved, median of 5 rounds, load average 10 to 17).

🤖 Generated with Claude Code

The scanner's BufferedInput holds its stream as an `IO`, so reading each
character made two dynamically dispatched calls (`eof` and `read`). It
now branches on the stream types `load` and `load_file` create (IOBuffer
and IOStream), so those reads are static calls. Other streams, such as
the decoder for UTF-16 and UTF-32 input, make one dynamic call per
buffer fill.

`prefix` builds its string from a view of the buffer instead of a copy,
and `forwardchars!` compares the line-break characters directly instead
of searching a string for them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.24%. Comparing base (6ccf984) to head (a2aecd3).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #286      +/-   ##
==========================================
+ Coverage   92.21%   92.24%   +0.03%     
==========================================
  Files          15       15              
  Lines        1759     1767       +8     
==========================================
+ Hits         1622     1630       +8     
  Misses        137      137              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@quinnj-bot
quinnj-bot Bot merged commit 2ffad61 into master Nov 19, 2026
22 checks passed
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