Skip to content
New issue

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

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

Already on GitHub? # to your account

Reduce memory pressure when decrypting large binary size #119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

wingyplus
Copy link

@wingyplus wingyplus commented Feb 6, 2023

Hi,

I found memory pressure issues when encrypting ~200 MB bytes and decrypting it. Let me show reproduce steps:

First, setup vault in project:

defmodule CloakBug.Vault do
  use Cloak.Vault, otp_app: :cloak_bug
end
import Config

config :cloak_bug, CloakBug.Vault,
  ciphers: [
    default:
      {Cloak.Ciphers.AES.GCM,
       tag: "AES.GCM.V1",
       key: Base.decode64!("nZqBx2TpKXU/c+SX54Ki65igNzqPO8JT8iUfUSkKV7g="),
       iv_length: 12}
  ]

Then open IEx shell and prepare encryption:

iex(1)> pid = CloakBug.Vault.start_link()      
iex(2)> b = :rand.bytes(209_715_200) # ~200MB
iex(3)> ciphertext = CloakBug.Vault.encrypt!(b)

Open observer before decrypting ciphertext and decrypting it!:

iex(4)> :observer.start
iex(5)> CloakBug.Vault.decrypt!(ciphertext)  

Here is screen captures from observer:

Memory Usage:

Screenshot 2566-02-06 at 10 34 17

Carrier Size:

Screenshot 2566-02-06 at 10 34 28

All Allocator Types:

Screenshot 2566-02-06 at 10 34 40

After tweak with this PR:

Memory Usage:

Screenshot 2566-02-06 at 10 38 24

Carrier Size:

Screenshot 2566-02-06 at 10 38 37

All Allocator Types:

Screenshot 2566-02-06 at 10 38 40

The issue that I found is copy message that is converted to list during decoding tag. The code is not perfect, feel free to discuss.

Do not copying `message` to calculate tag length. This could reduce
memory pressure during decode the tag.
@coveralls
Copy link

coveralls commented Feb 6, 2023

Coverage Status

Coverage: 98.98%. Remained the same when pulling d939911 on wingyplus:optimize-cloak-tags-decoder into 404727b on danielberkompas:master.

@wingyplus
Copy link
Author

ping @danielberkompas 🙇

@danielberkompas
Copy link
Owner

@wingyplus Sorry for the long delay here. My prior role at my job didn't leave me with much energy for open source maintenance.

I really appreciate all the charts and graphs showing the performance improvement here. Can you elaborate on the exact problem though? I know it's been awhile, so you may not remember the details at this point.

The issue that I found is copy message that is converted to list during decoding tag. The code is not perfect, feel free to discuss.

@wingyplus
Copy link
Author

wingyplus commented Apr 6, 2024

@danielberkompas Let me recall the situation ...😂

In my work, I used this library to encrypt the zip file that's contain a bunch of XML file.

One day our production got crash, I start investigating, and found that it crash because of OOM while decrypting the zip file, the file size is ~115MB.

@wingyplus
Copy link
Author

Hi @danielberkompas, what else I need to do to get it merge?

@logandk
Copy link

logandk commented Jun 20, 2024

We've encountered memory exhaustion for large files (~100 MB file consuming >4 GB memory) during decryption. I tested this PR and confirmed that memory consumption is significantly reduced.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants