Skip to content

Truly independent web browser

LibWeb: Hash an element's dispatch keys once for all sources

Andreas Klingb74a56fd1569
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/extent_map.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/extent_map.rs
index d3ca344a494..0d345659bcf 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/extent_map.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/extent_map.rs
@@ -16,6 +16,10 @@ use super::shared_pages::{PagePool, SharedPages};
use std::ops::Range;
use std::sync::Arc;
+/// The hash a frozen map looks a key up by, for a caller looking the same key up in many maps.
+#[derive(Clone, Copy)]
+pub(super) struct KeyHash(u64);
+
/// Where one key's list stands in the backing, and how much room it has there.
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
pub(super) struct Extent {
@@ -222,11 +226,11 @@ impl<K, V> Default for FrozenExtentMap<K, V> {
}
impl<K: Copy + Eq + std::hash::Hash, V: Copy> FrozenExtentMap<K, V> {
- fn hash_of(key: &K) -> u64 {
+ pub(super) fn hash_of(key: &K) -> KeyHash {
use std::hash::Hasher;
let mut hasher = super::super::fast_hash::fast_hasher();
key.hash(&mut hasher);
- hasher.finish()
+ KeyHash(hasher.finish())
}
/// The lists of `map`, packed.
@@ -249,7 +253,7 @@ impl<K: Copy + Eq + std::hash::Hash, V: Copy> FrozenExtentMap<K, V> {
};
let mut slots = vec![0_u32; length].into_boxed_slice();
for (number, record) in records.iter().enumerate() {
- let hash = Self::hash_of(&record.key);
+ let KeyHash(hash) = Self::hash_of(&record.key);
let mut slot = hash as usize & (length - 1);
while slots[slot] != 0 {
slot = (slot + 1) & (length - 1);
@@ -267,10 +271,15 @@ impl<K: Copy + Eq + std::hash::Hash, V: Copy> FrozenExtentMap<K, V> {
/// The list of `key`, where the map has it, which may be empty.
#[inline]
pub(super) fn get(&self, key: &K) -> Option<&[V]> {
+ self.get_hashed(key, Self::hash_of(key))
+ }
+
+ /// The list of `key`, whose hash is `hash`, where the map has it, which may be empty.
+ #[inline]
+ pub(super) fn get_hashed(&self, key: &K, KeyHash(hash): KeyHash) -> Option<&[V]> {
if self.slots.is_empty() {
return None;
}
- let hash = Self::hash_of(key);
let mask = self.slots.len() - 1;
let tag = (hash >> (64 - (32 - FROZEN_TAG_SHIFT))) as u32;
let mut slot = hash as usize & mask;
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/matching.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/matching.rs
index 454809cce73..3cc76699715 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/matching.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/matching.rs
@@ -12,6 +12,7 @@ use super::super::input_types::TransactionFactSide;
use super::super::selector_evaluation::StructuralRead;
use super::super::*;
use super::bloom::{AncestorFilter, SiblingFilter};
+use super::extent_map::KeyHash;
use super::projection::{ElementFacts, Projection, ProjectionEvaluator};
use super::rules::{BucketSlots, BucketSource, Buckets, RuleIndex, ScopeGroupID, ScopeIndex};
use crate::css::selector::Specificity;
@@ -468,21 +469,21 @@ impl<'a> Matcher<'a> {
// NB: A tag that is its own ASCII-lowercase form has no folding of its own.
tag_is_lowercase: facts.folded_tag().is_none(),
};
+ // NB: Each source is asked for the same keys, which are read from the element and hashed once.
+ let mut probes: SmallVec<[(DispatchKey, KeyHash); 16]> = SmallVec::new();
+ projection.for_each_dispatch_probe(facts, is_document_root, |key, _| {
+ probes.push((key, Buckets::hash_of(&key)));
+ });
for source in sources {
let buckets = source.buckets.family(family);
if buckets.is_empty() {
continue;
}
- let mut result = Ok(());
- projection.for_each_dispatch_probe(facts, is_document_root, |key, _| {
- if result.is_err() {
- return;
+ for (key, hash) in &probes {
+ if let Some(bucket) = buckets.get_hashed(key, *hash) {
+ self.offer_all(bucket, source, node, reach, keys, out)?;
}
- if let Some(bucket) = buckets.get(&key) {
- result = self.offer_all(bucket, source, node, reach, keys, out);
- }
- });
- result?;
+ }
}
Ok(())
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/rules.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/rules.rs
index 22de1bedb7e..fd221ef3846 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/rules.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/rules.rs
@@ -24,7 +24,7 @@ use super::super::selector_analysis::{EntryFacts, collect_tested_ids, state_disp
use super::super::weak_pool::WeakPool;
use super::super::*;
use super::bloom::AncestorHashes;
-use super::extent_map::{Extent, ExtentMap, FrozenExtentMap, PagedExtentMap};
+use super::extent_map::{Extent, ExtentMap, FrozenExtentMap, KeyHash, PagedExtentMap};
use super::rule_changes::{Regrouped, RuleIndexChanges, StayedEntry};
use super::rule_store::UserAgentVariant;
use super::shared_pages::{PagePool, SharedPages};
@@ -493,6 +493,22 @@ impl Buckets {
words.map(|words| BucketSlots { words })
}
+ /// The hash [`Self::get_hashed`] takes for `key`.
+ #[inline]
+ pub(super) fn hash_of(key: &DispatchKey) -> KeyHash {
+ FrozenExtentMap::<DispatchKey, u32>::hash_of(key)
+ }
+
+ /// The slots of `key`, whose hash is `hash`, where it has any.
+ #[inline]
+ pub(super) fn get_hashed(&self, key: &DispatchKey, hash: KeyHash) -> Option<BucketSlots<'_>> {
+ let words = match self {
+ Self::Growing(map) => map.get(key),
+ Self::Frozen(map) => map.get_hashed(key, hash),
+ };
+ words.map(|words| BucketSlots { words })
+ }
+
pub(super) fn is_empty(&self) -> bool {
match self {
Self::Growing(map) => map.is_empty(),