Bug 1768905 - Move size of tests to compile-time tests in the style crate. r=dshin

Same reasoning as the previous commit.

Differential Revision: https://phabricator.services.mozilla.com/D146104
This commit is contained in:
Emilio Cobos Álvarez 2022-05-11 19:02:58 +00:00
Родитель 2cf5c869e6
Коммит 96360cdae3
17 изменённых файлов: 54 добавлений и 165 удалений

18
Cargo.lock сгенерированный
Просмотреть файл

@ -4749,10 +4749,6 @@ version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba1eead9e94aa5a2e02de9e7839f96a007f686ae7a1d57c7797774810d24908a"
[[package]]
name = "size_of_test"
version = "0.0.1"
[[package]]
name = "slab"
version = "0.4.5"
@ -4959,22 +4955,8 @@ dependencies = [
name = "stylo_tests"
version = "0.0.1"
dependencies = [
"atomic_refcell",
"cssparser",
"cstr",
"env_logger",
"euclid",
"geckoservo",
"libc",
"log",
"malloc_size_of",
"num-traits",
"selectors",
"size_of_test",
"smallvec",
"style",
"style_traits",
"to_shmem",
]
[[package]]

Просмотреть файл

@ -1,9 +0,0 @@
[package]
name = "size_of_test"
version = "0.0.1"
authors = ["The Servo Project Developers"]
license = "MPL-2.0"
publish = false
[lib]
path = "lib.rs"

Просмотреть файл

@ -1,34 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#[macro_export]
macro_rules! size_of_test {
($testname: ident, $t: ty, $expected_size: expr) => {
#[test]
fn $testname() {
let new = ::std::mem::size_of::<$t>();
let old = $expected_size;
if new < old {
panic!(
"Your changes have decreased the stack size of {} from {} to {}. \
Good work! Please update the expected size in {}.",
stringify!($t),
old,
new,
file!()
)
} else if new > old {
panic!(
"Your changes have increased the stack size of {} from {} to {}. \
Please consider choosing a design which avoids this increase. \
If you feel that the increase is necessary, update the size in {}.",
stringify!($t),
old,
new,
file!()
)
}
}
};
}

Просмотреть файл

@ -208,3 +208,6 @@ impl ApplicableDeclarationBlock {
(self.source, self.cascade_priority)
}
}
// Size of this struct determines sorting and selector-matching performance.
size_of_test!(ApplicableDeclarationBlock, 24);

Просмотреть файл

@ -159,6 +159,9 @@ pub struct ElementStyles {
pub pseudos: EagerPseudoStyles,
}
// There's one of these per rendered elements so it better be small.
size_of_test!(ElementStyles, 16);
impl ElementStyles {
/// Returns the primary style.
pub fn get_primary(&self) -> Option<&Arc<ComputedValues>> {
@ -249,6 +252,9 @@ pub struct ElementData {
pub flags: ElementDataFlags,
}
// There's one of these per rendered elements so it better be small.
size_of_test!(ElementData, 24);
/// The kind of restyle that a single element should do.
#[derive(Debug)]
pub enum RestyleKind {

Просмотреть файл

@ -445,3 +445,9 @@ unsafe impl HasFFI for SelectorList<SelectorImpl> {
}
unsafe impl HasSimpleFFI for SelectorList<SelectorImpl> {}
unsafe impl HasBoxFFI for SelectorList<SelectorImpl> {}
// Selector and component sizes are important for matching performance.
size_of_test!(selectors::parser::Selector<SelectorImpl>, 8);
size_of_test!(selectors::parser::Component<SelectorImpl>, 24);
size_of_test!(PseudoElement, 16);
size_of_test!(NonTSPseudoClass, 16);

Просмотреть файл

@ -128,3 +128,11 @@ macro_rules! local_name {
$crate::values::AtomIdent(atom!($s))
};
}
/// Asserts the size of a type at compile time.
macro_rules! size_of_test {
($t: ty, $expected_size: expr) => {
#[cfg(target_pointer_width = "64")]
const_assert_eq!(std::mem::size_of::<$t>(), $expected_size);
};
}

Просмотреть файл

@ -267,6 +267,9 @@ pub enum PropertyDeclaration {
% endfor
}
// There's one of these for each parsed declaration so it better be small.
size_of_test!(PropertyDeclaration, 32);
#[repr(C)]
struct PropertyDeclarationVariantRepr<T> {
tag: u16,
@ -2599,6 +2602,10 @@ pub struct SourcePropertyDeclaration {
all_shorthand: AllShorthand,
}
// This is huge, but we allocate it on the stack and then never move it,
// we only pass `&mut SourcePropertyDeclaration` references around.
size_of_test!(SourcePropertyDeclaration, 600);
impl SourcePropertyDeclaration {
/// Create one. Its big, try not to move it around.
#[inline]
@ -4222,6 +4229,11 @@ macro_rules! longhand_properties_idents {
}
}
// Large pages generate tens of thousands of ComputedValues.
size_of_test!(ComputedValues, 232);
// FFI relies on this.
size_of_test!(Option<Arc<ComputedValues>>, 8);
// There are two reasons for this test to fail:
//
// * Your changes made a specified value type for a given property go

Просмотреть файл

@ -203,9 +203,6 @@ impl RuleTree {
/// where it likely did not result from a rigorous performance analysis.)
const RULE_TREE_GC_INTERVAL: usize = 300;
/// Used for some size assertions.
pub const RULE_NODE_SIZE: usize = std::mem::size_of::<RuleNode>();
/// A node in the rule tree.
struct RuleNode {
/// The root node. Only the root has no root pointer, for obvious reasons.
@ -768,3 +765,8 @@ impl hash::Hash for StrongRuleNode {
(&*self.p as *const RuleNode).hash(state)
}
}
// Large pages generate thousands of RuleNode objects.
size_of_test!(RuleNode, 80);
// StrongRuleNode should be pointer-sized even inside an option.
size_of_test!(Option<StrongRuleNode>, 8);

Просмотреть файл

@ -20,7 +20,7 @@ mod map;
mod source;
mod unsafe_box;
pub use self::core::{RuleTree, StrongRuleNode, RULE_NODE_SIZE};
pub use self::core::{RuleTree, StrongRuleNode};
pub use self::level::{CascadeLevel, ShadowCascadeOrder};
pub use self::source::StyleSource;

Просмотреть файл

@ -3130,6 +3130,12 @@ impl Rule {
}
}
// The size of this is critical to performance on the bloom-basic
// microbenchmark.
// When iterating over a large Rule array, we want to be able to fast-reject
// selectors (with the inline hashes) with as few cache misses as possible.
size_of_test!(Rule, 40);
/// A function to be able to test the revalidation stuff.
pub fn needs_revalidation_for_testing(s: &Selector<SelectorImpl>) -> bool {
let mut attribute_dependencies = Default::default();

Просмотреть файл

@ -31,6 +31,9 @@ pub use specified::ImageRendering;
pub type Image =
generic::GenericImage<Gradient, MozImageRect, ComputedImageUrl, Color, Percentage, Resolution>;
// Images should remain small, see https://github.com/servo/servo/pull/18430
size_of_test!(Image, 16);
/// Computed values for a CSS gradient.
/// <https://drafts.csswg.org/css-images/#gradients>
pub type Gradient = generic::GenericGradient<

Просмотреть файл

@ -39,6 +39,9 @@ use style_traits::{SpecifiedValueInfo, StyleParseErrorKind, ToCss};
pub type Image =
generic::Image<Gradient, MozImageRect, SpecifiedImageUrl, Color, Percentage, Resolution>;
// Images should remain small, see https://github.com/servo/servo/pull/18430
size_of_test!(Image, 16);
/// Specified values for a CSS gradient.
/// <https://drafts.csswg.org/css-images/#gradients>
pub type Gradient = generic::Gradient<

Просмотреть файл

@ -10,19 +10,5 @@ path = "lib.rs"
doctest = false
[dependencies]
atomic_refcell = "0.1"
cssparser = "0.29"
cstr = "0.2"
env_logger = { version = "0.8", default-features = false }
geckoservo = {path = "../../../ports/geckolib"}
libc = "0.2"
log = {version = "0.4", features = ["release_max_level_info"]}
malloc_size_of = {path = "../../../components/malloc_size_of"}
num-traits = "0.2"
selectors = {path = "../../../components/selectors"}
size_of_test = {path = "../../../components/size_of_test"}
smallvec = "1.0"
style_traits = {path = "../../../components/style_traits"}
style = {path = "../../../components/style", features = ["gecko"]}
to_shmem = {path = "../../../components/to_shmem"}
euclid = "0.22"

Просмотреть файл

@ -11,24 +11,7 @@
// as theyre not used in any code called from this crate.
#![cfg(any(linking_with_gecko, not(windows)))]
extern crate atomic_refcell;
extern crate cssparser;
extern crate cstr;
extern crate geckoservo;
extern crate log;
extern crate malloc_size_of;
extern crate num_traits;
extern crate selectors;
extern crate smallvec;
#[cfg(target_pointer_width = "64")]
#[macro_use]
extern crate size_of_test;
#[cfg_attr(target_pointer_width = "64", macro_use)]
extern crate style;
extern crate style_traits;
extern crate to_shmem;
extern crate euclid;
extern crate style;
#[cfg(target_pointer_width = "64")]
mod size_of;
mod piecewise_linear;

Просмотреть файл

@ -12,70 +12,3 @@ use style::rule_tree::{StrongRuleNode, RULE_NODE_SIZE};
use style::servo_arc::Arc;
use style::values::computed;
use style::values::specified;
size_of_test!(
size_of_selector,
selectors::parser::Selector<SelectorImpl>,
8
);
size_of_test!(size_of_pseudo_element, selector_parser::PseudoElement, 16);
size_of_test!(
size_of_component,
selectors::parser::Component<SelectorImpl>,
24
);
size_of_test!(size_of_pseudo_class, selector_parser::NonTSPseudoClass, 16);
// The size of this is critical to performance on the bloom-basic microbenchmark.
// When iterating over a large Rule array, we want to be able to fast-reject
// selectors (with the inline hashes) with as few cache misses as possible.
size_of_test!(test_size_of_rule, style::stylist::Rule, 40);
// Large pages generate tens of thousands of ComputedValues.
size_of_test!(test_size_of_cv, ComputedValues, 232);
size_of_test!(test_size_of_option_arc_cv, Option<Arc<ComputedValues>>, 8);
size_of_test!(test_size_of_option_rule_node, Option<StrongRuleNode>, 8);
size_of_test!(test_size_of_element_styles, ElementStyles, 16);
size_of_test!(test_size_of_element_data, ElementData, 24);
size_of_test!(
test_size_of_property_declaration,
style::properties::PropertyDeclaration,
32
);
size_of_test!(
test_size_of_application_declaration_block,
ApplicableDeclarationBlock,
24
);
#[test]
fn test_size_of_rule_node() {
assert_eq!(RULE_NODE_SIZE, 80, "RuleNode size changed");
}
// This is huge, but we allocate it on the stack and then never move it,
// we only pass `&mut SourcePropertyDeclaration` references around.
size_of_test!(
test_size_of_parsed_declaration,
style::properties::SourcePropertyDeclaration,
600
);
size_of_test!(test_size_of_computed_image, computed::image::Image, 16);
size_of_test!(test_size_of_specified_image, specified::image::Image, 16);
size_of_test!(
test_size_of_computed_image_layer,
computed::image::Image,
16
);
size_of_test!(
test_size_of_specified_image_layer,
specified::image::Image,
16
);

Просмотреть файл

@ -17,7 +17,6 @@ extern crate servo_arc;
extern crate servo_atoms;
extern crate servo_config;
extern crate servo_url;
#[macro_use] extern crate size_of_test;
#[macro_use] extern crate style;
extern crate style_traits;
extern crate test;