[Lens] Improve performance for large formulas #141456
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #140875
Formulas are parsed multiple times during their own lifecycle before rendering, but somehow the expression parsing step seems to be very slow (really slow!):
Check in particular the aggregated time of 5,96 seconds (!) for this basic formulas like this:
As noted in the linked issue that formula is rewritten as this new formula:
After this rewriting performances get really bad, probably due to some recursion issue in the peg(gy) library used to generate the parser from the grammar - or rather non idiomatic way the tinymath grammar has been written.
Rewriting the grammar seemed not a trivial task, therefore the way the formula is rewritten has been targeted.
The idea here is to "flatten" the rewritten formula, but only the
add
function accepts more than 2 arguments (i.e.add( arg1, arg2, arg3, arg4, ....)
). The intuition is that the first parsing timings were pretty good, so why not reuse the same technique there?So here's the PR implementation to use the
+
/-
/*
//
math operations instead of their long versions, which proved to be way faster: in fact the grammar is highly optimized to parse those 4 symbols vs the functions counterpart.New implementation is now in line with the monaco editor performance with few ms of parsing time (see overall timing of ~15ms!):
Taking as example the previous formula:
The new rewrite will be something like:
Round brackets in the new rewrite are used to preserve the original explicit grouping (i.e.
sum(bytes) + (sum(bytes) - sum(bytes)) / (sum(bytes) - sum(bytes) * sum(bytes))
) and avoid issues.From user perspective results will appear now few seconds earlier.
Before:
After:
This fix does not affect only single operations cases (as in the issue example), rather any mixed combination of the 4 basic operations:
Note this is only a client side parsing step optimization. For further formulas requests optimization see #140859
Note 2 this fix is not a general solution for nesting performance issue, which has to be tackled at grammar level. A formula like the following will still suffer of performance issues:
pick_min(pick_max(abs(sqrt(log(fix(floor(sum(bytes)))))), 5), 10)
Checklist
Delete any items that are not applicable to this PR.
Risk Matrix
Delete this section if it is not applicable to this PR.
Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release.
When forming the risk matrix, consider some of the following examples and how they may potentially impact the change:
For maintainers