Repository navigation
perf: replay unchanged element subtrees - #143
Merged
Merged
Conversation
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
commit: |
cowboyd
marked this pull request as ready for review
December 1, 2026 23:23
cowboyd
marked this pull request as draft
December 1, 2026 23:24
cowboyd
marked this pull request as ready for review
December 1, 2026 23:25
cowboyd
force-pushed
the
perf/element-replay
branch
from
December 1, 2026 23:37
f070321 to
2b53d36
Compare
3 of 12 tasks
Merging this PR will regress 3 benchmarks
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
natemoo-re
reviewed
Dec 2, 2026
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>
This was referenced Dec 3, 2026
natemoo-re
force-pushed
the
perf/element-replay
branch
from
December 3, 2026 21:35
2b53d36 to
3405517
Compare
natemoo-re
approved these changes
Dec 3, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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.valueupdates. Insertions and moves invalidate the affected parent chains. DOM notifications go through the owning document (#150).TTY 0.12.0 resolves
colorinheritance andcurrentcoloritself, including inside replayed subtrees, so layout no longer resolves colors locally:open()carries the element'scolor, and text inherits it. The inherited-color check before replay andresolveColorValueare gone.border-colordraw in the terminal's default foreground instead of falling back to padding.createUIpassesdetectTerminal()tocreateTerm, because TTY 0.12.0 otherwise defaults to 256-color output. The style rendering tests stubCOLORTERM=truecolorso CI renders the same colors as a truecolor terminal.Type of change
Checklist
pnpm test)pnpm format)AI-generated code disclosure