Skip to content

perf(ui): skip grapheme segmentation for fitting strings - #177478

Merged
steipete merged 1 commit into
mainfrom
solid2/w-perf-graphemes
Oct 19, 2026
Merged

steipete merged 1 commit into
mainfrom
solid2/w-perf-graphemes

Conversation

@steipete

@steipete steipete commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Related: #174756

What Problem This Solves

Short person labels were still segmented character by character when generating activity links during sidebar rendering and live updates.

User Impact

Less main-thread work for short labels. Truncation, Unicode grapheme boundaries, routes, and rendered text stay the same.

Why This Change Was Made

The source-mapped Team-scale profile identified takeGraphemes as the largest application self-time on reload (63.944 ms). A grapheme occupies at least one UTF-16 code unit, so a string whose UTF-16 length already fits the requested grapheme limit needs no segmentation. Return it directly; retain the existing segmenter loop whenever truncation might be needed.

This deletes work before considering caching. No cache, dependency, new owner, or lazy-boundary change is involved. Production LOC is 15 → 19 (+4) for the guard and its explanation. The direct test adds 26 lines and checks zero Segmenter calls for fitting labels plus Unicode and numeric-limit compatibility. All Solid refactor-checklist counts remain 0 → 0 in the touched production module.

Evidence

Acceptance rule, established before comparison: use the same committed Team-scale harness and fixed fixture on the same machine; preserve event/RPC counts and Unicode behavior; eliminate segmentation of fitting labels; compare five interleaved unprofiled samples per side, rejecting any unexplained ≥20% regression. Profile runs identify cost separately from unprofiled latency.

Workload: 390 fixture sessions, 240 transcript messages, 240 live session events over about 30.75 seconds. These are synthetic browser/Gateway fixtures, not a production Gateway measurement. The initial baseline sample did not record host load; subsequent pairs do.

Source-mapped CPU profile Base Head
takeGraphemes reload self/inclusive 63.944 ms 1.023 ms
activityPersonLocation reload inclusive 89.486 ms 11.772 ms
takeGraphemes live self/inclusive 17.839 ms 6.790 ms
Live events / RPCs / long tasks 240 / 0 / 0 240 / 0 / 0
Startup JavaScript gzip 324,570 B 324,576 B
Startup CSS requests 1 1

The 6-byte gzip increase buys the constant-time fast path; the strict startup check passes. No visible layout changed.

Check Result
pnpm tsgo:ui Pass
pnpm test ui/src/lib/graphemes.test.ts --maxWorkers=1 10 cases pass
Targeted oxlint / format Pass
pnpm deadcode:knip Pass
Production build per side + strict performance comparison Pass
Independent Codex review (P0–P2), full source supplied Pass; no actionable findings
Interleaved unprofiled comparison 5 samples per side; no median/p95 increase reaches 20%

Commands: node --import ./scripts/tsx.mjs scripts/bench-control-ui-cold-load.ts --live-events --session-event-churn --cpu-profile --untraced --runs 1 --fixture-now-ms 1792422000000; head additionally uses --cpu-profile-compare <baseline>/cpu-profile.json. Unprofiled runs reuse each built bundle with --bundle-dir, omit CPU profiling, and retain the other workload flags. Startup: pnpm ui:check-performance --dist <head>/ui --base-dist <base>/ui. Full UI CI is requested with ci:full because this helper has multiple UI consumers.

Measured source heads: baseline 8b78b230a1c109fe6456964e59cb0eeefe63b7e5, candidate a9b327714c18d845e88a9971e3e364af0b1b28de.

Unprofiled result

Overall timings are mixed; this PR claims the targeted CPU-work reduction, not a broad latency improvement. On the shared 32-CPU host, recorded one-minute load ranged from 41.5 to 67.5. Every sample is retained. p95 uses linear interpolation over five samples and is an estimate.

Metric (ms) Base median / p95 Head median / p95 Base range Head range
Reload settle 3601.7 / 3851.3 3512.9 / 4136.0 3386.9–3909.9 3317.1–4174.7
Reload long-task duration 297.0 / 437.4 255.0 / 291.6 236.0–461.0 244.0–294.0
Live task CPU 3248.1 / 3533.4 3325.5 / 3439.1 3073.3–3587.0 3118.5–3443.5
Live script CPU 1885.2 / 1914.7 1889.6 / 1981.1 1823.3–1921.5 1854.3–1987.5
Live layout CPU 153.4 / 161.7 159.3 / 170.4 144.8–163.5 158.2–172.1
Live style CPU 237.2 / 247.8 248.0 / 264.5 223.3–248.2 226.9–264.9

