⚡ perf: optimize symmetric crypto by caching Cipher and Mac instances#7
⚡ perf: optimize symmetric crypto by caching Cipher and Mac instances#7SirDank wants to merge 1 commit into
Conversation
Replaced repeated `Cipher.getInstance()` and `Mac.getInstance()` calls in `CryptoHelper` with thread-local caches. These instantiations involve provider lookups and creation overhead. Because calling `init()` on a Cipher or Mac resets its state, reusing them is safe as long as they are not shared across threads. This results in a roughly 4x speedup (from 2769 ms to 660 ms for 100k encrypt/decrypt iterations). Co-authored-by: SirDank <52797753+SirDank@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What: Added
ThreadLocalcaches for theAES/ECB/NoPaddingcipher,AES/CBC/PKCS5Paddingcipher, andHmacSHA1mac inCryptoHelper.java. ThesymmetricDecrypt,symmetricEncryptWithIV,symmetricDecryptHMACIV, andsymmetricEncryptWithHMACIVmethods were updated to use.get()on these caches instead ofCipher.getInstance()andMac.getInstance().🎯 Why:
Cipher.getInstance()andMac.getInstance()involve expensive security provider lookups and object instantiation overhead. Since calling.init()on these objects completely resets their state, they can be safely reused for subsequent operations. BecauseCipherandMacare not thread-safe,ThreadLocalprovides an ideal caching mechanism to avoid this overhead without introducing concurrency issues.📊 Measured Improvement: A focused benchmark performing 100,000 iterations of encrypting and then decrypting a 1024-byte payload with a 32-byte key showed significant improvement:
PR created automatically by Jules for task 7903506315813761402 started by @SirDank