Repository navigation
Core: keep the memory of a parse in pools between parses - #94
Merged
Merged
Conversation
4.2.0 measured 7% slower than 4.1.0 on a warm pass over 1,000 news
sentences, and the same build could differ by that much from one process
to the next. Most of that variation was not work but page faults.
A parse allocates about 3 MB in large blocks - an array of a pointer per
nonterminal for each column, chunks of states and slabs of the forest -
and frees them within milliseconds. glibc returns the top of the heap to
the operating system when more than 128 KB is free there, and every page
is then faulted in again by the next parse. Whether that happens depends
on what else lies on the heap: a warm pass took from 300 to 170,000 minor
page faults, and the core from 0.64 s to 0.81 s, depending on the
addresses that the process got and even on the size of its environment.
4.1.0 is exposed to this as well. The work on names, which changed no
C++, only shifted the layout.
Those three kinds of block now come from pools that are kept between
parses (BlockPool):
- A pool of 64 KB blocks, shared by all parsers and threads, for the
chunks of states (1,364 states each, where a chunk had 2,048) and the
slabs of the forest. Up to 256 free blocks are kept.
- A pool in each parser for the arrays of its columns, with the bitmap of
the column in the same block. Up to 8 MB is kept.
- Deleting a parser returns the free blocks to the heap.
- A block is never initialized. A build with the assertions of the core
fills each one with a pattern when it is handed out.
The states are created in operator new(size_t, StateChunk*&). With the
pool inlined there, the common case got slower by 4% of the core; the
rare case is now a function of its own (addChunk(), NOINLINE).
Warm pass over the 1,000 sentences, default settings of glibc:
pass mean page faults
4.2.0 (8 runs) 1.655 to 1.770 s 1.695 s 310 to 113,000
pools (11 runs) 1.654 to 1.687 s 1.672 s 8 to 17
The best case is half a percent slower than before, which is what the
visits to the pools cost. Throughput in threads on free-threaded 3.14 is
unchanged in one thread and 2 to 4% higher in 4 to 16. The peak of
resident memory is unchanged; up to 24 MB now stays with the process.
The parse forests of the 1,000 sentences and of benchmarks/corpus.txt
are identical to those of 4.2.0, including the order of the families.
test_block_pool checks that a repeated parse obtains nothing from the
heap for its states and forest. The allocation report has two new lines.
Section 13 of doc/earley-core-performance.md has the measurements, how
to compare the speed of two versions, and two things that were looked at
and not done: a cache of the preposition bonuses that outlives the
sentence (2% in the benchmark, about 1% in text that has not been seen)
and the second phase of the C++ reducer, which is 30% slower since the
work on the head noun of a noun phrase. It also corrects what CLAUDE.md
said about where the time of 4.2.0 went: the work that was added is 6%,
over half of it with the head noun, and next to none with the scoring of
"á" or the static phrases.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.
The same build of 4.2.0 could parse up to a tenth slower in one process than in the next. The cause was page faults in the parser core, not work. This PR takes them away by keeping the large blocks of a parse in pools between parses.
What was wrong
A parse allocates about 3 MB in large blocks and frees them within milliseconds. glibc returns the top of the heap to the operating system when more than 128 KB is free there, and the next parse faults every page in again. Whether that happens depends on what else lies on the heap, so on the addresses the process got and even on the size of its environment.
Per warm pass over 1,000 news sentences, 93% of the 3.1 GB of large allocations were of three kinds:
A warm pass took from 300 to 170,000 minor page faults, and the core from 0.64 s to 0.81 s. 4.1.0 is exposed as well; the work on names in 4.2.0 changed no C++ and only shifted the layout of the heap.
The change
BlockPool) for the chunks of states and the slabs of the forest, in all parsers and threads. A chunk now has 1,364 states where it had 2,048. Up to 256 free blocks are kept (16 MB).Greynir.cleanup()) returns the free blocks to the heap.addChunk()is a function of its own, markedNOINLINE. With the pool inlined intooperator new(size_t, StateChunk*&), which creates every state, the core was 4% slower.The interface of the extension is unchanged. The allocation report has two new lines, for the blocks that the shared pool holds and those of them that are free.
Measurements
Warm pass over the 1,000 sentences, default settings of glibc, Python 3.15, one P-core:
benchmarks/corpus.txtis 2% faster.Checks
benchmarks/corpus.txtare identical to those of 4.2.0, including the order of the families (bench_parse.py --digest), also in the build with assertions and pattern-filled blocks.test_block_pool: a repeated parse obtains nothing from the heap for its states and forest, gives the same forest and leaves all blocks free; deleting the parser returns them.--parallel-threads=8), and withGREYNIR_MATCHING_PARITY=1 GREYNIR_REDUCER_PARITY=1.Documentation
Section 13 of
doc/earley-core-performance.mdhas the above in full, plus:CLAUDE.mdsaid otherwise and is corrected.🤖 Generated with Claude Code