refactor(stats): rename UnsafeCounter to Counter

The counter is now a plain atomic (no longer UnsafeCell, no longer
sharded), so the "Unsafe" prefix is a misleading leftover. Rename to
Counter. Also fix a stale bench comment that claimed the ShardedAtomic
variant mirrors production.
This commit is contained in:
fanyang
2026-06-25 00:59:49 +08:00
parent 7d249979ea
commit ebb97fd4f4
2 changed files with 11 additions and 9 deletions
+3 -1
View File
@@ -94,7 +94,9 @@ impl Counter for CasSaturating {
// ---------------------------------------------------------------------------
// 3. ShardedAtomic: 16 cache-aligned shards + per-thread shard index.
// Mirrors production `stats_manager::UnsafeCounter`.
// Comparison-only variant (production `stats_manager::Counter` is
// single-atomic; sharding was evaluated and dropped as no benefit for the
// default 1-16 worker deployments).
// ---------------------------------------------------------------------------
thread_local! {
+8 -8
View File
@@ -375,19 +375,19 @@ impl Default for LabelSet {
}
}
/// UnsafeCounter provides a high-performance atomic counter
/// Counter provides a high-performance atomic counter
#[derive(Debug)]
pub struct UnsafeCounter {
pub struct Counter {
value: AtomicU64,
}
impl Default for UnsafeCounter {
impl Default for Counter {
fn default() -> Self {
Self::new()
}
}
impl UnsafeCounter {
impl Counter {
pub fn new() -> Self {
Self {
value: AtomicU64::new(0),
@@ -445,21 +445,21 @@ fn now_millis() -> u64 {
/// MetricData contains both the counter and last update timestamp
#[derive(Debug)]
struct MetricData {
counter: UnsafeCounter,
counter: Counter,
last_updated: AtomicU64,
}
impl MetricData {
fn new() -> Self {
Self {
counter: UnsafeCounter::new(),
counter: Counter::new(),
last_updated: AtomicU64::new(now_millis()),
}
}
fn new_with_value(initial: u64) -> Self {
Self {
counter: UnsafeCounter::new_with_value(initial),
counter: Counter::new_with_value(initial),
last_updated: AtomicU64::new(now_millis()),
}
}
@@ -757,7 +757,7 @@ mod tests {
#[tokio::test]
async fn test_unsafe_counter() {
let counter = UnsafeCounter::new();
let counter = Counter::new();
assert_eq!(counter.get(), 0);
counter.inc();