servo/components/shared/layout/layout_damage.rs
JoeDow 9d29017c0d
layout: optimize the propagation and cleanup of RestyleDamage (#38199)
This change optimizes `RestyleDamage` propagation and cleanup to reduce
unnecessary box reconstruction and relayouts, while preventing damage
from the current reflow affecting subsequent ones. Improvements include:
- For box damage caused by `NodeDamage`, `RestyleDamage::RELAYOUT` is no
longer marked immediately—avoiding erroneous propagation of
`LayoutDamage::RECOLLECT_BOX_TREE_CHILDREN` to descendants during
`RestyleDamage` propagation.
- Clearing damage for nodes whose boxes will be preserved, preventing it
from carrying over to the next reflow and increasing its workload.

Testing: This should not change observable behavior and is thus covered
by existing WPT tests. Although Servo lacks performance test cases,
manual testing shows that this modification reduces reflow time by
nearly 250ms, representing a decrease of approximately 25%.

Signed-off-by: sharpshooter_pt <ibluegalaxy_taoj@163.com>
2025-07-25 12:10:16 +00:00

52 lines
1.8 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use bitflags::bitflags;
use style::selector_parser::RestyleDamage;
bitflags! {
/// Individual layout actions that may be necessary after restyling. This is an extension
/// of `RestyleDamage` from stylo, which only uses the 4 lower bits.
#[derive(Clone, Copy, Default, Eq, PartialEq)]
pub struct LayoutDamage: u16 {
/// Recollect the box children for this element, because some of the them will be
/// rebuilt.
const RECOLLECT_BOX_TREE_CHILDREN = 0b011111111111 << 4;
/// Rebuild the entire box for this element, which means that every part of layout
/// needs to happena again.
const REBUILD_BOX = 0b111111111111 << 4;
}
}
impl LayoutDamage {
pub fn recollect_box_tree_children() -> RestyleDamage {
RestyleDamage::from_bits_retain(LayoutDamage::RECOLLECT_BOX_TREE_CHILDREN.bits())
}
pub fn rebuild_box_tree() -> RestyleDamage {
RestyleDamage::from_bits_retain(LayoutDamage::REBUILD_BOX.bits())
}
pub fn has_box_damage(&self) -> bool {
self.intersects(Self::REBUILD_BOX)
}
}
impl From<RestyleDamage> for LayoutDamage {
fn from(restyle_damage: RestyleDamage) -> Self {
LayoutDamage::from_bits_retain(restyle_damage.bits())
}
}
impl std::fmt::Debug for LayoutDamage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.contains(Self::REBUILD_BOX) {
f.write_str("REBUILD_BOX")
} else if self.contains(Self::RECOLLECT_BOX_TREE_CHILDREN) {
f.write_str("RECOLLECT_BOX_TREE_CHILDREN")
} else {
f.write_str("EMPTY")
}
}
}