Repository navigation
perf: reuse encoder and views in element info lookups - #179
Merged
Merged
Conversation
|
Size Increased — +1.1 KB 142.3 KB unpacked |
commit: |
5 of 12 tasks
Contributor
Merging this PR will improve performance by ×2.6
Performance Changes
Tip Curious why performance improved? Comment Comparing |
Merged
cowboyd
pushed a commit
to bombshell-dev/ui
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.
getElementInfoallocated aTextEncoder, an encodedUint8Array, a full-memoryUint8Arrayview, and aDataViewon every call. Callers that replay many small siblings (oneinfo.get(id)per unchanged child) pay that per sibling, per frame.This reuses one encoder plus cached transfer/
DataViewviews, and encodes straight into wasm memory withencodeInto. The views refresh whenmemory.bufferis detached by growth or whenopsBufmoves on resize. Theread < id.lengthcheck replaces the oldbytes.length > transferBytesoverflow guard.packhad the same problem: it ranencoder.encode()on every element id and text node, and each call made a native allocation that only a GC frees. In ui's 1,500-element initial render, that was about 12,000 allocations per frame. It now writes ASCII bytes straight into the transfer buffer and usesencodeIntofor everything else. New tests cover non-ASCII UTF-8 output and multibyte overflow.No API change.
Numbers
CodSpeed, ui #143 vs bombshell-dev/ui#151 using this PR's build: 122 improvements, 3 regressions (2–3 KB), 175 unchanged. Initial-render peak memory drops to ~127 KB at every size (from ~265–402 KB), and walltime is 28–32% faster. Comparison
Local JS heap, before the
packchange:Measured from
@clack/ui's render benchmark helpers (bombshell-dev/ui#143). JS heap allocated per frame, 400 frames, averaged over 2 runs:I also tried a batched
info.getMany(ids)with a nativeget_element_infos. It came out worse than this change alone, because joining ids and building the results array cost more than the separate wasm calls it saved, so I dropped it.