Skip to content

perf(gateway): avoid repeated session pin checks while sorting - #183967

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-sort-limit-session-entries-20260919
Oct 26, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-sort-limit-session-entries-20260919

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Session-list sorting repeatedly parses the same pinned session keys during comparisons, allocating temporary strings and key objects.

User Impact

Ordering, pin eligibility, paging, and returned entry identities stay identical. This reduces sorting work when pins are present; it does not claim a measured whole-Gateway latency improvement.

Why This Change Was Made

Prepare eligibility once for a synchronous sort, on its first comparison involving a pin timestamp. Keep the result in a sort-local set and discard it on return. Unpinned lists and other sort modes never prepare eligibility; subsequent sorts read current entry metadata. The existing comparator remains the ordering owner, including direct callers outside list sorting.

The earlier prepared-key change #179211 was reverted by #180166 after flat Gateway measurements (+1.6% CPU/RPC and +10% promotion) and added retained state/ordering machinery. This change does not restore its row snapshots, caller changes, sharing wrappers, or cross-request cache. Runtime production delta: +22 lines (benchmark +107, tests +65; total non-test delta +129): a lazy preparation callback and its local set preserve the direct-comparator contract without mutating entries or retaining facts between sorts.

Evidence

  • Regression: current base made 821 eligibility checks for 74 pin-bearing entries; candidate makes 74. Re-sorting after entry edits uses fresh eligibility.
  • Property-style equivalence against the uncached comparator covers all supported sort modes, ascending/descending/random inputs, empty/single-entry input, bounded/full sorting, and limits around the top-N boundary. Input order and returned identities are preserved.
  • Independent subagent review (autoreview route unavailable): clean for P0/P1/P2. The helper's provider preflight refused a route/config mismatch; operator configuration was left unchanged.
  • The benchmark is committed in scripts/perf/session-list-order-measure.mjs, with usage in its header. Synthetic 10,000-session inventory, 80% subagent keys, 200 pinned dashboard entries or zero pins; 2,500 sorts per case; separate untraced CPU/GC and allocation-sampling passes. Baseline e79654199eb1; the measured import closure is unchanged by the pre-push rebase. Linux, Node 26.12.1, exclusive benchmark slot, simultaneous interleaved CPUs, swapped placement. No production Gateway load was generated.
  • Acceptance: exact ordering, lower allocation, and no descending CPU regression established beyond identical-source controls. The measurements below satisfy that owner-level acceptance rule.

Validation: the new ordering file (5 tests), shared sorting (9 tests), and existing session utilities (72 tests) pass. Formatting, focused type-aware lint, and the export-name guard pass. The owner test file took 9.85 s on the busy local host (2.36 s test time); the existing session-utils file took 536.38 s including its compiled-worker setup. An initial session-utils setup was invalidated by an edit during compilation; the fixed-source rerun passed. The eager-import closure, PR-wrapper source closure, and CI Node test-plan guards also pass (178 tests; 701.63 s total on this host). The lane's explicitly authorized fast-path proof uses owner measurements and PR CI; a ten-minute Gateway stress campaign is not claimed.

Paired owner measurements, controls, and tradeoffs

Three A/B pairs (two swapped slot-A pairs, then the predeclared extra pair in slot B after opposite-sign descending results); 7,500 sorts per arm/case. CPU is untraced process CPU, including GC. Four matched A/A pairs used the same workload and instrumentation.

