Skip to content

Document selection updates should be display-list only when possible #47392

Closed

Description

@mrobinson

Plan

  1. When clearing flags in step 1 of the flag traversal, record nodes with flags / selection endpoints in some sort of HashMap / HashSet.
  2. When setting flags, only mark flagged nodes as dirty if they previously did not have a flag / previously were not a selection boundary node.
  3. 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.
  4. When the endpoint of the selection changes during flag traversal, reach into its BoxSlot and update the SharedSelection stored on TextRun.
  5. 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.

Activity

  1. SimonSapin commented on Aug 26, 2026

    @SimonSapin
    Member
    1. 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:

    Selection::set_visible_selection_dirty callers:
    	Selection::set_range
    	Selection::collapse_current_range
    	Selection::extend_current_range
    	
    	Range::report_change
    		Range::set_start
    		Range::set_end
    		Range::set_the_start_or_end
    		Range::DeleteContents
    		WeakRangeVec::drain_to_parent
    			Node::moving_steps
    			Node::unbind_from_tree
    		WeakRangeVec::drain_to_preceding_text_sibling
    			Node::Normalize
    		WeakRangeVec::move_to_text_child_at
    			Node::Normalize
    		WeakRangeVec::move_to_following_text_sibling_above
    			Text::SplitText
    		WeakRangeVec::increment_at
    			Text::SplitText
    		WeakRangeVec::map_offset_above
    			WeakRangeVec::increase_above
    				Node::move_fn
    				Node::insert
    			WeakRangeVec::decrease_above
    				Node::live_range_pre_remove_steps
    					Node::remove
    					Node::move_fn
    			WeakRangeVec::replace_code_units
    				CharacterData::ReplaceData
    				CharacterDate::SetData
    	
    	Node::maybe_dirty_visible_selection_for_newly_inserted_nodes
    		Node::insert
    		Node::moving_steps
    			Node::move_fn
    				Node::move_before
    
    	HTMLSlotElement::assign_slottables
    		Element::attribute_mutated
    		Slottable::assign_a_slot
    	
    	Element::attach_shadow
    

    The broad categories are:

    • Range changes
    • DOM tree changes
    • <slot> assignment changes
    • Shadow root changes

    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.

  2. mrobinson commented on Aug 26, 2026

    @mrobinson
    MemberAuthor

    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!

  3. SimonSapin commented on Aug 26, 2026

    @SimonSapin
    Member

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions