Skip to content

ui: drop the import costs that slowed Llama Chat's boot, tests and dev server - #74

Draft
allozaur wants to merge 3 commits into
allozaur/refactor/ui-typed-routesfrom
allozaur/perf/ui-import-cost
Draft

allozaur wants to merge 3 commits into
allozaur/refactor/ui-typed-routesfrom
allozaur/perf/ui-import-cost

Conversation

@allozaur

@allozaur allozaur commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Overview

Llama Chat downloads a quarter less JavaScript when it starts (2.93 MB -> 2.18 MB gzipped), and its unit tests and dev server start much faster. The app's own barrel imports stay: measured, they cost nothing in the shipped bundle.

Additional information

Part 11 of 13 of the work-in-progress stack on the fork, under Pele (#75), based on allozaur/refactor/ui-typed-routes (#73).

Full write-up with the bundle breakdown: https://claude.ai/artifact/FDY5n7ssJgorp5NoBnE1rG

Cause

The question was whether importing through barrels (import { chatStore } from '#lib/stores/index.ts', about 760 of them) costs anything. It doesn't in the build: rebuilding with every src/lib module declared free of side effects changed the bundle by 2.9 KB out of 10 MB, since everything the barrels re-export is used somewhere and bundleStrategy: 'single' ships all reachable code anyway. The costs were elsewhere:

  • @lucide/svelte's barrel in Node. Twelve .ts modules imported icons as { Globe } from '@lucide/svelte', which re-exports ~1,700 icons. The browser gets it prebundled; Node (Vitest's unit project, and the dev server, which loads a route's +page.ts in Node) resolves, compiles and runs every icon. Through #lib/constants/index.ts nearly every unit test paid ~4.5 s for it, and the dev server took 4.7 s to answer /.
  • highlight.js twice. import hljs from 'highlight.js' loads the package's CommonJS build with all 192 languages; lowlight, used by the Markdown pipeline, loads the ES build of the same languages. 861 KB + 859 KB.
  • Two rarely used files in the boot bundle. The pdf.js worker (1.04 MB) was imported with ?raw and turned into a Blob URL on every start; nerdamer (472 KB) was a string in a virtual module. Under single both shipped with the bundle every page load downloads. The ?raw import comes from the first SvelteKit web UI, whose inline strategy allowed no separate files.
What changed
  • The 12 .ts modules import each icon from its own path (import Globe from '@lucide/svelte/icons/globe'), as Lucide recommends. The 165 .svelte files keep the barrel: converting them saved ~0.7 s more on a cold dev start and nothing warm or in tests.
  • utils/code.ts imports highlight.js/lib/core and registers lowlight's language modules on it: the same languages, one copy.
  • The pdf.js worker is its own file (?url), which pdf.js starts when it first opens a PDF. nerdamer is emitted as its own file in the client build and fetched when the sandbox first needs it; dev and tests still get the string. Both sit in _app/immutable/assets, which the service worker precaches, so they work offline.
  • scripts/ui-assets.cmake maps .mjs to application/javascript, which module workers require. llama-server already embeds and serves every file in dist; the npx CLI already maps .mjs.
Numbers
Before After
Boot JS bundle, raw 10.09 MB 7.68 MB
Boot JS bundle, gzip 2.93 MB 2.18 MB
Boot JS bundle, brotli 2.06 MB 1.67 MB
App boot, 10 Mbit/s, 4x CPU throttle (median) 4.99 s 4.47 s
App boot, 50 Mbit/s, 4x CPU throttle (median) 3.24 s 2.77 s
Unit tests, wall time (median of 3) 14.0 s 4.2 s
Unit tests, import time summed over workers 195 s 37 s
Dev server first load, cold / warm 6.1 s / 4.8 s 4.1 s / 2.9 s

On localhost boot time doesn't change (~0.95 s); the gain is bytes on the wire, for llama-server opened over the LAN or Tailscale. The client and Storybook test projects run where Vite prebundles dependencies, and their times didn't change.

Not in this PR:

  • split instead of single: measured on the code before this PR, first-load JS would go from 10.09 MB to 6.33 MB raw (2.95 -> 1.90 MB gzip), mostly Mermaid, which is already behind import(). single was chosen when the build output was committed to the repo (server: (webui) no more gzip compression ggml-org/llama.cpp#21073); it no longer is. It would need the bundle*.js/bundle*.css check in ui-assets.cmake changed and embeds ~120 files instead of a handful. Splitting by route gains nothing: the root layout reaches most of the app.
  • highlight.js's common set (36 languages) would save ~1.47 MB more, but changes which code blocks get colored.
Testing
  • ui/apps/chat: npm run check (0 errors), npm run lint, the 1085 unit tests, the Storybook tests (115 files, 251 tests) and npm run build pass.
  • Client tests: all 72 files (382 tests) pass when run in two halves. A full single run drops its browser session partway ("Browser connection was closed") in a random file; a clean checkout of ui: navigate through resolve() with typed route ids #73 does the same in 2 of 3 runs, so it doesn't come from this change. The two unhandled Context "Tooltip.Provider" not found errors from dialog-focus-mobile show up on ui: navigate through resolve() with typed route ids #73 too.
  • Timings: each side in its own checkout of the same base, alternating runs: unit tests 3 runs each, dev server cold plus 2 warm loads in headless Chromium, boot 5-7 runs with 4x CPU throttling and gzip, like llama-server serves it.
  • On the build, in Chromium: the pdf.js worker starts as a module worker and answers pdf.js's handshake; the fetched nerdamer evaluates diff(sin(x)/x, x) correctly.
  • scripts/ui-assets.cmake on this dist (gzip on) embeds 82 assets, with the worker (.mjs) and nerdamer as application/javascript. Not checked in a running llama-server.

Requirements

@allozaur
allozaur added this pull request to stack #72 November 17, 2026 07:05
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch from 3708174 to 534c841 Compare November 17, 2026 10:31
@allozaur
allozaur removed this pull request from stack #72 November 17, 2026 11:15
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch from 534c841 to d379e06 Compare November 17, 2026 11:16
@allozaur
allozaur added this pull request to stack #76 November 17, 2026 11:16
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch from d379e06 to fa0c62a Compare November 17, 2026 11:20
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch from fa0c62a to 27e8227 Compare November 17, 2026 12:27
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch from 27e8227 to 79b57f8 Compare November 17, 2026 12:44
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch 2 times, most recently from 1b93ec2 to c4a7475 Compare November 17, 2026 13:13
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch from c4a7475 to 4d399f1 Compare November 17, 2026 13:20
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch 2 times, most recently from 910d218 to c02663c Compare November 17, 2026 13:44
@allozaur
allozaur removed this pull request from stack #76 November 17, 2026 13:44
@allozaur
allozaur added this pull request to stack #78 November 17, 2026 13:44
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch from c02663c to 3abbf3c Compare November 17, 2026 15:28
@allozaur
allozaur removed this pull request from stack #78 November 17, 2026 16:25
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch from 3abbf3c to f262e01 Compare November 17, 2026 16:26
@allozaur
allozaur added this pull request to stack #79 November 17, 2026 16:26
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch from f262e01 to 4204d72 Compare November 17, 2026 16:42
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch 3 times, most recently from e2797b0 to a220108 Compare November 17, 2026 17:30
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch 2 times, most recently from 2ceeb47 to 4fc7e54 Compare November 17, 2026 22:21
@allozaur
allozaur force-pushed the allozaur/perf/ui-import-cost branch from 4fc7e54 to 593f18a Compare November 17, 2026 22:35

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant