Open Bug 2083741 Opened 1 month ago Updated 27 days ago

`:has()` causes a large performance penalty compared to chrome

Categories

(Core :: CSS Parsing and Computation, defect, P3)

Firefox 157
defect

Tracking

()

People

(Reporter: bbouvier, Unassigned, NeedInfo)

References

(Blocks 1 open bug)

Details

Attachments

(1 file)

Attached file Minimal repro —

It seems that using :has() with a conjunction of other CSS conditions causes a performance cliff. You'll find a minimal repro, that shows the slowdown when HAS is set to true, and doesn't show it when HAS is set to false. Time it takes is quadratic based on the number of items in the list (if I double the number of items, time to render is multiplied by 4 or so).

Chrome doesn't show the slowdown when using the :has selector, so I consider it a performance bug in Firefox:

Rows Firefox, :has Firefox, class Chromium, :has Chromium, class
800 73 12–24 9 8
1600 190 45 25 25
3200 790 173 55 43

Observed in the real-world by using the Matrix open-source client sable-next, which uses this patterns for rendering space above a category item in a list.

Blocks: has-issues
Component: Layout → CSS Parsing and Computation

Why using *:has(...)? :'(

Most of the time is spent in invalidation in both cases (not unexpectedly):

What is going on in this particular test-case is that when you toggle .closed, Firefox spends more effort than Chromium trying to prove that things don't change, so we actually match down the sibling and sibling subtrees. :has() is relatively slow to match so that's why it scales more poorly than classes. This is of course not great when ~everything actually changes.

Do you have some examples of that pattern to see what is the code trying to do? In general doing an unbounded sibling selector like that seems unlikely to be what you really want?

Some more realistic comparisons would also be good... Do you have some profiles of the Matrix client?

Flags: needinfo?(bugzilla)

David, do you happen to have other thoughts / something I might've missed? I think in practice we're not spending a lot of time in the relative matching itself.

In this particular case we could try to coalesce the sibling invalidations / prove that we've already gone through the siblings for that particular chain of selectors... But not sure it'd be worth it in the common case. Right now we don't persist any state across top-level invalidation of siblings.

Maybe some sort of structure around here that's shared for all siblings would work for this case, but doesn't seem particularly general... Other ideas?

Severity: -- → S3
Flags: needinfo?(dshin)
Priority: -- → P3

Also, that's flat tree siblings, which are not guaranteed to be dom siblings... So we'd need some checks to avoid sharing it for shadow dom or ~something...

(In reply to Emilio Cobos Álvarez [:emilio] from comment #2)

Do you have some examples of that pattern to see what is the code trying to do? In general doing an unbounded sibling selector like that seems unlikely to be what you really want?

This is the code that triggers the slowness, as far as I can tell. I can't comment on whether it's the best way to achieve what it wants to achieve, my CSS skills being rather limited).

https://git.sable.moe/SableClient/sable-next/src/commit/e0f46c3a1459f0207494495e7ac14f756ae14a45/src/lib/features/sidebar/RoomNav.svelte#L1186-L1190

Some more realistic comparisons would also be good... Do you have some profiles of the Matrix client?

The matrix client can be tried at https://next.sable.moe/
The repro there is:

  • have lots of rooms
  • in the settings, enable "all rooms in the sidebar"
  • from the all rooms screen (house icon, in the sidebar), click the "Rooms" toggle to hide/display unread rooms

Here's a repro saved in the profiler: https://share.firefox.dev/4iiMLHN

Flags: needinfo?(bugzilla)

Hmm, that profile has no native symbols... Oh, is Zen rather than Firefox.

have lots of rooms

How many out of curiosity? document.querySelectorAll(".room-list > .room-row-wrap").length says ~500 here and I see some long restyles, but just want to make a sense of how many do you have: https://share.firefox.dev/4yJe1Fu

from the all rooms screen (house icon, in the sidebar), click the "Rooms" toggle to hide/display unread rooms

What toggle? I don't see it... You mean the Rooms / Favourites dropdowns? Hopefully the profile above catches it.

Hmm, that profile has no native symbols... Oh, is Zen rather than Firefox.

Here's one saved in Firefox: https://share.firefox.dev/4iKqoMT

How many out of curiosity?

Got ~700 here.

What toggle? I don't see it... You mean the Rooms / Favourites dropdowns? Hopefully the profile above catches it.

Yes, the Rooms dropdown in particular, at the top of the room list: https://bouvier.cc/pub/roomlist.png

See Also: → 2086739

Hm. Feel like the :has bloom filter should help here? I guess we get rid of it between the mutations? So in that sense, coalescing would help.

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: