Commit 0965028
perf: optimize CSS selector pruning (#17846)
## Summary
Reduces CSS selector pruning overhead by eliminating unnecessary
allocations and redundant work in the hot path.
**Changes:**
- Replace `.slice()` + `.shift()`/`.pop()` in `apply_selector` with
index-based `from`/`to` params — avoids O(n) array copies per recursive
call
- Merge `has_selectors`/`other_selectors` split in
`relative_selector_might_apply_to_node` into a single pass — eliminates
2 temporary array allocations per call
- Hoist `name.toLowerCase()` out of the inner loop in
`attribute_matches`
- Replace `value.split(/\s/).includes()` with `indexOf` + boundary
checks in `test_attribute` for `~=` — avoids array allocation on every
class match
- Skip `name.replace()` regex when selector name has no backslash
## Benchmark
Interleaved benchmark (5 rounds, alternating baseline/optimized):
```
--- Round 1 ---
Baseline: min=59.58ms median=64.63ms
Optimized: min=47.20ms median=54.35ms
--- Round 2 ---
Baseline: min=59.11ms median=64.90ms
Optimized: min=48.36ms median=54.74ms
--- Round 3 ---
Baseline: min=58.38ms median=64.06ms
Optimized: min=48.38ms median=53.83ms
--- Round 4 ---
Baseline: min=58.49ms median=63.99ms
Optimized: min=48.45ms median=53.82ms
--- Round 5 ---
Baseline: min=58.40ms median=64.07ms
Optimized: min=48.97ms median=54.64ms
Best min: Before=58.38ms After=47.20ms Improvement=19.1%
Best median: Before=63.99ms After=53.82ms Improvement=15.9%
```
CPU profile before → after:
| Function | Before | After |
|---|---|---|
| `relative_selector_might_apply_to_node` | 14.3% | 5.2% |
| `attribute_matches` | 4.0% | 3.3% |
| `test_attribute` | 3.2% | <0.9% |
## Test plan
- [x] All 196 CSS tests pass (180 samples + 16 parse)
- [x] All 31 snapshot tests pass
- [x] All 2380 runtime-runes tests pass
- [x] All 3291 runtime-legacy tests pass
- [x] All 145 compiler-errors tests pass
- [x] All 326 validator tests pass
- [x] Added `css-prune-edge-cases` test covering: `~=` word matching
(substring vs whole word), deep combinator chains (4+ levels), `:has()`
combined with class selectors, escaped selectors,
`:is()`/`:where()`/`:not()` with combinators
- [x] Edge case test passes on both baseline and optimized code
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Rich Harris <rich.harris@vercel.com>1 parent 32111f9 commit 0965028
5 files changed
Lines changed: 347 additions & 50 deletions
File tree
- .changeset
- packages/svelte
- src/compiler/phases/2-analyze/css
- tests/css/samples/css-prune-edge-cases
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 89 additions & 50 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
| 239 | + | |
| 240 | + | |
239 | 241 | | |
240 | 242 | | |
241 | | - | |
242 | | - | |
243 | | - | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
244 | 257 | | |
245 | 258 | | |
246 | | - | |
247 | 259 | | |
248 | | - | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
249 | 269 | | |
250 | 270 | | |
251 | 271 | | |
| |||
260 | 280 | | |
261 | 281 | | |
262 | 282 | | |
263 | | - | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
264 | 286 | | |
265 | 287 | | |
266 | 288 | | |
267 | 289 | | |
268 | 290 | | |
269 | | - | |
| 291 | + | |
270 | 292 | | |
271 | | - | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
272 | 298 | | |
273 | 299 | | |
274 | 300 | | |
| |||
282 | 308 | | |
283 | 309 | | |
284 | 310 | | |
285 | | - | |
| 311 | + | |
286 | 312 | | |
287 | 313 | | |
288 | 314 | | |
| |||
291 | 317 | | |
292 | 318 | | |
293 | 319 | | |
294 | | - | |
| 320 | + | |
295 | 321 | | |
296 | 322 | | |
297 | 323 | | |
| |||
308 | 334 | | |
309 | 335 | | |
310 | 336 | | |
311 | | - | |
| 337 | + | |
312 | 338 | | |
313 | 339 | | |
314 | | - | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
315 | 343 | | |
316 | 344 | | |
317 | 345 | | |
| |||
320 | 348 | | |
321 | 349 | | |
322 | 350 | | |
323 | | - | |
| 351 | + | |
324 | 352 | | |
325 | 353 | | |
326 | 354 | | |
| |||
330 | 358 | | |
331 | 359 | | |
332 | 360 | | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
333 | 375 | | |
334 | 376 | | |
335 | 377 | | |
| |||
392 | 434 | | |
393 | 435 | | |
394 | 436 | | |
395 | | - | |
396 | | - | |
397 | | - | |
| 437 | + | |
| 438 | + | |
398 | 439 | | |
399 | 440 | | |
| 441 | + | |
400 | 442 | | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
425 | 463 | | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
431 | 468 | | |
432 | 469 | | |
433 | 470 | | |
| |||
465 | 502 | | |
466 | 503 | | |
467 | 504 | | |
| 505 | + | |
| 506 | + | |
468 | 507 | | |
469 | | - | |
470 | 508 | | |
471 | | - | |
472 | 509 | | |
473 | 510 | | |
474 | | - | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
475 | 514 | | |
476 | 515 | | |
477 | 516 | | |
| |||
672 | 711 | | |
673 | 712 | | |
674 | 713 | | |
| 714 | + | |
| 715 | + | |
675 | 716 | | |
676 | 717 | | |
677 | 718 | | |
678 | | - | |
679 | | - | |
680 | 719 | | |
681 | 720 | | |
682 | 721 | | |
| |||
Lines changed: 48 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
Lines changed: 80 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
0 commit comments