Skip to content

Optimize Base.valid16?/2 #14429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2025
Merged

Optimize Base.valid16?/2 #14429

merged 1 commit into from
Apr 12, 2025

Conversation

sabiwara
Copy link
Contributor

@sabiwara sabiwara commented Apr 12, 2025

EDIT: the benchmark below was checking for an invalid string - discard and see sabiwara/elixir_benches@af34130 (still quite a nice speedup)

  • Using and instead of relying on a global rescue has a significant impact
  • Using a guard instead of a local rescue to check if the char is a non-nil member of a tuple also has almost as big of an impact

I haven't benched/done other functions yet, wanted to first share this one and wait for the feedback.

Reusing @whatyouhide 's big_string benchmark:

##### With input big string #####
Name                      ips        average  deviation         median         99th %
bool_plus_guard       11.97 M       83.56 ns ±13110.49%       20.90 ns      104.20 ns
bool                   4.72 M      212.01 ns ±19289.61%          84 ns         250 ns
original               1.31 M      765.72 ns  ±3523.35%         500 ns        1417 ns

Comparison: 
bool_plus_guard       11.97 M
bool                   4.72 M - 2.54x slower +128.46 ns
original               1.31 M - 9.16x slower +682.17 ns

Memory usage statistics:

Name               Memory usage
bool_plus_guard            40 B
bool                      288 B - 7.20x memory usage +248 B
original                 1216 B - 30.40x memory usage +1176 B

**All measurements for memory usage were the same**

Reduction count statistics:

Name            Reduction count
bool_plus_guard               8
bool                          8 - 1.00x reduction count +0
original                     73 - 9.13x reduction count +65

**All measurements for reduction count were the same**

@sabiwara sabiwara requested a review from whatyouhide April 12, 2025 00:05
@sabiwara sabiwara mentioned this pull request Apr 12, 2025
@sabiwara
Copy link
Contributor Author

Thx! Will merge this one and send a follow-up for other bases

@sabiwara sabiwara merged commit fecd6e3 into elixir-lang:main Apr 12, 2025
11 checks passed
@sabiwara sabiwara deleted the opti-base branch April 12, 2025 06:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants