Skip to content

Build a region's attribute bags once, when its fragment arrives - #1

Open
simonhamp wants to merge 2 commits into
mainfrom
prepared-fragments
Open

simonhamp wants to merge 2 commits into
mainfrom
prepared-fragments

Conversation

@simonhamp

@simonhamp simonhamp commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

A server region used to hold its fragment as the nested tree that came off the wire. Every frame walked that tree, normalized each node's children and rebuilt each node's attribute bag with attrsFor(). None of that changes between frames.

Now applyFragment() holds the fragment prepared: three flat lists in pre-order with each node's type, child count and attribute bag. emit() walks those lists and calls the collector's open(), leaf() and close() the same as before, so the collected tree doesn't change. paint(), which server screen chrome uses, prepares its nodes the same way.

This came out of the binary wire experiment. On a 1,202-node list it took a frame from about 8.9 ms to 8.0 ms, and added about 1.7 ms when the fragment arrives. Those numbers were measured in mobile-air before the client moved here, not re-measured in midgard. Most of the remaining frame time is the collector itself, so caching a region's collected subtree between frames would be the bigger win. That isn't in this PR.

Things to check in review:

  • The child counts have to put every node back under the right parent. The new test in ServerRegionTest covers several roots, siblings and nesting levels, and checks a held fragment paints the same on two frames.
  • Anything that reads a held fragment directly would now see the prepared form. Only open() does, through fragmentFor().

708 tests pass locally. A local checkout needs composer update nativephp/mobile first, because the composer.lock lying around has no entry for it (the lock is gitignored).

This PR also adds CI, which midgard didn't have. .github/workflows/tests.yml runs Pest on PHP 8.3 and 8.4 and pint --test, on every pull request and on pushes to main. The install is composer update since there's no committed lock, so a run picks up whatever is on mobile-air's idea/asgard at the time. The Kotlin and Swift tests under tests/Native are not run.

🤖 Generated with Claude Code

simonhamp and others added 2 commits November 14, 2026 19:18
A held fragment used to be the nested tree that came off the wire, and
every frame walked it, normalized each node's children and rebuilt each
attribute bag with attrsFor(). Nothing in that changes between frames.

applyFragment() now holds the fragment prepared: three flat lists in
pre-order (type, child count, attribute bag). emit() walks those lists
and calls the collector's open(), leaf() and close() as before, so the
collected tree is the same. paint() prepares its nodes the same way.

In the binary-wire experiment this took a 1,202-node list from about
8.9 ms to 8.0 ms per frame, and moved about 1.7 ms onto receipt. Most of
what is left is the collector itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Tests run on PHP 8.3 and 8.4 for every pull request and for pushes to
main. composer.lock is ignored, so the install resolves fresh each time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant