Skip to content

perf: reuse encoder and views in element info lookups - #179

Merged
natemoo-re merged 2 commits into
mainfrom
natemoo-re/perf/element-info-encode-into
Dec 3, 2026
Merged

natemoo-re merged 2 commits into
mainfrom
natemoo-re/perf/element-info-encode-into

Conversation

@natemoo-re

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

Copy link
Copy Markdown
Member

getElementInfo allocated a TextEncoder, an encoded Uint8Array, a full-memory Uint8Array view, and a DataView on every call. Callers that replay many small siblings (one info.get(id) per unchanged child) pay that per sibling, per frame.

This reuses one encoder plus cached transfer/DataView views, and encodes straight into wasm memory with encodeInto. The views refresh when memory.buffer is detached by growth or when opsBuf moves on resize. The read < id.length check replaces the old bytes.length > transferBytes overflow guard.

pack had the same problem: it ran encoder.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 uses encodeInto for 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 pack change:

Measured from @clack/ui's render benchmark helpers (bombshell-dev/ui#143). JS heap allocated per frame, 400 frames, averaged over 2 runs:

workload before after
single value update · grass (500) 787 KB 579 KB
add one component · grass (500) 1153 KB 849 KB
single value update · grass (1500) 2336 KB 1715 KB
add one component · bamboo (500) 373 KB 276 KB

I also tried a batched info.getMany(ids) with a native get_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.

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Size Increased — +1.1 KB

142.3 KB unpacked

@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@bomb.sh/tty@179

commit: cb05949

@codspeed

codspeed Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by ×2.6

⚡ 4 improved benchmarks
✅ 7 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ pack complex layout 47.9 ms 9.7 ms ×4.9
⚡ pack large list 70.1 ms 14.5 ms ×4.9
⚡ render steady diff 50.7 ms 33.8 ms +49.92%
⚡ render mixed frames 53 ms 42.8 ms +23.83%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing natemoo-re/perf/element-info-encode-into (cb05949) with main (30dc338)

Open in CodSpeed

@cowboyd cowboyd left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

⚡ 🥇 ⚡

@natemoo-re
natemoo-re merged commit 1585d55 into main Dec 3, 2026
13 checks passed
@natemoo-re
natemoo-re deleted the natemoo-re/perf/element-info-encode-into branch December 3, 2026 05:55
@bombshell-bot bombshell-bot Bot mentioned this pull request Dec 3, 2026
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
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