Repository navigation
4.1.0
Highlights
GreynirEngine now runs on free-threaded Python (CPython 3.14t and 3.15t), with the GIL off. A Greynir instance can be used from several threads at once, and the threads parse in parallel.
| 1,000 news sentences, warm, CPython 3.14t | Sentences/s | Speedup |
|---|---|---|
| 1 thread | 297 | 1x |
| 4 threads | 868 | 2.9x |
| 8 threads | 1,043 | 3.5x |
Measured on an Intel i7-4770 (4 cores, 8 hyperthreads), with one Greynir shared by all threads. On an 8-core laptop (Ryzen 7 PRO 7840U), 8 threads gave 4.4x and 16 threads 4.9x the throughput of one thread, about 25% more than the same number of processes.
With a regular (GIL) Python, 4.1.0 is about 5% faster than 4.0.0 in a single thread. The public API is unchanged, and so are the parse trees.
Thanks to @HaukurPall for the free-threading work (#85).
Changes
Threads
- Wheels for free-threaded CPython 3.14t and 3.15t, on Linux, macOS and Windows. The extension module declares that it does not need the GIL, so importing
reynirleaves it off. - A crash when threads parsed at once is fixed. When two threads met a new token at the same moment, one of them could be left using a buffer that had been freed. This could corrupt memory or give wrong matches. It could also happen with a regular Python, as the C++ parser core has always run without the GIL, though it was much less likely there.
- Grammars are read one at a time, so reading grammars in several threads no longer mixes up the numbering of their terminals, nonterminals and productions.
- A long parse keeps its parse job handle. Handles were reused after 10,000 parses, even if the job of an earlier one was still open; its callbacks could then go to another thread's parse.
- Parsing scales with the number of threads. Before, 8 threads were no faster than 4 on free-threaded Python.
- The README has a new section on using
Greynirfrom several threads.
Performance
- The C++ core gets the buffers of the tokens that it has seen before when it is called, instead of asking for them one token at a time (#84).
- The grammar classes no longer modify themselves as their instances are created, which had made attribute lookups on grammar objects slower than they need to be.
- Source builds are optimized by default: the parser core is compiled with
-O2when neither the Python build norCXXFLAGS/CPPFLAGSset an optimization level. On such systems (Gentoo's system Python is one) the core was compiled without optimization, and ran about 3x slower.
Packaging
- Python 3.15 is tested and supported.
- Wheels: one
abi3wheel for CPython 3.10 and newer, version-specific wheels for free-threaded CPython 3.14t and 3.15t, and PyPy 3.11 wheels for both PyPy 7.3 and PyPy 8.0. Every wheel is now installed and used to parse a sentence before it is published.
Upgrading
- cffi 2.0.0 or newer is required (was 1.17.1). It is the first version that supports free-threaded Python.
- With a regular (GIL) Python, threads take turns at everything except the C++ parser core, so use processes to parse in parallel there.
Greynir.cleanup()frees the parser that allGreynirinstances share: call it only when no other thread is parsing.- Free threading needs CPython 3.14t or newer.
- More than 10,000 parse jobs open at the same time now raise a
RuntimeError. - The binary grammar is regenerated the first time the package is used after the upgrade, which takes a few seconds.
- Tested with Tokenizer 3.7.0 and BinPackage 2.1.0, on CPython 3.10–3.15, free-threaded CPython 3.14t and 3.15t, and PyPy 3.11.