Skip to content

Core: keep the memory of a parse in pools between parses - #94

Merged
vthorsteinsson merged 1 commit into
masterfrom
parse-memory
Dec 1, 2026
Merged

vthorsteinsson merged 1 commit into
masterfrom
parse-memory

Conversation

@vthorsteinsson

Copy link
Copy Markdown
Member

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:

Allocation Size Per pass Share
The array of a pointer per nonterminal of each column 59,624 bytes 19,967 38%
Chunks of states 98,328 bytes 9,003 28%
Slabs of the forest 65,544 bytes 12,591 27%

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

  • A shared pool of 64 KB blocks (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).
  • 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 (Greynir.cleanup()) 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, so that nothing can depend on zeroed memory.
  • addChunk() is a function of its own, marked NOINLINE. With the pool inlined into operator 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:

Warm pass Mean Core Page faults
4.2.0 (8 processes) 1.655 to 1.770 s 1.695 s 0.645 to 0.746 s 310 to 113,000
This PR (11 processes) 1.654 to 1.687 s 1.672 s 0.653 to 0.663 s 8 to 17
  • Best case: half a percent slower than the best case of 4.2.0 (the core 0.655 s against 0.646 s with trimming of the heap turned off). That is what about a hundred visits to a pool per parse cost.
  • Threads (free-threaded 3.14, 2,000 sentences, sentences per second): 536 against 535 in one thread, 2,820 against 2,730 in 8, 3,560 against 3,460 in 16.
  • Memory: the peak is unchanged (215 MB after the 1,000 sentences). The pools keep up to 24 MB that glibc would sometimes have returned: after all 5,000 sentences of the sample, 293 MB are resident against 278 MB.
  • A first pass is the same (2.63 s); benchmarks/corpus.txt is 2% faster.

Checks

  • The parse forests of the 1,000 news sentences and of benchmarks/corpus.txt are 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.
  • The test suite on 3.14, 3.10, PyPy 3.11 and free-threaded 3.14 (also with --parallel-threads=8), and with GREYNIR_MATCHING_PARITY=1 GREYNIR_REDUCER_PARITY=1.
  • The tests of the core, the matching, the reducer and threads, and 300 news sentences, in a build with AddressSanitizer, UndefinedBehaviorSanitizer and the assertions of the core.
  • ruff and mypy; GreynirCorrect's tests against a wheel of this branch.
  • Not run: GreynirServer's suite. Windows and macOS are left to CI.

Documentation

Section 13 of doc/earley-core-performance.md has the above in full, plus:

  • Where the time of 4.2.0 went. The 7% in the release notes was 6% of added work, the page faults, and a comparison across two Python versions. Over half of the added work came with the head noun of a noun phrase (the second phase of the C++ reducer is 30% slower, and the bonus callbacks went from 7,900 to 13,800 per pass); the scoring of "á" and the static phrases cost next to nothing. CLAUDE.md said otherwise and is corrected.
  • How to compare the speed of two versions: the same Python, several processes of each, a pinned core.
  • Looked at and not done: a cache of the preposition bonuses that outlives the sentence gains 2% in the benchmark but about 1% in text that has not been seen (45% of lookups hit after 4,000 sentences); and the reducer's 30% has no single hot spot.

🤖 Generated with Claude Code

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>
@vthorsteinsson
vthorsteinsson merged commit 57d1dea into master Dec 1, 2026
12 checks passed
@vthorsteinsson
vthorsteinsson deleted the parse-memory branch December 1, 2026 17:45
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