Skip to content

perf(plugins): reuse bound model stream iterator methods - #183875

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-memory-observer-per-call-20261025
Oct 25, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-memory-observer-per-call-20261025

Conversation

@steipete

@steipete steipete commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Model stream diagnostics repeatedly look up the provider iterator's next method for every streamed event, including plugin-bound iterators whose method is already prepared.

User Impact

Streaming keeps the same events, result, diagnostics and plugin authority checks while allocating about 1.83% fewer bytes per model call in the measured owner workload. No configuration or public API changes.

Why This Change Was Made

Capture next once, with its original receiver, in the existing iterator loop. Each call still enters the current transport context and invokes the original plugin-wrapped method; no invocation frame or live-authority check is removed. Iterator cancellation and final-result settlement are unchanged. Production LOC delta: zero.

Evidence

Five alternating pairs on Node 26.12.1, using the real diagnostic wrapper and real PluginInstance bindings around a synthetic provider. Each call produces 100 text deltas, a terminal event and EOF. The benchmark now exposes --plugin-bound and main-thread CPU measurement; usage is in its header.

Per model call Baseline Candidate Sample count per side
Sampled allocation, including collected objects 418,427.6 B 410,768.2 B 5 × 2,000 calls
Main-thread CPU 0.224728 ms 0.225550 ms 5 × 20,000 calls
Wall time 0.801452 ms 0.753151 ms 5 × 20,000 calls
Total GC time 0.070502 ms 0.076853 ms 5 × 20,000 calls
Promotion, separate traced pass 35,855.8 B 35,265.0 B 5 × 20,000 calls

Allocation fell in every pair: −1.76%, −1.82%, −2.13%, −1.69%, −1.75% (paired mean −1.83%). Heap-sampling observations: 1,003,013 baseline and 984,550 candidate.

Timing is inconclusive. Four identical-source A/A controls produced main-thread CPU differences of +12.28%, +14.23%, −3.82%, −4.05%, and large GC/latency swings. No timing improvement or regression is claimed. Candidate GC rose only +0.006351 ms/call in absolute terms. This PR is justified by the consistent small allocation improvement and unchanged behavior, not timing percentages.

The count probe observed next property reads 102 → 1 per model call, with 102 iterator calls and one final-result call on both sides. Owner probes preserve exact event/result identity, diagnostic byte/usage/completion metrics, interleaved request identities, and exact plugin invocation frames across asynchronous work.

The focused regression extends the existing frozen-stream test: one next property read, two actual calls including EOF, and the original receiver. Baseline must fail at the property-read assertion; fixed source must pass. Existing tests retain cancellation, error, result-first and transport-context coverage.

Validation: direct real-owner regression probe failed on baseline (2040 !== 20 next-property reads) and passed on the fix (20 reads, 2,040 iterator calls, 20 identical results/completions). Formatting and changed-file core/scripts lint passed. Independent subagent review (autoreview route unavailable): no actionable P0–P2 findings. The only later edit renames the captured binding to avoid shadowing.

Exact-head PR CI passed: run 40237037577, including the CI gate, lint, production/test types, guards, prompt snapshots, security and selected test shards. The embedded-run shard passed 91 tests across 5 files, including all 28 cases in attempt.model-diagnostic-events.test.ts (the changed frozen-stream regression passed) and all 38 cases in attempt.model-diagnostic-lifecycle.test.ts. The shared run integration file passed 156 tests. The automated code review on this head also completed without findings.

Local Vitest gap: attempted the single changed test file using both the default Bun route and the documented OPENCLAW_VITEST_RUNTIME=node route. Both prepared their worker caches but stalled before assertions; both were stopped through their wrapper. These are setup/tooling failures, not product test failures or passing tests. PR CI subsequently ran and passed the committed regression. No full suite or full local typecheck was run. Measurements above compare the same production delta on baseline e0c6d5e91545fe3923d601ddb9ac1ac7922a1e4f; the affected owner remained unchanged when rebased. This is an owner benchmark, not a full Gateway/provider-network latency claim.

