Skip to content

perf: reuse styles for unchanged elements - #151

Merged
cowboyd merged 1 commit into
perf/element-replayfrom
natemoo-re/perf/layout-style-reuse
Dec 4, 2026
Merged

cowboyd merged 1 commit into
perf/element-replayfrom
natemoo-re/perf/layout-style-reuse

Conversation

@natemoo-re

@natemoo-re natemoo-re commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Stacked on #143. Cuts the per-node allocations behind the grass memory regressions CodSpeed flagged on that PR.

In grass, every component is a direct child of the root. A single edit marks the root as changed, so Layout replays each of the other N−1 siblings one by one. Before replaying a sibling, it calls computeStyle(child), and computeDefaultStyles builds 14 new objects every time. A heap profile that also counts collected objects puts about 72% of grass's per-frame churn in style computation. Most of the rest is info.get(), which bombshell-dev/tty#179 addresses.

  • Default styles are two shared frozen objects, one for flex and one for inline, instead of being rebuilt per call.
  • Layout reuses the style it saved at an element's last #open when that element isn't in changes. This is safe because every inline-style and attribute change already notifies its element, which puts it in changes.

CodSpeed

Compared with #143, using tty#179's build: 122 improvements, 3 regressions (2–3 KB each), 175 unchanged. Comparison

#143 this PR + tty#179
memory: initial render (500 / 1000 / 1500) ~265 / ~402 / ~388 KB ~127 KB at every size
memory: batch 100 updates, deep shapes (1000–1500) 200–420 KB 8–10 KB
memory: grass updates (1000 / 1500) ~327 KB ~190 KB
walltime: initial render 28–32% faster on every shape
walltime: single value update · grass (500) 3.9 ms 2.4 ms

Without tty#179, CodSpeed shows little change for this PR: its memory instrument counts native allocations, not the JS heap. Fewer JS allocations also meant fewer collections freeing tty's per-string encode() buffers, so initial render (1500) peaks went up about 28%. tty#179 removes those buffers.

Still open: initial render::grass (100) (663 KB) and single value update::grass (500) (492 KB). Their allocation counts dropped 85–90%, but a few large untraced blocks remain. ui doesn't affect them, and they don't appear in other shapes or sizes.

lays out a deeply nested tree without recursive traversal times out locally on a loaded machine both with and without this change. It's not related.

Type of change

  • Bug fix
  • Feature
  • Refactor (no behavior change)
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • All tests pass (pnpm test)
  • Files are formatted (pnpm format)
  • I have added/updated tests for my changes (if applicable)
  • I have added a changeset

AI-generated code disclosure

  • This PR includes AI-generated code

@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

commit: 1249093

@codspeed

codspeed Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 10.72%

⚡ 1 improved benchmark
❌ 5 regressed benchmarks
✅ 234 untouched benchmarks
⏩ 60 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory maple (1000) 9.3 KB 14.3 KB -34.95%
❌ Memory bamboo (1500) 11.2 KB 16.6 KB -32.47%
❌ Memory bush (1500) 5.6 KB 7.9 KB -29.92%
❌ Memory maple (1500) 11.3 KB 16 KB -29.04%
❌ Memory bamboo (1000) 10.7 KB 15 KB -28.47%
⚡ Memory evergreen (500) 413.9 KB 127.7 KB ×3.2

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing natemoo-re/perf/layout-style-reuse (1249093) with perf/element-replay (d0d6ace)

Open in CodSpeed

Footnotes

  1. 60 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@natemoo-re
natemoo-re force-pushed the natemoo-re/perf/layout-style-reuse branch from 9941830 to 1249093 Compare December 3, 2026 22:11
@natemoo-re
natemoo-re marked this pull request as ready for review December 3, 2026 22:11
@cowboyd
cowboyd merged commit 3481fbe into main Dec 4, 2026
11 checks passed
@cowboyd
cowboyd deleted the natemoo-re/perf/layout-style-reuse branch December 4, 2026 02:59
@natemoo-re natemoo-re mentioned this pull request Dec 4, 2026
3 of 12 tasks
natemoo-re added a commit that referenced this pull request Dec 4, 2026
## What does this PR do?

Compiles WebAssembly eagerly in the bench project so that V8's wasm
compilation stops showing up as memory regressions.

V8 compiles `@bomb.sh/tty`'s wasm module lazily. Each function is
compiled the first time it's called, and hot functions are recompiled
later. Those are native allocations with no JS stack. Every `createUI`
reuses the same compiled module, so the cost lands on whichever bench
happens to trigger it. That's the source of the "still open" peaks from
#151: `initial render::grass (100)` and `single value update::grass
(500)`. Before #143, the same cost landed on `evergreen (100)` and
`bamboo (100)`.

- The bench project now passes `--no-liftoff
--no-wasm-lazy-compilation`. All wasm is compiled with TurboFan when the
module loads, before any bench runs.

#### CodSpeed

Each experiment compares against main (`3481fbe`):

| experiment | result |
|---|---|
| throwaway warmup case first | 757 KB peak moved to the warmup; every
other spike shifted one slot later |
| fixed semi-space size | no change (not heap growth) |
| `--no-wasm-lazy-compilation --no-wasm-tier-up` | spikes gone, walltime
~30% slower (stuck on Liftoff) |
| **`--no-liftoff --no-wasm-lazy-compilation`** (this PR) | spikes gone,
no walltime change |

| bench | main | this PR |
|---|---|---|
| initial render · grass (100) | 756.9 KB | 8 KB |
| single value update · grass (500) | 462.5 KB | 6.8 KB |
| initial render · evergreen (100) | 290.2 KB | 6.1 KB |
| initial render · bamboo (100) | 257.7 KB | 5.1 KB |


[Comparison](https://app.codspeed.io/bombshell-dev/ui/runs/compare/6b122f80fdca397d91618ec7..6b1242ba84edce48c581d53b)

The same comparison also shows 2–3 KB swings in peak memory on 5 benches
with identical ui code. That's normal variation between runs, not a
regression.

## Type of change

- [ ] Bug fix
- [ ] Feature
- [ ] Refactor (no behavior change)
- [ ] Documentation
- [ ] Performance improvement
- [ ] Tests
- [x] Chore (dependencies, CI, tooling)

## Checklist

- [ ] All tests pass (`pnpm test`)
- [x] Files are formatted (`pnpm format`)
- [ ] I have added/updated tests for my changes (if applicable)
- [ ] I have added a changeset

No changeset: benchmark tooling only.

## AI-generated code disclosure

- [x] This PR includes AI-generated code
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.

2 participants