Skip to content

Prefer compare-and-set over locks in perf review (AI perf review) - #13005

Open
dougqh wants to merge 2 commits into
masterfrom
dougqh/perf-review-prefer-cas
Open

dougqh wants to merge 2 commits into
masterfrom
dougqh/perf-review-prefer-cas

Conversation

@dougqh

@dougqh dougqh commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What Does This Do

Makes "prefer a compare-and-set over a lock whenever possible" part of the perf-review skill (SKILL.md, references/checks/collections.md and references/posture.md):

  1. New Java check J17: lock guarding publishable state → compare-and-set on an immutable snapshot. It flags:

    • a new synchronized block or ReentrantLock whose critical section only reads the current state, computes the next and writes it back (one field, or several that could form one immutable snapshot)
    • a per-instance lock object (private final Object lock = new Object()) on a per-span or per-trace type

    The suggested fix is an immutable snapshot behind one volatile field, updated by a CAS loop. J17 also tells the reviewer what to check for correctness in a lock → CAS change, and when a lock is still the right choice.

  2. Replacing a lock with such a CAS no longer triggers the "visibly-contestable tradeoff" benchmark nudge. That nudge fires for changes that remove a lock. A lock → CAS swap now counts as adopting the preferred idiom, and is reviewed for correctness instead.

  3. The SKILL.md check table lists J17, and notes that it's a trigger in its own right, unlike J7–J11, which only route existing findings to a fix.

Motivation

Measured while replacing PTags' per-trace sampling lock with a CAS (2026-10-22, 3 forks per arm):

  • −24 B per PTags on every JDK: no lock object or lock field.
  • +7–28% throughput in span-level benchmarks on JDK 17 and 21, with forks not overlapping.
  • JDK 8 with biased locking off: +40–57%.
  • Slower only on JDK 8/11 with default flags. There, biased locking makes an uncontended synchronized nearly free. Those JVMs are about 30% of orgs and shrinking, so the cost is accepted.

From JDK 15 a monitor enter is itself a CAS on the object header, so a lock costs the atomic plus monitor bookkeeping, often plus a lock object per instance. synchronized also pins virtual-thread carriers on JDK 21–23.

The correctness checks in J17 come from the same change. Two mutations were tried against its stress test:

  • Blind writes instead of CAS (lost updates): failed the test in 2 of 2 runs.
  • Clearing the decision-maker flag after its CAS instead of before: wasn't caught in 3 × 2000 trials. That's why J17 says to reason about ordering rather than rely on a stress test.

Tracked in APMLP-2100. Related: APMLP-2099 (an adaptive updater that would choose synchronized on biased-locking JVMs, coming) and APMLP-1838 (synchronized vs ReentrantLock under virtual threads).

Additional Notes

  • Renumbered J13 → J17 on the sync with master. Master now uses J13 for large retained byte[], J14 for product-off work, and the perf-review stack claims J15 and J16. The check lives in checks/collections.md, next to the concurrency-choice guidance, since master split the old checks.md into per-topic files.

  • Documentation-only change to the skill.

  • Overlaps Flag code a change carries into a hotter context, and weight header paths up (AI perf review) #12902 on one line: that PR adds bullets right above the "visibly-contestable tradeoff" clause this PR amends. Whichever merges second needs a trivial rebase.

  • J12 is already taken by the benchmark-validity check, so this is J13.

  • The PTags lock → CAS change that motivates this will be its own PR.

🤖 Generated with Claude Code

Add J13: flag a new lock whose critical section reads, computes and writes
back state that could be one immutable snapshot, and per-instance lock objects
on per-span or per-trace types; suggest a CAS loop on the snapshot instead.
Replacing a lock with such a CAS no longer triggers the benchmark nudge for
removed locks; it is reviewed for correctness instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh dougqh added comp: core Tracer core type: documentation tag: performance Performance related changes tag: no release notes Changes to exclude from release notes tag: ai generated Largely based on code generated by an AI or LLM labels Oct 23, 2026
@datadog-datadog-prod-us1-2

This comment has been minimized.

@dd-octo-sts

dd-octo-sts Bot commented Oct 23, 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 14.87 s 14.81 s [-0.4%; +1.2%] (no difference)
startup:insecure-bank:tracing:Agent 13.71 s 13.73 s [-0.9%; +0.7%] (no difference)
startup:petclinic:appsec:Agent 17.80 s 17.65 s [-0.0%; +1.7%] (no difference)
startup:petclinic:iast:Agent 17.54 s 17.75 s [-1.9%; -0.4%] (maybe better)
startup:petclinic:profiling:Agent 17.41 s 16.86 s [-1.3%; +7.8%] (no difference)
startup:petclinic:sca:Agent 17.25 s 17.79 s [-7.4%; +1.3%] (no difference)
startup:petclinic:tracing:Agent 16.94 s 16.87 s [-0.5%; +1.3%] (no difference)

Commit: aa8afa3f · 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.

…prefer-cas

# Conflicts:
#	.agents/skills/perf-review/SKILL.md
#	.agents/skills/perf-review/references/checks.md
@dougqh
dougqh marked this pull request as ready for review November 19, 2026 21:57
@dougqh
dougqh requested a review from a team as a code owner November 19, 2026 21:57
@dougqh
dougqh requested review from jandro996 and removed request for a team November 19, 2026 21:57
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T21:59:57.824996Z aa8afa3 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

The CAS preference retains explicit exceptions for expensive snapshot copies and blocking or non-repeatable work, alongside retry and ordering checks. The completed static review identified no actionable defects.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit aa8afa3

This branch has not been deployed

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

Labels

comp: core Tracer core tag: ai generated Largely based on code generated by an AI or LLM tag: no release notes Changes to exclude from release notes tag: performance Performance related changes type: documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant