Skip to content

Commit c3c6acb

Browse files
committed
Greatly reduce allocations by treating existing context containers as no-op scopes
1 parent 1d73e24 commit c3c6acb

4 files changed

Lines changed: 24 additions & 32 deletions

File tree

components/context/src/main/java/datadog/context/NoopContextContinuation.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package datadog.context;
22

33
/** {@link ContextContinuation} that has no effect on execution units. */
4-
final class NoopContextContinuation implements ContextContinuation {
5-
static final ContextContinuation ROOT_CONTINUATION = new NoopContextContinuation(Context.root());
4+
final class NoopContextContinuation implements ContextContinuation, ContextScope {
5+
static final NoopContextContinuation ROOT_CONTINUATION =
6+
new NoopContextContinuation(Context.root());
67

78
private final Context context;
89

@@ -22,9 +23,12 @@ public Context context() {
2223

2324
@Override
2425
public ContextScope resume() {
25-
return NoopContextScope.create(context);
26+
return this; // acts as no-op scope, avoiding allocation
2627
}
2728

2829
@Override
2930
public void release() {}
31+
32+
@Override
33+
public void close() {}
3034
}

components/context/src/main/java/datadog/context/NoopContextScope.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

components/context/src/main/java/datadog/context/ThreadLocalContextManager.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ ContextScope doAttach(Context context, @Nullable ContextContinuationImpl continu
3232
if (continuation != null) {
3333
// already attached, safe to release early to avoid resource leak
3434
continuation.releaseOnScopeClose();
35+
return continuation; // acts as no-op scope, avoiding allocation
3536
}
36-
return NoopContextScope.create(previous);
37+
return holder; // acts as no-op scope, avoiding allocation
3738
}
3839

3940
ContextListener[] ls = listeners;
@@ -196,7 +197,7 @@ public void close() {
196197
}
197198
}
198199

199-
private static final class ContextContinuationImpl implements ContextContinuation {
200+
private static final class ContextContinuationImpl implements ContextContinuation, ContextScope {
200201

201202
private static final AtomicIntegerFieldUpdater<ContextContinuationImpl> COUNT =
202203
AtomicIntegerFieldUpdater.newUpdater(ContextContinuationImpl.class, "count");
@@ -253,7 +254,7 @@ public ContextScope resume() {
253254
} else {
254255
// continuation released or too many resumes; rollback count
255256
COUNT.decrementAndGet(this);
256-
return NoopContextScope.create(context);
257+
return this; // acts as no-op scope, avoiding allocation
257258
}
258259
}
259260

@@ -284,9 +285,20 @@ void releaseOnScopeClose() {
284285
release();
285286
} /* else there are outstanding resumes or hold is in place */
286287
}
288+
289+
@Override
290+
public void close() {}
287291
}
288292

289-
private static final class ContextHolder {
293+
private static final class ContextHolder implements ContextScope {
290294
Context current = Context.root();
295+
296+
@Override
297+
public Context context() {
298+
return current;
299+
}
300+
301+
@Override
302+
public void close() {}
291303
}
292304
}

components/context/src/test/java/datadog/context/ContextProvidersForkedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Context current() {
7373

7474
@Override
7575
public ContextScope attach(Context context) {
76-
return NoopContextScope.ROOT_SCOPE;
76+
return NoopContextContinuation.ROOT_CONTINUATION;
7777
}
7878

7979
@Override

0 commit comments

Comments
 (0)