Pins Input Limit CPU µs/sort, base → candidate CPU delta Sampled bytes/sort, base → candidate GC ms/1,000 sorts, base → candidate
200 ascending 100 3650.48 → 284.39 -92.2% 6,620,038 → 131,065 39.236 → 1.312
200 ascending all 288.13 → 268.78 -6.7% 394,169 → 290,359 3.144 → 1.912
200 descending 100 459.93 → 452.60 -1.6% 555,076 → 451,334 1.824 → 1.702
200 descending all 291.75 → 268.55 -8.0% 393,767 → 289,901 1.770 → 1.535
200 random 100 2230.74 → 268.31 -88.0% 3,744,630 → 150,449 10.930 → 0.643
200 random all 2300.51 → 1483.44 -35.5% 1,941,955 → 369,183 8.649 → 2.354
0 ascending 100 100.04 → 101.11 +1.1% 2,430 → 2,525 0.000 → 0.000
0 ascending all 128.33 → 129.81 +1.2% 160,969 → 161,055 0.653 → 0.691
0 descending 100 309.25 → 315.18 +1.9% 322,209 → 323,045 1.044 → 1.087
0 descending all 143.92 → 145.82 +1.3% 160,949 → 161,095 0.637 → 0.781
0 random 100 118.35 → 122.70 +3.7% 21,360 → 21,534 0.109 → 0.135
0 random all 1082.16 → 1110.93 +2.7% 240,677 → 240,851 1.162 → 1.433

Pinned descending CPU: −1.6% bounded / −8.0% full. Unpinned descending CPU: +1.9% (+5.93 µs/sort) bounded / +1.3% (+1.89 µs/sort) full; these small shifts are within identical-source controls, not established regressions. A/A bounded descending deltas: pinned +0.29%, +4.64%, −4.06%, +2.74%; unpinned +5.13%, +3.01%, +0.38%, −2.35%. Unpinned full-sort A/A deltas: +1.51%, +1.08%, +0.63%, −2.35%.

Tradeoff: this is not a universal speedup. Unpinned random inputs cost +3.7% bounded (+4.35 µs) / +2.7% full (+28.77 µs); pin-heavy bounded inputs reduce CPU 88–92% and allocation 96–98%. An eager preparatory scan was discarded because it added unnecessary work to unpinned lists.

Pair 1: maximum natural GC pause 0.325 → 0.221 ms; host 1-minute load 2.57–2.90. Base/candidate maximum RSS 890.0/212.8 MiB, sampled end-of-timing heap 247.1/34.7 MiB. GC by kind (1=minor, 4=major, 8=incremental), base {'1': {'count': 3454, 'ms': 172.067}}, candidate {'1': {'count': 477, 'ms': 36.703}, '8': {'count': 1, 'ms': 0.221}}.

Pair 2: maximum natural GC pause 0.490 → 0.228 ms; host 1-minute load 2.32–2.82. Base/candidate maximum RSS 847.7/203.9 MiB, sampled end-of-timing heap 236.2/34.7 MiB. GC by kind (1=minor, 4=major, 8=incremental), base {'1': {'count': 3411, 'ms': 166.116}}, candidate {'1': {'count': 477, 'ms': 35.255}, '8': {'count': 1, 'ms': 0.228}}.

Pair 3: maximum natural GC pause 0.505 → 0.211 ms; host 1-minute load 1.23–2.86. Base/candidate maximum RSS 862.5/208.8 MiB, sampled end-of-timing heap 236.6/35.0 MiB. GC by kind (1=minor, 4=major, 8=incremental), base {'1': {'count': 3397, 'ms': 180.5}}, candidate {'1': {'count': 477, 'ms': 29.271}, '8': {'count': 1, 'ms': 0.211}}.

No pause percentile is used as a gate. Owner GC deltas are far below +1 ms/operation. RSS/heap snapshots include effects of earlier allocation-sampling passes in the same process and are context only; this is not a retained-heap or whole-Gateway benchmark.

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

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime scripts Repository scripts size: M labels Oct 25, 2026
@steipete
steipete marked this pull request as ready for review October 25, 2026 23:50
@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-25T23:52:17.665823Z d9ce1df Draft marked ready
ℹ️ 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.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 26, 2026
@steipete
steipete merged commit e256b65 into main Oct 26, 2026
208 of 209 checks passed
@steipete
steipete deleted the steipete/perf-micro-sort-limit-session-entries-20260919 branch October 26, 2026 00:05
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

clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. gateway Gateway runtime scripts Repository scripts size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant