Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite lexer and parser #196

Merged
merged 138 commits into from
Jul 22, 2024
Merged

Rewrite lexer and parser #196

merged 138 commits into from
Jul 22, 2024

Conversation

01mf02
Copy link
Owner

@01mf02 01mf02 commented Jul 17, 2024

In the beginning, jaq used pest for its parser and lexer.
Later (3e26651), a new lexer/parser written using chumsky replaced the pest-based lexer/parser.
Over time, several shortcomings of the chumsky-based parser became apparent: First, at runtime, it was so slow that it became necessary to cache parse results, in particular of the standard library (std.jq, the equivalent of builtin.jq in jq), to achieve fast startup times. This meant additional dependencies on serde and bincode. Second, the compilation speed of the jaq parser was quite slow as well (making up for the largest part of the build process), despite my investing quite some time in trying to remedy this problem (mostly by sprinkling .boxed() throughout the parser).

This PR adds a new, hand-written lexer/parser for jaq.
Its build time is 1.55 seconds, compared to 47.59 seconds for the old chumsky-based parser (measured with cargo build --release --no-default-features -p jaq-{parse,syn}). This solves the long-standing issue #2.

The runtime speed of the new parser is significantly higher than that of the old parser; consider the following benchmark:

$ (for i in `seq 1000000`; do echo "def a: 0;"; done; echo 0) > bla.jq
$ time jaq -n -f bla.jq 0

This writes a file containing 1M instances of def a: 0; (amounting to 9.6MB), then executes jaq on that file.
Using the new parser, jaq takes 1.6 seconds, whereas with the old parser, jaq takes 30.3 seconds.
(For the same file, gojq 0.12.15 takes 4 minutes and 49 seconds, and jq 1.7.1 fails immediately with "error: memory exhausted".)

The executable size of jaq also decreases a bit, going down from 4.8MB (old parser) to about 3.8MB (new parser).

The new parser can parse a few constructs that the old parser could not; for example, object keys can now be keywords (e.g. {if: 1}.if).
Furthermore, the new parser adds support for several new syntactical constructs such as label ... break, generalised nested definitions (#157) and module syntax (module, import, include). However, while the parser can identify these constructs, the jaq compiler currently either ignores certain constructs (module) or panics ( label, break, import, include, generalised nested definitions) when encountering them. Handling this situation correctly (without panicking) will need to break the current API, so these syntactical constructs will be compiled only as of jaq 2.0.

If you are using jaq as an API, you can either continue using the old parser (in jaq-parse) or transition to the new parser (in jaq-syn). See jaq/src/main.rs for how to use the new parser.

@01mf02 01mf02 linked an issue Jul 22, 2024 that may be closed by this pull request
@01mf02 01mf02 merged commit afc2af6 into main Jul 22, 2024
1 check passed
@01mf02 01mf02 deleted the faster-lexer branch July 22, 2024 16:36
@01mf02 01mf02 linked an issue Jul 22, 2024 that may be closed by this pull request
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.

ER: ignore module directive (for the time being) Rewrite parser to reduce release build times
1 participant