Optimize location info computation
Before this commit, position details (line and column) weren't computed efficiently from the current parse position. There was a cache but it held only one item and it was rarely hit in practice. This resulted in frequent rescanning of the whole input when the |location| function was used in various places in a grammar. This commit extends the cache to remember position details for any position they were ever computed for. In case of a cache miss, the cache is searched for a value corresponding to the nearest lower position, which is then used to compute position info for the desired position (which is then cached). The whole input never needs to be rescanned. No items are ever evicted from the cache. I think this is fine as the max number of entries is the length of the input. If this becomes a problem I can introduce some eviction logic later. The performance impact of this change is significant. As the benchmark suite doesn't contain any grammar with |location| calls I just used a little ad-hoc benchmark script which measured time to parse the grammar of PEG.js itself (which contains |location| calls): var fs = require("fs"), parser = require("./lib/parser"); var grammar = fs.readFileSync("./src/parser.pegjs", "utf-8"), startTime, endTime; startTime = (new Date()).getTime(); parser.parse(grammar); endTime = (new Date()).getTime(); console.log(endTime - startTime); The measured time went from ~293 ms to ~54 ms on my machine. Fixes #337.redux
parent
29bb921994
commit
f2200e48af
Loading…
Reference in New Issue