style/use_counters/
mod.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Various stuff for CSS property use counters.
6
7use crate::properties::{property_counts, CountedUnknownProperty, NonCustomPropertyId};
8use std::cell::Cell;
9
10#[cfg(target_pointer_width = "64")]
11const BITS_PER_ENTRY: usize = 64;
12
13#[cfg(target_pointer_width = "32")]
14const BITS_PER_ENTRY: usize = 32;
15
16/// One bit per each non-custom CSS property.
17#[derive(Default)]
18pub struct CountedUnknownPropertyUseCounters {
19    storage:
20        [Cell<usize>; (property_counts::COUNTED_UNKNOWN - 1 + BITS_PER_ENTRY) / BITS_PER_ENTRY],
21}
22
23/// One bit per each non-custom CSS property.
24#[derive(Default)]
25pub struct NonCustomPropertyUseCounters {
26    storage: [Cell<usize>; (property_counts::NON_CUSTOM - 1 + BITS_PER_ENTRY) / BITS_PER_ENTRY],
27}
28
29macro_rules! property_use_counters_methods {
30    ($id: ident) => {
31        /// Returns the bucket a given property belongs in, and the bitmask for that
32        /// property.
33        #[inline(always)]
34        fn bucket_and_pattern(id: $id) -> (usize, usize) {
35            let bit = id.bit();
36            let bucket = bit / BITS_PER_ENTRY;
37            let bit_in_bucket = bit % BITS_PER_ENTRY;
38            (bucket, 1 << bit_in_bucket)
39        }
40
41        /// Record that a given property ID has been parsed.
42        #[inline]
43        pub fn record(&self, id: $id) {
44            let (bucket, pattern) = Self::bucket_and_pattern(id);
45            let bucket = &self.storage[bucket];
46            bucket.set(bucket.get() | pattern)
47        }
48
49        /// Returns whether a given property ID has been recorded
50        /// earlier.
51        #[inline]
52        pub fn recorded(&self, id: $id) -> bool {
53            let (bucket, pattern) = Self::bucket_and_pattern(id);
54            self.storage[bucket].get() & pattern != 0
55        }
56
57        /// Merge `other` into `self`.
58        #[inline]
59        fn merge(&self, other: &Self) {
60            for (bucket, other_bucket) in self.storage.iter().zip(other.storage.iter()) {
61                bucket.set(bucket.get() | other_bucket.get())
62            }
63        }
64    };
65}
66
67impl CountedUnknownPropertyUseCounters {
68    property_use_counters_methods!(CountedUnknownProperty);
69}
70
71impl NonCustomPropertyUseCounters {
72    property_use_counters_methods!(NonCustomPropertyId);
73}
74
75/// The use-counter data related to a given document we want to store.
76#[derive(Default)]
77pub struct UseCounters {
78    /// The counters for non-custom properties that have been parsed in the
79    /// document's stylesheets.
80    pub non_custom_properties: NonCustomPropertyUseCounters,
81    /// The counters for css properties which we haven't implemented yet.
82    pub counted_unknown_properties: CountedUnknownPropertyUseCounters,
83}
84
85impl UseCounters {
86    /// Merge the use counters.
87    ///
88    /// Used for parallel parsing, where we parse off-main-thread.
89    #[inline]
90    pub fn merge(&self, other: &Self) {
91        self.non_custom_properties
92            .merge(&other.non_custom_properties);
93        self.counted_unknown_properties
94            .merge(&other.counted_unknown_properties);
95    }
96}