Skip to content

Commit 36bc792

Browse files
committed
Possibly use a fast path for accessing scope stacks
1 parent c714e59 commit 36bc792

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,8 @@ public Context swap(Context context) {
391391

392392
static final class ScopeStackThreadLocal extends ThreadLocal<ScopeStack> {
393393

394+
private final ScopeStack[] fastStacks = new ScopeStack[2048]; // This should be tuned
395+
394396
private final ProfilingContextIntegration profilingContextIntegration;
395397

396398
ScopeStackThreadLocal(ProfilingContextIntegration profilingContextIntegration) {
@@ -401,6 +403,41 @@ static final class ScopeStackThreadLocal extends ThreadLocal<ScopeStack> {
401403
protected ScopeStack initialValue() {
402404
return new ScopeStack(profilingContextIntegration);
403405
}
406+
407+
@Override
408+
public ScopeStack get() {
409+
final long id = Thread.currentThread().getId();
410+
if (id >= 0 && id < this.fastStacks.length) {
411+
ScopeStack ret = fastStacks[(int) id];
412+
if (ret == null) {
413+
ret = initialValue();
414+
fastStacks[(int) id] = ret;
415+
}
416+
return ret;
417+
} else {
418+
return super.get();
419+
}
420+
}
421+
422+
@Override
423+
public void set(ScopeStack value) {
424+
final long id = Thread.currentThread().getId();
425+
if (id >= 0 && id < this.fastStacks.length) {
426+
fastStacks[(int) id] = value;
427+
} else {
428+
super.set(value);
429+
}
430+
}
431+
432+
@Override
433+
public void remove() {
434+
final long id = Thread.currentThread().getId();
435+
if (id >= 0 && id < this.fastStacks.length) {
436+
fastStacks[(int) id] = null;
437+
} else {
438+
super.remove();
439+
}
440+
}
404441
}
405442

406443
private void scheduleRootIterationScopeCleanup(ScopeStack scopeStack, ContinuableScope scope) {

0 commit comments

Comments
 (0)