Can an AI Coding Agent Find Useful C++ Optimizations?
A transparent engineering case study integrating AI coding agents with deterministic analysis and independent validation.
Experiment Architecture
The experiment pipeline combines deterministic program analysis with AI-generated code candidates. Each stage is independently verifiable, and the pipeline rejects candidates that fail any gate.
Stage Responsibilities
UTFCPP v4.0.6 — Primary Case Study
October 24, 2026 · Source revision b26a5f718f4f370af1852a0d5c6ae8fa031ba7d0
Measured Results
| Project | UTFCPP v4.0.6 (revision b26a5f7) |
| Model provider | OpenAI Codex (codex exec, ephemeral, read-only sandbox) |
| Input tokens | 52,307 |
| Output tokens | 930 |
| Reasoning tokens | 233 |
| Functional checks | 42 optimization tests pass |
| Benchmark measurements | 24 samples + 6 warmups |
| Performance ratio | 0.998909× |
| 95% confidence interval | [0.974, 1.052] |
| Outcome | INCONCLUSIVE |
| Total elapsed | 103.95 seconds |
What Happened
The model proposed bypassing a redundant scalar-validity predicate after UTF-16 surrogate checking, using the same internal append implementation. The candidate passed all correctness, sanitizer, and reference checks. However, the measured performance showed no statistically supported improvement. The confidence interval spans both sides of 1.0, so neither a gain nor a regression can be established.
Validation Workload
Protected multilingual roundtrip workload (148,488 bytes, 32 full passes), independently generated Python-codec/FNV reference, 71 Unicode golden/boundary cases plus malformed UTF-8/UTF-16 checks, and the unmodified upstream negative-conformance program. Initial and final ASan/UBSan/LeakSanitizer checks were CLEAN for both original and candidate.
What This Demonstrates
The experiment demonstrated operational feasibility: the full pipeline from deterministic analysis through model invocation, strict validation, and independent native measurement completed without manual intervention. It did not demonstrate optimization efficiency. A technically valid candidate that passes all checks is not valuable unless it produces a proven improvement.
Salsa20 — End-to-End Pipeline Exercise
Phase 2.2 · Salsa20 from LLVM Test Suite (revision 624469628023)
Measured Results
| Project | Salsa20 (LLVM Test Suite) |
| Model | Codex (structured body, one-shot) |
| Input tokens | 26,064 |
| Output tokens | 1,347 |
| Measurements | 24 samples + 6 warmups |
| Performance ratio | 0.944× [0.919, 0.962] |
| Outcome | NO_SUPPORTED_GAIN |
What Happened
The model independently proposed interleaving four disjoint quarter-rounds at each rotation stage of the Salsa20 cipher. The candidate passed strict validation including scope/replay checks, reference matching against Bernstein's published test vectors and 264 independent expectations, and CLEAN ASan/UBSan/LeakSanitizer results for both variants.
However, the candidate measured slower than the original: 0.944× with a confidence interval entirely below 1.0, indicating a detected local regression. The pipeline correctly classified this as NO_SUPPORTED_GAIN. No retry, repair, or retiming was performed.
Why This Matters
This demonstrates the complete pipeline working correctly, including correctly rejecting an unsupported optimization. A system that only reports wins would be unreliable. The ability to detect and refuse a plausible-sounding but actually harmful change is a necessary engineering property.
Earlier Experiment Phases
The autonomous optimization infrastructure was developed iteratively. Earlier phases exercised individual gates before the full pipeline was connected.
| Phase | Target | Model Outcome | Engineering Significance |
|---|---|---|---|
| Phase 1 | AlmaBench | NONE | Model identified possible repeated trigonometric work but declined to propose a change, citing rounding, errno, and alias obligations. Demonstrates honest termination when no safe optimization is found. |
| Phase 2 | AlmaBench | INCORRECT | Model proposed a candidate, but the generated unified diff was malformed. GNU patch rejected it at zero fuzz. The harness terminated without repair or retry. Demonstrates fail-closed behavior. |
| Phase 2.1 | Synthetic fixture | INTERFACE TEST | A deterministic synthetic replacement body exercised the structured edit adapter, zero-fuzz replay, and real validator with sanitizer checks. 27 tests pass. No model was invoked; this is an interface-engineering milestone. |
Limitations and Conclusions
Observed Limitations
- High inference and context consumption per candidate (52K+ input tokens for a single proposal)
- Substantial validation infrastructure cost beyond the LLM call itself (compilation, sanitizer builds, measurement rounds)
- No guaranteed relationship between token consumption and achieved speedup
- A small code edit that passes all checks is not valuable unless it produces a proven runtime improvement
- No demonstrated favorable cost-benefit ratio in any measured case to date
- Model familiarity with public source code cannot be excluded; controlled novelty is not established
Conclusion
The agent integration was technically functional: the pipeline correctly invoked a model, received structured proposals, validated candidates through multiple independent gates, measured native performance, and accepted or rejected results based on evidence. But its measured optimization benefit did not justify broad adoption as the primary optimization engine.
The experiment establishes that the infrastructure works end-to-end. It does not establish that LLM-generated optimizations are generally effective, cost-efficient, or superior to deterministic analysis alone.
Important Distinction
Using an AI agent to develop the Precedes tool itself (as a programming assistant during implementation) is different from invoking an AI agent inside Precedes to optimize users' source code. This experiment tests the latter: can an AI agent, given deterministic analysis evidence, propose source-level C++ optimizations that survive independent validation and produce measurable runtime improvements?
Provenance
b26a5f7. Evidence retained in work/consolidation/acceptance/utfcpp-host/
precedes-rea-lab/autonomous-optimizer-mvp-v2_2/runs/one-shot-salsa20/
codex exec CLI. Host default model/settings inherited. Exact backend revision and temperature not exposed.
-O3 -g -march=znver2 -fno-omit-frame-pointer, no fast-math.
Precedes