Skip to content
2018kguoPublic

About

Render AI chat Markdown 10x faster with the exact same output: a Rust/WebAssembly engine for Streamdown

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Repository files navigation

rill

Faster streaming Markdown for React, with Streamdown's component API and DOM.

Get started · Reference · Benchmarks · Custom plugins

Long AI answers should stream smoothly, and opening a long chat should not freeze the page. rill replaces the work behind Streamdown while keeping its component, styles and interactive controls. Change the imports below to try it.

In the recorded browser workloads, long chats opened 5–7× sooner, streamed answers finished 4–5× sooner, and the page used less JavaScript heap. The comparison checks matching final HTML; compatibility and fallbacks define where the fast path applies.

Streamdown on the left and rill on the right, recorded at 10× CPU throttle: streaming a table, streaming a long answer, typing while a long chat opens, then the benchmark results

Real recordings, Streamdown on the left and rill on the right, with Chromium CPU throttling set to 10×. The counters at the top of each pane are measured in the page.

Use it

npm install @2018kguo/rill

Change one import:

- import { Streamdown } from "streamdown";
+ import { Streamdown } from "@2018kguo/rill/streamdown";

If you highlight code with @streamdown/code, change that import too, and the page stops shipping Shiki (rill's highlighter gives the same colors):

- import { code } from "@streamdown/code";
+ import { code } from "@2018kguo/rill/code";

@2018kguo/rill/streamdown exports everything streamdown exports, with the same props and behavior: copy and download buttons, table menus, the link safety modal, the caret, server rendering and hydration, components overrides and plugins.

Bundler setup varies: some configurations need a Vite plugin or an explicit WebAssembly URL. Setup per bundler lists tested configurations; the reference records the supported Streamdown API.

If your <Streamdown> has remarkPlugins or rehypePlugins of its own, or Markdown syntax of its own (mentions, citations, custom links), see customizing.md: which plugins rill reproduces as they are, and how to ship your own as a custom build.

How much faster

Measured in Chromium on a phone-sized screen, with Chromium CPU throttling set to 10×. This approximates a slower CPU; it is not a measurement on an Android device. Both engines produce the same final HTML. Each number is the median of runs that alternate between the two engines.

Scenario Done sooner Less JS execution time Stutter, Streamdown / rill Memory, Streamdown / rill
Open a chat of 18 long answers 4.7× 6.8× 5.6 s / 1.2 s 41.9 / 14.5 MB
Open a code-heavy chat 7.3× 10.4× 4.7 s / 0.6 s 41.8 / 9.7 MB
Stream a 118-row table 4.0× 17.0× 13.4 s / 29 ms 12.4 / 9.0 MB
Stream a 56 KB answer 4.9× 21.3× 111 s / 65 ms 20.5 / 12.2 MB

JS execution time is time the main thread spent running JavaScript, garbage collection included. Stutter is how long the main thread stayed busy past 50 ms in a frame, which is what a user feels as lag. Memory is the JavaScript heap once the page settles.

The table measures a page that does nothing but render Markdown. A real app spends time on other work too, and that time is the same with either engine: React rendering the message list, the sidebar and the chat state. So the time ratios come out smaller, while the memory saved carries over. In Vercel's ai-chatbot template (measured with rill 0.4), changing the one import made an 18-answer chat open 3.1× faster (4.4× on the Markdown-only page), with 3.7× less JS execution time (6.6× there) and a JavaScript heap of 22 MB instead of 49.

benchmarks.md has every scenario with confidence intervals, page weight and the raw results. npm run bench reruns them.

What's different

rill keeps Streamdown's component and changes how the work gets done:

  • Reuse completed blocks. Streamdown re-reads the whole answer on every update, so long answers get slower as they grow. rill remembers its work between updates, so most updates avoid reparsing the completed blocks.
  • A streaming message edits the DOM in place. For a message in Streamdown's default setup, a small WebAssembly session works out what each update changed and applies it as DOM edits; React renders the root once. Your own components and code blocks still render as React, in portals inside that DOM.
  • Tables stream a row at a time. A table being written re-renders only its new row, not the whole table.
  • Finished text is plain HTML. React only builds the interactive parts (buttons, menus, links), which keeps memory down.
  • Footnotes don't slow streaming. Streamdown re-renders a whole answer with footnotes on every update; rill renders it in pieces.
  • One compiled pass. Parsing and cleaning the Markdown runs in Rust (as WebAssembly) in one pass instead of a chain of JavaScript plugins, 20 to 30 times faster on its own.
  • Code highlighting off the main thread, with the same colors as Shiki.

how-it-works.md explains the parsing, rendering and worker design.

A lighter entry

@2018kguo/rill/stream is <Streamdown> for pages that care about download size. A message in Streamdown's default setup streams through rill-stream alone, under 100 KB gzipped with its WebAssembly, and the full drop-in (with Streamdown) loads the first time a message needs it. The streaming view and the WebAssembly load on first use in both entries, so a page that has not shown a message yet does not carry them.

- import { Streamdown } from "streamdown";
+ import { Streamdown } from "@2018kguo/rill/stream";

It follows your Streamdown's dependencies

Streamdown's dependencies change their output between versions: how a table's whitespace is kept, whether sr-only clears its margins, where marked cuts a blockquote. A page's lockfile decides which it gets. When @2018kguo/rill/streamdown loads, rill reads the versions your page installed and renders what they render, so a locked install and a fresh one both match Streamdown. CI runs the tests both ways. The reference lists what it follows, and legacyBehaviors() reports what it found.

Compatibility

Switching renderers usually means subtly different lists, links or emphasis. rill is checked against Streamdown itself: the same documents rendered by both and compared element by element, every step of streamed answers, every short string over Markdown's tricky characters, server rendering and the interactive parts. Every bug found that way becomes a test. See conformance.md.

Fallbacks

A few things render with Streamdown's own code instead of rill's, with the same output: raw HTML that needs a full HTML5 parser, remarkPlugins or rehypePlugins rill doesn't reproduce, some filtering props, and everything until the WebAssembly has loaded. In development rill says so once per reason in the console; setFallbackListener hears every one, in production too:

import { setFallbackListener } from "@2018kguo/rill/streamdown";
setFallbackListener((reason, detail) => track("rill-fallback", { reason, detail }));

The common plugins (remark-breaks, remark-math with rehype-katex, rehype-slug, rehype-external-links) stay on the fast path, and plugins of your own can run there as a custom build. Every reason is listed in the reference.

One line of CSS for long chats

Once rill renders a chat, most of what's left when one opens is the browser laying out answers nobody can see yet. Let it skip them: give each settled message's row

.message-row { content-visibility: auto; contain-intrinsic-size: auto 0px auto 240px; }

The DOM stays the same (find-in-page and selection work); rows off screen are laid out when they scroll near.

Documentation

License

MIT. crates/rill-cmark is derived from pulldown-cmark (MIT, Copyright 2015 Google Inc.).

About

Render AI chat Markdown 10x faster with the exact same output: a Rust/WebAssembly engine for Streamdown

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages