use crate::properties::{property_counts, CountedUnknownProperty, NonCustomPropertyId};
use std::sync::atomic::{AtomicUsize, Ordering};
#[cfg(target_pointer_width = "64")]
const BITS_PER_ENTRY: usize = 64;
#[cfg(target_pointer_width = "32")]
const BITS_PER_ENTRY: usize = 32;
#[derive(Debug, Default)]
pub struct CountedUnknownPropertyUseCounters {
storage:
[AtomicUsize; (property_counts::COUNTED_UNKNOWN + BITS_PER_ENTRY - 1) / BITS_PER_ENTRY],
}
#[derive(Debug, Default)]
pub struct NonCustomPropertyUseCounters {
storage: [AtomicUsize; (property_counts::NON_CUSTOM + BITS_PER_ENTRY - 1) / BITS_PER_ENTRY],
}
#[derive(Copy, Clone, Debug)]
#[repr(u32)]
pub enum CustomUseCounter {
HasNonLocalUriDependency = 0,
MaybeHasPathBaseUriDependency,
MaybeHasFullBaseUriDependency,
Last,
}
impl CustomUseCounter {
#[inline]
fn bit(self) -> usize {
self as usize
}
}
#[derive(Debug, Default)]
pub struct CustomUseCounters {
storage:
[AtomicUsize; ((CustomUseCounter::Last as usize) + BITS_PER_ENTRY - 1) / BITS_PER_ENTRY],
}
macro_rules! use_counters_methods {
($id: ident) => {
#[inline(always)]
fn bucket_and_pattern(id: $id) -> (usize, usize) {
let bit = id.bit();
let bucket = bit / BITS_PER_ENTRY;
let bit_in_bucket = bit % BITS_PER_ENTRY;
(bucket, 1 << bit_in_bucket)
}
#[inline]
pub fn record(&self, id: $id) {
let (bucket, pattern) = Self::bucket_and_pattern(id);
let bucket = &self.storage[bucket];
bucket.fetch_or(pattern, Ordering::Relaxed);
}
#[inline]
pub fn recorded(&self, id: $id) -> bool {
let (bucket, pattern) = Self::bucket_and_pattern(id);
self.storage[bucket].load(Ordering::Relaxed) & pattern != 0
}
#[inline]
fn merge(&self, other: &Self) {
for (bucket, other_bucket) in self.storage.iter().zip(other.storage.iter()) {
bucket.fetch_or(other_bucket.load(Ordering::Relaxed), Ordering::Relaxed);
}
}
};
}
impl CountedUnknownPropertyUseCounters {
use_counters_methods!(CountedUnknownProperty);
}
impl NonCustomPropertyUseCounters {
use_counters_methods!(NonCustomPropertyId);
}
impl CustomUseCounters {
use_counters_methods!(CustomUseCounter);
}
#[derive(Debug, Default)]
pub struct UseCounters {
pub non_custom_properties: NonCustomPropertyUseCounters,
pub counted_unknown_properties: CountedUnknownPropertyUseCounters,
pub custom: CustomUseCounters,
}
impl UseCounters {
#[inline]
pub fn merge(&self, other: &Self) {
self.non_custom_properties
.merge(&other.non_custom_properties);
self.counted_unknown_properties
.merge(&other.counted_unknown_properties);
self.custom.merge(&other.custom);
}
}
impl Clone for UseCounters {
fn clone(&self) -> Self {
let result = Self::default();
result.merge(self);
result
}
}