Skip to content

perf: prefer recursion over multiple Keyword.has_key?#129

Merged
mhanberg merged 1 commit intoelixir-tools:mainfrom
katafrakt:avoid-keyword-has-key
Apr 15, 2026
Merged

perf: prefer recursion over multiple Keyword.has_key?#129
mhanberg merged 1 commit intoelixir-tools:mainfrom
katafrakt:avoid-keyword-has-key

Conversation

@katafrakt
Copy link
Copy Markdown
Contributor

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.

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.
@katafrakt katafrakt changed the title perf: prefer recursion over Keyword.has_key? perf: prefer recursion over multiple Keyword.has_key? Apr 15, 2026
@mhanberg
Copy link
Copy Markdown
Contributor

Can you share the benchmarking method you are using? Could be useful for the repo.

@katafrakt
Copy link
Copy Markdown
Contributor Author

katafrakt commented Apr 15, 2026

I used existing bench.exs with some minor modifications and ran it before and after the changes. So not anything super-scientific, but few runs like that gave generally similar results.

For this PR I also created a standalone test with Benchee to see that recursive version is actually faster than Keyword.has_key? (although Benchee warns that for such fast operations it might not be super accurate).

@mhanberg mhanberg merged commit 47fad18 into elixir-tools:main Apr 15, 2026
39 checks passed
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.

2 participants