Each run retained 82 boot RPCs, 18 boot events, 240 live frames, zero live RPCs, and zero live long tasks. Reload long-task count median stayed 3; estimated p95 went 5.6 → 3.8. The untraced harness does not emit total reload CPU; profile CPU and wall-clock results are kept separate.

CI result and inherited-failure evidence

Run 39285492333 passed all selected UI unit, UI E2E, performance, type, lint and security jobs. The real-Gateway job was not selected by this run, despite ci:full; no real-Gateway pass is claimed. The only root failure is checks-fast-baseline-ratchets, reporting src/infra/openclaw-process-census.missing-command.test.ts:12: ./container-environment.js. The exact diagnostic reproduces from the complete CI base tree d0e8fc8a103b2c37e98809ee46370748bb965155 with the checker’s isolated-index --staged mode. CI tested merge 342199358a7e5e6d6e909749cbafed22bd8cb026 has that base and this PR head as its ordered parents. Native inherited-failure recovery will verify complete CI/security accounting before landing; the failed gate remains recorded as failed.

@steipete steipete added the ci:full Keep ordinary native-locale CI selection on the next PR run. label Oct 19, 2026
@clawsweeper

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 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-19T16:28:59.172418Z a9b3277 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a9b327714c

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

for (const label of ["Synthetic Viewer", "Café", "👩🏽‍💻"]) {
expect(takeGraphemes(label, 48)).toBe(label);
}
expect(segment).not.toHaveBeenCalled();

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.

P1 Badge Remove the Segmenter call-shape assertion

When this helper is refactored, spying on Intl.Segmenter.prototype.segment makes the test depend on the current implementation rather than the returned grapheme behavior, while also failing to prove that an alternative implementation avoids equivalent work. Keep the output-based Unicode cases and leave the zero-work evidence in the benchmark/PR; repository guidance requires independent unit contracts that prove the invariant rather than private call shape.

AGENTS.md reference: AGENTS.md:L78-L80

Useful? React with 👍 / 👎.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 19, 2026
@steipete
steipete enabled auto-merge (squash) October 19, 2026 16:41
@steipete
steipete disabled auto-merge October 19, 2026 16:53
@clawsweeper clawsweeper Bot removed the clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. label Oct 19, 2026
@steipete
steipete merged commit d70a6bb into main Oct 19, 2026
113 of 117 checks passed
@steipete
steipete deleted the solid2/w-perf-graphemes branch October 19, 2026 17:09
@clawsweeper clawsweeper Bot added P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 19, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via explicitly authorized admin squash with attributed pre-existing CI failures.

@clawsweeper

clawsweeper Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

Skip grapheme segmentation when a string’s UTF-16 length already fits the requested grapheme limit.

Example: Generate an activity link for the label Synthetic Viewer with a 48-grapheme limit.

  • Before: Segment the entire label and return Synthetic Viewer.
  • After: Return Synthetic Viewer immediately with identical link text.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Minimal and mathematically sound production change, supported by scoped browser-benchmark results; synthetic transport limits end-to-end confidence, and the added spy is low-value coverage.
Proof confidence 🦐 gold shrimp (3/6) This maintainer-authored PR is outside the ordinary contributor-proof gate; its pinned browser-bundle benchmark exercises the changed helper during reload and live rendering, with synthetic Gateway fixtures and reported after-fix CPU reduction.
Patch quality 🐚 platinum hermit (4/6) 1 actionable review finding remain.

Product

Kind: Performance · Worth it: Yes · Fix scope: Complete
User problem: Sidebar rendering and live updates spend unnecessary main-thread CPU segmenting short person labels.
Reason: The maintainer-authored direction removes measured work at the existing owner with four production lines and no new state, configuration, or output contract.

Merge readiness

⛔ Needs changes before merge - 1 item remains

Keep this PR open: it removes measured UI work with a minimal, behavior-preserving change; the Segmenter spy needs a small test cleanup, and required CI remains blocked.

Priority: P3
Reviewed head: a9b327714c18d845e88a9971e3e364af0b1b28de

