0.10.0
Brand new #[derive]
The derive macro at heart of Logos has been rewritten virtually from scratch between 0.10.0-rc2 and now. The state machine is now built from a graph that permits arbitrary jumps between nodes, instead of a tree that needs to build up permutations of every possible path leading to a token match. This has fixed a whole number of old outstanding issues (#87, #81, #80, #79, #78, #70).
The new codebase is nearly 1k LOC shorter, compiles faster, outputs smaller code, is more cache friendly, and is already proving itself to be easier to debug and optimize. This new release also gets rid of a number of hacks that were previously introduced to manage token disambiguation and loops, which were a huge source of bugs. All nodes in the new state machine are indexed, and loops are described as circular state jumps. Jumps between states are realized by tail recursion calls in the generated code, which in most cases should have performance profile of goto
in C.
No more CPU melting
A case that was breaking the old Logos was using a simple \w+
regex. This is a particularly devious regex flag, since it expands to cover codepoints for every Unicode alphabet, which was then further expand into corresponding byte sequences.
Old logos produced staggering 228k lines of Rust code just for that single pattern, which then usually failed to compile after rustc consumed all available memory. That's because a single \w
produced a tree with 303 leaf nodes, which then had to be duplicated for every leaf to handle the loop, which in the end produced a monstrous tree with 91809 leaf nodes (not counting any branches leading to those).
By contrast, in the new version a single \w
produces a graph with 278 nodes total, while \w+
produces a graph with 279 nodes. That is to say, we went from n ** 2
, to n + 1
, which is a universe of a difference.
Token disambiguation
This release also improves the previously flaky token disambiguation, which is now properly defined and documented. It also leaves us with an option to provide different strategies in the future.
Future
I think the next minor/major release will do a clean-up of the API surface. Since Logos currently pollutes the attribute namespace quite a lot, using pretty generic labels like token
or callback
, it might be wise to wrap most if not all of them into #[logos(...)]
. This should help to make the crate more future-proof, and play nicer with other custom derives that you might want to put on your token enum
s.
I'm very excited to have this release out, and have a whiteboard full of ideas of what can bet tweaked to improve the performance, before even touching SIMD (which is on the horizon somewhere).
API changes:
- Derive macro now allows discriminant values to be set, as long as they are greater than 0, and smaller than the total number of variants in the enum.
- Removed
NulTermStr
support. In turn, a lot of effort has been put to increase the performance using regular&str
sources, which more than makes up for it. - Logos no longer considers nul byte (
0x00
) to terminate input. - It's now possible to define binary, non-Unicode token definitions (both as plain literals and regex). Doing so will force the lexer to use non-Unicode sources such as
&[u8]
, and will fail to compile with&str
. - It's now possible to change the default trivia (whitespace), including the option to completely disable it (more documentation will follow).
- Removed the
Split
trait. - Removed the
Lexicon
type alias.Logos::lexicon
has been replaced byLogos::lex
, which then implements all logic internally.