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
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