Skip to content

fix(ui): put the typeset styles first, so a streamed document stops restyling - #470

Merged
vite-hub[bot] merged 3 commits into
mainfrom
t3/measure-chat-block-reuse
Nov 19, 2026
Merged

vite-hub[bot] merged 3 commits into
mainfrom
t3/measure-chat-block-reuse

Conversation

@onmax

@onmax onmax commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Streaming a long reply through comark-chat's React MessageParts with the preset restyles the whole document on almost every update. In the SIMD replay (38 Python fences, 256 characters every 40 ms), Chromium spends 4.3 s on style recalculation alone. The caret also sits on a line of its own under the text, not after the last word.

The typeset plugin appended its node last, and @comark/react matches a document's top-level nodes by index. So each update that added a block put that block where the <style> had been. React replaced the stylesheet, and the new one restyled every element, up to 32 ms per update by the end of SIMD. comark-code's frame callback then measured every code block under the new styles. The caret goes into the last top-level node, which was the empty typeset node.

This PR puts the typeset node first, and keeps exactly one when a streaming parse reuses the previous tree's first nodes. The rule that removes the space above the first block now skips a leading <style> (:nth-child(1 of :not(style, script))), so a finished document renders as before. The HTML output now opens with the <style> rather than ending with it.

SIMD replay, median of 5 paired runs main This PR
Main-thread tasks 11,975 ms 5,857 ms
Style recalculation 4,347 ms 283 ms
Layout 1,377 ms 192 ms
Update to DOM commit, cumulative 4,686 ms 4,559 ms

Headless Chromium (Playwright 1.64.1), React 19.3.1, @comark/react 0.9.0 and Shiki 4.5.0 from the lockfile, Linux. The machine was busy with other jobs, so absolute times run high; the runs pair main and the branch and alternate which goes first.

The second commit adds the benchmark that produced these numbers, packages/chat/bench/stream.mjs. It builds the chat page from source twice, at --base (default origin/main) and from the working tree. --mode check confirms the DOM after every tenth update matches main, apart from where the <style> and the caret sit, with all 38 fences highlighted. --mode chat streams a short reply and confirms a stateful component and a code group's selected tab keep their state.

I also tried the block reuse from the plan: keep the React output of completed blocks through render.markdown. It was slower (5.6 s to 7.3 s with updates back to back) and its DOM didn't match main. Comark reparses the whole document whenever the unparsed tail holds a #, which every Python comment does, so few blocks keep their identity. A Shiki token cache saved about 9%. Neither is in this PR.

I haven't measured Firefox, Safari, or the Vue and Svelte chat entries.

Before/after and numbers: https://drop.vitehub.dev/d/b6mpbfesf6sd/before-after

…estyling

The typeset plugin appended its node last, and renderers match a document's
top-level nodes by index, so every block a stream added replaced the <style>
and restyled the whole document. Streaming SIMD through comark-chat's React
MessageParts spent 4.3 s in style recalculation; it now spends 0.3 s.

The streaming caret, which went into the last node, now ends the text instead
of sitting after the typeset node on a line of its own. The first-child rule
skips a leading <style>.
… base commit

packages/chat/bench/stream.mjs builds a page with comark-chat's React
MessageParts and comark-ui's preset with Shiki twice, from --base and from
the working tree, and replays the SIMD fixture in Chromium. It reports commit
time and Chromium's main-thread, style, and layout time over paired runs,
checks the DOM matches, and checks a stateful component and a code group keep
their state while prose streams after them.
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-19T08:29:14.686784Z
We couldn’t complete this review. Comment @codex review to try again.
cf21e85 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

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Important

Keep the stylesheet stable, but fix the duplicate streaming caret before merging. A React host re-render can add a second caret without receiving any new text.

Approach: Putting the scoped stylesheet at a stable index addresses the style churn; putting prose last also exposes a caret mutation in the pinned Comark renderer.

Reviewed changes — I checked the shared preset, the first-block spacing rule, and the Chromium replay.

  • Typeset placement and spacing: The workspace build, 57 focused tests, and the new Chromium spacing test passed.
  • Chat replay: All 16 sampled SIMD snapshots matched after normalization, with all 38 fences highlighted on both sides. The mixed replay retained the counter and selected tab.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using gpt-6.2-sol(high) | 𝕏

Comment thread packages/ui/src/typeset.ts Resolved
Comment thread packages/chat/bench/stream.mjs Resolved
Comment thread packages/chat/bench/stream.mjs Resolved
@pkg-pr-new

pkg-pr-new Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

comark-annotate

pnpm add https://pkg.pr.new/comark-annotate@470

comark-badge

pnpm add https://pkg.pr.new/comark-badge@470

comark-bundle

pnpm add https://pkg.pr.new/comark-bundle@470

comark-cards

pnpm add https://pkg.pr.new/comark-cards@470

comark-carousel

pnpm add https://pkg.pr.new/comark-carousel@470

comark-cast

pnpm add https://pkg.pr.new/comark-cast@470

comark-charts

pnpm add https://pkg.pr.new/comark-charts@470

comark-chat

pnpm add https://pkg.pr.new/comark-chat@470

comark-chips

pnpm add https://pkg.pr.new/comark-chips@470

comark-cjk

pnpm add https://pkg.pr.new/comark-cjk@470

comark-code

pnpm add https://pkg.pr.new/comark-code@470

comark-code-notes

pnpm add https://pkg.pr.new/comark-code-notes@470

comark-colors

pnpm add https://pkg.pr.new/comark-colors@470

comark-comments

pnpm add https://pkg.pr.new/comark-comments@470

comark-critic

pnpm add https://pkg.pr.new/comark-critic@470

comark-deflist

pnpm add https://pkg.pr.new/comark-deflist@470

comark-details

pnpm add https://pkg.pr.new/comark-details@470

comark-diagrams

pnpm add https://pkg.pr.new/comark-diagrams@470

comark-diff

pnpm add https://pkg.pr.new/comark-diff@470

comark-embed

pnpm add https://pkg.pr.new/comark-embed@470

comark-fields

pnpm add https://pkg.pr.new/comark-fields@470

comark-figure

pnpm add https://pkg.pr.new/comark-figure@470

comark-file-tree

pnpm add https://pkg.pr.new/comark-file-tree@470

comark-footnotes

pnpm add https://pkg.pr.new/comark-footnotes@470

comark-git-graph

pnpm add https://pkg.pr.new/comark-git-graph@470

comark-github

pnpm add https://pkg.pr.new/comark-github@470

comark-icon

pnpm add https://pkg.pr.new/comark-icon@470

comark-include

pnpm add https://pkg.pr.new/comark-include@470

comark-keys

pnpm add https://pkg.pr.new/comark-keys@470

comark-links

pnpm add https://pkg.pr.new/comark-links@470

comark-linter

pnpm add https://pkg.pr.new/comark-linter@470

comark-magic-move

pnpm add https://pkg.pr.new/comark-magic-move@470

comark-mark

pnpm add https://pkg.pr.new/comark-mark@470

markdown-it-to-comark

pnpm add https://pkg.pr.new/markdown-it-to-comark@470

marked-to-comark

pnpm add https://pkg.pr.new/marked-to-comark@470

comark-mathml

pnpm add https://pkg.pr.new/comark-mathml@470

comark-media

pnpm add https://pkg.pr.new/comark-media@470

comark-metrics

pnpm add https://pkg.pr.new/comark-metrics@470

migrate-to-comark

pnpm add https://pkg.pr.new/migrate-to-comark@470

comark-outline

pnpm add https://pkg.pr.new/comark-outline@470

comark-properties

pnpm add https://pkg.pr.new/comark-properties@470

comark-qr

pnpm add https://pkg.pr.new/comark-qr@470

comark-quiz

pnpm add https://pkg.pr.new/comark-quiz@470

comark-reading-time

pnpm add https://pkg.pr.new/comark-reading-time@470

remark-to-comark

pnpm add https://pkg.pr.new/remark-to-comark@470

comark-sources

pnpm add https://pkg.pr.new/comark-sources@470

comark-steps

pnpm add https://pkg.pr.new/comark-steps@470

comark-table

pnpm add https://pkg.pr.new/comark-table@470

comark-tabs

pnpm add https://pkg.pr.new/comark-tabs@470

comark-tests

pnpm add https://pkg.pr.new/comark-tests@470

comark-time

pnpm add https://pkg.pr.new/comark-time@470

comark-ui

pnpm add https://pkg.pr.new/comark-ui@470

comark-xref

pnpm add https://pkg.pr.new/comark-xref@470

comark-zoom

pnpm add https://pkg.pr.new/comark-zoom@470

commit: 06a3c71

@vite-hub

vite-hub Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor
Session Status Started Duration
Pending Completed Not started —
View session Last seen running 2026-11-19T09:18:14.353Z 0s
View session Cancelled 2026-11-19T08:49:42.624Z 10m 17s

Latest result

Pull request merged.

Final answers

Session

Pull request merged.

Session

Pull request evidence changed.

Session

Pull request evidence changed.

Session

Pull request evidence changed.

Session

Pull request evidence changed.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ℹ️ No P0 or P1 findings remain; one benchmark portability issue is noted inline.

Reviewed changes — I reviewed 06a3c71 since the prior Pullfrog review.

  • Kept the caret out of the parsed tree: React and Vue now use the sibling caret. Tests confirm that rendering the same document twice does not duplicate it and that turning streaming off removes it.
  • Made the replay checks fail on bad inputs or lost state: Verified that an invalid baseline ref exits nonzero and that the Chromium chat replay retains the counter and selected tab. All 16 sampled SIMD snapshots matched, with 38 highlighted fences on each side.

The workspace build, typecheck, 458 UI and chat tests, and Chromium spacing test passed.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using gpt-6.2-sol(high) | 𝕏

Comment thread packages/chat/bench/stream.mjs Resolved
@vite-hub
vite-hub Bot merged commit 21c78f8 into main Nov 19, 2026
7 checks passed
@vite-hub
vite-hub Bot deleted the t3/measure-chat-block-reuse branch November 19, 2026 09:21
@github-actions github-actions Bot mentioned this pull request Nov 19, 2026
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