In EEX Compiler, flatten expr list only once, not on every iteration#15331
Merged
josevalim merged 3 commits intoelixir-lang:mainfrom May 1, 2026
Merged
In EEX Compiler, flatten expr list only once, not on every iteration#15331josevalim merged 3 commits intoelixir-lang:mainfrom
josevalim merged 3 commits intoelixir-lang:mainfrom
Conversation
Contributor
Author
|
This is a before and after of how the expression block is aggregated: Before with
|
josevalim
reviewed
May 1, 2026
Co-authored-by: José Valim <jose.valim@gmail.com>
Contributor
Author
|
Seems like an unrelated test failed in the CI |
Member
|
💚 💙 💜 💛 ❤️ |
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.
Problem
This PR fixes a performance issue in
/lib/eex/lib/eex/compiler.ex:444-452->wrap_expr/5which computescount = current ++ placeholder ++ new_lines ++ charsfor every middle block and the end block of an expression. Thecurrentis the aggregator of the expression and because++is used, the compiler walks every character in that charlist before it appends the placeholder, new_lines, and chars. So, the longer thecurrentbecomes, the longer the compiler needs to walk the charlist until it finally appends the new block to the aggregator. This is roughlyO(N^2).Fix
With the help of Opus, I created a fix which builds a nested list of expressions and then flattens the list into a single charlist once the end block is reached, so flattening the list only once instead of after every middle block.
I also fixed another small issue which was that the
quotedexpressions were stored as Keyword list but retrieved like a Map (i.e. "find the expression for a given key") ininsert_quoted/2. I refactoredquotedto be a map which took care of the quadratic performance with large numbers of case arms. You can see it at theN = 1600case in the benchmarks where the average iteration takes 4.4x longer if N doubles (from 800 -> 1600).Benchmarks
Here are the before/after benchmarks:
I built a very long
case x do ... endexpression withNcase arms like1,2`, ... N as cases.