Repository navigation
Avoid parsing the join HTML three times - #4508
Open
etareduction wants to merge 2 commits into
Open
etareduction wants to merge 2 commits into
etareduction wants to merge 2 commits into
Conversation
On a view's first join, maybeRecoverForms parsed the rendered HTML into a template even when there were no forms to recover, and findPhxChildrenInFragment parsed it again to look for child LiveViews, before the patch parsed it a third time. On large pages each parse is a noticeable part of the mount. Skip form recovery when there are no forms to recover, and only parse for child LiveViews when the HTML contains the parent id attribute they are rendered with. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collect the forms for recovery into an object without a prototype, so that a form with the id "__proto__" is a key of it: before, it replaced the object's prototype, left no keys behind and was skipped by the empty check. Match the parent id attribute case-insensitively, as the HTML parser lowercases attribute names. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This branch has not been deployed
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.
On a LiveView's first join, the client turns the rendered HTML into DOM three times:
maybeRecoverFormssetstemplate.innerHTML = htmleven whenformsForRecoveryis empty, which it always is on a first join (getFormsForRecoveryreturns{}whilejoinCount === 0), so the template is thrown away unused.onJoinCompletecallsDOM.findPhxChildrenInFragment(html, this.id), which parses the HTML again to look for child LiveViews.DOMPatchhands the HTML string to morphdom, which parses it a third time.Without forms to recover and without child LiveViews, only the third parse is needed. This happens on every page load and every live navigation, and on a large page each parse is a noticeable part of the frame that applies the mount.
This PR:
maybeRecoverFormswhen there are no forms to recover, before creating the template;findPhxChildrenInFragmentwhen the HTML does not containdata-phx-parent-id(case-insensitively), the attribute every nested LiveView is rendered with and thatfindPhxChildrenmatches on;__proto__replaces the object's prototype instead of becoming a key, and the new empty check would skip recovering it.Why behavior doesn't change
maybeRecoverForms: whenformsForRecoveryhas no keys, the existing filternewForm.id && oldForms[newForm.id]can only match throughObject.prototype(ids likeconstructor), and the code then throws on.getAttribute. In every other case it calls the callback synchronously. Everything it does before that touches only the throwaway template.findPhxChildrenInFragment: the HTML parser takes attribute names from the source, lowercasing ASCII letters and not decoding character references. So if those characters are not in the HTML in some letter case, no element can carry the attribute, and the query returns[].To check this beyond the argument, I ran the jest and e2e suites (Chromium, Firefox, WebKit) against a build where each early return also ran the old code on the same input and threw if the two disagreed. There were no disagreements over 2,004 skipped parses in the e2e suite and 450 in jest, and the e2e suite passed. With the conditions deliberately broken, the same build reported 5 disagreements.
Numbers
A single-file app (based on
.github/single-file-samples/main.exs) navigates from a small page to a page of N cards, each about 270 elements. The minified CDN bundle was built frommainand from this branch. It ran on a Hetzner CCX33 (8 dedicated EPYC Milan vCPUs, 0% steal), with the app, the browser and the rest of the system pinned to separate cores, using the browsers that Playwright 1.65 installs. Runs alternate in ABBA order, 30 pairs per row. Values are medians. "Parse time" is the time spent intemplate.innerHTMLduring the join, "onJoin" the wall time ofView.prototype.onJoin.onJoinThe 95% bootstrap interval of every paired difference excludes zero. In Chromium, the long animation frame holding the 2 MiB live navigation's join went from 240 to 176 ms.
Tests
dom_test.ts:findPhxChildrenInFragmentstill finds the children of the given parent, also when the attribute is not lowercase, and does not parse HTML without child LiveViews.view_test.ts:maybeRecoverFormsdoes not parse without forms to recover, and still recovers a form with the id__proto__. A first join without child LiveViews parses its HTML once (three times onmain), and a first join still joins a child LiveView found in its HTML.🤖 Generated with Claude Code