Skip to content
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

Use a binary accumulator in QuotedPrintable encoder to reduce memory usage #145

Merged
merged 1 commit into from May 24, 2022
Merged

Use a binary accumulator in QuotedPrintable encoder to reduce memory usage #145

merged 1 commit into from May 24, 2022

Conversation

michallepicki
Copy link
Contributor

@michallepicki michallepicki commented May 24, 2022

Hi! I noticed huge memory usage when encoding big emails (~15MB, that big mainly because of someone's huge base64-encoded inline image in their mail signature) after investigation, I found that currently the code builds a list of small binaries, reverses and joins it. As per the Erlang Efficiency Guide, appending to binaries is well optimized and should perform better.

I also confirmed the functions can be tail-call optimized the way they're written currently, with returning if expressions (at least on newest OTP 25).

Similarly to #86 , here is the benchmark used and results:

len = 1024 * 1024 * 15
bin =
  (len * 2)
  |> :crypto.strong_rand_bytes()
  |> Base.encode64()
  |> String.slice(0, len)

Benchee.run(%{
  "OldQuotedPrintable.encode/1" => fn ->
    Mail.Encoders.OldQuotedPrintable.encode(bin)
  end,
  "BinaryAccQuotedPrintable.encode/1" => fn ->
    Mail.Encoders.QuotedPrintable.encode(bin)
  end,
}, time: 10, memory_time: 2)

Results:

# mix run bench.exs
Operating System: Linux
CPU Information: AMD Ryzen 9 5900HX with Radeon Graphics
Number of Available Cores: 16
Available memory: 13.58 GB
Elixir 1.14.0-dev
Erlang 25.0

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 10 s
memory time: 2 s
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 28 s

Benchmarking BinaryAccQuotedPrintable.encode/1 ...
Benchmarking OldQuotedPrintable.encode/1 ...

Name                                        ips        average  deviation         median         99th %
BinaryAccQuotedPrintable.encode/1          2.73         0.37 s     ±2.48%         0.37 s         0.38 s
OldQuotedPrintable.encode/1                0.23         4.29 s     ±0.93%         4.27 s         4.34 s

Comparison: 
BinaryAccQuotedPrintable.encode/1          2.73
OldQuotedPrintable.encode/1                0.23 - 11.73x slower +3.93 s

Memory usage statistics:

Name                                 Memory usage
BinaryAccQuotedPrintable.encode/1         0.59 GB
OldQuotedPrintable.encode/1               1.06 GB - 1.81x memory usage +0.48 GB

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

@bcardarella
Copy link
Member

Nice!

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.

None yet

2 participants