Skip to content

Commit aea9545

Browse files
author
bors-servo
authored
Auto merge of #13459 - servo:no-arc-heapsize, r=emilio
Use parking_lot::RwLock for PropertyDeclarationBlock <!-- Please describe your changes on the following line: --> As discussed in https://bugzilla.mozilla.org/show_bug.cgi?id=1305141 Closes #13176 --- Original PR title: Stop relying on `impl<T: HeapSizeOf> HeapSizeOf for Arc<T>` servo/heapsize#37 (comment) This builds on top of that. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [ ] These changes fix #__ (github issue number if applicable). <!-- Either: --> - [ ] There are tests for these changes OR - [x] These changes do not require tests because refactor <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> <!-- 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/13459) <!-- Reviewable:end -->
2 parents b772f43 + d15ac9b commit aea9545

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+313
-265
lines changed

components/layout/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ range = {path = "../range"}
3333
rustc-serialize = "0.3"
3434
script_layout_interface = {path = "../script_layout_interface"}
3535
script_traits = {path = "../script_traits"}
36-
selectors = {version = "0.13", features = ["heap_size"]}
36+
selectors = "0.13"
3737
serde_macros = "0.8"
3838
smallvec = "0.1"
3939
string_cache = {version = "0.2.26", features = ["heap_size"]}

components/plugins/lints/unrooted_must_root.rs

-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ fn is_unrooted_ty(cx: &LateContext, ty: &ty::TyS, in_new_function: bool) -> bool
5151
false
5252
} else if match_def_path(cx, did.did, &["core", "cell", "Ref"])
5353
|| match_def_path(cx, did.did, &["core", "cell", "RefMut"])
54-
|| match_def_path(cx, did.did, &["style", "refcell", "Ref"])
55-
|| match_def_path(cx, did.did, &["style", "refcell", "RefMut"])
5654
|| match_def_path(cx, did.did, &["core", "slice", "Iter"])
5755
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "OccupiedEntry"])
5856
|| match_def_path(cx, did.did, &["std", "collections", "hash", "map", "VacantEntry"]) {

components/script/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ net_traits = {path = "../net_traits"}
5151
num-traits = "0.1.32"
5252
offscreen_gl_context = "0.4"
5353
open = "1.1.1"
54+
parking_lot = "0.3"
5455
phf = "0.7.16"
5556
phf_macros = "0.7.16"
5657
plugins = {path = "../plugins"}
@@ -62,7 +63,7 @@ regex = "0.1.43"
6263
rustc-serialize = "0.3"
6364
script_layout_interface = {path = "../script_layout_interface"}
6465
script_traits = {path = "../script_traits"}
65-
selectors = {version = "0.13", features = ["heap_size"]}
66+
selectors = "0.13"
6667
serde = "0.8"
6768
smallvec = "0.1"
6869
string_cache = {version = "0.2.26", features = ["heap_size", "unstable"]}

components/script/body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ use js::jsapi::JS_ParseJSON;
1919
use js::jsapi::Value as JSValue;
2020
use js::jsval::UndefinedValue;
2121
use mime::{Mime, TopLevel, SubLevel};
22+
use std::cell::Ref;
2223
use std::rc::Rc;
2324
use std::str;
24-
use style::refcell::Ref;
2525
use url::form_urlencoded;
2626

2727
pub enum BodyType {

components/script/dom/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use dom::element::{AttributeMutation, Element};
1515
use dom::virtualmethods::vtable_for;
1616
use dom::window::Window;
1717
use std::borrow::ToOwned;
18+
use std::cell::Ref;
1819
use std::mem;
1920
use string_cache::{Atom, Namespace};
2021
use style::attr::{AttrIdentifier, AttrValue};
21-
use style::refcell::Ref;
2222

2323
// https://dom.spec.whatwg.org/#interface-attr
2424
#[dom_struct]

components/style/domrefcell.rs components/script/dom/bindings/cell.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
//! A shareable mutable container for the DOM.
66
7-
use refcell::{BorrowError, BorrowMutError, Ref, RefCell, RefMut};
8-
use thread_state;
7+
use std::cell::{BorrowError, BorrowMutError, Ref, RefCell, RefMut};
8+
use style::thread_state;
99

1010
/// A mutable field in the DOM.
1111
///
1212
/// This extends the API of `core::cell::RefCell` to allow unsafe access in
1313
/// certain situations, with dynamic checking in debug builds.
14-
#[derive(Clone)]
15-
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
14+
#[derive(Clone, PartialEq, Debug, HeapSizeOf)]
1615
pub struct DOMRefCell<T> {
1716
value: RefCell<T>,
1817
}

components/script/dom/bindings/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,8 @@
128128
//! return `Err()` from the method with the appropriate [error value]
129129
//! (error/enum.Error.html).
130130
131-
pub use style::domrefcell as cell;
132-
133131
pub mod callback;
132+
pub mod cell;
134133
pub mod constant;
135134
pub mod conversions;
136135
pub mod error;

components/script/dom/bindings/trace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use cssparser::RGBA;
3535
use devtools_traits::CSSError;
3636
use devtools_traits::WorkerId;
3737
use dom::abstractworker::SharedRt;
38+
use dom::bindings::cell::DOMRefCell;
3839
use dom::bindings::js::{JS, Root};
3940
use dom::bindings::refcounted::{Trusted, TrustedPromise};
4041
use dom::bindings::reflector::{Reflectable, Reflector};
@@ -89,7 +90,6 @@ use std::sync::mpsc::{Receiver, Sender};
8990
use std::time::{SystemTime, Instant};
9091
use string_cache::{Atom, Namespace, QualName};
9192
use style::attr::{AttrIdentifier, AttrValue, LengthOrPercentageOrAuto};
92-
use style::domrefcell::DOMRefCell;
9393
use style::element_state::*;
9494
use style::properties::PropertyDeclarationBlock;
9595
use style::selector_impl::{ElementSnapshot, PseudoElement};

components/script/dom/characterdata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use dom::element::Element;
1919
use dom::node::{Node, NodeDamage};
2020
use dom::processinginstruction::ProcessingInstruction;
2121
use dom::text::Text;
22-
use style::refcell::Ref;
22+
use std::cell::Ref;
2323
use util::opts;
2424

2525
// https://dom.spec.whatwg.org/#characterdata

components/script/dom/cssstyledeclaration.rs

+37-33
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ use dom::bindings::str::DOMString;
1313
use dom::element::Element;
1414
use dom::node::{Node, NodeDamage, window_from_node};
1515
use dom::window::Window;
16+
use parking_lot::RwLock;
1617
use std::ascii::AsciiExt;
17-
use std::slice;
1818
use std::sync::Arc;
1919
use string_cache::Atom;
2020
use style::parser::ParserContextExtraData;
21-
use style::properties::{PropertyDeclaration, Shorthand, Importance};
21+
use style::properties::{Shorthand, Importance};
2222
use style::properties::{is_supported_property, parse_one_declaration, parse_style_attribute};
23-
use style::refcell::Ref;
2423
use style::selector_impl::PseudoElement;
2524

2625
// http://dev.w3.org/csswg/cssom/#the-cssstyledeclaration-interface
@@ -93,7 +92,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
9392
fn Length(&self) -> u32 {
9493
let elem = self.owner.upcast::<Element>();
9594
let len = match *elem.style_attribute().borrow() {
96-
Some(ref declarations) => declarations.declarations.len(),
95+
Some(ref declarations) => declarations.read().declarations.len(),
9796
None => 0,
9897
};
9998
len as u32
@@ -119,43 +118,42 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
119118

120119
// Step 2
121120
if let Some(shorthand) = Shorthand::from_name(&property) {
121+
let style_attribute = owner.style_attribute().borrow();
122+
let style_attribute = if let Some(ref style_attribute) = *style_attribute {
123+
style_attribute.read()
124+
} else {
125+
// shorthand.longhands() is never empty, so with no style attribute
126+
// step 2.2.2 would do this:
127+
return DOMString::new()
128+
};
129+
122130
// Step 2.1
123131
let mut list = vec![];
124132

125133
// Step 2.2
126134
for longhand in shorthand.longhands() {
127135
// Step 2.2.1
128-
let declaration = owner.get_inline_style_declaration(&Atom::from(*longhand));
136+
let declaration = style_attribute.get(longhand);
129137

130138
// Step 2.2.2 & 2.2.3
131139
match declaration {
132-
Some(declaration) => list.push(declaration),
140+
Some(&(ref declaration, _importance)) => list.push(declaration),
133141
None => return DOMString::new(),
134142
}
135143
}
136144

137145
// Step 2.3
138-
// Work around closures not being Clone
139-
#[derive(Clone)]
140-
struct Map<'a, 'b: 'a>(slice::Iter<'a, Ref<'b, (PropertyDeclaration, Importance)>>);
141-
impl<'a, 'b> Iterator for Map<'a, 'b> {
142-
type Item = &'a PropertyDeclaration;
143-
fn next(&mut self) -> Option<Self::Item> {
144-
self.0.next().map(|r| &r.0)
145-
}
146-
}
147-
148146
// TODO: important is hardcoded to false because method does not implement it yet
149147
let serialized_value = shorthand.serialize_shorthand_value_to_string(
150-
Map(list.iter()), Importance::Normal);
148+
list, Importance::Normal);
151149
return DOMString::from(serialized_value);
152150
}
153151

154152
// Step 3 & 4
155-
match owner.get_inline_style_declaration(&property) {
153+
owner.get_inline_style_declaration(&property, |d| match d {
156154
Some(declaration) => DOMString::from(declaration.0.value()),
157155
None => DOMString::new(),
158-
}
156+
})
159157
}
160158

161159
// https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-getpropertypriority
@@ -172,13 +170,18 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
172170
.all(|priority| priority == "important") {
173171
return DOMString::from("important");
174172
}
175-
// Step 3
176173
} else {
177-
if let Some(decl) = self.owner.get_inline_style_declaration(&property) {
178-
if decl.1.important() {
179-
return DOMString::from("important");
174+
// Step 3
175+
return self.owner.get_inline_style_declaration(&property, |d| {
176+
if let Some(decl) = d {
177+
if decl.1.important() {
178+
return DOMString::from("important");
179+
}
180180
}
181-
}
181+
182+
// Step 4
183+
DOMString::new()
184+
})
182185
}
183186

184187
// Step 4
@@ -328,13 +331,14 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
328331
let elem = self.owner.upcast::<Element>();
329332
let style_attribute = elem.style_attribute().borrow();
330333
style_attribute.as_ref().and_then(|declarations| {
331-
declarations.declarations.get(index)
332-
}).map(|&(ref declaration, importance)| {
333-
let mut css = declaration.to_css_string();
334-
if importance.important() {
335-
css += " !important";
336-
}
337-
DOMString::from(css)
334+
declarations.read().declarations.get(index).map(|entry| {
335+
let (ref declaration, importance) = *entry;
336+
let mut css = declaration.to_css_string();
337+
if importance.important() {
338+
css += " !important";
339+
}
340+
DOMString::from(css)
341+
})
338342
})
339343
}
340344

