From ebb97fd4f4495d252770b064ddc9a8ecac21cc16 Mon Sep 17 00:00:00 2001 From: fanyang Date: Thu, 25 Jun 2026 00:59:49 +0800 Subject: [PATCH] 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. --- easytier/benches/counter_contention.rs | 4 +++- easytier/src/common/stats_manager.rs | 16 ++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/easytier/benches/counter_contention.rs b/easytier/benches/counter_contention.rs index ec8c8b1f..3723f3b2 100644 --- a/easytier/benches/counter_contention.rs +++ b/easytier/benches/counter_contention.rs @@ -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! { diff --git a/easytier/src/common/stats_manager.rs b/easytier/src/common/stats_manager.rs index 7a280dcf..9d8ad1e3 100644 --- a/easytier/src/common/stats_manager.rs +++ b/easytier/src/common/stats_manager.rs @@ -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();