use std::cell::RefCell;
use std::sync::atomic::{AtomicBool, Ordering};
use super::chain::{ChainGroupVec, ChainNodeVec};
use super::comments::CommentVec;
static ENABLED: AtomicBool = AtomicBool::new(false);
pub fn set_buffer_stats(enabled: bool) {
ENABLED.store(enabled, Ordering::Relaxed);
}
#[inline]
fn enabled() -> bool {
ENABLED.load(Ordering::Relaxed)
}
#[derive(Debug, Default, Clone)]
pub struct BufferStats {
pub chain_nodes: Vec<usize>,
pub chain_groups: Vec<usize>,
pub leading_comments: Vec<usize>,
}
#[derive(Debug, Clone, Copy)]
pub struct BufferInlineCapacities {
pub chain_nodes: usize,
pub chain_groups: usize,
pub leading_comments: usize,
}
pub fn inline_capacities() -> BufferInlineCapacities {
BufferInlineCapacities {
chain_nodes: ChainNodeVec::new().inline_size(),
chain_groups: ChainGroupVec::new().inline_size(),
leading_comments: CommentVec::new().inline_size(),
}
}
thread_local! {
static SINK: RefCell<BufferStats> = RefCell::new(BufferStats::default());
}
pub(crate) fn record_chain_nodes(len: usize) {
if enabled() {
SINK.with(|s| s.borrow_mut().chain_nodes.push(len));
}
}
pub(crate) fn record_chain_groups(len: usize) {
if enabled() {
SINK.with(|s| s.borrow_mut().chain_groups.push(len));
}
}
pub(crate) fn record_leading_comments(len: usize) {
if enabled() {
SINK.with(|s| s.borrow_mut().leading_comments.push(len));
}
}
pub fn take_buffer_stats() -> BufferStats {
SINK.with(|s| std::mem::take(&mut *s.borrow_mut()))
}