Skip to content

perf: replay unchanged element subtrees - #143

Merged
cowboyd merged 9 commits into
mainfrom
perf/element-replay
Dec 4, 2026
Merged

cowboyd merged 9 commits into
mainfrom
perf/element-replay

Conversation

@cowboyd

@cowboyd cowboyd commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

When one part of the DOM changes, UI currently serializes the entire tree again. Track changed nodes and their ancestors for each frame, then use TTY 0.12.0's native replay directives for unchanged subtrees. Changed branches are rebuilt, and each frame retains the render information needed by the next frame.

Attribute mutations invalidate their owner element, including direct Attr.value updates. Insertions and moves invalidate the affected parent chains. DOM notifications go through the owning document (#150).

TTY 0.12.0 resolves color inheritance and currentcolor itself, including inside replayed subtrees, so layout no longer resolves colors locally:

  • open() carries the element's color, and text inherits it. The inherited-color check before replay and resolveColorValue are gone.
  • Borders without a border-color draw in the terminal's default foreground instead of falling back to padding.
  • createUI passes detectTerminal() to createTerm, because TTY 0.12.0 otherwise defaults to 256-color output. The style rendering tests stub COLORTERM=truecolor so CI renders the same colors as a truecolor terminal.

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

@socket-security

socket-security Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updated@​bomb.sh/​tty@​0.10.0 ⏵ 0.12.097 +1310099 +194 +1100

View full report

@pkg-pr-new

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

Copy link
Copy Markdown

commit: d0d6ace

@cowboyd
cowboyd marked this pull request as ready for review December 1, 2026 23:23
@cowboyd
cowboyd marked this pull request as draft December 1, 2026 23:24
@cowboyd
cowboyd marked this pull request as ready for review December 1, 2026 23:25
@cowboyd
cowboyd force-pushed the perf/element-replay branch from f070321 to 2b53d36 Compare December 1, 2026 23:37
@codspeed

codspeed Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 3 benchmarks

⚡ 212 improved benchmarks
❌ 3 regressed benchmarks
✅ 25 untouched benchmarks
⏩ 60 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory grass (100) 34.2 KB 756.3 KB -95.48%
❌ Memory grass (500) 145.4 KB 479.7 KB -69.68%
❌ Memory evergreen (500) 263.7 KB 413.9 KB -36.29%
⚡ Memory bush (1500) 421.3 KB 5.6 KB ×76
⚡ Memory evergreen (1500) 419.7 KB 7.2 KB ×58
⚡ Memory bamboo (1500) 419.4 KB 7.3 KB ×57
⚡ Memory evergreen (1500) 420.5 KB 7.4 KB ×57
⚡ Memory bush (1500) 422.4 KB 7.8 KB ×54
⚡ Memory maple (1500) 419.7 KB 7.8 KB ×54
⚡ Memory sequoia (1500) 419.6 KB 7.8 KB ×54
⚡ Memory bamboo (1500) 420.5 KB 8.1 KB ×52
⚡ Memory sequoia (1500) 421 KB 8.1 KB ×52
⚡ Memory sequoia (1500) 420.3 KB 8.4 KB ×50
⚡ Memory evergreen (1500) 421.9 KB 8.6 KB ×49
⚡ Memory bush (1500) 421 KB 8.6 KB ×49
⚡ Memory maple (1500) 421.2 KB 8.6 KB ×49
⚡ Memory bamboo (1500) 420.4 KB 8.8 KB ×48
⚡ Memory evergreen (1000) 280.3 KB 6.6 KB ×43
⚡ Memory bamboo (1000) 281.6 KB 6.9 KB ×41
⚡ Memory sequoia (1000) 280.5 KB 7.1 KB ×40
... ... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

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


Comparing perf/element-replay (d0d6ace) with main (9312bdb)

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 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a beautiful thing to see! Few small cleanup comments, mostly questions.

CleanShot 2026-12-01 at 22 02 31@2x

Comment thread packages/ui/src/layout.ts Resolved
Comment thread packages/ui/src/ui.ts Resolved
Comment thread packages/ui/src/layout.ts Resolved
natemoo-re added a commit that referenced this pull request Dec 3, 2026
This PR improves the performance of Node creation after profiling.
Eagerly creating Node properties before anything reads them was causing
a lot of overhead. We now generate attribute properties the first time
`attributes` is read, among some other laziness tweaks. (Lazy
`EventTarget` setup was dropped from this PR, since #111 replaced that
path with its own listener registry.)

No conflicts with #143 ❤️ 

<details>
<summary>Slop Output</summary>

This PR makes building DOM nodes with `createElement`, `setAttribute`,
and `appendChild` 3–4x faster. Observable DOM behavior is unchanged.

A CPU profile of building small trees on `main` showed most of the time
going to bookkeeping that nothing reads:

- `NamedNodeMap#changed` (13.5%) rebuilt every indexed and named
property on each attribute change, even when nothing had read
`element.attributes`.
- `asciiLowercase` and `asciiUppercase` (about 20%) ran a regex replace
on every tag and attribute name.
- Eight per-instance constant fields (about 6%) were copied onto every
node.

Each commit fixes one of these and was measured on its own. The figures
below compare each commit with the one before it, so the commits can be
reviewed one at a time. They were measured before the rebase onto #111
and #150:

| commit | change |
|---|--:|
| put node type constants on the prototype | −7% to −10% |
| reflect attribute properties only once the map is exposed | −31% to
−51% |
| case-map ascii-only names natively | −22% to −44% |
| push appended children instead of splicing | −4% to −9% |

#### Overall

Median µs per tree, from an in-process harness that alternates `main`
(`966ab37`, after #111 and #150) and this branch. It was run twice, with
the same ratios each time.

| case | main | branch | change |
|---|--:|--:|--:|
| select (9 elements) | 38.2 | 13.1 | −66% |
| form (31 elements) | 238.3 | 35.1 | −85% |
| table (204 elements) | 883.2 | 213.6 | −76% |
| 100 components (100 elements) | 1112.9 | 263.8 | −76% |

#### Behavior

- **Attribute properties:** a `NamedNodeMap` creates its indexed and
named properties the first time `element.attributes` is read, then keeps
them live. Script can only see these properties through `attributes`, so
nothing observable changes.
- **Constants:** the node type constants are on `Node.prototype`, as in
browsers. `Object.keys(node)` no longer lists them.

#### Tried and dropped

These were measured and either didn't help or were too small to justify
the change:

- Creating `NamedNodeMap` lazily
- Creating `childNodes` lazily. This was slower, and layout reads every
node's children anyway.
- A capped `nodeName` cache
- Creating `StyleDeclaration`'s `!important` set lazily
- Sharing one HTML namespace object across `createElement` calls

</details>
@natemoo-re
natemoo-re added this pull request to stack #153 December 3, 2026 05:33
@cowboyd
cowboyd merged commit d1fdb68 into main Dec 4, 2026
12 checks passed
@cowboyd
cowboyd deleted the perf/element-replay branch December 4, 2026 02:59
cowboyd pushed a commit that referenced this pull request Dec 4, 2026
## 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](https://app.codspeed.io/bombshell-dev/ui/runs/compare/6b0f5feb17ede57c78d55e18..6b10f4627d28fe5c160e3faa)

| | #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
- [x] Performance improvement
- [ ] Tests
- [ ] Chore (dependencies, CI, tooling)

## Checklist

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

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