Open Bug 2005808 Opened 11 months ago Updated 24 days ago

Use querySelector bloom filter for relative selector matching / invalidation.

Categories

(Core :: Layout, enhancement, P3)

enhancement

Tracking

()

People

(Reporter: emilio, Unassigned)

References

(Blocks 1 open bug)

Details

Attachments

(2 obsolete files)

Seems we could compute a hash during parsing of the :has() selector, and early-reject using the bloom filter introduced in bug 1997380.

So.. If I understand correctly:

  1. Generalize hash computation here to work for selectors
  2. Compute the hash using the function from step 1, for any relative selector whose match_hint is subtree, and store the hash in there
  3. During match, if the hash is valid, we'd try element.bloom_may_have_hash(relative_selector_hash)
  4. Make sure to add some comment delineating this bloom filter vs one we already have

That seems pretty straightforward?

Yes, agreed :)

See Also: → 2045461

This could also be useful for descendant / subtree invalidation, see bug 2045461.

Bug 1997380 added a bloom filter on every element summarizing the local
names, class names and null-namespace attribute names of that element and
of all of its DOM descendants, and used it to skip subtrees during
querySelector. It is maintained incrementally by the DOM, so unlike the
filter in relative_selector::filter it costs nothing to consult.

This generalizes the hash computation that querySelector was doing inline
to work on a compound selector, precomputes it for the rightmost compound
of every relative selector, and consults it before the (much more
expensive to build) RelativeSelectorFilterMap.

Since a lookup is (filter & hashes) == hashes, the hashes of the whole
compound can be OR'd into a single u64 and tested in one go. Ids and
namespaces are deliberately left out, as the DOM filter records attribute
names rather than values, and no namespaces at all.

Two things worth a reviewer's attention:

Rejecting here skips RelativeSelectorFilterMap::get_filter entirely, so
the "seen once" marker no longer gets inserted for that element. Any
other selector looking at the same element therefore has its expensive
per-element filter built one lookup later than it used to be. That seemed
an acceptable trade for not building it at all in the common case, but it
is a behaviour change beyond the pure fast path.

This has not been measured. The mechanism is clear enough, but no number
is being claimed for it.

A descendant invalidation is carried down through an entire subtree,
matching its compound against every element on the way. When the compound
names something the subtree provably doesn't contain, all of that work is
wasted: on the testcase in the bug, toggling a class on the root of a
200k node subtree runs eight such invalidations over every element,
none of which can match.

Reuse the subtree filter hashes added for bug 2005808 to reject those
invalidations at the top of the subtree instead. The filter covers the
child and everything below it, so a miss means neither the child nor any
of its descendants can match, and therefore nothing there can generate
further invalidations either.

That last part is only true if the filter covers everything the
invalidator traverses, so propagate it across shadow roots as well:
querySelector doesn't look into those, so for it this only means a few
more false positives, but style invalidation does walk them. Native
anonymous content is already covered, since it is bound with the element
as its DOM parent.

Three things worth a reviewer's attention:

Invalidation grows from 40 to 56 bytes, because the hashes are stored
twice (classes match ASCII case-insensitively in quirks mode while the
DOM hashes them as authored, so the quirks set leaves them out).
DescendantInvalidationLists is three SmallVec<[Invalidation; 10]> and is
default-constructed per invalidate_child, so this costs roughly 480 extra
bytes of stack per level of a recursion that already needs a
StackLimitChecker, plus a wider copy per clone on the path where nothing
gets rejected. It can be brought down to a single u64 by resolving quirks
mode at construction time and threading QuirksMode through the
Invalidation::new callers; that is not done here because the cost is
unmeasured and the plumbing is not obviously worth it.

Crossing shadow roots when propagating weakens the querySelector
optimization bug 1997380 landed, by adding false positives on
shadow-heavy pages. The alternative would be to leave propagation alone
and gate the skip on there being no shadow host anywhere in the subtree,
but there is no cheap signal for that today.

None of this has been measured. What can be said from the source alone is
that #tree-slow in the testcase is already skipped today, since
should_process_descendants bails on is_display_none(), so the reported
time is #tree-fast; and that #tree-fast's filter holds four distinct
atoms, i.e. 8 of 63 bits, so a two-bit query hash is very unlikely to
collide. That is a mechanism, not a number, and no percentage is being
claimed until it is run.

Comment on attachment 9616116 [details]
WIP: Bug 2045461 - Use the DOM subtree filter to skip descendant invalidation subtrees. r?#firefox-style-system-reviewers,#dom-core

Revision D314249 was moved to bug 2045461. Setting attachment 9616116 [details] to obsolete.

Attachment #9616116 - Attachment is obsolete: true
See Also: → 2070760
Attachment #9616068 - Attachment is obsolete: true
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: