Repository navigation
Core: divide eparser.cpp into modules - #95
Merged
Merged
Conversation
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>
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.
eparser.cpphad grown to 3,033 lines with six concerns in it, of which the Earley algorithm was one, andeparser.hto 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
eparser.cppState,StateStore,Column,StateSet,HNode,NodeDict,ParseRun,Parsereparser.hParserclass; includes the other headersegrammar.h,egrammar.cppNonterminal,Production,Grammar: reading the binary file, the analysis for the lookahead filtereforest.h,eforest.cppLabel,Node,Forestematcher.h,ematcher.cppMatcher, and the structures thatbinparser.pyfills inereducer.h,ereducer.cppememory.h,ememory.cppBlockPool,Pool,AllocCounterebase.heparser_api.cppThe top of
eparser.cpphas this overview too.The one constraint
The extension is built without link-time optimization, so a call from one
.cppfile into another is never inlined. The classes that call each other for every state therefore stay together ineparser.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:
Parser. They are now aMatcher, of which three things are asked:setTable(),match()andprefill(). The parser core no longer knows what aTerminalSpecor aMeaningRecis. This is the code that must agree with the matchers inbinparser.py, which is now what its file is about.Parser::parse()was one function of 350 lines with the predictor, the completer and the scanner inline.ParseRunis one parse: its members are what the local variables were, and its methods are the steps,start(),predict(),complete()andscan().Parseris left with what parses share (the grammar, the matcher, the callbacks, the pools). AColumnnow keeps the states that wait for its scanner, andStateStoreis the storage of states.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.pyonly gets the longer list of sources.Commits
One per step, each built and checked against the forest digests:
ebase.h,ememory)egrammar)eforest)ematcher)ereducer.h,ReducerSetup)eparser_api.cpp)ParseRun)Measurements
1,000 news sentences, the same Python, one core, as section 13 of the performance document describes:
Parser::parse()as it wasMoving 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
benchmarks/corpus.txtare identical to those of master, including the order of the families (bench_parse.py --digest), after each step.--parallel-threads=8), and withGREYNIR_MATCHING_PARITY=1 GREYNIR_REDUCER_PARITY=1.DEBUGbuild compiles and traces.-Wall -Wextra.Documentation
CLAUDE.mddescribes the files and the constraint.doc/earley-core-performance.mdgets 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 ateparser.cpporeparser.hfor something that has moved now point to where it is.🤖 Generated with Claude Code