Skip to content

fix: lazy-load @pierre/diffs and Shiki engine off critical render path - #1070

Closed
toasterman234 wants to merge 1 commit into
get-bb:mainfrom
toasterman234:fix/mobile-bundle-optimization
Closed

toasterman234 wants to merge 1 commit into
get-bb:mainfrom
toasterman234:fix/mobile-bundle-optimization

Conversation

@toasterman234

@toasterman234 toasterman234 commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1063

Problem

bb on iPhone was "insanely laggy" because the critical rendering path bundled the entire Shiki syntax-highlighting engine (~80KB core + 170-language grammar registry) and @pierre/diffs worker pool into workspace-checkout-display (2.3MB). Every click triggered a massive JS parse/compile cycle on mobile WebKit.

What this does

Lazy-loads the diff worker pool provider behind Suspense boundaries so the thread view renders immediately without Shiki:

  • SplitThreadArea: lazy(() => import(ThreadDetailWorkerPoolProvider)) wrapped in <Suspense fallback={null}>
  • New LazyWorkerPoolProvider component for RootComposeView and PluginPanelView to avoid static @pierre/diffs imports
  • Bundle budget CI script (scripts/check-bundle-budget.mjs) and chunkSizeWarningLimit bumped to 800KB

Results

Metric Before After
workspace-checkout-display 2.3MB 1.7MB (-26%)
Shiki in critical path ✅ bundled ✅ moved to lazy
@pierre/diffs bootstrap 814KB 2.5KB
Worker (shiki) main-thread bundled web worker (off main thread)
  • All 315 test files (2366 tests) pass
  • Shiki verified removed from workspace-checkout-display (grep clean)
  • Follow-up: the remaining 1.7MB in workspace-checkout-display is the Monaco/Eddie code editor — tracked in Cut the web app boot payload by 60% #1071

How to verify

  1. Build: cd apps/app && pnpm vite build
  2. Check: grep createHighlighter dist/assets/workspace-checkout-display-*.js should be empty
  3. Run budget: node scripts/check-bundle-budget.mjs

Lazy-loads ThreadDetailWorkerPoolProvider, RootComposeView, and
PluginPanelView's WorkerPoolContextProvider behind Suspense boundaries
so the heavy syntax-highlighting engine (~80KB Shiki core + ~170
language grammar registry) no longer blocks initial thread view paint.

Changes:
- SplitThreadArea: lazy(() => import ThreadDetailWorkerPoolProvider)
  wrapped in <Suspense fallback={null}>
- New LazyWorkerPoolProvider component for RootComposeView and
  PluginPanelView to avoid static @pierre/diffs imports
- Vite build config: chunkSizeWarningLimit raised to 800KB
  (workspace-checkout-display at ~1.7MB tracked separately)
- New check-bundle-budget.mjs CI script
- Updated tests: parity + stories tests mock lazy providers

Results:
- workspace-checkout-display: 2.3MB → 1.7MB (-600KB, -26%)
- Shiki engine fully removed from critical rendering path
- diff/file-preview bootstrap: <3KB (down from bundling 814KB worker)
- All 315 test files (2366 tests) passing

Refs: get-bb#1063
Co-authored-by: Ben Charney <[email protected]>
Signed-off-by: Ben Charney <[email protected]>
@georgecollier-nqu

Copy link
Copy Markdown
Contributor

thank you this was bothering me too

@SawyerHood

Copy link
Copy Markdown
Collaborator

Thanks for digging into this, and sorry for the collision — I had #1071 in flight on the same issue before I spotted yours. Closing this one in favour of that, and I've added you as a co-author there since you were onto the problem independently.

Being straight about what differed, in case it's useful:

The three views this PR touches — PluginPanelView, RootComposeView, SplitThreadArea — are all already behind React.lazy routes in App.tsx, so none of them are on the boot path. Deferring the worker pool inside them makes those routes lighter, but it doesn't change what blocks first paint. The entry chunk was pulling @pierre/diffs through a different edge entirely: App → usePluginFrontendBoot → plugin-frontend.ts, the plugin runtime shim, which statically imports every library a plugin may resolve at runtime. Cutting that one edge (plus a barrel leak in the sidebar) took the boot payload from 3.97 MB to 1.58 MB.

On the budget script: I measured the flat 500 KB per-chunk rule against the optimised build and 8 chunks still fail it — emacs-lisp (762 KB), cpp (611 KB), wasm (608 KB) are on-demand Shiki grammars and worker-portable (814 KB) is the diff worker. None of them are downloaded unless something asks for them, so the rule would fail CI on day one. #1071 budgets the boot closure instead, and names the packages that must stay off it.

Your idea is still worth landing, though, and I'd welcome it as a follow-up. Measuring the landing route on top of #1071 turned up something neither PR fixes: getDiffWorkerPoolSize() returns hardwareConcurrency - 1, so simply opening / spins up ~8 syntax-highlighting workers each parsing 814 KB, before you've looked at a single diff. Not creating the pool until a diff actually renders is a bigger mobile win than either of us went after.

One thing to watch if you rebase onto #1071: LazyWorkerPoolProvider nests children inside the lazy component under <Suspense fallback={null}>. That's harmless today because @pierre/diffs is in the eager graph and never suspends — but once #1071 makes it lazy, that boundary would withhold the whole route subtree while a ~535 KB chunk loads, so you'd get a blank screen instead of the compose view. Hoisting children outside the boundary avoids it.

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.

Mobile lag: Shiki grammar registry bundles 2.3MB of JS into workspace-checkout-display, no service worker for caching

3 participants