Skip to content

Commit 8d2029d

Browse files
authored
chore(telemetry): use weaker mem ordering for SEQ_ID (#1749)
# 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. Although this is just in an example, so it's mostly cosmetic. # How to test the change? Should see no difference in behavior except maybe for performance. Co-authored-by: yann.hamdaoui <[email protected]>
1 parent bfdbeae commit 8d2029d

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

libdd-telemetry/examples/tm-ping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn build_app_started_payload() -> AppStarted {
2222

2323
fn seq_id() -> u64 {
2424
static SEQ_ID: AtomicU64 = AtomicU64::new(0);
25-
SEQ_ID.fetch_add(1, Ordering::SeqCst)
25+
SEQ_ID.fetch_add(1, Ordering::Relaxed)
2626
}
2727

2828
fn build_request<'a>(

libdd-telemetry/examples/tm-send-sketch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use libdd_telemetry::{
2020

2121
fn seq_id() -> u64 {
2222
static SEQ_ID: AtomicU64 = AtomicU64::new(1);
23-
SEQ_ID.fetch_add(1, Ordering::SeqCst)
23+
SEQ_ID.fetch_add(1, Ordering::Relaxed)
2424
}
2525

2626
fn build_request<'a>(

0 commit comments

Comments
 (0)