mirror of https://github.com/sveltejs/svelte
perf: build identifier and member chain expressions without acorn (#18740)
After #18738 the biggest thing left in the parser profile was acorn's constructor, `getOptions` and `wordsRegexp` alone were around 12% of parse time. `parseExpressionAt` is `new Parser()` followed by `parseExpression()`, and we call it for every expression in the template. For something like `{name}` the constructor (normalizing options, four keyword regexes, scope state) is about 2 µs while the actual parse is under 1 µs, and components have dozens of these. The fix seemed to belong in acorn, cache the options and regexes so repeated `parseExpressionAt` calls stop redoing them. I lasted about twenty minutes in acorn's constructor... I was way in over my head. The Svelte alternative was one parser per component, reset between expressions, but that means re-initializing acorn's and acorn-typescript's internals by hand and breaking whenever either adds a field. I realized most of the expressions in the test corpus are an identifier or an `a.b.c` chain, and you don't need a JS parser for those. So that's this fix. `read_expression` now scans for that shape, builds the nodes itself in the form acorn would, and hands everything else to acorn like before.No behavior change, verified byte-for-byte against acorn on the test corpus. About 18% faster parsing on typical components on top of #18738. I guess this should be a fix for acorn, rather than here, so I'm not against closing this. --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> Co-authored-by: Simon Holthausen <simon.holthausen@vercel.com>pull/18751/head
parent
504a7536c6
commit
34142af3a1
@ -0,0 +1,5 @@
|
||||
---
|
||||
'svelte': patch
|
||||
---
|
||||
|
||||
perf: speed up parser interactions with Acorn or avoid them where possible
|
||||
Loading…
Reference in new issue