Skip to content

Avoid unnecessary Context allocations when (re)adding same values#11673

Draft
mcculls wants to merge 1 commit into
masterfrom
mcculls/context-avoid-alloc
Draft

Avoid unnecessary Context allocations when (re)adding same values#11673
mcculls wants to merge 1 commit into
masterfrom
mcculls/context-avoid-alloc

Conversation

@mcculls

@mcculls mcculls commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Motivation

Avoid unnecessary allocations when adding the same key-value to an existing IndexedContext/SingletonContext

Contributor Checklist

  • Format the title according to the contribution guidelines
  • Assign the type: and (comp: or inst:) labels in addition to any other useful labels
  • Avoid using close, fix, or any linking keywords when referencing an issue
    Use solves instead, and assign the PR milestone to the issue
  • Update the CODEOWNERS file on source file addition, migration, or deletion
  • Update public documentation with any new configuration flags or behaviors
  • Add your completed PR to the merge queue by commenting /merge. You can also:
    • Customize the commit message associated with the merge with /merge --commit-message "..."
    • Remove your PR from the merge queue with /merge -c
    • Skip all merge queue checks with /merge -f --reason "reason"; please use this judiciously, as some checks do not run at the PR-level (note: the PR still needs to be mergeable, this will only skip the pre-merge build)
    • Get more information in this doc

Jira ticket: [PROJ-IDENT]

@mcculls mcculls requested a review from a team as a code owner June 18, 2026 15:39
@mcculls mcculls added the type: enhancement Enhancements and improvements label Jun 18, 2026
@mcculls mcculls requested review from amarziali and removed request for a team June 18, 2026 15:39
@mcculls mcculls added comp: core Tracer core tag: performance Performance related changes tag: no release notes Changes to exclude from release notes labels Jun 18, 2026
@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Pipelines

Fix all issues with BitsAI

⚠️ Warnings

🚦 4 Pipeline jobs failed

Run system tests | main / End-to-end #10 / akka-http 10   View in Datadog   GitHub Actions

DataDog/apm-reliability/dd-trace-java | check_smoke 1/4   View in Datadog   GitLab

DataDog/apm-reliability/dd-trace-java | linux-java-spring-petclinic-sca-load-parallel   View in Datadog   GitLab

View all 4 failed jobs.

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 08c0b45 | Docs | Datadog PR Page | Give us feedback!

@mcculls mcculls enabled auto-merge June 18, 2026 15:41

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 08c0b4584d

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

} else if (secondValue != this.value) {
return new SingletonContext(this.index, secondValue);
} else {
return this;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve distinct contexts for nested scope attachment

When a caller derives a nested scope with the same key/value, e.g. Context.current().with(k, v).attach(), this now reuses the outer Context instance. With the default ThreadLocalContextManager, ContextScope.close() decides whether a scope is current by context == holder[0]; if the outer scope is closed before the inner one (the out-of-order case covered by ContextManagerTest.testOnlyCurrentScopeCanBeClosed for derived contexts), both scopes now carry the same context, so closing the outer scope incorrectly restores the previous context and detaches the still-active inner scope. Please keep with() returning a distinct context for this case, or make scope-close tracking distinguish scope instances rather than context identity.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting point

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue should be addressed once my other PR is merged, as it introduces no-op scopes when the same context is attached multiple times...

@mcculls mcculls disabled auto-merge June 18, 2026 15:58
@mcculls mcculls marked this pull request as draft June 18, 2026 16:00
@mcculls mcculls removed the request for review from amarziali June 18, 2026 16:00
@dd-octo-sts

dd-octo-sts Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 13.94 s 13.82 s [+0.1%; +1.6%] (maybe worse)
startup:insecure-bank:tracing:Agent 12.86 s 12.94 s [-1.4%; +0.0%] (no difference)
startup:petclinic:appsec:Agent 16.85 s 16.61 s [+0.4%; +2.4%] (maybe worse)
startup:petclinic:iast:Agent 16.80 s 16.95 s [-1.7%; +0.1%] (no difference)
startup:petclinic:profiling:Agent 16.84 s 16.88 s [-1.4%; +0.9%] (no difference)
startup:petclinic:sca:Agent 16.79 s 16.22 s [-0.8%; +7.8%] (no difference)
startup:petclinic:tracing:Agent 16.03 s 15.67 s [-2.0%; +6.6%] (no difference)

Commit: 08c0b458 · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: core Tracer core tag: no release notes Changes to exclude from release notes tag: performance Performance related changes type: enhancement Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant