Skip to content

perf(site): find the grapheme boundary without a full walk - #31572

Draft
mafredri wants to merge 7 commits into
perf/site-monaco-chunkfrom
perf/chat-grapheme-slice
Draft

mafredri wants to merge 7 commits into
perf/site-monaco-chunkfrom
perf/chat-grapheme-slice

Conversation

@mafredri

@mafredri mafredri commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Base: #31367 (perf/site-monaco-chunk). Last of five stacked fixes.

While a reply streams, sliceAtGraphemeBoundary walked every grapheme cluster from the message start on each animation frame. It now asks Segments.containing() for the cluster at the cut. Tests compare it with a full walk at every cut of emoji, combining-mark, Hangul and CRLF samples. The lib Intl.Segmenter types replace the local ones.

#31278 bench (a tool, not for merging), production builds of the pre-rebase versions of 99783c681e (base) and 401b7007ae (this PR), both on main at f348cd66d7. Later commits in #29596, #31365, #31366 and #31367 change only comments, attachment controls, tests and stories; later commits here keep one containing() call per frame. 3 alternating runs of 2 fresh chats, each streaming a reply that grows from 43k characters to 48k or 49k during typing:

typing while streaming, key p50 base this PR
WebKit 246 to 302 ms 236 to 277 ms
Chromium 125 to 137 ms 119 to 124 ms

WebKit shows no gain beyond noise: its ranges overlap. Chromium suggests a small one: its ranges separate by 1 ms (medians 130 against 121 ms), as 3 runs per side also do by chance 1 time in 10. Frames over 50 ms fill 100 to 104% of every run's phase because that figure counts each in full, even one already running when the phase starts, so it cannot show an effect; key latency follows frame length and can.

An earlier WebKit profile on a smaller transcript put the full walk at 8.8% of JS samples and containing() at 2.1%. This run has no profile: production builds lack source maps, and the profiling build, which has them, timed out WebKit's inspector at 121k to 123k DOM nodes.

🤖 This PR was created with the help of Coder Agents, and will be reviewed by a human. 🏂🏻

@mafredri

This comment has been minimized.

@coder-agents-review

coder-agents-review Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Chat: Review posted | View chat
Requested: 2026-10-30 16:14 UTC by @mafredri

Review history
  • R1 (2026-10-30): 9 reviewers, 1 Nit, 2 P4, COMMENT. Review
  • R2 (2026-10-30): 7 reviewers, 1 Nit, 3 P4, COMMENT. Review
  • R3 (2026-10-30): 6 reviewers, 2 Nit, 3 P4, COMMENT. Review
  • R4 (2026-10-30): 6 reviewers, 2 Nit, 3 P4, APPROVE. Review

deep-review v0.13.0 | Round 4 | 13c9108..aaa839c

Last posted: Round 4, 5 findings (3 P4, 2 Nit), APPROVE. Review

Finding inventory

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 P4 Author contested; panel closed R2 (2/2 accept) SmoothText.ts:288 typeof Intl.Segmenter guard keeps fallbacks no supported browser or test reaches R1 Nami P4, Bisky P4, Netero (OOS item SmoothText.ts:320) Yes
CRF-2 Nit Author fixed (b1ad7a8) SmoothText.ts:316 cut names the cluster while the comment uses "the cut" for the limit R1 Gon P2 (313) and Nit (316), Leorio Nit Yes
CRF-3 P4 Author fixed (PR description edited before R2) PR description (anchored SmoothText.ts:313-317) Benchmark ran before 6c31a25, which rewrites the measured branch; description does not say so R1 Leorio Yes
CRF-4 P2 Dropped by orchestrator ("without walking earlier ones" states why containing() is used; comment wording is Nit-class and Leorio judged the text good) SmoothText.ts:313 Comment narrates the deleted loop R1 Gon No
CRF-5 Nit Dropped by orchestrator (coder/coder squash-merges; intermediate commit subjects do not reach main) commit 6c31a25 Commit subject "cut at the cluster past the limit" contradicts the code R1 Leorio No
CRF-6 OOS Out of scope (body) SmoothText.ts:285 Unchanged segmenter comment restates the const and repeats the fallback note R1 Gon No
CRF-7 OOS Out of scope (body) SmoothText.ts:293 Unchanged doc comment narrates mechanism and repeats the fallback note R1 Gon No
CRF-8 OOS Out of scope (body) SmoothText.ts:320 Unchanged fallback comment cites an earlier Array.from() implementation R1 Gon No
CRF-9 OOS Out of scope (body) SmoothText.ts:341 sliceGraphemes materializes every cluster of the input to keep the first N R1 Netero No
CRF-10 OOS Out of scope (body) Avatar.tsx:18 Second segmenter with locale "en" and the same unreachable guard R1 Netero, Nami, Bisky No
CRF-11 OOS Out of scope (body) RenameChatDialog.tsx:29 Same unreachable Intl.Segmenter guard and fallback R1 Nami No
CRF-12 Note Note (body) SmoothText.ts:316 containing() cost stays flat with text length in V8; flag runs grow but stay below the walk R1 Killua, Pariston No
CRF-13 P4 Author fixed (19dd288) SmoothText.ts:313 ?.index reads as if containing() can be undefined; comment no longer states the length-check invariant R2 Netero Yes
CRF-14 OOS Out of scope (body) SmoothText.ts:288 Unreachable Intl.Segmenter guard and fallbacks predate the PR; cleanup across SmoothText.ts, Avatar.tsx, RenameChatDialog.tsx has no ticket R2 Nami No
CRF-15 Nit Author fixed (aaa839c) SmoothText.ts:313 Comment names one length check; the invariant needs both early returns R3 Gon Yes

Contested and acknowledged

CRF-1 (P4, SmoothText.ts:288) - unreachable Intl.Segmenter guard and fallbacks

  • Finding: The typeof Intl.Segmenter === "function" guard keeps a codepoint fallback in sliceAtGraphemeBoundary and an Array.from(text) branch in sliceGraphemes that no browserslist target and no test reaches; proposed constructing the segmenter directly and deleting both fallbacks.
  • Author defense: Main's SmoothText.ts already returns null when Intl.Segmenter is missing and this PR keeps that behavior; Avatar.tsx and RenameChatDialog.tsx on main pair the lib types with the same guard, so removal belongs in a separate cleanup of all three files. No ticket linked.
  • Panel closure (R2, 2/2): Nami and Bisky, the agents that raised it in R1, re-checked: origin/main:SmoothText.ts:300-314 has the null fallback through try/catch, and Avatar.tsx:18 and RenameChatDialog.tsx:29 use the same guard. The PR adds no new fallback, and the branch cannot change a user-visible cut in any supported browser. Both accepted; the three-file cleanup is listed out of scope (CRF-14, CRF-10, CRF-11) for a human to ticket or accept.

Round log

Round 1

Full panel (round 1). Netero: no findings, so the panel ran. Law not run (effective additions 49). Specialists: ging-ts, killua, nami. Core: pariston, mafuuu. Pool: bisky (tests changed), gon, leorio (once-per-PR floor). Wildcard: knov (random from distilled set). 2 P4, 1 Nit posted; 2 dropped; 6 out of scope; 1 note. Gon P2 on the 313 comment was merged into the naming Nit for its "the cut" part (comment wording is Nit-class per the severity scale); keep-argument for P2 was that a reader may confuse the two positions, but the new test pins every cut, so a misread edit fails CI. Reviewed against 13c9108..6c31a25.

Round 2

Fix round (not a restructure: since R1 the head changed only the comment at SmoothText.ts:313-314 and replaced the ! with ?.index, via 15e0164 and b1ad7a8). Churn guard PROCEED: CRF-2, CRF-3 addressed; CRF-1 contested. Netero: 1 P4 (CRF-13). Panel: nami, bisky (dispute check-in, reused R1 agents), gon, leorio (CRF-2 raisers, code touched), ging-ts, killua (specialists for SmoothText.ts), wildcard mafuuu. CRF-1 closed 2/2; CRF-2 and CRF-3 verified by Gon and Leorio; CRF-13 kept at P4 (keep-argument: the reason containing() cannot return undefined here, and that an undefined end would return text past the limit, lives only in a commit message the squash merge drops). 1 new out-of-scope item (CRF-14). Reviewed against 13c9108..b1ad7a8.

Round 3

Fix round (not a restructure: since R2 only the comment at SmoothText.ts:313-315 changed, in 19dd288). Churn guard PROCEED: CRF-13 addressed. Netero: no findings, CRF-13 fix verified. Panel: gon, leorio (comment and commit message changed), ging-ts, nami, killua (specialists for SmoothText.ts), wildcard mafuuu. CRF-13 verified by Netero, Nami, Leorio, Mafuuu. 1 new Nit (CRF-15, Gon); Leorio read the same comment as adequate but did not address the negative-limit case Gon verified (containing(-1) returns undefined), so Gon's evidence sets the floor. Reviewed against 13c9108..19dd288.

Round 4

Fix round (not a restructure: since R3 only one comment word at SmoothText.ts:313 changed, in aaa839c). Churn guard PROCEED: CRF-15 addressed. Netero: no findings. Panel: gon, leorio (comment and commit message changed), ging-ts, nami, killua (specialists for SmoothText.ts), wildcard razor. CRF-15 verified by Gon, Leorio, Ging-TS, Nami, Razor, Netero. No new findings; no open in-scope findings remain. Out-of-scope items CRF-6 to CRF-11 and CRF-14 still need a human ticket or acceptance. Reviewed against 13c9108..aaa839c.

About deep-review

CRF = Coder Review Finding (P0-P4, Nit, Note)

Reviewer Focus
Bisky tests
Chopper ops/errors
Churn-guard change verification
Ging language modernization
Gon naming
Hisoka edge cases
Killua perf
Kite change integrity
Knov contracts
Knuckle SQL
Komugi flake/determinism
Kurapika security
Law decomposition
Leorio docs
Luffy product
Mafu-san process
Mafuuu contracts
Melody dispatch/pairing
Meruem structural
Nami frontend
Netero mechanical checks
Pariston premise testing
Pen-botter product gaps
Razor verification
Robin duplication
Ryosuke Go arch
Takumi concurrency
Zoro shape

🤖 Managed by Coder Agents.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

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-30T16:15:53.326996Z aaa839c Manual request
ℹ️ 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[bot]

This comment was marked as outdated.

@coder-agents-review coder-agents-review 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.

This PR replaces the per-frame grapheme walk in sliceAtGraphemeBoundary with one Segments.containing() call and switches to the lib Intl.Segmenter types. 2 P4, 1 Nit.

Out of scope (needs a ticket or explicit acceptance by a human):

  • site/src/pages/AgentsPage/components/ChatConversation/SmoothText.ts:285: the unchanged comment on graphemeSegmenter restates the top-level const and repeats the fallback note from the doc comment at line 293, so the two must be kept in sync.
  • site/src/pages/AgentsPage/components/ChatConversation/SmoothText.ts:293: the doc comment describes mechanism ("it is used for correct grapheme handling") instead of the contract, the largest boundary within maxCodeUnitLength code units.
  • site/src/pages/AgentsPage/components/ChatConversation/SmoothText.ts:320: the fallback comment cites "the O(n) Array.from() cost" of an earlier implementation that no longer exists.
  • site/src/pages/AgentsPage/components/ChatConversation/SmoothText.ts:341: sliceGraphemes builds an array of every cluster in text to keep the first maxGraphemes, the same full walk this PR removes, applied to the whole input.
  • site/src/components/Avatar/Avatar.tsx:18: a second module-scoped segmenter uses locale "en" while SmoothText.ts uses the default locale, so the two can segment the same text differently; it also keeps an unreachable typeof Intl.Segmenter fallback.
  • site/src/pages/AgentsPage/components/ChatsSidebar/dialogs/RenameChatDialog.tsx:29: the same unreachable Intl.Segmenter guard and fallback.

Notes:

  • Measured in Node 22 (V8, ICU) with the cut near the end: on 48k characters of mixed text the removed walk took 11.2 ms per call and containing() 0.049 ms. On runs of regional-indicator flags containing() grows with run length because ICU backs up to the run start, but it stayed below the walk at every size measured. SpiderMonkey was not measured.

🤖 This review was automatically generated with Coder Agents.

}
return null;
})();
const graphemeSegmenter =

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.

P4 [CRF-1] The typeof Intl.Segmenter === "function" guard keeps a fallback that no supported browser and no test reaches. (Nami, Bisky)

site/package.json browserslist is chrome 111, firefox 128, safari 16.4; Intl.Segmenter shipped in Chrome 87, Firefox 125 and Safari 14.1, and the Node runtime that runs vitest has it too, so the codepoint loop in sliceAtGraphemeBoundary and the Array.from(text) branch of sliceGraphemes never run. The lib types this PR adopts declare Intl.Segmenter as always present, so the guard no longer matches the types it relies on. Make graphemeSegmenter a plain new Intl.Segmenter(undefined, { granularity: "grapheme" }), delete both fallback branches, and update the doc comments that describe the fallback.

🤖

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main's SmoothText.ts already falls back when Intl.Segmenter is missing and this PR keeps that, and on main Avatar.tsx and RenameChatDialog.tsx already pair the lib types with the same typeof guard, so the pairing is main's pattern and removing it belongs in a separate cleanup of all three.

🤖 Posted using /amend-review skill via Coder Agents.

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.

The defense holds: the fallback and the guard are on main, and the PR adds no new fallback. The cleanup across SmoothText.ts, Avatar.tsx and RenameChatDialog.tsx is listed as out of scope in the review body.

🤖

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reply above stands: the guard and fallback are main's, and the cleanup of all three files is out of scope here.

🤖 Posted using /amend-review skill via Coder Agents.

@mafredri

This comment has been minimized.

@chatgpt-codex-connector

This comment has been minimized.

@coder-agents-review coder-agents-review 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.

Since the last review, the PR renames away cut, drops the ! on containing() in favor of ?.index, and updates the benchmark paragraph in the description. 1 P4.

The variable naming and the description fixes are verified. The defense on the unreachable Intl.Segmenter fallback is accepted: the guard and fallback are on main, and the same guard is in Avatar.tsx and RenameChatDialog.tsx.

Out of scope (needs a ticket or explicit acceptance by a human):

  • site/src/pages/AgentsPage/components/ChatConversation/SmoothText.ts:288: the typeof Intl.Segmenter guard and the fallbacks in sliceAtGraphemeBoundary and sliceGraphemes, together with the same guards in Avatar.tsx:18 and RenameChatDialog.tsx:29, are code that no browserslist target or test runs. The proposed cleanup of all three files has no ticket.

🤖 This review was automatically generated with Coder Agents.

@mafredri

This comment has been minimized.

@chatgpt-codex-connector

This comment has been minimized.

coder-agents-review[bot]

This comment was marked as outdated.

@mafredri

This comment has been minimized.

@chatgpt-codex-connector

This comment has been minimized.

coder-agents-review[bot]

This comment was marked as outdated.

@mafredri
mafredri force-pushed the perf/site-monaco-chunk branch from 13c9108 to 158e5ab Compare October 30, 2026 18:37
@mafredri
mafredri force-pushed the perf/chat-grapheme-slice branch 2 times, most recently from cd9adb6 to f2cb1be Compare November 4, 2026 11:00
@mafredri
mafredri force-pushed the perf/site-monaco-chunk branch 2 times, most recently from 0771b38 to efb20b8 Compare November 6, 2026 19:45
@mafredri
mafredri force-pushed the perf/chat-grapheme-slice branch from f2cb1be to f97530c Compare November 6, 2026 19:45
@linear-code

linear-code Bot commented Nov 13, 2026

Copy link
Copy Markdown

CODAGT-1081

@mafredri
mafredri force-pushed the perf/site-monaco-chunk branch from efb20b8 to eff8991 Compare November 18, 2026 14:53
sliceAtGraphemeBoundary iterated every grapheme cluster from index 0 on
each animation frame while streaming. Segments.containing() finds the
cluster at the cut directly. The local Intl.Segmenter types give way to
the lib types.
…de Hangul clusters

The Hangul sample used precomposed syllables, each one code unit, so no
cut fell inside a Hangul cluster. Conjoining jamo form clusters of 3 and
2 code units, so the sample now has cuts inside them.
…at the cluster past the limit

After the guards, 0 < maxCodeUnitLength < text.length, so a cluster
always contains code unit maxCodeUnitLength, and it starts at the
largest boundary that fits. Slicing at its index drops the end check and
a fallthrough to the codepoint loop that never ran.
… the non-null assertion on containing()

The site rules forbid non-null assertions. containing() returns
undefined exactly when maxCodeUnitLength is at or past the end of the
text, so that case now returns the whole text, and the length guard
serves only the codepoint fallback.
…e length before segmenting

A call at or past the end of the text returns again before segment()
runs. After that check a cluster always holds index maxCodeUnitLength,
and if containing() returned undefined, slicing to an undefined end
would keep the whole text, so the optional index needs no assertion.
…y containing() finds a cluster

The optional index read as if containing() could return undefined
there. The comment now says the length check above leaves a cluster
at index maxCodeUnitLength.
…h checks the cluster depends on

containing() returns undefined for an index below 0 as well as at or
past the end, so the comment now credits both early returns.
@mafredri
mafredri force-pushed the perf/chat-grapheme-slice branch from f97530c to ed1928d Compare November 18, 2026 15:08

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant