mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Auto merge of #17504 - bholley:arc_pseudos, r=bholley
Make EagerPseudoStyles use an Arc, not a Box Reviewed in https://bugzilla.mozilla.org/show_bug.cgi?id=1371049 Fixes #17300 <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17504) <!-- Reviewable:end -->
This commit is contained in:
commit
a29261dec1
2 changed files with 48 additions and 18 deletions
|
@ -332,7 +332,7 @@ impl Clone for EagerPseudoCascadeInputs {
|
||||||
impl EagerPseudoCascadeInputs {
|
impl EagerPseudoCascadeInputs {
|
||||||
/// Construct inputs from previous cascade results, if any.
|
/// Construct inputs from previous cascade results, if any.
|
||||||
fn new_from_style(styles: &EagerPseudoStyles) -> Self {
|
fn new_from_style(styles: &EagerPseudoStyles) -> Self {
|
||||||
EagerPseudoCascadeInputs(styles.0.as_ref().map(|styles| {
|
EagerPseudoCascadeInputs(styles.as_array().map(|styles| {
|
||||||
let mut inputs: [Option<CascadeInputs>; EAGER_PSEUDO_COUNT] = Default::default();
|
let mut inputs: [Option<CascadeInputs>; EAGER_PSEUDO_COUNT] = Default::default();
|
||||||
for i in 0..EAGER_PSEUDO_COUNT {
|
for i in 0..EAGER_PSEUDO_COUNT {
|
||||||
inputs[i] = styles[i].as_ref().map(|s| CascadeInputs::new_from_style(s));
|
inputs[i] = styles[i].as_ref().map(|s| CascadeInputs::new_from_style(s));
|
||||||
|
|
|
@ -13,6 +13,7 @@ use properties::longhands::display::computed_value as display;
|
||||||
use rule_tree::StrongRuleNode;
|
use rule_tree::StrongRuleNode;
|
||||||
use selector_parser::{EAGER_PSEUDO_COUNT, PseudoElement, RestyleDamage};
|
use selector_parser::{EAGER_PSEUDO_COUNT, PseudoElement, RestyleDamage};
|
||||||
use shared_lock::{Locked, StylesheetGuards};
|
use shared_lock::{Locked, StylesheetGuards};
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
use stylearc::Arc;
|
use stylearc::Arc;
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
|
@ -98,24 +99,41 @@ impl RestyleData {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A list of styles for eagerly-cascaded pseudo-elements.
|
/// A lazily-allocated list of styles for eagerly-cascaded pseudo-elements.
|
||||||
/// Lazily-allocated.
|
///
|
||||||
#[derive(Debug)]
|
/// We use an Arc so that sharing these styles via the style sharing cache does
|
||||||
pub struct EagerPseudoStyles(pub Option<Box<[Option<Arc<ComputedValues>>; EAGER_PSEUDO_COUNT]>>);
|
/// not require duplicate allocations. We leverage the copy-on-write semantics of
|
||||||
|
/// Arc::make_mut(), which is free (i.e. does not require atomic RMU operations)
|
||||||
|
/// in servo_arc.
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct EagerPseudoStyles(Option<Arc<EagerPseudoArray>>);
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct EagerPseudoArray(EagerPseudoArrayInner);
|
||||||
|
type EagerPseudoArrayInner = [Option<Arc<ComputedValues>>; EAGER_PSEUDO_COUNT];
|
||||||
|
|
||||||
|
impl Deref for EagerPseudoArray {
|
||||||
|
type Target = EagerPseudoArrayInner;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for EagerPseudoArray {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Manually implement `Clone` here because the derived impl of `Clone` for
|
// Manually implement `Clone` here because the derived impl of `Clone` for
|
||||||
// array types assumes the value inside is `Copy`.
|
// array types assumes the value inside is `Copy`.
|
||||||
impl Clone for EagerPseudoStyles {
|
impl Clone for EagerPseudoArray {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
if self.0.is_none() {
|
let mut clone = Self::default();
|
||||||
return EagerPseudoStyles(None)
|
|
||||||
}
|
|
||||||
let self_values = self.0.as_ref().unwrap();
|
|
||||||
let mut values: [Option<Arc<ComputedValues>>; EAGER_PSEUDO_COUNT] = Default::default();
|
|
||||||
for i in 0..EAGER_PSEUDO_COUNT {
|
for i in 0..EAGER_PSEUDO_COUNT {
|
||||||
values[i] = self_values[i].clone();
|
clone[i] = self.0[i].clone();
|
||||||
}
|
}
|
||||||
EagerPseudoStyles(Some(Box::new(values)))
|
clone
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,6 +143,14 @@ impl EagerPseudoStyles {
|
||||||
self.0.is_none()
|
self.0.is_none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Grabs a reference to the list of styles, if they exist.
|
||||||
|
pub fn as_array(&self) -> Option<&EagerPseudoArrayInner> {
|
||||||
|
match self.0 {
|
||||||
|
None => None,
|
||||||
|
Some(ref x) => Some(&x.0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a reference to the style for a given eager pseudo, if it exists.
|
/// Returns a reference to the style for a given eager pseudo, if it exists.
|
||||||
pub fn get(&self, pseudo: &PseudoElement) -> Option<&Arc<ComputedValues>> {
|
pub fn get(&self, pseudo: &PseudoElement) -> Option<&Arc<ComputedValues>> {
|
||||||
debug_assert!(pseudo.is_eager());
|
debug_assert!(pseudo.is_eager());
|
||||||
|
@ -134,7 +160,10 @@ impl EagerPseudoStyles {
|
||||||
/// Returns a mutable reference to the style for a given eager pseudo, if it exists.
|
/// Returns a mutable reference to the style for a given eager pseudo, if it exists.
|
||||||
pub fn get_mut(&mut self, pseudo: &PseudoElement) -> Option<&mut Arc<ComputedValues>> {
|
pub fn get_mut(&mut self, pseudo: &PseudoElement) -> Option<&mut Arc<ComputedValues>> {
|
||||||
debug_assert!(pseudo.is_eager());
|
debug_assert!(pseudo.is_eager());
|
||||||
self.0.as_mut().and_then(|p| p[pseudo.eager_index()].as_mut())
|
match self.0 {
|
||||||
|
None => return None,
|
||||||
|
Some(ref mut arc) => Arc::make_mut(arc)[pseudo.eager_index()].as_mut(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the EagerPseudoStyles has the style for |pseudo|.
|
/// Returns true if the EagerPseudoStyles has the style for |pseudo|.
|
||||||
|
@ -145,9 +174,10 @@ impl EagerPseudoStyles {
|
||||||
/// Sets the style for the eager pseudo.
|
/// Sets the style for the eager pseudo.
|
||||||
pub fn set(&mut self, pseudo: &PseudoElement, value: Arc<ComputedValues>) {
|
pub fn set(&mut self, pseudo: &PseudoElement, value: Arc<ComputedValues>) {
|
||||||
if self.0.is_none() {
|
if self.0.is_none() {
|
||||||
self.0 = Some(Box::new(Default::default()));
|
self.0 = Some(Arc::new(Default::default()));
|
||||||
}
|
}
|
||||||
self.0.as_mut().unwrap()[pseudo.eager_index()] = Some(value);
|
let arr = Arc::make_mut(self.0.as_mut().unwrap());
|
||||||
|
arr[pseudo.eager_index()] = Some(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Inserts a pseudo-element. The pseudo-element must not already exist.
|
/// Inserts a pseudo-element. The pseudo-element must not already exist.
|
||||||
|
@ -158,9 +188,9 @@ impl EagerPseudoStyles {
|
||||||
|
|
||||||
/// Removes a pseudo-element style if it exists, and returns it.
|
/// Removes a pseudo-element style if it exists, and returns it.
|
||||||
pub fn take(&mut self, pseudo: &PseudoElement) -> Option<Arc<ComputedValues>> {
|
pub fn take(&mut self, pseudo: &PseudoElement) -> Option<Arc<ComputedValues>> {
|
||||||
let result = match self.0.as_mut() {
|
let result = match self.0 {
|
||||||
None => return None,
|
None => return None,
|
||||||
Some(arr) => arr[pseudo.eager_index()].take(),
|
Some(ref mut arc) => Arc::make_mut(arc)[pseudo.eager_index()].take(),
|
||||||
};
|
};
|
||||||
let empty = self.0.as_ref().unwrap().iter().all(|x| x.is_none());
|
let empty = self.0.as_ref().unwrap().iter().all(|x| x.is_none());
|
||||||
if empty {
|
if empty {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue