Skip to content

perf: replace list length-checks with pattern matching#128

Merged
mhanberg merged 1 commit intoelixir-tools:mainfrom
katafrakt:list-length-to-patttern-matching
Apr 23, 2026
Merged

perf: replace list length-checks with pattern matching#128
mhanberg merged 1 commit intoelixir-tools:mainfrom
katafrakt:list-length-to-patttern-matching

Conversation

@katafrakt
Copy link
Copy Markdown
Contributor

This replaces few length check on hot paths with patternmatching. According to benchmarks I ran it gives 3-8% or performance gains on parsing a realistic files.

Comment thread lib/spitfire.ex
arity = length(args)
case args do
[_, _ | _] ->
arity = length(args)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We still need arity here to pass to Macro.operator? and Macro.special_form?, but we calculate it only if we know it's more than one.

mhanberg pushed a commit that referenced this pull request Apr 15, 2026
Less obvious (for me) than #128, but also seems like a quick perf win.
Perhaps surprisingly, using tail recursion is generally faster than
Keyword.has_key? by about 10%. In this case though the most important
thing is that we iterate over the list only once, instead of 2-3 times.

The benchmark shows 7-8% improvement on parsing files.
This replaces few length check on hot paths with patternmatching. According
to benchmarks I ran it gives 3-8% or performance gains on parsing a realistic
files.
@katafrakt katafrakt force-pushed the list-length-to-patttern-matching branch from 4594904 to e927453 Compare April 23, 2026 20:40
@mhanberg mhanberg merged commit 883764e into elixir-tools:main Apr 23, 2026
37 checks passed
@katafrakt katafrakt deleted the list-length-to-patttern-matching branch April 23, 2026 21:14
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.

3 participants