Repository navigation
improve: reduce CPU spent on multilingual context estimates - #148663
Conversation
Use one character-range declaration for a native prefilter and fixed lookup. Preserve token estimates, raw safety floors, and UTF-16 budget boundaries while reducing CPU spent on multilingual prompt and tool-result text.
|
🦞👀 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 14, 2026, 9:23 PM ET / September 15, 2026, 01:23 UTC (Revision 2). ClawSweeper reviewWhat this changesReplace repeated multilingual character scans with a shared lookup table while preserving context estimates, safety floors, and text cuts. Merge readiness✅ Ready for maintainer review This remains a useful performance improvement: current main still performs repeated category scans. No actionable correctness or security defect was found in the introduced changes. Priority: P2 Review scores
Verification
How this fits togetherOpenClaw’s shared text estimator converts prompt and tool-result text into approximate context costs. Compaction and truncation callers use those costs to keep requests within their budgets. flowchart TD
A[Prompt and tool-result text] --> B[Shared text estimator]
B --> C{Weighted characters present?}
C -->|No| D[Raw text cost]
C -->|Yes| E[Unicode category lookup]
E --> F[Weighted context cost]
D --> G[Compaction and truncation budgets]
F --> G
Before mergeNone. Agent review detailsSecurityNone. PR surfaceSource +79, Tests +4. Total +83 across 2 files. View PR surface stats
Review metrics
Technical reviewBest possible solution: Keep one shared estimator with identical budgeting semantics and the explicitly bounded lookup-memory tradeoff. Do we have a high-confidence way to reproduce the issue? Not applicable as a correctness bug: this is a performance optimization. Source confirms repeated scans on main, and the PR reports bounded before/after component measurements. Is this the best way to solve the issue? Yes. The patch optimizes the existing owner, preserves its public contracts, and uses one range declaration for both filtering and lookup. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against dc1bbb4f5b12. LabelsLabel 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.
|
…#148663) * perf: avoid repeated scans in weighted text estimates Use one character-range declaration for a native prefilter and fixed lookup. Preserve token estimates, raw safety floors, and UTF-16 budget boundaries while reducing CPU spent on multilingual prompt and tool-result text. * fix: handle unweighted character lookup results explicitly
What Problem This Solves
Multilingual prompt, compaction, and tool-result budgeting repeatedly scans the same text for different character weights, adding synchronous CPU work to agent turns.
User Impact
Weighted text estimates use less CPU while preserving the same token counts, safety floors, and text cuts. The tradeoff is a fixed 205 KiB lookup table per loaded module instance. No configuration, schema, or API changes are required.
Why This Change Was Made
A single range declaration now builds the category lookup and a native prefilter. ASCII and text without weighted characters return early; the numeric scan starts at the first weighted character. This removes the repeated category scans and duplicated range union without changing the weighting policy.
Evidence
All 2,268,082 differential comparisons passed on the final source, covering every Unicode code point at both optimized floors, mixed boundaries, lone surrogates, repeated calls, fractional floors, Infinity, and NaN. The candidate also passed 229 tests across the actual estimator, compaction, request budgeting, tool-result truncation, review transcripts, and memory chunking. Targeted type-aware lint passed. Independent review through P2 found no actionable issues.
One bounded ABBA screen on Node 26.7.0 used text up to 16,384 UTF-16 units. Mean CPU per 128 calls fell from 32.94 to 3.43 ms for common CJK, 29.91 to 4.04 ms for rare/supplementary text, and 27.06 to 3.88 ms for alternating categories. Additional cases cover early accents, early and sparse common CJK, supplementary prefixes, short mixed text, and single code points. The accented-text case stayed near baseline; ASCII wall time differed by about 0.027 ms per 128 calls. ASCII and single-code-point CPU readings were small and noisy, so they do not establish a speedup. These are synthetic component measurements, not a whole-Gateway or startup-speed claim.
The full local build and complete changed-file checks passed, including core/test type checks, lint, dead-export scans, storage guards and import-cycle checks. Hosted CI passed on
614228c974042e89dc0159b648f446144c4147d0.