Optimize mergeRule selector merging with WeakMap caching#1748
Optimize mergeRule selector merging with WeakMap caching#1748ludofischer merged 1 commit intocssnano:masterfrom
Conversation
|
Some tests are failed, can you look at them? |
|
Ah, I was pretty sure |
0681f8c to
405e970
Compare
|
ok... it was working, but I did a cleanup and ended up removing a flush that was actually necessary 😅, that was tricky to debug I also forgot to update the dts files. They should be updated now |
|
Could you show how you run the benchmark (so we can check this is really faster)? |
Sure, take this as input: @for $i from 1 through 20000 {
.foo-#{$i} {
color: red;
}
}And run the following commands (on my phone currently, I can double check if needed on Monday): Uses default cssnano config. I use |
|
alright, I'm at the PC now. This gist should help https://gist.github.com/Goodwine/3759bf6dd78a180d6153b752a05d00be
npm install
\time -v npm run test |
|
@ludofischer Can you take a look? Thanks |
|
It does seem to speed up the test case considerably. I have benchmarked the cli using hyperfine and these are the results: without the PR: WIth the PR applied: |
ludofischer
left a comment
There was a problem hiding this comment.
The performance improvements are real, on the other hand the code becomes a lot more complicated in my view. @alexander-akait Realistically, I don't have much time to dedicate to cssnano lately, so if you're OK with merging I will defer to your choice.
|
|
||
| if (!ensureCompatibility(selectors, browsers, compatibilityCache)) { | ||
| if (ruleCache.has(ruleA) && ruleCache.has(ruleB)) { | ||
| // Both already validated |
There was a problem hiding this comment.
Is the cache just used to skip the ensureCompatiblity check?
alexander-akait
left a comment
There was a problem hiding this comment.
@ludofischer let's merge and release
|
Thanks! I understand that this makes the implementation more complex. I've tried to add documentation to explain it without overwhelming the code. Ultimately, the problem is due to the expensive getters/setters in PostCSS. If PostCSS were to parse lazily, this wouldn't have been an issue (or at least, not as significant 😅). |
Fixes #1747
In the original bug, I reported that processing the file from my example resulted in 1 minute and 800M to parse 20k rules.
With the performance improvements in this CL, the same code took 7s and 360M
The main gains come from not having to reparse selectors every time, the getter is very expensive