style: Allow printing the rule tree on unit tests.

This commit is contained in:
Emilio Cobos Álvarez 2019-06-04 01:02:28 -04:00
parent 6f1df517e0
commit aad49357c3

View file

@ -11,7 +11,7 @@ use style::error_reporting::{ContextualParseError, ParseErrorReporter};
use style::media_queries::MediaList; use style::media_queries::MediaList;
use style::properties::{longhands, Importance, PropertyDeclaration, PropertyDeclarationBlock}; use style::properties::{longhands, Importance, PropertyDeclaration, PropertyDeclarationBlock};
use style::rule_tree::{CascadeLevel, RuleTree, StrongRuleNode, StyleSource}; use style::rule_tree::{CascadeLevel, RuleTree, StrongRuleNode, StyleSource};
use style::shared_lock::SharedRwLock; use style::shared_lock::{SharedRwLock, StylesheetGuards};
use style::stylesheets::{CssRule, Origin, Stylesheet}; use style::stylesheets::{CssRule, Origin, Stylesheet};
use style::thread_state::{self, ThreadState}; use style::thread_state::{self, ThreadState};
use test::{self, Bencher}; use test::{self, Bencher};
@ -29,18 +29,23 @@ impl ParseErrorReporter for ErrorringErrorReporter {
} }
} }
struct AutoGCRuleTree<'a>(&'a RuleTree); struct AutoGCRuleTree<'a>(&'a RuleTree, &'a SharedRwLock);
impl<'a> AutoGCRuleTree<'a> { impl<'a> AutoGCRuleTree<'a> {
fn new(r: &'a RuleTree) -> Self { fn new(r: &'a RuleTree, lock: &'a SharedRwLock) -> Self {
AutoGCRuleTree(r) AutoGCRuleTree(r, lock)
} }
} }
impl<'a> Drop for AutoGCRuleTree<'a> { impl<'a> Drop for AutoGCRuleTree<'a> {
fn drop(&mut self) { fn drop(&mut self) {
const DEBUG: bool = false;
unsafe { unsafe {
self.0.gc(); self.0.gc();
if DEBUG {
let guard = self.1.read();
self.0.dump_stdout(&StylesheetGuards::same(&guard));
}
assert!( assert!(
::std::thread::panicking() || !self.0.root().has_children_for_testing(), ::std::thread::panicking() || !self.0.root().has_children_for_testing(),
"No rule nodes other than the root shall remain!" "No rule nodes other than the root shall remain!"
@ -49,8 +54,7 @@ impl<'a> Drop for AutoGCRuleTree<'a> {
} }
} }
fn parse_rules(css: &str) -> Vec<(StyleSource, CascadeLevel)> { fn parse_rules(lock: &SharedRwLock, css: &str) -> Vec<(StyleSource, CascadeLevel)> {
let lock = SharedRwLock::new();
let media = Arc::new(lock.wrap(MediaList::empty())); let media = Arc::new(lock.wrap(MediaList::empty()));
let s = Stylesheet::from_str( let s = Stylesheet::from_str(
@ -58,7 +62,7 @@ fn parse_rules(css: &str) -> Vec<(StyleSource, CascadeLevel)> {
ServoUrl::parse("http://localhost").unwrap(), ServoUrl::parse("http://localhost").unwrap(),
Origin::Author, Origin::Author,
media, media,
lock, lock.clone(),
None, None,
Some(&ErrorringErrorReporter), Some(&ErrorringErrorReporter),
QuirksMode::NoQuirks, QuirksMode::NoQuirks,
@ -105,15 +109,16 @@ fn test_insertion_style_attribute(
fn bench_insertion_basic(b: &mut Bencher) { fn bench_insertion_basic(b: &mut Bencher) {
let r = RuleTree::new(); let r = RuleTree::new();
thread_state::initialize(ThreadState::SCRIPT); thread_state::initialize(ThreadState::SCRIPT);
let lock = SharedRwLock::new();
let rules_matched = parse_rules( let rules_matched = parse_rules(
&lock,
".foo { width: 200px; } \ ".foo { width: 200px; } \
.bar { height: 500px; } \ .bar { height: 500px; } \
.baz { display: block; }", .baz { display: block; }",
); );
b.iter(|| { b.iter(|| {
let _gc = AutoGCRuleTree::new(&r); let _gc = AutoGCRuleTree::new(&r, &lock);
for _ in 0..(4000 + 400) { for _ in 0..(4000 + 400) {
test::black_box(test_insertion(&r, rules_matched.clone())); test::black_box(test_insertion(&r, rules_matched.clone()));
@ -126,14 +131,16 @@ fn bench_insertion_basic_per_element(b: &mut Bencher) {
let r = RuleTree::new(); let r = RuleTree::new();
thread_state::initialize(ThreadState::SCRIPT); thread_state::initialize(ThreadState::SCRIPT);
let lock = SharedRwLock::new();
let rules_matched = parse_rules( let rules_matched = parse_rules(
&lock,
".foo { width: 200px; } \ ".foo { width: 200px; } \
.bar { height: 500px; } \ .bar { height: 500px; } \
.baz { display: block; }", .baz { display: block; }",
); );
b.iter(|| { b.iter(|| {
let _gc = AutoGCRuleTree::new(&r); let _gc = AutoGCRuleTree::new(&r, &lock);
test::black_box(test_insertion(&r, rules_matched.clone())); test::black_box(test_insertion(&r, rules_matched.clone()));
}); });
@ -147,22 +154,19 @@ fn bench_expensive_insertion(b: &mut Bencher) {
// This test case tests a case where you style a bunch of siblings // This test case tests a case where you style a bunch of siblings
// matching the same rules, with a different style attribute each // matching the same rules, with a different style attribute each
// one. // one.
let lock = SharedRwLock::new();
let rules_matched = parse_rules( let rules_matched = parse_rules(
&lock,
".foo { width: 200px; } \ ".foo { width: 200px; } \
.bar { height: 500px; } \ .bar { height: 500px; } \
.baz { display: block; }", .baz { display: block; }",
); );
let shared_lock = SharedRwLock::new();
b.iter(|| { b.iter(|| {
let _gc = AutoGCRuleTree::new(&r); let _gc = AutoGCRuleTree::new(&r, &lock);
for _ in 0..(4000 + 400) { for _ in 0..(4000 + 400) {
test::black_box(test_insertion_style_attribute( test::black_box(test_insertion_style_attribute(&r, &rules_matched, &lock));
&r,
&rules_matched,
&shared_lock,
));
} }
}); });
} }
@ -172,14 +176,16 @@ fn bench_insertion_basic_parallel(b: &mut Bencher) {
let r = RuleTree::new(); let r = RuleTree::new();
thread_state::initialize(ThreadState::SCRIPT); thread_state::initialize(ThreadState::SCRIPT);
let lock = SharedRwLock::new();
let rules_matched = parse_rules( let rules_matched = parse_rules(
&lock,
".foo { width: 200px; } \ ".foo { width: 200px; } \
.bar { height: 500px; } \ .bar { height: 500px; } \
.baz { display: block; }", .baz { display: block; }",
); );
b.iter(|| { b.iter(|| {
let _gc = AutoGCRuleTree::new(&r); let _gc = AutoGCRuleTree::new(&r, &lock);
rayon::scope(|s| { rayon::scope(|s| {
for _ in 0..4 { for _ in 0..4 {
@ -203,32 +209,29 @@ fn bench_expensive_insertion_parallel(b: &mut Bencher) {
let r = RuleTree::new(); let r = RuleTree::new();
thread_state::initialize(ThreadState::SCRIPT); thread_state::initialize(ThreadState::SCRIPT);
let lock = SharedRwLock::new();
let rules_matched = parse_rules( let rules_matched = parse_rules(
&lock,
".foo { width: 200px; } \ ".foo { width: 200px; } \
.bar { height: 500px; } \ .bar { height: 500px; } \
.baz { display: block; }", .baz { display: block; }",
); );
let shared_lock = SharedRwLock::new();
b.iter(|| { b.iter(|| {
let _gc = AutoGCRuleTree::new(&r); let _gc = AutoGCRuleTree::new(&r, &lock);
rayon::scope(|s| { rayon::scope(|s| {
for _ in 0..4 { for _ in 0..4 {
s.spawn(|s| { s.spawn(|s| {
for _ in 0..1000 { for _ in 0..1000 {
test::black_box(test_insertion_style_attribute( test::black_box(test_insertion_style_attribute(&r, &rules_matched, &lock));
&r,
&rules_matched,
&shared_lock,
));
} }
s.spawn(|_| { s.spawn(|_| {
for _ in 0..100 { for _ in 0..100 {
test::black_box(test_insertion_style_attribute( test::black_box(test_insertion_style_attribute(
&r, &r,
&rules_matched, &rules_matched,
&shared_lock, &lock,
)); ));
} }
}) })