Repository navigation
Read input characters without a dynamic call per character - #286
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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.
Why
YAML.loadspent about a quarter of its time on two dynamic calls per input character, because the scanner's input buffer holds its stream as an abstractIO. With this change, loading a 3 MB document takes 501 ms instead of 704 ms (1.4x faster), andYAML.load_fileon 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 thatloadandload_filecreate (IOBufferandIOStream), 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.prefixbuilds 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
Tradeoffs
Making
BufferedInputandTokenStreamparametric 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
eofandreadmethods run as before; only how Julia dispatches to them changes. When a decoder throws partway through a buffer fill,availis not advanced, as before.buffer!is called only frompeekandprefix.Verification
load_allvalues, and full error messages. Inputs aretest/yamland four benchmark documents (each also throughload_file,load_all_file, UTF-16LE, UTF-32BE, and CRLF line ends), 100,000 inputs from the fuzz generators used in earlier review rounds (withload_fileon every 10th), and the 20,000- and 30,000-value writer corpora. Both trees had the localfetch_block_entryhang patch, as in earlier rounds.Pkg.test()passes on Julia 1.12 and on Julia 1.0 (Docker).🤖 Generated with Claude Code