Repository navigation
Commit 0b5c4e9
authored
perf(css): scope duplicate rules, and reuse interned names and minified style attributes (#21832)
* perf: reuse interned names and minified style attributes across a print
Three per-invocation costs on the HTML and CSS hot paths, none of which
changes a byte of output — the whole wpt corpus (49,571 documents, minified
and beautified) hashes identically before and after.
Keep the name intern memo warm across parses. Only an unknown name's entry
is a slice of the document that asked for it; one naming a known element is
a shared constant, so clearing all 1,024 slots per parse threw away a warm
cache to drop the few that needed dropping. Tracking the source-backed slots
takes the clear from 1,024 entries to 0.7 per parse and the share of intern
calls reaching the hash walk from 39.4% to 6.5% (520,442 fewer walks).
Memoize a `style` attribute's minified text for the print. Minifying one is
a pure function of the value and the CSS options, which are fixed for the
print, and 41.8% of the corpus's style attributes repeat inside their own
document — so the CSS minifier runs once per distinct value instead of once
per attribute. Dropped in the teardown with the input it holds slices of.
Reset only the stream frames a parse used. Nesting is one level deep in
almost every sheet, but the teardown cleared all 64 slots of six arrays.
Measured over the corpus, interleaved, n=9: HTML minification ~9% faster,
beautification ~8% (which reaches no CSS, so it is the interning alone).
* perf(css): scope duplicate rules instead of re-keying them by their whole nesting
Finding a rule an identical later one makes dead was 17% of minifying a
stylesheet, and only a seventh of that was the cutting. The rest was the
bookkeeping: a rule's key was the text it printed to with every enclosing
opener glued in front, rebuilt at each level it travelled outward through, and
every one of them kept in a single sheet-wide map.
A rule is now identified by the pair it always was - what encloses it, and the
text it printed to - rather than by those two joined. Each set of enclosing
conditions gets a scope holding its own rules by their text, and a span carries
a reference to one rather than a string. Travelling outward replaces the
reference; neither the text nor a scope's prefix is ever rewritten.
Scopes are canonical by their joined prefix, so the same conditions reached a
level at a time and reached in a single step are the one scope - which is what
lets the collected and the streamed path still agree on what a duplicate is.
The prefix is joined once per block, memoized on the scope it extends.
Over the 14 framework stylesheets `yarn benchmark:css-minifiers` runs on
(6.72 MB): 34,020 string joins become 851, key hashing drops from 6.8M to 5.3M
characters, and the sheet-wide map of long keys is gone. Dedup's own cost falls
from 138 ms to 68 ms and minifying them from 786 ms to 748 ms; measured as ten
alternating single-process pairs, all ten favour this, median ~7%, against a
before-vs-before control centred on zero. Parsing and beautifying are unchanged,
beautifying being the control that reaches no dedup at all.
Output is byte-identical over 43,587 inputs - those stylesheets, every wpt
style body and style attribute, and the wpt css corpus - in both print modes.
* fix(html): note a name memo slot once, however many names land in it
The list of slots to drop before the next parse grew by one entry per unknown
name written, not per slot dirtied — so a document of custom elements or
data-* attributes carried a list far longer than the 512 slots it can possibly
have dirtied: 50,000 custom elements left over 10,000 entries where at most
512 are reachable. Marking a slot when it first enters the list bounds it, and
the flags are cleared with the slots they stand for rather than all at once,
so a parse still pays for what it dirtied.
Output is unchanged over the whole wpt corpus in both print modes.
* test(html): repeat the raw value the style-attribute memo is keyed on
The case named for a repeated `style` attribute wrote two different values, so
the second never found the first and the memoized return was never taken —
instrumenting the cache counts 0 hits as it was written and 1 as it is now.
Also brings the changeset entry back under 80 characters.
* test(html): dirty every memo slot, not five of them
A slot is picked by a name's first character and length, so 20,000 names all
starting `x-el` reached about five of the 512 — leaving the branch that drops
the memo whole unrun, which is the patch coverage the report flagged. Names of
512 consecutive lengths reach every slot; the region is covered with them.
* fix(test): type the memo test's walk, and shorten two comments
`lint:types-test` reads `test/` with `noImplicitAny`, and the walk added with
the memo test annotated neither its accumulator nor its parameter. Types them
the way the file's other refs are typed, and brings the two comments the same
commits added down to two lines.
* fix(html): never memoize a caller's renderer for a style attribute
`renderEmbeddedSource` is handed every `style=""` a document holds, so what it
returns is not a function of the value alone — a renderer keeping a counter, or
reading anything outside the text, gets called once for a value the document
repeats and its first answer stands in for the rest. The memo now covers only
the built-in minifier, which is a pure function of the value and the options.
The two paths only met when this branch rebased onto the renderer, so nothing
failed: instrumenting the cache counts one renderer call for two equal
attributes before, and two after.1 parent 8750c1f commit 0b5c4e9
5 files changed
Lines changed: 307 additions & 28 deletions
File tree
- .changeset
- lib
- css
- html
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4279 | 4279 | | |
4280 | 4280 | | |
4281 | 4281 | | |
| 4282 | + | |
| 4283 | + | |
| 4284 | + | |
4282 | 4285 | | |
4283 | 4286 | | |
4284 | 4287 | | |
| |||
4326 | 4329 | | |
4327 | 4330 | | |
4328 | 4331 | | |
4329 | | - | |
| 4332 | + | |
| 4333 | + | |
| 4334 | + | |
| 4335 | + | |
| 4336 | + | |
| 4337 | + | |
| 4338 | + | |
| 4339 | + | |
4330 | 4340 | | |
4331 | 4341 | | |
4332 | 4342 | | |
| |||
4362 | 4372 | | |
4363 | 4373 | | |
4364 | 4374 | | |
4365 | | - | |
4366 | | - | |
4367 | | - | |
4368 | | - | |
| 4375 | + | |
| 4376 | + | |
| 4377 | + | |
| 4378 | + | |
| 4379 | + | |
| 4380 | + | |
| 4381 | + | |
| 4382 | + | |
| 4383 | + | |
| 4384 | + | |
| 4385 | + | |
| 4386 | + | |
| 4387 | + | |
| 4388 | + | |
| 4389 | + | |
| 4390 | + | |
| 4391 | + | |
| 4392 | + | |
| 4393 | + | |
| 4394 | + | |
| 4395 | + | |
| 4396 | + | |
| 4397 | + | |
| 4398 | + | |
| 4399 | + | |
| 4400 | + | |
| 4401 | + | |
| 4402 | + | |
| 4403 | + | |
| 4404 | + | |
| 4405 | + | |
| 4406 | + | |
| 4407 | + | |
| 4408 | + | |
| 4409 | + | |
| 4410 | + | |
| 4411 | + | |
| 4412 | + | |
| 4413 | + | |
| 4414 | + | |
| 4415 | + | |
| 4416 | + | |
| 4417 | + | |
| 4418 | + | |
| 4419 | + | |
| 4420 | + | |
| 4421 | + | |
| 4422 | + | |
| 4423 | + | |
| 4424 | + | |
| 4425 | + | |
| 4426 | + | |
| 4427 | + | |
| 4428 | + | |
| 4429 | + | |
| 4430 | + | |
4369 | 4431 | | |
4370 | 4432 | | |
4371 | 4433 | | |
| |||
4838 | 4900 | | |
4839 | 4901 | | |
4840 | 4902 | | |
| 4903 | + | |
4841 | 4904 | | |
4842 | 4905 | | |
4843 | 4906 | | |
| |||
4934 | 4997 | | |
4935 | 4998 | | |
4936 | 4999 | | |
4937 | | - | |
| 5000 | + | |
4938 | 5001 | | |
4939 | 5002 | | |
4940 | | - | |
| 5003 | + | |
| 5004 | + | |
| 5005 | + | |
4941 | 5006 | | |
4942 | 5007 | | |
4943 | | - | |
| 5008 | + | |
| 5009 | + | |
4944 | 5010 | | |
4945 | 5011 | | |
4946 | 5012 | | |
| |||
4988 | 5054 | | |
4989 | 5055 | | |
4990 | 5056 | | |
4991 | | - | |
4992 | 5057 | | |
4993 | 5058 | | |
4994 | 5059 | | |
4995 | | - | |
| 5060 | + | |
4996 | 5061 | | |
4997 | 5062 | | |
4998 | 5063 | | |
4999 | | - | |
| 5064 | + | |
| 5065 | + | |
5000 | 5066 | | |
5001 | | - | |
| 5067 | + | |
5002 | 5068 | | |
5003 | 5069 | | |
5004 | 5070 | | |
| |||
5483 | 5549 | | |
5484 | 5550 | | |
5485 | 5551 | | |
5486 | | - | |
5487 | | - | |
5488 | | - | |
5489 | | - | |
5490 | | - | |
5491 | | - | |
| 5552 | + | |
| 5553 | + | |
| 5554 | + | |
| 5555 | + | |
| 5556 | + | |
| 5557 | + | |
| 5558 | + | |
| 5559 | + | |
5492 | 5560 | | |
5493 | | - | |
| 5561 | + | |
| 5562 | + | |
5494 | 5563 | | |
5495 | 5564 | | |
5496 | 5565 | | |
| |||
12690 | 12759 | | |
12691 | 12760 | | |
12692 | 12761 | | |
12693 | | - | |
| 12762 | + | |
12694 | 12763 | | |
12695 | 12764 | | |
12696 | 12765 | | |
12697 | 12766 | | |
12698 | 12767 | | |
12699 | | - | |
| 12768 | + | |
| 12769 | + | |
12700 | 12770 | | |
12701 | 12771 | | |
12702 | 12772 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5418 | 5418 | | |
5419 | 5419 | | |
5420 | 5420 | | |
5421 | | - | |
| 5421 | + | |
5422 | 5422 | | |
5423 | 5423 | | |
5424 | 5424 | | |
| |||
5441 | 5441 | | |
5442 | 5442 | | |
5443 | 5443 | | |
5444 | | - | |
| 5444 | + | |
| 5445 | + | |
| 5446 | + | |
| 5447 | + | |
| 5448 | + | |
| 5449 | + | |
| 5450 | + | |
| 5451 | + | |
| 5452 | + | |
| 5453 | + | |
5445 | 5454 | | |
5446 | 5455 | | |
5447 | 5456 | | |
5448 | 5457 | | |
| 5458 | + | |
| 5459 | + | |
| 5460 | + | |
| 5461 | + | |
| 5462 | + | |
| 5463 | + | |
| 5464 | + | |
| 5465 | + | |
| 5466 | + | |
| 5467 | + | |
| 5468 | + | |
| 5469 | + | |
| 5470 | + | |
| 5471 | + | |
| 5472 | + | |
| 5473 | + | |
| 5474 | + | |
| 5475 | + | |
| 5476 | + | |
| 5477 | + | |
| 5478 | + | |
| 5479 | + | |
| 5480 | + | |
| 5481 | + | |
5449 | 5482 | | |
5450 | 5483 | | |
5451 | 5484 | | |
| |||
5491 | 5524 | | |
5492 | 5525 | | |
5493 | 5526 | | |
| 5527 | + | |
| 5528 | + | |
| 5529 | + | |
| 5530 | + | |
5494 | 5531 | | |
5495 | 5532 | | |
5496 | 5533 | | |
| |||
8428 | 8465 | | |
8429 | 8466 | | |
8430 | 8467 | | |
8431 | | - | |
8432 | | - | |
8433 | | - | |
8434 | | - | |
| 8468 | + | |
| 8469 | + | |
| 8470 | + | |
| 8471 | + | |
8435 | 8472 | | |
8436 | 8473 | | |
8437 | 8474 | | |
| |||
9176 | 9213 | | |
9177 | 9214 | | |
9178 | 9215 | | |
| 9216 | + | |
| 9217 | + | |
| 9218 | + | |
| 9219 | + | |
| 9220 | + | |
9179 | 9221 | | |
9180 | 9222 | | |
9181 | 9223 | | |
| |||
9487 | 9529 | | |
9488 | 9530 | | |
9489 | 9531 | | |
| 9532 | + | |
| 9533 | + | |
| 9534 | + | |
| 9535 | + | |
| 9536 | + | |
| 9537 | + | |
| 9538 | + | |
| 9539 | + | |
| 9540 | + | |
| 9541 | + | |
| 9542 | + | |
| 9543 | + | |
| 9544 | + | |
| 9545 | + | |
| 9546 | + | |
| 9547 | + | |
| 9548 | + | |
| 9549 | + | |
| 9550 | + | |
| 9551 | + | |
9490 | 9552 | | |
9491 | 9553 | | |
9492 | 9554 | | |
| |||
12186 | 12248 | | |
12187 | 12249 | | |
12188 | 12250 | | |
| 12251 | + | |
| 12252 | + | |
12189 | 12253 | | |
12190 | 12254 | | |
12191 | 12255 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6750 | 6750 | | |
6751 | 6751 | | |
6752 | 6752 | | |
| 6753 | + | |
| 6754 | + | |
| 6755 | + | |
| 6756 | + | |
| 6757 | + | |
| 6758 | + | |
| 6759 | + | |
| 6760 | + | |
| 6761 | + | |
| 6762 | + | |
| 6763 | + | |
| 6764 | + | |
| 6765 | + | |
| 6766 | + | |
| 6767 | + | |
| 6768 | + | |
| 6769 | + | |
| 6770 | + | |
| 6771 | + | |
| 6772 | + | |
| 6773 | + | |
| 6774 | + | |
| 6775 | + | |
| 6776 | + | |
| 6777 | + | |
| 6778 | + | |
| 6779 | + | |
| 6780 | + | |
| 6781 | + | |
| 6782 | + | |
| 6783 | + | |
| 6784 | + | |
| 6785 | + | |
| 6786 | + | |
| 6787 | + | |
| 6788 | + | |
| 6789 | + | |
| 6790 | + | |
| 6791 | + | |
| 6792 | + | |
| 6793 | + | |
| 6794 | + | |
| 6795 | + | |
| 6796 | + | |
0 commit comments