Skip to content

Speed up input preparation for long conversation histories - #6220

Open
markstuart-oai wants to merge 1 commit into
mainfrom
perf/input-message-traversal
Open

markstuart-oai wants to merge 1 commit into
mainfrom
perf/input-message-traversal

Conversation

@markstuart-oai

@markstuart-oai markstuart-oai commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

For 32 concurrent requests with 2,048 plain messages each, the median time to finish the batch has been dropping from 491 to 415 ms. For one request with 2,000 messages, this change has been saving about 1 ms. These gains have been coming from input preparation before the model call.

The SDK has been copying each message dictionary just to read its deduplication key. It has also been checking each dictionary against the generic mapping interface and making a recursive call for each string while preparing input.

With this change, the SDK has been reading keys directly from plain dictionaries and copying string values without another function call. It has been building fresh containers for each occurrence of a message, including nested content. Custom mappings, models and string subclasses have been keeping their existing conversion paths.

Benchmarks

Complete request Main, ms PR, ms Change [95% interval]
2,000 input messages 6.327 5.34 -16.14% [-16.87, -14.08]
720 input messages 2.743 2.364 -13.43% [-14.84, -11.61]
120 input messages 0.9396 0.8783 -6.55% [-8.12, -6.04]
Warm final response 0.494 0.4923 -0.30% [-2.00, +2.65]
32 concurrent streams, second batch 433.2 422.8 -5.26% [-9.42, +4.33]
32 concurrent streams, sum of three timed batches 1711 1746 -0.77% [-7.75, +7.08]

32 concurrent requests with client-side history. A separate offline test with the same agent, a distinct history per request, one tool call and two scripted replies. Each request has been returning the completed answer and the full replay history. Six timed batches after two warmups per process, 24 matched process pairs.

History per request Main, ms per batch PR, ms per batch Paired change [95% interval]
No prior messages 41.81 40.54 −0.36% [−3.70, +0.05]
720 plain messages 212.94 185.95 −13.02% [−15.17, −12.07]
2,048 plain messages 491.12 414.65 −15.08% [−16.43, −13.80]
720 messages with nested text, annotations and log probabilities 1,226.42 1,165.62 −4.89% [−6.63, −4.39]

For 2,048 plain messages, the total across six batches, including checks and cleanup, has been dropping from 2.89 to 2.42 seconds (−15.95%). Both sides have been doing a median of four full garbage collections during the six batches. A separate pilot with one request and 4,096 nested messages has been inconclusive. No expected reduction in model or network time.

In a third test with one tool call and 2,048 plain messages per request, elapsed time has been dropping from 11.71 to 9.65 ms (−18.5%, 95% interval −22.6 to −14.0%). The warm request with one registered tool, no tool calls and no prior messages has been measuring 0.619 ms on main and 0.637 ms on the branch (+2.9% [0.7, 4.8]). The 83-case suite's warm final response has been measuring −0.30% [−2.00, +2.65].

In 48 new blocks with warmed Python bytecode on every build, the same no-call case has been measuring −0.05% [−0.83, +0.48] on this branch. Two identical builds of main have been differing by −0.43% [−1.51, +0.18]. The test has been using every order of four builds on each of two CPUs.

Competing work on the benchmark host. During the first 83 comparisons and the independent repeats, the median one-minute load averages have been running at 80 and 49 respectively on a 32-CPU host. The 64-pair repeat has been leaving eight comparisons uncertain, including the large SQLite write at +11.53% [0.10, 21.49]. All samples have been staying in the table below.

A synchronized SQLite comparison has been rotating two copies of each build across four writer positions in 128 blocks. The large write has been measuring −0.05% [−0.38, +0.05] and the small one −3.05% [−5.81, +0.04]. Both same-code controls have been covering zero for each write size. An untimed trace has been recording zero calls to either changed helper. These results have been pointing to disk timing.

Separate run on a lightly loaded host. With four workers and 64 new process pairs, all six other uncertain comparisons and the 32-stream control have been meeting the +3% limit. No pooling or substitution in the original table.

