Skip to content

Avoid quadratic scans when confirming delivery of conversation history - #6218

Merged
markstuart-oai merged 1 commit into
mainfrom
perf/conversation-delivery-batches
Nov 25, 2026
Merged

markstuart-oai merged 1 commit into
mainfrom
perf/conversation-delivery-batches

Conversation

@markstuart-oai

Copy link
Copy Markdown
Contributor

Summary

With 4,096 prior messages and one function call, a complete request through the Responses HTTP client has been taking 593 ms instead of 847 ms in offline tests. At 2,048 messages, elapsed time has been dropping from 334 to 275 ms.

The SDK has been marking conversation items as sent both before the model request and after the response. On the second pass, it has been scanning from the start of the sent list for each item. For a long history in the same order, the work has been growing quadratically.

With this change, the tracker has been checking for the same source object at the same position first. On a mismatch, it has been searching the whole list by identity. Both passes have been updating fingerprints and pending input as before. No cache or change to saved state.

Related work in #6193 on batch delivery. This version has been avoiding a separate index and a batch-size threshold.

Benchmarks

Complete request Main, ms PR, ms Change [95% interval]
2,048 prior messages, one tool call 94.14 40.53 -57.04% [-57.42, -56.34]
2,048 messages with the Responses HTTP client 333.7 275.1 -17.87% [-19.99, -14.96]
4,096 messages with the Responses HTTP client 846.6 592.6 -29.21% [-32.84, -26.01]
One prior message 1.814 1.8 -0.59% [-1.62, +0.68]
32 concurrent streams, second batch 372.8 362.9 -2.21% [-3.33, -1.59]
32 concurrent streams, sum of three timed batches 1344 1320 -1.57% [-2.55, -0.71]

In a separate 18-block replay with a mock HTTP transport, the 4,096-message request has been taking 522 ms on this branch and 731 ms on main (−29.03%, 95% interval −30.79 to −27.18%). With no prior messages, both have been taking about 5.2 ms. The tests have been checking the call, its output, the full returned history and the single item sent on the second request.

Still uncertain — the small SQLite write, at +0.73% [−1.74, +5.53] in the independent repeat. The large write has been measuring +0.58% [−4.00, +2.17]. No changes to the SQLite path.

Regression checks across all 83 comparisons with Codex off. Offline Python SDK workloads on one Linux host with CPython 3.12.16. This change alone against main at 4e162c79, without network or model latency.

95% interval versus a 3% slowdown Within margin Uncertain Above margin
Initial, all 83 76 7 0
Independent repeat, 10 selected 9 1 0
Latest result per workload 82 1 0
Timings and 95% intervals for all 83 comparisons
  • Initial — 32 matched pairs of fresh processes, balanced by run order.
  • Repeat — 64 new pairs for every initial upper bound above +3%, both SQLite writes and 32 concurrent streams. No pooling.
  • Main / PR — median elapsed time in milliseconds, from the repeat where available. For repeated calls, the median across process medians. For 32 streams, the median of three timed batches.
  • Change — median within-pair percentage change, with 95% bounds from 5,000 resamples of whole pairs. Negative for faster. Calculated from individual pairs before taking the median.
  • Margin — within for an upper bound ≤ +3%, above for a lower bound > +3%, otherwise uncertain. A result within the margin can include a small slowdown. Individual intervals only, without correction for multiple comparisons.
Workload Main, ms PR, ms Initial % [95% interval] Repeat % [95% interval]
Fresh-agent control, normal 39.982 39.987 +0.83 [-4.97, +10.84] +0.33 [-2.58, +1.81]
Fresh-agent control, streaming 269.73 269.14 -1.01 [-7.75, +3.67] -0.39 [-2.04, +0.68]
Integer-list output, 17 calls, 32769 initial entries, normal 238.97 238.56 -0.24 [-0.84, +1.05] —
Integer-list output, 17 calls, 32769 initial entries, streaming 264.99 265.96 +0.21 [-0.37, +1.21] —
Integer-list output, 3 calls, 8193 initial entries, normal 13.241 13.139 -0.31 [-1.12, +0.66] —
Integer-list output, 3 calls, 8193 initial entries, streaming 16.165 16.166 +2.64 [-0.99, +4.82] -0.25 [-2.09, +1.24]
Reused-agent control, normal 39.913 39.303 -0.59 [-3.76, +1.25] —
Reused-agent control, streaming 38.971 38.726 +1.17 [-3.19, +4.71] +0.07 [-0.92, +0.69]
Typed text-list output, 3 calls, 257 initial entries, normal 6.5836 6.6598 +1.23 [-0.50, +2.15] —
Typed text-list output, 3 calls, 257 initial entries, streaming 8.2025 8.1764 -0.28 [-1.06, +1.79] —
Dynamic tool addition 1.7338 1.7187 -0.89 [-1.38, -0.64] —
Agent with 12 tools, dictionary settings, construction only 0.050841 0.050826 -0.06 [-0.29, +0.38] —
Agent with 12 tools, default settings, construction only 0.068918 0.068931 +0.19 [-0.11, +0.37] —
Agent with 12 tools, typed settings, construction only 0.037992 0.0381 +0.10 [-0.50, +0.75] —
Concurrent runs, 1 task 0.51175 0.51207 -1.35 [-3.47, +2.29] —
Concurrent runs, 32 tasks 12.393 12.364 -0.13 [-2.28, +1.07] —
Concurrent runs, 8 tasks 2.9508 2.9235 -0.28 [-1.42, +0.16] —
32 concurrent runs with GC observation 12.503 12.484 -0.99 [-2.17, +1.06] —
Stream completion, 128 history messages 1.2752 1.2763 +0.45 [-0.53, +1.15] —
0 available handoffs 0.51377 0.51247 -0.08 [-2.35, +1.74] —
12 available handoffs 0.7126 0.71621 +1.09 [+0.12, +2.02] —
Input history, 120 messages 0.93203 0.93109 -0.20 [-0.94, +0.18] —
Input history, 2000 messages 6.2651 6.2598 -0.36 [-0.76, +0.50] —
Input history, 720 messages 2.6414 2.6375 +0.56 [-0.29, +1.53] —
6 handoff routes, mixed agent names 0.72397 0.72375 +0.25 [-0.79, +0.93] —
Structured streamed response, 67 fields, 16 tool turns 76.182 75.165 -0.38 [-2.46, +1.22] —
8 handoff routes, repeated agent names 0.98962 0.99097 -0.13 [-0.67, +0.67] —
Retained history, 16384 existing + 1 new message 0.39343 0.19672 -50.13 [-51.42, -49.56] —
Retained history, 2048 existing + 1 new message 0.053546 0.029309 -45.20 [-45.47, -44.78] —
Agent with 6 tools, dictionary settings, construction + run 0.77041 0.77028 -0.37 [-1.23, +0.41] —
Agent with 6 tools, default settings, construction + run 0.8242 0.8218 -0.36 [-1.15, +0.85] —
Agent with 6 tools, typed settings, construction + run 0.74544 0.74058 -0.34 [-1.57, +0.54] —
24 available tool schemas 0.85792 0.85452 -0.72 [-2.62, +0.69] —
Server-managed conversation, 1 history message 1.8143 1.8005 -0.59 [-1.62, +0.68] —
Server-managed conversation, 1024 history messages 35.076 20.894 -40.19 [-41.34, -39.50] —
Server-managed conversation, 128 history messages 4.7153 4.3297 -7.74 [-8.68, -6.70] —
Server-managed conversation, 512 history messages 15.401 11.493 -24.97 [-25.59, -24.11] —
0 handoff routes, distinct agent names 0.50077 0.49911 -0.37 [-1.44, +0.31] —
12 handoff routes, distinct agent names 0.595 0.59267 -0.75 [-1.29, -0.12] —
Streamed final response 0.80096 0.79554 -1.17 [-1.63, +0.04] —
32 sustained concurrent runs 11.92 11.897 +0.15 [-0.82, +0.72] —
SQLite committed batch, 1024 items × 4096 payload characters 58.025 57.084 -1.09 [-7.13, +2.69] +0.58 [-4.00, +2.17]
Retained history, 16384 existing + 2 new messages 0.84129 0.39785 -52.43 [-53.32, -51.75] —
Retained history, 2048 existing + 2 new messages 0.10494 0.057995 -44.68 [-45.30, -44.46] —
SQLite committed batch, 2 items × 256 payload characters 7.4858 7.6989 -1.92 [-7.05, +2.24] +0.73 [-1.74, +5.53]
Retained history, 4096 existing + 8 new messages 0.83976 0.42505 -49.04 [-49.53, -48.47] —
Tool loop, 1 turn 1.6447 1.6399 +0.22 [-1.36, +1.25] —
Tool loop, 16 turns 19.598 19.451 -0.56 [-0.89, -0.16] —
Tool loop, 4 turns 4.6017 4.6228 +0.59 [-0.73, +1.32] —
Warm final response 0.4864 0.48655 -0.06 [-0.58, +2.04] —
Tool-turn repetition control, 1 call, 1024 history messages 36.151 21.972 -39.37 [-40.61, -38.15] —
Tool-turn repetition control, 2 calls, 32 history messages 3.8225 3.7231 -2.10 [-3.73, -0.84] —
Tool turn, 12 calls, 64 history messages 17.055 16.834 -1.22 [-1.82, -0.94] —
Tool turn, 1 call, 1024 history messages 35.085 21.151 -39.36 [-40.33, -39.06] —
Tool turn, 1 call, 2048 history messages 94.143 40.526 -57.04 [-57.42, -56.34] —
Tool turn, 1 call, 256 history messages 7.8663 6.8996 -12.18 [-13.15, -11.38] —
Structured tool workload, 4 calls, 1024 history messages 61.696 48.253 -22.72 [-23.94, -19.73] —
Structured tool workload, 8 calls, 128 history messages 37.596 37.349 +1.53 [-3.20, +4.11] -0.51 [-0.82, +0.02]
32 concurrent streams, 2 tool calls, 128 history messages 480.98 467.55 -4.03 [-5.46, -2.12] -2.32 [-3.12, -1.06]
First completed run, 1 tool turn, including launch 1095.7 1092.6 +1.85 [-2.97, +4.64] +0.46 [-0.67, +1.11]
First completed run with empty tool server, 1 tool turn, including launch 1103.5 1108.6 +1.00 [-0.46, +4.24] +0.45 [-0.43, +1.14]
List output, 1 call × 1024 entries 6.0945 6.0681 -0.05 [-4.23, +2.39] —
List output, 8 calls × 8192 entries 61.097 59.735 -1.15 [-4.47, +0.81] —
Provider-response conversion, 1 call, 2048 history messages 333.67 275.05 -17.87 [-19.99, -14.96] —
Provider-response conversion, 1 call, 4096 history messages 846.57 592.6 -29.21 [-32.84, -26.01] —
Line-item calculation, 1 call × 1 record 1.7544 1.7562 +0.04 [-0.49, +0.82] —
Line-item calculation, 1 call × 64 records 2.6586 2.6551 -0.21 [-0.93, +0.81] —
Line-item calculation, 8 calls × 16 records 11.807 11.827 -0.26 [-0.98, +0.57] —
Line-item calculation, 8 calls × 256 records 38.254 38.012 -0.50 [-1.32, +1.47] —
Line-item calculation, 8 calls × 64 records 17.465 17.323 -0.45 [-2.59, +0.79] —
Structured response, 12 tool calls, 96 fields, normal 36.603 36.436 -0.86 [-2.03, +0.52] —
Structured response, 12 tool calls, 96 fields, stream 59.602 59.212 -0.61 [-2.04, +0.77] —
Structured response, 4 tool calls, 12 fields, stream 12.141 12.13 -0.17 [-1.70, +0.86] —
Structured response, 8 tool calls, 0 fields, stream 15.071 14.964 -0.05 [-1.95, +1.11] —
Sustained structured tool workload, 4 calls, 1024 history messages 59.643 45.055 -24.59 [-25.11, -23.84] —
Integer list, 1 call × 1 entry 1.7266 1.7271 +0.31 [-0.69, +0.82] —
Integer list, 8 calls × 8192 entries 33.783 33.566 -0.53 [-1.98, +1.60] —
Mixed list, 4 calls × 512 entries 10.431 10.443 +0.78 [-0.25, +1.34] —
Record list, 8 calls × 512 entries 13.386 13.331 -0.61 [-1.83, +0.31] —
Typed text list, 16 calls × 1024 entries 83.914 82.871 +0.07 [-2.51, +1.07] —
Typed text list, 4 calls × 128 entries 7.2285 7.2514 +0.37 [-0.14, +1.48] —
Warm tool control, 1 call, 0 history messages 1.7547 1.7458 -0.45 [-1.27, +0.15] —
Warm tool control, 1 call, 4096 history messages 36.036 36.38 +1.78 [-0.03, +2.80] —