Before merge

  • Remove the Segmenter implementation-call assertion (P3) - The new fitting-label test spies on Intl.Segmenter.prototype.segment and requires zero calls. An equivalent implementation that obtains graphemes another way would change this test’s meaning without changing the public contract. The test-audit guidance rejects implementation-call assertions, and performance guidance places work-reduction evidence in the benchmark. Remove the spy and its assertion while retaining the output cases. This agrees with the earlier automated concern, at low priority.

Findings

  • [P3] Remove the Segmenter implementation-call assertion — ui/src/lib/graphemes.test.ts:7-11

Tests

  • Low-value test ui/src/lib/graphemes.test.ts: Remove the prototype spy and zero-call assertion: they pin the chosen segmentation implementation; retain the output-based compatibility cases and benchmark evidence.
  • Missing end-to-end proof: The supplied performance comparison uses synthetic Gateway fixtures rather than a real Gateway, and no executed base-red/head-green regression result is supplied.
Agent review details

How this fits together

The shared Control UI helper receives labels and numeric limits from activity routes, avatar initials, provider icons, plugin monograms, and question headers, returning text truncated at grapheme boundaries.

flowchart TD
  A[UI labels and limits] --> B[Shared grapheme helper]
  B --> C[UTF-16 length check]
  C --> D[Return fitting string]
  C --> E[Existing grapheme segmentation]
  D --> F[Routes and rendered text]
  E --> F
Loading

Technical review

Best possible solution:

Keep the shared helper’s constant-time guard and existing Unicode loop, retain useful output cases, and remove the Segmenter spy.

Do we have a high-confidence way to reproduce the issue?

Current main visibly segments fitting labels, including activity-link labels limited to 48 graphemes; the PR supplies matching browser-profile evidence, but no target code was executed during this review.

Is this the best way to solve the issue?

The guard is the smallest complete production fix: grapheme count cannot exceed UTF-16 length, and inputs that might truncate still use the existing loop, including fractional, negative, and NaN limits.

Full review comments:

  • [P3] Remove the Segmenter implementation-call assertion — ui/src/lib/graphemes.test.ts:7-11
    The new fitting-label test spies on Intl.Segmenter.prototype.segment and requires zero calls. An equivalent implementation that obtains graphemes another way would change this test’s meaning without changing the public contract. The test-audit guidance rejects implementation-call assertions, and performance guidance places work-reduction evidence in the benchmark. Remove the spy and its assertion while retaining the output cases. This agrees with the earlier automated concern, at low priority.
    Confidence: 0.97

Overall correctness: patch is correct
Overall confidence: 0.96

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against a13e869e7d51.

Provenance checked

  • Unicode-safe shared truncation keeps the original intent (9ad0a97: Preserve complete emoji, flags, joined emoji, and combining characters in provider fallback icons by sharing the existing plugin helper.)
  • Required Intl.Segmenter browser capability keeps the original intent (1e9431b: The supported browser floor supplies Segmenter, allowing removal of the code-point fallback.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test ui/src/lib/graphemes.test.ts --maxWorkers=1
  • pnpm tsgo:ui
  • pnpm exec oxlint ui/src/lib/graphemes.test.ts
  • pnpm exec oxfmt --check ui/src/lib/graphemes.test.ts
  • git diff --check

What I checked:

Likely related people:

  • Leon-SK668: Raw commit 9ad0a97 adds ui/src/lib/graphemes.ts:12 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 9ad0a978f7fe; files: ui/src/lib/graphemes.ts)
  • Peter Steinberger: Raw commit 1e9431b adds ui/src/lib/graphemes.ts:2 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 1e9431b71fea; files: ui/src/lib/graphemes.ts)

PR surface

Source +4, Tests +26. Total +30 across 2 files. Added test files: 1.

View PR surface stats
Area Files Added Removed Net
Source 1 4 0 +4
Tests 1 26 0 +26
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 30 0 +30

Labels

Label changes:

  • add P3: A narrow UI CPU optimization preserves existing output and addresses measured work rather than an urgent user-facing failure.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Label justifications:

  • P3: A narrow UI CPU optimization preserves existing output and addresses measured work rather than an urgent user-facing failure.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 19, 2026, 1:09 PM ET / 17:09 UTC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui ci:full Keep ordinary native-locale CI selection on the next PR run. P3 Low-priority cleanup, docs, polish, ergonomics, or speculative work. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: XS status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant