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

perf(parser): remove backtracking #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

bgotink
Copy link

@bgotink bgotink commented Jan 9, 2025

I noticed a performance regression when updating my benchmark to the new kdljs version.
Chevrotain is very slow at backtracking. Removing all backtracking results in a 7x faster parse time on my benchmark document and a 14x faster parse time on the KDL Playground's default document.

bench before x 1,695 ops/sec ±0.38% (99 runs sampled)
bench after x 12,230 ops/sec ±0.62% (96 runs sampled)

play before x 6,355 ops/sec ±0.33% (97 runs sampled)
play after x 88,774 ops/sec ±0.23% (99 runs sampled)
The document used in the benchmark
node \
{
	child; "child"; ###"child"###;
}

deeply { nested { node { but { like { really { deeply { nested { or { whatever; }; }; }; }; }; }; }; }; }

node \
  with="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
  #"no really,"#="a lot" \
	"of" "children" 4.20;
Playground document
foo 1 "two" three=(decimal)0xff {
  (thing)bar #true #false #null
}

This PR changes the way properties and arguments are parsed. The way this is defined in the spec requires unbounded lookahead due to the support for node-space between a property name and the equals sign, and unbounded lookahead only works in chevrotain if you use backtracking.
This PR changes the parser to use a different but equivalent ruleset to parse properties and arguments:

  • Does it start with a tag? It is an argument
  • Is it a non-string value? It is an argument
  • Is it a string value? Check if it's followed by an equals sign.
    • If not, it is an argument
    • If yes, it is a property, check for an optional tag and a value

The propertyOrArgument rule returns whether it encountered trailing node-space, which is then used in the node rule.

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.

1 participant