Test plan and checks

  • New regression for two deliveries, equal but distinct messages, reordered and repeated references, and rewind. Existing tests for retries and resumption.
  • Full repository verifier (.agents/skills/code-change-verification/scripts/run.sh) including formatting, lint, mypy, Pyright and both pytest phases. 12,865 passed, 30 skipped.
  • Independent review of the complete diff. Identical Git tree for the local checks and PR head.

The example runner has been treating paths containing server as server examples. The first local suite has been reporting eight failures for this reason. In a neutral directory, the same files and file modes have been passing all 54 affected tests and the full verifier.

@markstuart-oai
markstuart-oai requested review from a team, rm-openai and seratch as code owners November 25, 2026 02:44
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 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-25T02:45:31.190779Z f2dd0d9 PR opened
🔒 Security Review ✅ Completed 2026-11-25T02:45:39.510748Z f2dd0d9 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.

@jbeckwith-oai jbeckwith-oai 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.

Reviewed delivery tracking, identity matching, fingerprint updates, and rewind/reordered-input coverage. The aligned-object shortcut preserves the existing identity fallback and delivery bookkeeping. No actionable findings. Static review only; no local code or tests run. Hosted CI: 24 successful checks on this commit.

@markstuart-oai
markstuart-oai added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 6afdd47 Nov 25, 2026
26 checks passed
@markstuart-oai
markstuart-oai deleted the perf/conversation-delivery-batches branch November 25, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants