Skip to content

Commit f10d348

Browse files
authored
chore(profiling): use weaker mem ordering for COUNTER (#1750)
# What does this PR do? This PR replaces a bunch of sequentially consistent atomic accesses on id generator by weaker relaxed accesses. # Motivation The motivation to use the weakest memory ordering applicable is two folds: 1. Performance: relaxed accesses compile to normal, non-atomic loads and stores on standard platforms (x86_64 and arm64 in particular). Whether this particular change has any performance impact is less obvious. 2. Readability: I think my main motivation is that I find it _easier_, at least as a reader, to reason about weaker orderings. For example, a relaxed access indicates that there's no other unsynchronized data that this atomic protect or interact with, which enables local reasoning (you don't have to care about what other threads might be doing). A sequentially consistent access is the converse: they lead to a global order which involves all other seqcst accesses to this atomic, which is a strong and far-reaching assumption. # Additional Notes This atomic is a counter, which is the poster child for `Relaxed` ordering (you usually only need the atomicity). This counter doesn't protect or interact with unsynchronized memory, so there's no reason to use a stronger ordering. # How to test the change? Should see no difference in behavior except maybe for performance. Co-authored-by: yann.hamdaoui <[email protected]>
1 parent c664ed7 commit f10d348

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

libdd-profiling/src/internal/profile/interning_api/generational_ids.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::sync::atomic::AtomicU64;
4+
use std::sync::atomic::{AtomicU64, Ordering};
55

66
/// Opaque identifier for the profiler generation
77
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -17,7 +17,7 @@ impl Generation {
1717
pub fn new() -> Self {
1818
static COUNTER: AtomicU64 = AtomicU64::new(0);
1919
Self {
20-
id: COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
20+
id: COUNTER.fetch_add(1, Ordering::Relaxed),
2121
}
2222
}
2323
}

0 commit comments

Comments
 (0)