You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Document selection updates should be display-list only when possible #47392
When clearing flags in step 1 of the flag traversal, record nodes with flags / selection endpoints in some sort of HashMap / HashSet.
When setting flags, only mark flagged nodes as dirty if they previously did not have a flag / previously were not a selection boundary node.
Have ServoLayoutNode::document_selection_in_text_node return a SharedSelection. In order to avoid making a SharedSelection for every single node in the selection, have fully selected nodes share a single SharedSelection.
When the endpoint of the selection changes during flag traversal, reach into its BoxSlot and update the SharedSelection stored on TextRun.
At the end of the flag traversal call self.owner_window().layout().set_needs_new_display_list();. This is redundant when a node gets damaged, but also the redundancy is harmless.
Testing
tests/wpt/mozilla/tests/incremental-layout/layout-phases-layout.html has some layout counting features, but these will not work well with TestDriver which is entirely asynchronous (layouts can be happening in while waiting for the event to complete). Instead, maybe add a new API that counts the amount of box tree / fragment tree layouts after a certain point. Otherwise, for live testing, print in box tree layout / fragment tree layout and ensure that increasing the selection within the boundaries of node isn't triggering them.
record nodes with flags / selection endpoints in some sort of HashMap / HashSet.
This feels like the kind of thing to do in a quick prototype, not in a performant long-term solution. I think it comes from the lack information about changes. Right now we only record a visible_selection_dirty boolean, then Selection::set_flags_for_visible_selection (called for every reflow) only known if it can return early or if it needs to update the entire tree.
I think we could do much better if we had information about what changed. So I traced the callers of Selection::set_visible_selection_dirty to see if they could be made to provide that info:
For range changes in particular (including click-dragging the selection with the mouse), we have available both the previous and new range. Keeping track of them both takes O(1) space and lets us walk exactly the nodes that were selected but aren’t anymore, or weren’t but now are.
For the other categories of changes, there are quite a number cases so maybe ad-hoc code to patch up the flags in every case would be error prone. Instead we might want to keep a slow(er) fallback, maybe the current traverse-the-entire document.
I think we want to avoid maintaining a list of selected nodes during DOM mutation. There are many moments when the DOM is in an indeterminate state and trying to update the flags mid-mutation can cause issues. In addition, performance of DOM mutation will suffer, which is the who;e idea of waiting until layout to commit the selection to flags. As for the suggested approach, it is the one that Chromium takes. It builds two HashSets of Nodes. I think if this works well for Chromium we can consider it good enough for us!
Activity
SimonSapin commentedon Aug 26, 2026
This feels like the kind of thing to do in a quick prototype, not in a performant long-term solution. I think it comes from the lack information about changes. Right now we only record a
visible_selection_dirtyboolean, thenSelection::set_flags_for_visible_selection(called for every reflow) only known if it can return early or if it needs to update the entire tree.I think we could do much better if we had information about what changed. So I traced the callers of
Selection::set_visible_selection_dirtyto see if they could be made to provide that info:The broad categories are:
Rangechanges<slot>assignment changesFor range changes in particular (including click-dragging the selection with the mouse), we have available both the previous and new range. Keeping track of them both takes O(1) space and lets us walk exactly the nodes that were selected but aren’t anymore, or weren’t but now are.
For the other categories of changes, there are quite a number cases so maybe ad-hoc code to patch up the flags in every case would be error prone. Instead we might want to keep a slow(er) fallback, maybe the current traverse-the-entire document.
mrobinson commentedon Aug 26, 2026
I think we want to avoid maintaining a list of selected nodes during DOM mutation. There are many moments when the DOM is in an indeterminate state and trying to update the flags mid-mutation can cause issues. In addition, performance of DOM mutation will suffer, which is the who;e idea of waiting until layout to commit the selection to flags. As for the suggested approach, it is the one that Chromium takes. It builds two
HashSets ofNodes. I think if this works well for Chromium we can consider it good enough for us!SimonSapin commentedon Aug 26, 2026
Oh, interesting re Chromium. I just pushed main...SimonSapin:servo:selection-previous-range to keep track of range changes but we may not need it if we go with the hash sets
script: Don’t mark nodes as dirty if they were and remain selected (s…
script: Update selection paint ranges without relayout (servo#47579)
script: Don’t mark nodes as dirty if they were and remain selected (s…
script: Update selection paint ranges without relayout (servo#47579)
script: Don’t mark nodes as dirty if they were and remain selected (s…
script: Update selection paint ranges without relayout (servo#47579)