style: Shrink maps if needed after stylist rebuilds

Hashbrown grows a lot sometimes making us waste a lot of memory. Shrink
some of these maps after CascadeData rebuild / stylesheet collection
invalidation.

Differential Revision: https://phabricator.services.mozilla.com/D134716
This commit is contained in:
Emilio Cobos Álvarez 2023-06-06 23:27:20 +02:00 committed by Oriol Brufau
parent f9610e5898
commit fcc55f2156
6 changed files with 122 additions and 17 deletions

View file

@ -11,7 +11,7 @@ use crate::selector_map::{
};
use crate::selector_parser::SelectorImpl;
use crate::AllocErr;
use crate::{Atom, LocalName, Namespace};
use crate::{Atom, LocalName, Namespace, ShrinkIfNeeded};
use selectors::attr::NamespaceConstraint;
use selectors::parser::{Combinator, Component};
use selectors::parser::{Selector, SelectorIter};
@ -237,6 +237,14 @@ impl InvalidationMap {
self.other_attribute_affecting_selectors.clear();
}
/// Shrink the capacity of hash maps if needed.
pub fn shrink_if_needed(&mut self) {
self.class_to_selector.shrink_if_needed();
self.id_to_selector.shrink_if_needed();
self.state_affecting_selectors.shrink_if_needed();
self.other_attribute_affecting_selectors.shrink_if_needed();
}
/// Adds a selector to this `InvalidationMap`. Returns Err(..) to
/// signify OOM.
pub fn note_selector(

View file

@ -18,7 +18,7 @@ use crate::shared_lock::SharedRwLockReadGuard;
use crate::stylesheets::{CssRule, StylesheetInDocument};
use crate::stylesheets::{EffectiveRules, EffectiveRulesIterator};
use crate::values::AtomIdent;
use crate::Atom;
use crate::{Atom, ShrinkIfNeeded};
use crate::LocalName as SelectorLocalName;
use selectors::parser::{Component, LocalName, Selector};
@ -119,6 +119,15 @@ impl StylesheetInvalidationSet {
self.fully_invalid = true;
}
fn shrink_if_needed(&mut self) {
if self.fully_invalid {
return;
}
self.classes.shrink_if_needed();
self.ids.shrink_if_needed();
self.local_names.shrink_if_needed();
}
/// Analyze the given stylesheet, and collect invalidations from their
/// rules, in order to avoid doing a full restyle when we style the document
/// next time.
@ -149,6 +158,8 @@ impl StylesheetInvalidationSet {
}
}
self.shrink_if_needed();
debug!(" > resulting class invalidations: {:?}", self.classes);
debug!(" > resulting id invalidations: {:?}", self.ids);
debug!(