Skip to content

Commit 81f3597

Browse files
Use a less constraining memory order for all "update" operations on Counter/Gauge.
Updates on metrics is something that happens much more often than collecting operations, and it doesn't require strong atomic guarantees (ie if two threads update a metric at the same time, we might accept that it's not always the very last update that is being kept in the metric). We sacrifice a bit correctness in case of multithread concurrent updates (which is a rather rare scenario) for better performances all the time (ie even with a single thread, or not concurrent updates).
1 parent 0f454e3 commit 81f3597

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

core/src/gauge.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ void Gauge::Decrement() { Decrement(1.0); }
1414

1515
void Gauge::Decrement(const double value) { Change(-1.0 * value); }
1616

17-
void Gauge::Set(const double value) { value_.store(value); }
17+
void Gauge::Set(const double value) {
18+
value_.store(value, std::memory_order_relaxed);
19+
}
1820

1921
void Gauge::Change(const double value) {
2022
#if __cpp_lib_atomic_float >= 201711L
21-
value_.fetch_add(value);
23+
value_.fetch_add(value, std::memory_order_relaxed);
2224
#else
2325
// Pre-C++ 20 fallback: busy loop (which might be more expansive than using
2426
// fetch_add).
25-
auto current = value_.load();
26-
while (!value_.compare_exchange_weak(current, current + value)) {
27+
auto current = value_.load(std::memory_order_relaxed);
28+
while (!value_.compare_exchange_weak(current, current + value,
29+
std::memory_order_relaxed,
30+
std::memory_order_relaxed)) {
2731
// intentionally empty block
2832
}
2933
#endif

0 commit comments

Comments
 (0)