Repository navigation
improve: reduce allocations while budgeting multilingual tool results - #138646
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: needs maintainer review before merge. Reviewed September 4, 2026, 7:17 PM ET / 23:17 UTC. ClawSweeper reviewWhat this changesThe PR moves weighted multilingual text counting into the shared estimator and avoids temporary match arrays while preserving tool-result budgets and truncation output. Merge readiness✅ Ready for maintainer review This remains a useful optimization absent from the reviewed main and release sources. The updated Gateway comparison addresses the previous review’s proof request, and no introduced correctness defect remains. Priority: P3 Review scores
Verification
How this fits togetherOpenClaw estimates tool-output size before passing it into model context. The shared Unicode counter feeds context guards, truncation, and file-read pagination so multilingual text stays within existing limits. flowchart TD
A[Tool output and file pages] --> B[Shared Unicode counter]
C[Existing caller safety floor] --> B
B --> D{Fits text budget?}
D -->|Yes| E[Unchanged output]
D -->|No| F[Unicode-safe truncation]
E --> G[Model context]
F --> G
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +2, Tests +32. Total +34 across 4 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep one shared weighted counter while preserving existing estimator signatures, numeric semantics, and Unicode-safe truncation boundaries. Do we have a high-confidence way to reproduce the issue? Not applicable to a correctness bug: this is an allocation optimization, supported by captured baseline/candidate measurements and output-equivalence checks rather than a reported functional failure. Is this the best way to solve the issue? Yes. Moving weighted arithmetic into the existing Unicode owner removes the local counting path while preserving callers; replacing the heuristic with a tokenizer would change the contract and broaden scope unnecessarily. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against dcdeede2fa47. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (1 earlier review cycle)
|
|
Merged via squash.
|
What Problem This Solves
Budgeting large multilingual tool results creates substantial temporary allocation as results are checked and trimmed for model context. The context guard's raw-weight floor makes this particularly expensive for dense CJK text.
Why This Change Was Made
Move weighted counting into the shared CJK owner, count the existing character categories without materializing match arrays, and calculate the common raw-weight floors over the whole string. Preserve the one-argument public estimator, existing Unicode weights, UTF-16 boundaries, and the original rounding and nonfinite arithmetic for other floors.
User Impact
Tool-result budgets and truncation output remain identical. Dense multilingual inputs use less temporary allocation in the measured counting path; some default-weight and sparse-input cases are slower, as detailed below.
Production LOC: +48/-46 (net +2). Tests: +32/-0. The small production increase retains the public callback contract while consolidating the weighted owner and removing the previous local weighted path.
Evidence
Exact baseline/candidate output hashes match for all 1,114,112 Unicode code points, 4,352 mixed-weight cases, 34,816 prefix/tail boundary cases, and six real context-guard/read-page scenarios. Coverage includes unpaired surrogates, fractional and very large weights, NaN/Infinity, notices, continuations, and unchanged input messages.
In baseline/candidate/candidate/baseline microbenchmarks, the following means cover 32 estimates of 65,536 UTF-16 units at raw weight 2:
Default-weight rare/supplementary and multilingual cases were 15% and 11% slower. The sparse weight-2 difference is about 0.043 ms per 64K call. These are two observations per arm, not an end-to-end latency claim.
In a separate fixed six-fixture workload, allocation sampling estimated 2.12/2.14 GB of cumulative allocations in the baseline estimator stacks and recorded no samples there after the change. Sampling does not measure retained heap, and zero samples do not prove zero engine allocation.
All 443 focused tests across nine files passed. The full build, native imports of the built SDK, exhaustive packaged Unicode comparison, complete changed-file gate, and independent managed Codex review through P2 passed. The native check also verifies shared-owner identity and callback behavior. An initial gate rejected global
NaNliterals in the new test table; changing them toNumber.NaN, rerunning the affected 19 tests, and rerunning the complete gate resolved it.Integrated Gateway comparison
The controlled built-Gateway comparison is complete: baseline/candidate/candidate/baseline, two observations per arm and ten model turns per run across Code Mode and ToolSearch. Candidate
15216423e92ccontains all 20 changes, including this exact patch. The independent audit verified the 40 turns, unchanged history prefixes, exact file output and Unicode truncation, and recomputed the recorded comparison values.These are main-isolate sampled allocation estimates, including collected objects, for the combined candidate; they do not isolate this PR's contribution. ToolSearch's two paired differences have opposite signs. Post-GC idle memory did not decrease: Code Mode RSS was 484.88→485.78 MiB and main heap 268.43→268.45 MiB; ToolSearch RSS was 480.64→480.71 MiB and main heap 259.92→260.13 MiB. RSS includes Gateway workers, while heap and allocation sampling cover the main isolate.
Latency was mixed: instrumented stage totals changed by −1.75% for Code Mode and +0.95% for ToolSearch. Those intervals include fixed settling and sampling overhead, with live provider and cache variability. Two observations per arm establish neither statistical significance nor a uniform latency or retained-memory improvement. Normal native landing gates still apply.