style/use_counters/
mod.rs1use 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#[derive(Default)]
18pub struct CountedUnknownPropertyUseCounters {
19 storage:
20 [Cell<usize>; (property_counts::COUNTED_UNKNOWN - 1 + BITS_PER_ENTRY) / BITS_PER_ENTRY],
21}
22
23#[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 #[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 #[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 #[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 #[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#[derive(Default)]
77pub struct UseCounters {
78 pub non_custom_properties: NonCustomPropertyUseCounters,
81 pub counted_unknown_properties: CountedUnknownPropertyUseCounters,
83}
84
85impl UseCounters {
86 #[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}