Skip to content

Truly independent web browser

LibWeb: Take an element's untouched groups without walking the sets

Andreas Kling21c1fcd527f0
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rs
index e924e3ea09a..1e952684238 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs
@@ -2032,17 +2032,22 @@ pub(crate) struct BorrowedGroups(u32);
impl BorrowedGroups {
pub(crate) const NONE: Self = Self(0);
- /// Every group of a set that `takes_as_it_is` answers true for.
- pub(crate) fn of_groups_where(takes_as_it_is: impl Fn(usize) -> bool) -> Self {
- let set_groups = INHERITED_SET_GROUPS
- .iter()
- .chain(&RARE_SET_GROUPS)
- .chain(&VISUAL_SET_GROUPS);
- Self(
- set_groups
- .filter(|&&group| takes_as_it_is(group))
- .fold(0_u32, |mask, group| mask | 1 << group),
- )
+ /// Adds `group` where it is a group of a set, answering whether it is.
+ pub(crate) fn note_if_of_a_set(&mut self, group: usize) -> bool {
+ const SET_GROUPS: u32 = {
+ let mut groups = (1_u32 << group_index::COUNT) - 1;
+ let mut slot = 0;
+ while slot < ROOT_GROUPS.len() {
+ groups &= !(1 << ROOT_GROUPS[slot]);
+ slot += 1;
+ }
+ groups
+ };
+ let is_of_a_set = SET_GROUPS & (1 << group) != 0;
+ if is_of_a_set {
+ self.0 |= 1 << group;
+ }
+ is_of_a_set
}
pub(crate) fn contains(self, group: usize) -> bool {
@@ -2269,7 +2274,20 @@ impl StyleGroups {
/// The groups, in group order.
pub(crate) fn pointers(&self) -> [*const c_void; group_index::COUNT] {
- std::array::from_fn(|group| self[group].as_ptr())
+ let mut pointers = [std::ptr::null(); group_index::COUNT];
+ for (&group, pointer) in ROOT_GROUPS.iter().zip(&self.root) {
+ pointers[group] = pointer.as_ptr();
+ }
+ for (&group, pointer) in INHERITED_SET_GROUPS.iter().zip(&self.inherited.get().groups) {
+ pointers[group] = pointer.as_ptr();
+ }
+ for (&group, pointer) in RARE_SET_GROUPS.iter().zip(&self.rare.get().groups) {
+ pointers[group] = pointer.as_ptr();
+ }
+ for (&group, pointer) in VISUAL_SET_GROUPS.iter().zip(&self.visual.get().groups) {
+ pointers[group] = pointer.as_ptr();
+ }
+ pointers
}
/// The custom properties the element holds.
@@ -4983,6 +5001,7 @@ pub(crate) struct GroupValues<'a> {
impl GroupValues<'_> {
/// Whether the values of `group` are those of `payload` itself, a seed the element holds as it
/// is, which taking the values into it leaves.
+ #[inline]
pub(crate) fn hold_the_seed(&self, group: usize, payload: *const c_void) -> bool {
self.written_groups & (1 << group) == 0 && self.seeds.pointers[group] == payload
}
@@ -5303,14 +5322,12 @@ impl<'a> GroupValuesWriter<'a> {
use crate::css::property_metadata::property_is_inherited;
// NB: A group's longhands all inherit or all do not, so a child borrows its parent's group of
// those that do and the initial group of those that do not.
+ let mut pointers = initial.groups.pointers();
+ for (&group, pointer) in INHERITED_SET_GROUPS.iter().zip(&parent.groups.inherited.get().groups) {
+ pointers[group] = pointer.as_ptr();
+ }
let seeded = SeedGroups {
- pointers: std::array::from_fn(|group| {
- match INHERITED_SET_GROUPS.contains(&group) {
- true => parent.group(group),
- false => initial.group(group),
- }
- .as_ptr()
- }),
+ pointers,
holds_references: false,
styles: std::marker::PhantomData,
};
@@ -7551,7 +7568,10 @@ mod tests {
unsafe { clone_group_payload(group, defaults.groups()[group].as_ptr()) }.cast_const()
});
let before = references(&held);
- let borrowed = BorrowedGroups::of_groups_where(|group| INHERITED_SET_GROUPS.contains(&group));
+ let mut borrowed = BorrowedGroups::default();
+ for &group in &INHERITED_SET_GROUPS {
+ borrowed.note_if_of_a_set(group);
+ }
// A computation thrown away releases the references it took, and none of the groups it
// borrowed.
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
index 436ffcbe058..a2903f435b6 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
@@ -1109,16 +1109,10 @@ impl RetainedState {
// NB: A group that is not built and whose values are the shared group's own is the parent's
// or the initial one, taken without a reference of its own: a record sharing its set
// needs none, and one that does not counts it as it makes its own set.
- let shared_payload = |group: usize| match group < ENGINE_INHERITED_GROUP_COUNT {
- true => parent_view
- .as_ref()
- .map_or(std::ptr::null(), |view| view.payloads[group].as_ptr()),
- false => initial.payloads[group].as_ptr(),
- };
- let borrowed = crate::css::computed_values::BorrowedGroups::of_groups_where(|group| {
- let shared = shared_payload(group);
- groups & (1 << group) == 0 && !shared.is_null() && values.hold_the_seed(group, shared)
- });
+ let parent_payloads = parent_view
+ .as_ref()
+ .map_or([std::ptr::null(); group_index::COUNT], |view| view.payloads.pointers());
+ let initial_payloads = initial.payloads.pointers();
// The lookups of the groups assembled anew, which the cache takes the groups for once they
// hold their values.
let mut assembled: smallvec::SmallVec<[GroupLookup<'_>; 4]> = smallvec::SmallVec::new();
@@ -1126,10 +1120,9 @@ impl RetainedState {
// The groups the cache had, which hold the element's values.
let mut found_holding = 0_u32;
let mut payloads = [std::ptr::null(); group_index::COUNT];
+ let mut borrowed = crate::css::computed_values::BorrowedGroups::default();
for (group, payload) in payloads.iter_mut().enumerate() {
- let parent_payload = parent_view
- .as_ref()
- .map_or(std::ptr::null(), |view| view.payloads[group].as_ptr());
+ let parent_payload = parent_payloads[group];
*payload = if groups & (1 << group) != 0 {
let key = (group != group_index::FONT && group_cache.worth_looking_for(group)).then(|| {
// A group holding plain `currentcolor` for its reader is the same whatever the
@@ -1195,24 +1188,18 @@ impl RetainedState {
} else {
let shared = match group < ENGINE_INHERITED_GROUP_COUNT && !parent_payload.is_null() {
true => parent_payload,
- false => initial.payloads[group].as_ptr(),
+ false => initial_payloads[group],
};
- if !borrowed.contains(group) {
- crate::css::computed_values::retain_group_payload(group, shared);
+ match values.hold_the_seed(group, shared) && borrowed.note_if_of_a_set(group) {
+ true => {}
+ false => crate::css::computed_values::retain_group_payload(group, shared),
}
shared
};
}
- let before = payloads;
// SAFETY: Each payload is a live payload of its group holding a reference for the element,
// but the borrowed ones, whose values are the very values the element holds.
unsafe { values.put_into(&mut payloads, found_holding) };
- for group in (0..group_index::COUNT).filter(|&group| borrowed.contains(group)) {
- assert_eq!(
- payloads[group], before[group],
- "a borrowed group holds the element's values"
- );
- }
if self.process_style.verifies_reads {
Self::verify_value_built_groups(
&values,
diff --git a/Libraries/LibWeb/Rust/src/css/style_compute.rs b/Libraries/LibWeb/Rust/src/css/style_compute.rs
index 363418e1ade..7977589a9a8 100644
--- a/Libraries/LibWeb/Rust/src/css/style_compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_compute.rs
@@ -5515,8 +5515,8 @@ pub(crate) fn value_computes_directly(
}
// The background layer longhands coordinate with the number of background-image layers,
// which is the identity only where every one of them holds a single layer.
- if (property_id == prop::BACKGROUND_IMAGE || BACKGROUND_LAYER_LONGHANDS.contains(&property_id))
- && matches!(data, StyleValueData::ValueList { values, .. } if values.as_slice().len() != 1)
+ if matches!(data, StyleValueData::ValueList { values, .. } if values.as_slice().len() != 1)
+ && (property_id == prop::BACKGROUND_IMAGE || BACKGROUND_LAYER_LONGHANDS.contains(&property_id))
{
return false;
}