Skip to content

4.0.0

Choose a tag to compare

@vthorsteinsson vthorsteinsson released this 10 Nov 22:54

Highlights

Parsing is 40–60x faster than in 3.10.0, and uses a third less memory. A typical news sentence now takes about 3.4 ms to parse instead of 207 ms (about 5 ms the first time its words are seen). The public API is unchanged.

1,000 news sentences (19,213 tokens) 3.10.0 4.0.0 Speedup
First pass (new text) 212 s 5.1 s 41x
Warm pass 207 s 3.4 s 60x
Throughput, first pass 91 tokens/s (4.7 sentences/s) 3,770 tokens/s (196 sentences/s)
Throughput, warm 93 tokens/s (4.8 sentences/s) 5,590 tokens/s (291 sentences/s)
Peak memory 319 MB 215 MB

GreynirCorrect, which builds on GreynirEngine, becomes about 19x faster without any change of its own (300 news sentences: 90 s to 4.7 s on a first pass).

"First pass" is what a process sees when it parses new text; "warm" is a repeat pass with the per-token caches filled, as in a long-running server. Measured on one core of an Intel i7-4770 (3.4 GHz) with CPython 3.14, both versions with BinPackage 2.1.0 and Tokenizer 3.7.0. The news sentences are 19 tokens long on average.

Changes

Performance

  • Earley parser core (C++): the hot data structures were rebuilt (constant-time lookups, an arena for the parse forest, prediction tables), and the core now has a one-token lookahead filter: a parser state is only created if what follows it can begin with the current token. The core is about 130x faster on news text.
  • Reduction of the parse forest to the best tree is done in C++, before any Python objects are created for the forest; Python only builds the nodes of the winning tree. The Python reducer remains, as the reference implementation and as a fallback.
  • Matching of tokens with terminals is done in the C++ core for nearly all words, verb terminals included. Calls into Python while parsing new text went from several hundred per token to about eight.
  • Tokenization: lookups of words in BÍN are cached, which nearly halves tokenization time.
  • Memory: the per-token matching cache is half the size, and far fewer objects are created during a parse.

Grammar

  • Reflexive agreement: sjálfur + reflexive pronoun now agrees with the subject in gender and number: Hún skammaði sjálfa sig, Þeir hlógu að sjálfum sér.
  • staða, stöðu are preferred as the noun staða over the adjective staður: stöðu forstjóra, styrkja stöðu framleiðenda.
  • Abbreviated adjective before a number (f. 1985) no longer leaves the choice of parse to chance.
  • Reproducible binary grammar: the compiled grammar (Greynir.grammar.bin) is now the same every time it is generated. Before, the numbering of productions varied between processes, and with it the parse of a few sentences.

For developers

  • Reducer.parse_and_reduce() parses and reduces in one step, and is what Greynir uses. Fast_Parser.go(), Fast_Parser.num_combinations() and Reducer.go_with_score() work as before, in Python.
  • The C++ paths can be switched off, or checked against the Python implementations, with environment variables: GREYNIR_DISABLE_CPP_MATCHING, GREYNIR_DISABLE_CPP_REDUCER, GREYNIR_DISABLE_LOOKAHEAD, GREYNIR_MATCHING_PARITY, GREYNIR_REDUCER_PARITY. Results are the same either way.
  • benchmarks/bench_parse.py measures parsing speed; doc/earley-core-performance.md describes the work in detail.
  • The development tools are also available as an extra: pip install -e ".[dev]" or uv sync --extra dev.

Upgrading

  • BinPackage (islenska) 2.1.0 or newer is required (was 1.3.2). It keeps its data in a separate, memory-mapped package, which lowers the memory use of a process at startup from about 265 MB to about 85 MB.
  • Some sentences get different parse trees than with 3.10.0, because of the grammar changes above.
  • The binary grammar is regenerated the first time the package is used after the upgrade, which takes a few seconds.
  • If you derive classes from BIN_Token, Fast_Parser or Reducer: overriding a method that the C++ code replicates makes the engine fall back on Python for that part automatically, so results stay correct, at the old speed. The methods that BIN_Token.verb_matches() calls (verb_matches_arguments(), verb_subject_matches() and the like) may be overridden without losing the fast path, but must depend on their arguments only.
  • GreynirBin.lookup_g() returns a new list on every call, as before, but the BIN_Tuple objects in it are now shared between calls.
  • Building from source needs a C++11 compiler, as before. Wheels are provided for Linux, macOS and Windows.
  • Tested with Tokenizer 3.7.0 and BinPackage 2.1.0, on CPython 3.10–3.14 and PyPy 3.11.