Per-pair GC, event-loop tails and controls
Pair Side GC kind Count Total ms p95 ms p99 ms Max ms
1 base 1 289 371.014 2.180 6.975 14.466
1 base 4 3 83.510 32.545 32.545 32.545
1 base 8 3 1.536 0.740 0.740 0.740
1 candidate 1 280 606.968 4.574 44.717 86.783
1 candidate 4 3 80.898 36.327 36.327 36.327
1 candidate 8 3 1.147 0.433 0.433 0.433
2 base 1 303 642.303 4.812 11.559 25.642
2 base 4 3 78.598 39.943 39.943 39.943
2 base 8 2 0.805 0.442 0.442 0.442
2 candidate 1 284 418.890 2.459 11.561 32.374
2 candidate 4 2 58.539 30.922 30.922 30.922
2 candidate 8 2 0.841 0.476 0.476 0.476
3 base 1 280 801.158 8.776 25.625 63.107
3 base 4 3 364.461 228.349 228.349 228.349
3 base 8 3 1.170 0.421 0.421 0.421
3 candidate 1 279 1656.582 21.341 58.768 100.325
3 candidate 4 3 278.571 167.053 167.053 167.053
3 candidate 8 3 1.539 0.594 0.594 0.594
4 base 1 289 2506.550 33.207 151.348 169.322
4 base 4 2 483.881 246.967 246.967 246.967
4 base 8 2 0.688 0.362 0.362 0.362
4 candidate 1 278 3298.881 56.626 120.901 153.939
4 candidate 4 3 530.375 401.804 401.804 401.804
4 candidate 8 3 2.123 1.408 1.408 1.408
5 base 1 289 1419.245 20.702 61.617 73.374
5 base 4 4 250.654 110.929 110.929 110.929
5 base 8 4 44.613 41.063 41.063 41.063
5 candidate 1 280 615.178 4.517 15.508 72.090
5 candidate 4 3 132.971 64.462 64.462 64.462
5 candidate 8 3 1.770 0.927 0.927 0.927

GC kinds: 1 minor, 4 major, 8 incremental. Major tails below 50 samples and p99 below 1,000 samples are context only. All pauses are included.

Pair Side Loop samples p95 ms p99 ms Max ms
1 base 1253 5.595 11.051 72.286
1 candidate 1250 12.485 45.777 126.681
2 base 1412 14.819 45.548 160.956
2 candidate 1266 6.468 12.763 144.048
3 base 1364 15.843 66.322 228.590
3 candidate 1490 53.379 101.908 302.514
4 base 1561 131.006 237.765 511.705
4 candidate 1535 114.098 221.250 402.129
5 base 1458 65.503 122.552 331.350
5 candidate 1287 11.715 46.137 204.997
A/A pair Main CPU ms/call A → B GC ms/call A → B Max GC pause ms A → B
1 0.248681 → 0.279214 0.031320 → 0.120506 37.254 → 759.689
2 0.207092 → 0.236552 0.022072 → 0.043290 34.101 → 83.117
3 0.222893 → 0.214370 0.026662 → 0.021790 31.728 → 23.640
4 0.221609 → 0.212638 0.023132 → 0.021717 42.805 → 30.435

Per-event follow-up (not included)

A separate long-stream profile used the real plugin binding, diagnostic run owner and session watchdog: 1,000,001 emitted events, 1,314 CPU samples. Exclusive samples were 24.89% in AsyncContextFrame, 7.76% in the plugin binding callback, 6.70% in observe, and 6.24% in response observation. Capturing the complete async context once would risk restoring stale plugin authority, so this PR preserves per-invocation frames and transport context.

A scratch timestamp-handoff candidate removed one clock read per event while preserving tested event/result identity, bytes, usage, watchdog callbacks and frames. It is not included because its wall-time result exceeded the A/A wall variation:

Per call (1,000 deltas + terminal) Next-bound baseline Timestamp candidate Samples per side
Allocation including collected objects 2,665,602.2 B 2,643,092.4 B 5 × 200 calls
Main-thread CPU 0.922792 ms 0.912702 ms 6 × 2,000 calls
Wall time 1.600458 ms 2.099999 ms 6 × 2,000 calls
Total GC time 0.176788 ms 0.302858 ms 6 × 2,000 calls
Promotion, separate traced pass 35,332.3 B 35,415.3 B 5 × 2,000 calls

Five alternating pairs plus one predeclared extra mixed-sign pair are all retained. Allocation improved in all five pairs (paired mean −0.844%); CPU is inconclusive. Signed mean wall delta is +0.499541 ms/call (+31.21% of mean baseline), versus four A/A pairs averaging −0.063393 ms/call, with individual A/A wall deltas −14.37%, −0.10%, −1.61%, +5.39%. GC's +0.126070 ms/call absolute rise is below the +1 ms/op materiality floor and is not the rejection reason. The largest concerning pause pair was 12.787 → 200.398 ms; all pauses were retained. These separate workloads must not be compared as if they measured the same event count.

After deploy, look for a small reduction in allocation normalized by streamed-event count, with stable response bytes, completions, cancellation/error behavior and plugin authority. No production latency improvement is established. The published benchmark's plugin-bound count smoke passed 20 calls, 2,040 iterator calls and 20 completions with all identity and diagnostic assertions.

@clawsweeper

clawsweeper Bot commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T21:48:06.740836Z 8de41f1 PR opened
ℹ️ 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.

@openclaw-barnacle openclaw-barnacle Bot added scripts Repository scripts agents Agent runtime and tooling size: S labels Oct 25, 2026
@steipete
steipete merged commit b982efa into main Oct 25, 2026
139 of 140 checks passed
@steipete
steipete deleted the steipete/perf-memory-observer-per-call-20261025 branch October 25, 2026 22:06
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling scripts Repository scripts size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant