Uh oh!
There was an error while loading. Please reload this page.
perf: compiled-runtime output flushing and match-table transcoding (1.5x) - #717
Merged
Merged
Conversation
Profiling a compiled -COMPILED run of parse-en-us (x86 RelWithDebInfo, 37.5 KB input, ~7k stack samples) put ~42% of self time in file-write syscalls and ~18% inclusive in the rule matcher's string comparison. Neither was where reading the code suggested: the string-keyed variable lookup (Var::find's linear list scan) is about 1% of runtime. 1. Arun::out flushed on every `<<`. NLP-ENGINE-499/505 added that so print-debugging survives a crash, with a comment saying the cost was negligible "for typical diagnostic volumes". parse-en-us writes ~6 MB across 19 files per run, one token at a time, so it cost a WriteFile plus a file-position query per write: ntdll!ZwWriteFile 25.5% self ntdll!ZwQueryInformationFile 13.2% self Arun::out 34.7% inclusive Durability now comes from Parse::flushostrs(), called from Parse::finPass, so every completed pass's output is on disk if the process dies. That is O(passes) instead of O(writes). NLP_FLUSH_EVERY_WRITE=1 restores the old behaviour for bisecting a crash within a single pass. 2. find_str_nocase rebuilt an icu::UnicodeString for every element of a match list on every call, even though every caller passes one of the static const tables emitted into the generated analyzer code. Cache the UTF-16 form in a direct-mapped table keyed on the array address. A std::map here is not cheap enough -- measured, the tree lookup cost as much as the transcoding it saved -- so the slot is found with a shift+mask and confirmed by comparing the array pointer and its first element. Self time 13.4% -> 4.5%. Measured on parse-en-us, interleaved A/B, 8 runs per arm (min/p25, the machine had background load): flush every write (old) 17.19s / 17.84s flush per pass (new) 11.69s / 11.72s 1.47x - 1.52x All 18 analyzer output files are byte-identical before and after; only dbg.log differs, because it contains the per-pass timings. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Uh oh!
There was an error while loading. Please reload this page.
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 freeto 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.
What
Two changes to the compiled-analyzer runtime, both found by profiling rather than by reading the code.
1.
Arun::outflushed the stream on every<<. NLP-ENGINE-499/505 added that so print-debugging survives a crash, with a comment saying the cost was negligible "for typical diagnostic volumes". parse-en-us writes ~6 MB across 19 files per run (a 2.35 MBout.xml), one token at a time, so it became aWriteFileplus a file-position query per write.Durability now comes from a new
Parse::flushostrs()called fromParse::finPass, so every completed pass's output is on disk if the process dies — O(passes) instead of O(writes).NLP_FLUSH_EVERY_WRITE=1restores the old behaviour for bisecting a crash within one pass.2.
find_str_nocaserebuilt anicu::UnicodeStringfor every element of a match list on every call, even though every caller passes one of the staticconsttables emitted into the generated analyzer code. Now cached in a direct-mapped table keyed on the array address.Profile (before)
Compiled
-COMPILEDrun of parse-en-us, x86 RelWithDebInfo, 37.5 KB input, ~7k stack samples:ntdll!ZwWriteFilentdll!ZwQueryInformationFilefind_str_nocase(+ICU under it)Var::find+Ipair::getKey+Delt<Ipair>::getDataArun::outwas 34.7% inclusive,ostream::flush32.1%,fflush32.0%.That last row is worth noting: the string-keyed variable lookup (
Var::find's linear list scan with_tcscmp) looks like the obvious villain when reading the generated code, but it is about 1% of runtime.Results
Interleaved A/B, 8 runs per arm, parse-en-us. The machine had ~35% background load, so min and p25 are the meaningful columns:
1.47x – 1.52x.
After the fix,
find_str_nocaseself time went 13.4% → 4.5%. (A first attempt usingstd::mapfor that cache was a complete wash — the tree lookup cost as much as the transcoding it saved. The direct-mapped version is what paid off.)Correctness
All 18 analyzer output files are byte-identical before and after. Only
dbg.logdiffers, because it contains the per-pass timings.Not in this PR
run!_chkstkis ~6% of self time. The generator emits one giantmatchRule<N>per pass containing aswitchover every rule, so the frame is sized for the union of all rules and every rule execution probes it. Confirmed the frames exceed a page: building with/Gs1048576crashes with an access violation. The fix is emitting one function per rule, which is a change in the analyzer code generator — separate PR.🤖 Generated with Claude Code