Workload Main, ms PR, ms Paired change [95% interval]
Three large integer lists, streamed 15.91 15.75 -0.29% [-1.12, +1.12]
First run including launch 1075 1076 +0.22% [-0.54, +1.09]
First run with an empty tool server 1070 1073 +0.03% [-0.68, +0.64]
Eight large integer lists through HTTP 55.09 55.51 +0.03% [-0.58, +0.81]
67-field output with 16 tool turns, streamed 73.19 73.07 -0.29% [-0.66, +0.21]
First request on a fresh agent 40.01 39.58 -2.06% [-3.09, +0.39]
32 concurrent streams, median of three batches 452.8 448.7 -0.77% [-2.89, +0.30]

For 32 streams, batch two has been measuring −1.68% [−2.47, −0.57], and the sum of all three −1.06% [−2.85, −0.01].

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 65 18 0
Independent repeat, 19 selected 11 8 0
Latest result per workload 75 8 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 49.163 48.077 +3.45 [-3.41, +14.53] -6.11 [-8.99, +3.26]
Fresh-agent control, streaming 338.1 315.5 -4.71 [-9.95, +5.39] -9.22 [-13.71, -0.97]
Integer-list output, 17 calls, 32769 initial entries, normal 249.77 243.98 -2.83 [-5.64, -0.04] —
Integer-list output, 17 calls, 32769 initial entries, streaming 316.92 300.62 +1.33 [-0.76, +3.66] -2.16 [-6.31, +2.74]
Integer-list output, 3 calls, 8193 initial entries, normal 14.657 13.989 +1.62 [-1.37, +4.53] -4.47 [-9.35, -0.62]
Integer-list output, 3 calls, 8193 initial entries, streaming 17.905 17.659 +0.85 [-1.88, +4.69] -1.49 [-7.07, +3.60]
Reused-agent control, normal 38.975 33.399 -13.96 [-16.78, -9.49] —
Reused-agent control, streaming 43.057 35.001 -17.08 [-20.05, -13.52] —
Typed text-list output, 3 calls, 257 initial entries, normal 6.8699 6.8103 -0.83 [-2.06, +2.25] —
Typed text-list output, 3 calls, 257 initial entries, streaming 8.4423 8.4122 -0.94 [-3.91, +1.09] —
Dynamic tool addition 1.7469 1.7383 +0.07 [-1.26, +1.01] —
Agent with 12 tools, dictionary settings, construction only 0.05163 0.051165 -0.65 [-1.68, +0.01] —
Agent with 12 tools, default settings, construction only 0.071152 0.070754 -0.43 [-1.12, +0.85] —
Agent with 12 tools, typed settings, construction only 0.038312 0.038165 -0.06 [-0.98, +0.17] —
Concurrent runs, 1 task 0.51015 0.50719 -0.51 [-1.48, +0.74] —
Concurrent runs, 32 tasks 12.549 12.448 -0.95 [-2.58, +1.62] —
Concurrent runs, 8 tasks 2.9547 2.9607 -0.37 [-0.95, +0.42] —
32 concurrent runs with GC observation 13.721 13.366 +0.51 [-3.79, +5.56] -2.40 [-6.44, +1.13]
Stream completion, 128 history messages 1.2765 1.2164 -4.97 [-5.65, -4.32] —
0 available handoffs 0.51454 0.51682 +1.20 [-0.93, +5.22] -0.59 [-2.11, +2.63]
12 available handoffs 0.72097 0.71716 -0.16 [-1.86, +1.40] —
Input history, 120 messages 0.93956 0.87828 -6.55 [-8.12, -6.04] —
Input history, 2000 messages 6.3275 5.3404 -16.14 [-16.87, -14.08] —
Input history, 720 messages 2.7429 2.3639 -13.43 [-14.84, -11.61] —
6 handoff routes, mixed agent names 0.75343 0.73921 -2.47 [-3.53, -0.75] —
Structured streamed response, 67 fields, 16 tool turns 93.193 93.011 +1.46 [-10.55, +12.09] +0.15 [-4.77, +13.41]
8 handoff routes, repeated agent names 1.0162 1.0165 -0.23 [-2.74, +0.94] —
Retained history, 16384 existing + 1 new message 0.41162 0.41456 +0.62 [-0.85, +1.81] —
Retained history, 2048 existing + 1 new message 0.053937 0.05356 -0.46 [-1.39, +0.34] —
Agent with 6 tools, dictionary settings, construction + run 0.78495 0.78572 -0.13 [-2.85, +1.09] —
Agent with 6 tools, default settings, construction + run 0.8394 0.84591 +0.84 [-0.74, +2.19] —
Agent with 6 tools, typed settings, construction + run 0.76463 0.757 -1.79 [-3.37, +2.13] —
24 available tool schemas 0.86639 0.85525 -1.19 [-2.21, +0.68] —
Server-managed conversation, 1 history message 1.9129 1.8665 -3.27 [-9.14, -0.80] —
Server-managed conversation, 1024 history messages 36.612 35.993 -2.44 [-4.33, +2.81] —
Server-managed conversation, 128 history messages 4.9388 4.8649 -0.89 [-2.89, +0.67] —
Server-managed conversation, 512 history messages 16.366 15.899 -3.69 [-8.67, +0.61] —
0 handoff routes, distinct agent names 0.51807 0.515 -0.23 [-2.67, +2.13] —
12 handoff routes, distinct agent names 0.61058 0.6 -0.99 [-5.39, +0.94] —
Streamed final response 0.80199 0.7944 -1.43 [-2.49, -0.30] —
32 sustained concurrent runs 12.489 12.394 -0.54 [-1.66, +1.55] —
SQLite committed batch, 1024 items × 4096 payload characters 86.525 94.695 -9.90 [-21.36, +33.39] +11.53 [+0.10, +21.49]
Retained history, 16384 existing + 2 new messages 0.81462 0.81328 +0.01 [-0.86, +0.79] —
Retained history, 2048 existing + 2 new messages 0.10636 0.10522 -0.48 [-1.39, +0.31] —
SQLite committed batch, 2 items × 256 payload characters 10.423 10.827 -1.28 [-8.83, +3.69] +0.72 [-5.20, +9.24]
Retained history, 4096 existing + 8 new messages 0.84372 0.84192 -1.48 [-1.87, +0.02] —
Tool loop, 1 turn 1.6407 1.6423 -0.19 [-1.31, +1.51] —
Tool loop, 16 turns 19.939 19.823 +0.33 [-3.89, +1.76] —
Tool loop, 4 turns 4.9174 4.806 -1.42 [-2.54, -0.16] —
Warm final response 0.49403 0.49231 -0.30 [-2.00, +2.65] —
Tool-turn repetition control, 1 call, 1024 history messages 43.715 40.097 -4.93 [-13.24, +15.12] -4.31 [-12.05, -0.65]
Tool-turn repetition control, 2 calls, 32 history messages 3.8687 3.8293 +0.08 [-1.75, +1.13] —
Tool turn, 12 calls, 64 history messages 17.028 16.748 -2.29 [-3.70, -0.79] —
Tool turn, 1 call, 1024 history messages 35.942 35.171 -3.31 [-4.35, -0.48] —
Tool turn, 1 call, 2048 history messages 94.835 93.385 -0.96 [-2.04, +2.74] —
Tool turn, 1 call, 256 history messages 7.9011 7.7483 -2.83 [-4.13, -0.65] —
Structured tool workload, 4 calls, 1024 history messages 61.377 59.927 -1.65 [-3.39, +0.07] —
Structured tool workload, 8 calls, 128 history messages 41.54 40.739 -0.68 [-2.60, +3.68] -0.54 [-6.02, +1.86]
32 concurrent streams, 2 tool calls, 128 history messages 524.38 532.36 -1.87 [-3.59, +0.03] -1.74 [-4.20, +0.49]
First completed run, 1 tool turn, including launch 1885.8 1883 +1.09 [-8.41, +9.35] +1.76 [-4.78, +5.84]
First completed run with empty tool server, 1 tool turn, including launch 1779.1 1913.5 +1.50 [-6.90, +7.42] +2.21 [-0.75, +7.52]
List output, 1 call × 1024 entries 5.9873 6.0328 -1.74 [-4.89, +2.46] —
List output, 8 calls × 8192 entries 66.048 66.022 -0.46 [-3.80, +3.36] -1.27 [-4.46, +3.39]
Provider-response conversion, 1 call, 2048 history messages 383.29 350.3 -0.15 [-0.79, +3.53] -7.90 [-11.78, -5.28]
Provider-response conversion, 1 call, 4096 history messages 936.63 899.83 +2.45 [-0.84, +4.53] -2.99 [-6.29, +0.67]
Line-item calculation, 1 call × 1 record 1.7492 1.7478 -0.01 [-0.80, +0.41] —
Line-item calculation, 1 call × 64 records 2.6575 2.6511 -0.37 [-1.36, +0.24] —
Line-item calculation, 8 calls × 16 records 11.737 11.776 -0.09 [-0.38, +0.32] —
Line-item calculation, 8 calls × 256 records 37.415 38.071 +0.94 [+0.07, +1.49] —
Line-item calculation, 8 calls × 64 records 17.198 17.206 +0.07 [-0.49, +0.56] —
Structured response, 12 tool calls, 96 fields, normal 36.454 36.529 -0.56 [-1.80, +1.53] —
Structured response, 12 tool calls, 96 fields, stream 61.435 60.207 -0.99 [-8.24, +3.04] -2.75 [-3.97, +0.04]
Structured response, 4 tool calls, 12 fields, stream 12.383 12.263 -0.42 [-3.52, +2.32] —
Structured response, 8 tool calls, 0 fields, stream 15.098 15.133 -0.63 [-2.42, +0.82] —
Sustained structured tool workload, 4 calls, 1024 history messages 59.471 57.409 -3.36 [-4.61, -2.32] —
Integer list, 1 call × 1 entry 1.7175 1.7158 +0.10 [-0.78, +0.49] —
Integer list, 8 calls × 8192 entries 33.344 33.247 +0.26 [-1.64, +0.80] —
Mixed list, 4 calls × 512 entries 10.387 10.306 -0.59 [-1.31, +0.38] —
Record list, 8 calls × 512 entries 13.206 13.089 -1.14 [-2.10, -0.29] —
Typed text list, 16 calls × 1024 entries 82.213 80.843 -1.64 [-3.53, -0.11] —
Typed text list, 4 calls × 128 entries 7.1485 7.0884 -0.21 [-1.15, +0.07] —
Warm tool control, 1 call, 0 history messages 1.7539 1.7463 -0.44 [-0.79, -0.03] —
Warm tool control, 1 call, 4096 history messages 36.062 29.912 -16.96 [-17.78, -15.08] —

Test plan and checks

  • New tests for mutating one of two repeated messages and for ordering mixed messages, reasoning, tool calls and outputs. Existing tests for non-dictionary mappings, iterables and saved state.
  • Full repository verifier (.agents/skills/code-change-verification/scripts/run.sh) including formatting, lint, mypy, Pyright and both pytest phases. 12,866 passed, 30 skipped.
  • Independent review of the complete diff. Identical Git tree for the local checks and PR head.

@markstuart-oai
markstuart-oai requested review from a team, rm-openai and seratch as code owners November 25, 2026 04:55
@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-25T04:56:46.226646Z 03209d5 PR opened
🔒 Security Review ✅ Completed 2026-11-25T04:57:23.132663Z 03209d5 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.

Approved after comprehensive static review and independent review. The exact-dictionary deduplication shortcut only reads identity fields, and the string shortcut preserves fresh nested containers and existing custom mapping/subclass behavior. No actionable findings.

Hosted CI: 24 successful checks on this commit. No candidate code, local tests, or benchmarks were executed.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants