Optimize JsonCompleter.parse append-only byte hot path#7
Merged
Conversation
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.
Summary
Optimize
JsonCompleter.parsefor append-only streaming input.The old hot path still paid for
input.start_with?(input_snapshot)on every growing chunk to prove the new input shared the old prefix. On long streams that turns into a repeatedmemcmpover already-processed data and showed up as the dominant profiler frame. This change keeps the existing parsing behavior for append-only streams, switches the syntax dispatch/scanners to raw ASCII bytes, and adds inline comments around the byte-oriented path so the optimization stays reviewable.Before / after
Benchmark command:
JSON_COMPLETER_BENCHMARK=1 bundle exec rspec spec/parse_benchmark_spec.rbBenchmark settings from the spec run:
776909712508parse1.2887s0.8435s34.5% faster25.774ms16.870ms34.5% faster2,047,568994,86551.4% fewer196,6080eliminatedRelative to
complete + JSON.parse12.93x19.89x8.83x17.14xWhat changed
start_with?check from the append-only parse hot pathbytesize/getbyte/byteslice)README.mdandCHANGELOG.mdWhy this helps
Most streaming updates are append-only and mostly boring bytes.
Before:
start_with?on every chunkAfter:
bytesliceSame result, less repeated work.
The profiler signal matched that story:
rb_str_start_withdominated the sample before the change and disappeared from the hot-path sample after it.Validation
bundle exec rubocopbundle exec rspecJSON_COMPLETER_BENCHMARK=1 bundle exec rspec spec/parse_benchmark_spec.rb