Skip to content

Optimize Base.decode* #14430

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.decode* #14430

merged 1 commit into from
Apr 12, 2025

Conversation

sabiwara
Copy link
Contributor

@sabiwara sabiwara commented Apr 12, 2025

Since #14429 seemed to indicate that using rescue to check for tuple membership was not optimal, I tried to replace it by defensively checking the index (not beautiful I know 😅).
The result looks promising, although since decode_ function is overall doing more work than valid_? the results are less impressive in comparison.

The result is more flaky here - sometimes getting 14% speed increase, sometimes 55% - but it's consistently quite faster for a big string.

##### With input small string #####
Name               ips        average  deviation         median         99th %
no_raise        3.50 M      285.86 ns ±10510.17%         208 ns         375 ns
original        3.32 M      301.58 ns ±10681.55%         208 ns         375 ns

Comparison: 
no_raise        3.50 M
original        3.32 M - 1.05x slower +15.72 ns

Memory usage statistics:

Name        Memory usage
no_raise           168 B
original           168 B - 1.00x memory usage +0 B

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

##### With input big string #####
Name               ips        average  deviation         median         99th %
original        254.71        3.93 ms     ±6.28%        3.90 ms        4.19 ms
no_raise        205.72        4.86 ms    ±42.61%        3.44 ms        8.25 ms

Comparison: 
original        254.71
no_raise        205.72 - 1.24x slower +0.93 ms

Memory usage statistics:

Name        Memory usage
original           184 B
no_raise           184 B - 1.00x memory usage +0 B

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

Benchee.run(
  [
    original: &Base.decode64!/1,
    no_raise: &MyBase.decode64!/1
  ],
  memory_time: 2,
  # reduction_time: 1,
  inputs: [
    "small string": Base.encode64("hello world"),
    "big string": Base.encode64(:crypto.strong_rand_bytes(1_000_000))
  ]
)

Also, perhaps we could reduce the duplication here, WDYT?

@sabiwara sabiwara changed the title Optimize Base.decode*: do not rescue elem/2 Optimize Base.decode* Apr 12, 2025
@sabiwara sabiwara merged commit 30711a7 into elixir-lang:main Apr 12, 2025
11 checks passed
@sabiwara sabiwara deleted the opti-base2 branch April 12, 2025 06:49
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