Skip to content

perf(gateway): reuse sharing keys across session deltas - #184201

Merged
steipete merged 1 commit into
mainfrom
steipete/perf-micro-sharing-cache-key-20260919
Oct 26, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/perf-micro-sharing-cache-key-20260919

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Streaming session broadcasts repeatedly sort and serialize the same viewer-sharing identity for each connected recipient, even when the session-sharing revision and viewer facts have not changed.

User Impact

Less allocation and CPU in session-event fanout. Wire payloads, audience grouping, membership reads, and live authorization remain unchanged.

Why This Change Was Made

Retain the serialized key under the existing projection sharing revision, with weak recipient keys. Every call still prepares current sharing policy and authorization. Reuse also checks config publication, access revision, resolved profile/policy, authenticated identity, and scope contents. Revision retirement drops the old cache; disconnected clients are weakly held. Generic synchronous readers do not opt into retained keys.

This deliberately retains only serialization, not sharing closures: a membership reader belongs to the current projection/read. Related: #184097 removes a different allocation source in the event envelope.

Production LOC is net +46 (+50/−4). Runtime growth is bounded to the key cache and its invalidation checks; these preserve config publication, profile merges, in-place scope changes, and canonical cross-client audience grouping. No new configuration or storage is introduced.

Evidence

Acceptance declared in scripts/perf/session-sharing-key-measure.ts: identical key/role digest with lower owner allocation and CPU. The workload uses 65 interleaved clients, eight identities, 10,000 synthetic target entries, and 3,000 events; sharing revisions and membership change every 100 events. Forty configured plugin entries are synthetic config shape only; no plugin or provider execution is measured.

Base: 29397d2a185c29d4b4d79d2ad41af0f7cf1f6a9b. Timing and sampled allocation are separate passes, five counterbalanced pairs each. Main-thread CPU uses process.threadCpuUsage; V8 sampling includes objects collected by minor and major GC. Host load and memory are recorded per run.

Metric per 3,000 events Base median (range) Candidate median (range) Paired mean change
Main-thread CPU 229.1 ms (221.2–238.0) 167.9 ms (167.2–211.4) −52.8 ms, −23.0%
Sampled allocation 743.9 MB (739.2–745.2) 574.9 MB (566.2–581.6) −167.9 MB, −22.6%

Every pair improved. Node 26.12.1 on macOS. One-minute host load ranges: timing base 45.9–93.6, candidate 40.9–85.6; allocation base 34.6–50.9, candidate 37.8–65.3. The paired order is AB/BA/AB/BA/AB for each mode. These are process-thread CPU measurements, not elapsed-time or Gateway-latency claims. All twenty runs produced digest 3db36efcb344be52f92c8c98cea318b4cbf4ee7f61bfc27cf5ca6df3a444bf22.

Command: node --expose-gc --import ./scripts/tsx.mjs scripts/perf/session-sharing-key-measure.ts --events 3000; add --owner src/gateway/.session-sharing-read-base.ts for a saved base sibling and --allocations for the separate allocation pass.

The broadcast regression fails on base with 24 key encodings; the expected 16 represent cold recipients, while eight already-primed recipients need no re-encoding. Existing payload, interleaved delivery-history, and mid-broadcast revocation assertions remain. The prepared-profile regression covers profile merges, scope edits, same-object policy changes with revision retirement, and identity detachment.

Seven focused tests pass across session-row-delta-audiences, session-row-publication-authority, and session-sharing.prepared. Changed audience file: 10.2 s test execution; changed prepared-profile file: 1.8 s. The unchanged publication-authority fixture took 25.3 s. Wrapper wall time was 874.6 s on the loaded host; compilation/import preparation dominated. This extends existing fixtures rather than adding another Gateway boot.

Changed-file formatting and core lint passed. The script typecheck passed. Script lint initially found three missing loop braces in the benchmark; these were corrected without changing its workload. Final script lint passed. Both closure guard files passed (22 tests, 434.6 s wrapper wall time), and the export-collision guard passed after the temporary measurement baseline source was removed.

Independent subagent review: clean through P2 (autoreview route unavailable because its provider selector did not match the operator configuration). No actionable findings. The review covered the diff, relevant source contracts, regressions, and benchmark in a fresh context. No Testbox lease was used.

The lane's explicit fast-path exception applies to this duplicate-serialization removal. No whole-Gateway latency, GC, or retained-memory improvement is claimed, and no long Gateway stress campaign was run.

Retain serialized viewer keys under the projection sharing revision while preparing live policy and membership on every call. Preserve canonical audience grouping and invalidate reuse for identity, scope, profile, policy, and config publication changes.

Owner benchmark: paired mean main-thread CPU -23.0%, sampled allocation -22.6%; identical key and role digests. Broadcast and profile regressions plus focused guards pass. Independent review clean through P2.
@clawsweeper

clawsweeper Bot commented Oct 26, 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.

@steipete
steipete marked this pull request as ready for review October 26, 2026 04:32
@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime scripts Repository scripts labels Oct 26, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T04:34:51.748322Z f3581ef 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.

@steipete
steipete merged commit 24fa04e into main Oct 26, 2026
198 of 200 checks passed
@steipete
steipete deleted the steipete/perf-micro-sharing-cache-key-20260919 branch October 26, 2026 04:45
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 27, 2026
)

Retain serialized viewer keys under the projection sharing revision while preparing live policy and membership on every call. Preserve canonical audience grouping and invalidate reuse for identity, scope, profile, policy, and config publication changes.

Owner benchmark: paired mean main-thread CPU -23.0%, sampled allocation -22.6%; identical key and role digests. Broadcast and profile regressions plus focused guards pass. Independent review clean through P2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Gateway runtime scripts Repository scripts size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant