Skip to content

Core: divide eparser.cpp into modules - #95

Merged
vthorsteinsson merged 9 commits into
masterfrom
split-eparser
Dec 2, 2026
Merged

vthorsteinsson merged 9 commits into
masterfrom
split-eparser

Conversation

@vthorsteinsson

Copy link
Copy Markdown
Member

eparser.cpp had grown to 3,033 lines with six concerns in it, of which the Earley algorithm was one, and eparser.h to 1,084. This PR gives each concern a file and a small interface. The parse forests are unchanged, and the parser is not slower.

The files

File Lines What is in it
eparser.cpp 1,689 The algorithm: State, StateStore, Column, StateSet, HNode, NodeDict, ParseRun, Parser
eparser.h 154 The Parser class; includes the other headers
egrammar.h, egrammar.cpp 890 Nonterminal, Production, Grammar: reading the binary file, the analysis for the lookahead filter
eforest.h, eforest.cpp 436 Label, Node, Forest
ematcher.h, ematcher.cpp 771 Matcher, and the structures that binparser.py fills in
ereducer.h, ereducer.cpp 1,640 The reducer, its tables and what it returns
ememory.h, ememory.cpp 382 BlockPool, Pool, AllocCounter
ebase.h 76 Types and macros
eparser_api.cpp 327 Every function that Python calls

The top of eparser.cpp has this overview too.

The one constraint

The extension is built without link-time optimization, so a call from one .cpp file into another is never inlined. The classes that call each other for every state therefore stay together in eparser.cpp, and the few functions of other files that are called that often are defined in headers (Grammar::operator[], Node::addFamily(), Forest::alloc()).

What changed in substance

Most of the code is moved as it was. Three things are not just moves:

  • Native matching is a class. The table of terminal specs, the masks, the prefilling patterns and the parity counter were members of Parser. They are now a Matcher, of which three things are asked: setTable(), match() and prefill(). The parser core no longer knows what a TerminalSpec or a MeaningRec is. This is the code that must agree with the matchers in binparser.py, which is now what its file is about.
  • A parse is an object. Parser::parse() was one function of 350 lines with the predictor, the completer and the scanner inline. ParseRun is one parse: its members are what the local variables were, and its methods are the steps, start(), predict(), complete() and scan(). Parser is left with what parses share (the grammar, the matcher, the callbacks, the pools). A Column now keeps the states that wait for its scanner, and StateStore is the storage of states.
  • Each module prints its own lines of the allocation report. AllocReporter, which every class declared a friend, is gone. The lines are the same; two of them come in a different place.

A side effect of the second: the working memory of a parse is released by destructors, so also when memory runs out in the middle of a parse, where it used to be left behind.

The interface of the extension is unchanged: eparser_build.py only gets the longer list of sources.

Commits

One per step, each built and checked against the forest digests:

  1. Each module prints its own lines of the allocation report
  2. Basic definitions and memory (ebase.h, ememory)
  3. The grammar (egrammar)
  4. The parse forest (eforest)
  5. Native matching as a class (ematcher)
  6. The tables of the reducer (ereducer.h, ReducerSetup)
  7. The interface to Python (eparser_api.cpp)
  8. A parse is an object (ParseRun)
  9. Documentation

Measurements

1,000 news sentences, the same Python, one core, as section 13 of the performance document describes:

First pass Core Warm pass Core
master 2.62 s 1.336 s 1.657 s 0.653 s
After step 5, Parser::parse() as it was 2.63 s 1.338 s 1.664 s 0.654 s
This PR 2.59 s 1.310 s 1.640 s 0.634 s

Moving the code made no difference. Making the parse an object made the core 3% faster, which was not the aim; I did not look into why. Throughput in threads on free-threaded 3.14 is the same (541 sentences per second against 535 in one thread, 3,622 against 3,633 in 16).

Checks

  • The parse forests of the 1,000 news sentences and of benchmarks/corpus.txt are identical to those of master, including the order of the families (bench_parse.py --digest), after each step.
  • 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, threads and the binary grammar, and 300 news sentences, in a build with AddressSanitizer, UndefinedBehaviorSanitizer and the assertions of the core; the whole suite in a build with the assertions alone; a DEBUG build compiles and traces.
  • All files compile without warnings under -Wall -Wextra.
  • ruff and mypy.
  • The sdist has all fourteen source files and installs from scratch; GreynirCorrect's tests pass against a wheel of this branch.
  • Not run: GreynirServer's suite. Windows and macOS are left to CI.

Documentation

CLAUDE.md describes the files and the constraint. doc/earley-core-performance.md gets section 14, which includes a table of where the functions that its earlier sections name are now. Comments in the Python sources and tests that pointed at eparser.cpp or eparser.h for something that has moved now point to where it is.

🤖 Generated with Claude Code

vthorsteinsson and others added 9 commits December 1, 2026 17:50
The report was written by AllocReporter, a class that every other class
in the core declared a friend so that it could read their counters. That
ties all of them to one place, which stands in the way of dividing
eparser.cpp into modules.

The lines are now printed by those who own the counters:
Grammar::reportAllocs(), Node::reportAllocs(), Parser::reportAllocs()
(states, columns, matching) and BlockPool::reportAllocs().
printAllocationReport() calls them in turn. The lines are the same; the
two about the pool of blocks come last instead of in the middle.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
ebase.h has the types and macros that all parts of the C++ module use.
ememory.h and ememory.cpp have what the memory of the parser comes from
and how it is counted: BlockPool, Pool and AllocCounter. The code is
moved as it was. eparser.h includes both, and now has an include guard.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
egrammar.h and egrammar.cpp have the grammar as the parser sees it:
Nonterminal, Production and Grammar, the reading of the binary file and
the analysis for the lookahead filter. Nothing here depends on the
parser, and the parser only reads the tables that the analysis leaves.

The code is moved as it was, except that Grammar::operator[] and
Production::setNext() are now defined in the header: the parser calls
the first for every nonterminal that it predicts, and a call into
another file is not inlined.

The DEBUG switch for a build that traces what it does is now in ebase.h,
where it applies to all files.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
eforest.h has Label, Node and Forest, which the parser creates and the
reducer reads. eforest.cpp has the parts that are not on the hot path:
deleting a forest, dumping it and counting its trees.

The code is moved as it was. The constructor of Node and
Node::addFamily() are defined in the header, as the parser calls them
for most of its states and a call into another file is not inlined.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The matching of tokens with terminals without asking Python was spread
over the Parser class: the table of terminal specs, the masks, the
patterns for prefilling, the index of literals and the parity counter
were all members of it, next to what the Earley algorithm needs.

ematcher.h and ematcher.cpp now have all of it, in a Matcher class with
three things to ask of it: setTable(), match() and prefill(). A parser
has one Matcher. The parser core itself no longer knows what a
TerminalSpec or a MeaningRec is: the matching data of a token is bytes
that it passes on.

This is also the code that must give the same answers as the matchers
in binparser.py, which is now what the file is about. The functions are
moved as they were (Parser::matchNatively() is Matcher::match(),
Parser::prefillCache() is Matcher::prefill(), evalMatch() and
verbMatchMeaning() keep their names). The interface of the extension is
unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
ereducer.h has what the reducer needs from outside and what it gives
back: the flags of nonterminals and terminals, the two callbacks,
ReducerTables and Reduction. The copy of the tables that a parser keeps
was made and freed by the Parser class in eparser.cpp; it is now a
ReducerSetup, with its code in ereducer.cpp.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
eparser_api.cpp has every function that Python can call through CFFI,
and nothing else: what eparser_build.py declares is what is in that
file. Each function is described where it is defined, and the reason
why none of them lets a C++ exception out is told once, at the top.

The functions are as they were. reduceStart() and reduceFinish() move
there from ereducer.cpp, which now offers startReduction() and
finishReduction() to them. eparser.h is left with the Parser class.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Parser::parse() was one function of 350 lines, with the predictor, the
completer and the scanner inline in a loop and fourteen local variables
between them. The Parser class also had the helpers of that function as
members, each taking the handle of the parse, the chunks of states and
the set of states as arguments.

There are now two classes, for two things:

- Parser is what stays the same from one parse to the next, and is
  shared by the parses of all threads: the grammar, the matcher, the
  callbacks and the pools. It has no private methods left but the ones
  that size its pool.
- ParseRun (in eparser.cpp) is one parse. Its members are what the
  local variables were, and its methods are the steps of the algorithm:
  start(), predict(), complete(), scan(), with run() going through the
  columns. makeNode(), push() and the lookahead filter are methods of
  it too, and need no arguments to tell them which parse they are in.

Along with that:

- StateStore is the storage of the states of a parse (it was a pointer
  to a chunk, passed around, with three functions that took it).
- A Column keeps the states that wait for its scanner, and knows the
  handle of its parse. The two lists of such states that the loop
  juggled between columns (pQ and pQ0) are gone.
- The working memory of a parse is released by destructors, and so also
  when a parse runs out of memory, where it used to be left behind.

The statements of the algorithm are as they were, in the same order,
and the parse forests of 1,000 news sentences and of
benchmarks/corpus.txt are identical, including the order of the
families. The core is 3% faster on a warm pass over the 1,000 sentences
(0.634 s against 0.653 s). The timings of a DEBUG build are gone; its
trace of columns and its dump of the result remain.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
CLAUDE.md says what is in which file of the C++ module and why the
classes of the algorithm stay together in eparser.cpp (a call into
another file is never inlined). doc/earley-core-performance.md gets
section 14, with the files, the three things that changed in substance,
a table of where the functions that the earlier sections name are now,
and the timings. Comments in the Python sources, the tests and the
benchmark that point to eparser.cpp or eparser.h for something that has
moved now point to where it is.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@vthorsteinsson
vthorsteinsson merged commit 8a6a23f into master Dec 2, 2026
12 checks passed
@vthorsteinsson
vthorsteinsson deleted the split-eparser branch December 2, 2026 10: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