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

Merged
merged 1 commit into from
Jan 27, 2025
Merged

perf(parser): remove backtracking #25

merged 1 commit into from
Jan 27, 2025

Conversation

bgotink
Copy link
Member

@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.

@larsgw larsgw merged commit e093f8c into kdl-org:main Jan 27, 2025
@larsgw
Copy link
Collaborator

larsgw commented Jan 27, 2025

The benchmark file is now 10.680s instead of ~35s, thank you very much!

@bgotink bgotink deleted the perf branch January 27, 2025 19:29
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.

2 participants