Skip to content

Commit f21e7d4

Browse files
bm1549claude
andcommitted
Cache isEmpty() results in local variables to avoid redundant volatile reads
Addresses review feedback from dougqh: the JIT will be conservative with volatile reads from CopyOnWriteArrayList.isEmpty() and won't hoist them, so we cache in locals ourselves. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1e13d53 commit f21e7d4

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

dd-trace-core/src/main/java/datadog/trace/core/scopemanager/ContinuableScope.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ void cleanup(final ScopeStack scopeStack) {
8080
* I would hope this becomes unnecessary.
8181
*/
8282
final void onProperClose() {
83-
if (!scopeManager.scopeListeners.isEmpty()) {
83+
boolean hasScopeListeners = !scopeManager.scopeListeners.isEmpty();
84+
boolean hasExtendedListeners = !scopeManager.extendedScopeListeners.isEmpty();
85+
if (!hasScopeListeners && !hasExtendedListeners) {
86+
return;
87+
}
88+
if (hasScopeListeners) {
8489
for (final ScopeListener listener : scopeManager.scopeListeners) {
8590
try {
8691
listener.afterScopeClosed();
@@ -89,7 +94,7 @@ final void onProperClose() {
8994
}
9095
}
9196
}
92-
if (!scopeManager.extendedScopeListeners.isEmpty()) {
97+
if (hasExtendedListeners) {
9398
for (final ExtendedScopeListener listener : scopeManager.extendedScopeListeners) {
9499
try {
95100
listener.afterScopeClosed();
@@ -173,14 +178,16 @@ public final void beforeActivated() {
173178
}
174179

175180
public final void afterActivated() {
176-
if (scopeManager.scopeListeners.isEmpty() && scopeManager.extendedScopeListeners.isEmpty()) {
181+
boolean hasScopeListeners = !scopeManager.scopeListeners.isEmpty();
182+
boolean hasExtendedListeners = !scopeManager.extendedScopeListeners.isEmpty();
183+
if (!hasScopeListeners && !hasExtendedListeners) {
177184
return;
178185
}
179186
AgentSpan span = span();
180187
if (span == null) {
181188
return;
182189
}
183-
if (!scopeManager.scopeListeners.isEmpty()) {
190+
if (hasScopeListeners) {
184191
for (final ScopeListener listener : scopeManager.scopeListeners) {
185192
try {
186193
listener.afterScopeActivated();
@@ -189,7 +196,7 @@ public final void afterActivated() {
189196
}
190197
}
191198
}
192-
if (!scopeManager.extendedScopeListeners.isEmpty()) {
199+
if (hasExtendedListeners) {
193200
for (final ExtendedScopeListener listener : scopeManager.extendedScopeListeners) {
194201
try {
195202
listener.afterScopeActivated(span.getTraceId(), span.getSpanId());

0 commit comments

Comments
 (0)