You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pegjs still matches [ \t] one char at a time, but it's in a while loop in the ws function.
There doesn't seem to be a way right now to get pegjs to use a /[ \t]+/ regex match instead of repeating /[ \t]/ in a loop.
So... rewriting the grammar slightly to reduce function calls will probably yield a fairly substantial speed increase, and then I'll look at other problems.
I've gotten part of the way to toml-j0.4 performance for the cargo.toml case just by refactoring the grammar.
But there's a large performance gap that's due to toml-node calling line() and column() all the time. I'm not sure what to do about that yet. Options:
Instead of line() and column(), use offset(), which is cheap, and then recompute line/column in the error reporter. Unfortunately, pegjs-0.10 gets rid of offset() and replaces it with location(), which is as expensive as line() and column().
Fold compile into parse, so we don't have to save line/column.
#44 and #45 combined makes toml-node about half the speed of toml-j0.4 for parsing my cargo.toml test case.
Also removing the pegjs --cache option will make toml-node about the same speed as toml-j0.4. I think the pegjs caching is unnecessary for this grammar, but I haven't worked through it fully yet.
There are more things to improve; toml parsing should be about as fast as yaml parsing, and potentially faster.
Activity
kuangyunsheng commentedon Jul 11, 2017
and I try to parse the same toml file with toml-j0.4, it only cost 45 ms.
felix9 commentedon Sep 1, 2017
This looks like it's mostly compile() being slow, and I think the best fix is to rewrite it.
@BinaryMuse do you mind if I rewrite compile.js? I'd probably also add more testing.
BinaryMuse commentedon Sep 1, 2017
I opened an PR here to begin to address this: #42
Some interesting findings so far.
felix9 commentedon Sep 2, 2017
ok, it looks like the grammar is slow.
This is a benchmark on my laptop of parsing a realistic cargo.toml file from the Rust Cargo project:
I modified toml.pegjs to remove all actions, so it's just doing parsing, and it's still worse than toml-j0.4 (which also uses pegjs).
So I'm now going to try to figure out what's slow in the grammar
felix9 commentedon Sep 2, 2017
pegjs doesn't generate very efficient parsers. In particular, every rule is a function call, So rules like:
will call the S function in a loop to satisfy the *.
Also, the pegjs --cache option increases the cost of each S call.
Rules like this are faster:
pegjs still matches
[ \t]one char at a time, but it's in a while loop in the ws function.There doesn't seem to be a way right now to get pegjs to use a
/[ \t]+/regex match instead of repeating/[ \t]/in a loop.So... rewriting the grammar slightly to reduce function calls will probably yield a fairly substantial speed increase, and then I'll look at other problems.
felix9 commentedon Sep 3, 2017
I've gotten part of the way to toml-j0.4 performance for the cargo.toml case just by refactoring the grammar.
But there's a large performance gap that's due to toml-node calling line() and column() all the time. I'm not sure what to do about that yet. Options:
1 seems fairly easy, I think I'll try that first.
faster grammar for BinaryMuse#39
use offset() instead of line()/column() for issue BinaryMuse#39
felix9 commentedon Sep 3, 2017
#44 and #45 combined makes toml-node about half the speed of toml-j0.4 for parsing my cargo.toml test case.
Also removing the pegjs
--cacheoption will make toml-node about the same speed as toml-j0.4. I think the pegjs caching is unnecessary for this grammar, but I haven't worked through it fully yet.There are more things to improve; toml parsing should be about as fast as yaml parsing, and potentially faster.
BinaryMuse commentedon Jul 13, 2026
Closed in #68