@@ -344,7 +348,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
344348
let style_attribute = elem.style_attribute().borrow();
345349

346350
if let Some(declarations) = style_attribute.as_ref() {
347-
DOMString::from(declarations.to_css_string())
351+
DOMString::from(declarations.read().to_css_string())
348352
} else {
349353
DOMString::new()
350354
}
@@ -366,7 +370,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration {
366370
*element.style_attribute().borrow_mut() = if decl_block.declarations.is_empty() {
367371
None // Step 2
368372
} else {
369-
Some(Arc::new(decl_block))
373+
Some(Arc::new(RwLock::new(decl_block)))
370374
};
371375
element.sync_property_with_attrs_style();
372376
let node = element.upcast::<Node>();

components/script/dom/document.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ use script_traits::UntrustedNodeAddress;
113113
use std::ascii::AsciiExt;
114114
use std::borrow::ToOwned;
115115
use std::boxed::FnBox;
116-
use std::cell::Cell;
116+
use std::cell::{Cell, Ref, RefMut};
117117
use std::collections::HashMap;
118118
use std::collections::hash_map::Entry::{Occupied, Vacant};
119119
use std::default::Default;
@@ -125,7 +125,6 @@ use std::time::{Duration, Instant};
125125
use string_cache::{Atom, QualName};
126126
use style::attr::AttrValue;
127127
use style::context::ReflowGoal;
128-
use style::refcell::{Ref, RefMut};
129128
use style::selector_impl::ElementSnapshot;
130129
use style::str::{split_html_space_chars, str_join};
131130
use style::stylesheets::Stylesheet;
@@ -146,6 +145,14 @@ enum ParserBlockedByScript {
146145
Unblocked,
147146
}
148147

148+
#[derive(JSTraceable, HeapSizeOf)]
149+
#[must_root]
150+
struct StylesheetInDocument {
151+
node: JS<Node>,
152+
#[ignore_heap_size_of = "Arc"]
153+
stylesheet: Arc<Stylesheet>,
154+
}
155+
149156
// https://dom.spec.whatwg.org/#document
150157
#[dom_struct]
151158
pub struct Document {
@@ -174,7 +181,7 @@ pub struct Document {
174181
anchors: MutNullableHeap<JS<HTMLCollection>>,
175182
applets: MutNullableHeap<JS<HTMLCollection>>,
176183
/// List of stylesheets associated with nodes in this document. |None| if the list needs to be refreshed.
177-
stylesheets: DOMRefCell<Option<Vec<(JS<Node>, Arc<Stylesheet>)>>>,
184+
stylesheets: DOMRefCell<Option<Vec<StylesheetInDocument>>>,
178185
/// Whether the list of stylesheets has changed since the last reflow was triggered.
179186
stylesheets_changed_since_reflow: Cell<bool>,
180187
ready_state: Cell<DocumentReadyState>,
@@ -1879,13 +1886,16 @@ impl Document {
18791886
node.get_stylesheet()
18801887
} else {
18811888
None
1882-
}.map(|stylesheet| (JS::from_ref(&*node), stylesheet))
1889+
}.map(|stylesheet| StylesheetInDocument {
1890+
node: JS::from_ref(&*node),
1891+
stylesheet: stylesheet
1892+
})
18831893
})
18841894
.collect());
18851895
};
18861896
}
18871897
self.stylesheets.borrow().as_ref().unwrap().iter()
1888-
.map(|&(_, ref stylesheet)| stylesheet.clone())
1898+
.map(|s| s.stylesheet.clone())
18891899
.collect()
18901900
}
18911901

components/script/dom/dommatrixreadonly.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ use dom::bindings::reflector::{reflect_dom_object, Reflectable, Reflector};
1414
use dom::dommatrix::DOMMatrix;
1515
use dom::dompoint::DOMPoint;
1616
use euclid::{Matrix4D, Point4D, Radians};
17-
use std::cell::Cell;
17+
use std::cell::{Cell, Ref};
1818
use std::f64;
19-
use style::refcell::Ref;
2019

2120
#[dom_struct]
2221
pub struct DOMMatrixReadOnly {

0 commit comments

Comments
 (0)