Optimize to_iodata to avoid accumulating components map#4271
Open
preciz wants to merge 1 commit into
Open
Conversation
The components map accumulator in the to_iodata recursive traversal was completely discarded at the end of the public to_iodata/2 function. Additionally, updating the accumulator to memoize resolved template pointers was redundant because each stateful component instance is visited exactly once per page rendering tree. By stripping out the unused accumulator logic from to_iodata and simplifying resolve_components_xrefs, we eliminate unneeded tuple allocation and map updates. Benchmarks show ~40% memory reduction and 10-30% speedup.
josevalim
approved these changes
Jun 11, 2026
Member
Is this correct? What if there's a chain where C->B->A share statics? |
Contributor
Author
|
@SteffenDE I believe there is a test case for this: phoenix_live_view/test/phoenix_live_view/diff_test.exs Lines 98 to 101 in 8d2ecf2 From a performance side it seems the version in main had its tradeoffs and this branch is faster in that case too. |
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.
The components map accumulator in the to_iodata recursive traversal was completely discarded at the end of the public to_iodata/2 function. Additionally, updating the accumulator to memoize resolved template pointers was redundant because each stateful component instance is visited exactly once per page rendering tree.
By stripping out the unused accumulator logic from to_iodata and simplifying resolve_components_xrefs, we eliminate unneeded tuple allocation and map updates.
Benchmarks show ~40% memory reduction and 10-30% speedup.