⚡️ Speed up function decrypt_api_key by 54% in PR #11397 (docs-mcp-header)#11398
Closed
codeflash-ai[bot] wants to merge 8 commits into
Closed
⚡️ Speed up function decrypt_api_key by 54% in PR #11397 (docs-mcp-header)#11398codeflash-ai[bot] wants to merge 8 commits into
decrypt_api_key by 54% in PR #11397 (docs-mcp-header)#11398codeflash-ai[bot] wants to merge 8 commits into
Conversation
…flow into docs-mcp-header
The optimization achieves a **54% speedup** by introducing **memoization** to avoid redundant cryptographic operations. ## Key Optimization: Caching Fernet Instance Creation The optimization introduces `@lru_cache(maxsize=128)` on a new helper function `_get_fernet_cached()` that caches Fernet instances by secret key. This addresses the most expensive operations in the original code: **Original Performance Bottlenecks:** - `ensure_valid_key()`: 66.6% of `get_fernet()` runtime (16.8ms out of 25.3ms) - `Fernet()` initialization: 21.3% of `get_fernet()` runtime (5.4ms) **How Caching Works:** When `get_fernet()` is called with the same secret key multiple times, `_get_fernet_cached()` returns the pre-computed Fernet instance from cache instead of: 1. Re-validating the key (which involves random number generation and base64 encoding for short keys) 2. Re-initializing the Fernet cipher (which involves cryptographic setup) **Performance Impact:** The line profiler shows `get_fernet()` dropped from **25.3ms** to **4.5ms** (82% faster). In `decrypt_api_key()`, the `get_fernet()` call dropped from **28.1ms** (37.9% of function time) to **7.2ms** (13.9% of function time). ## Why This Works In authentication systems, the same secret key is typically used repeatedly across many API key decryption operations. The test `test_large_scale_batch_decryption_performance_and_correctness` demonstrates this pattern: 500 decryptions using the same settings service benefit significantly from caching, as only the first call incurs the full initialization cost. The `lru_cache(maxsize=128)` is appropriately sized - authentication systems rarely use more than a handful of different secret keys in a single process, so a cache of 128 entries provides excellent hit rates without excessive memory overhead. ## Trade-offs - **Memory**: Cached Fernet instances persist in memory, but 128 entries is negligible - **Thread-safety**: `lru_cache` is thread-safe by default - **Correctness**: All test cases pass, confirming the optimization doesn't change behavior
Contributor
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## docs-1.8-release #11398 +/- ##
===================================================
Coverage ? 34.56%
===================================================
Files ? 1416
Lines ? 67428
Branches ? 9931
===================================================
Hits ? 23309
Misses ? 42895
Partials ? 1224
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Contributor
|
Closing automated codeflash PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #11397
If you approve this dependent PR, these changes will be merged into the original PR branch
docs-mcp-header.📄 54% (0.54x) speedup for
decrypt_api_keyinsrc/backend/base/langflow/services/auth/utils.py⏱️ Runtime :
17.9 milliseconds→11.6 milliseconds(best of26runs)📝 Explanation and details
The optimization achieves a 54% speedup by introducing memoization to avoid redundant cryptographic operations.
Key Optimization: Caching Fernet Instance Creation
The optimization introduces
@lru_cache(maxsize=128)on a new helper function_get_fernet_cached()that caches Fernet instances by secret key. This addresses the most expensive operations in the original code:Original Performance Bottlenecks:
ensure_valid_key(): 66.6% ofget_fernet()runtime (16.8ms out of 25.3ms)Fernet()initialization: 21.3% ofget_fernet()runtime (5.4ms)How Caching Works:
When
get_fernet()is called with the same secret key multiple times,_get_fernet_cached()returns the pre-computed Fernet instance from cache instead of:Performance Impact:
The line profiler shows
get_fernet()dropped from 25.3ms to 4.5ms (82% faster). Indecrypt_api_key(), theget_fernet()call dropped from 28.1ms (37.9% of function time) to 7.2ms (13.9% of function time).Why This Works
In authentication systems, the same secret key is typically used repeatedly across many API key decryption operations. The test
test_large_scale_batch_decryption_performance_and_correctnessdemonstrates this pattern: 500 decryptions using the same settings service benefit significantly from caching, as only the first call incurs the full initialization cost.The
lru_cache(maxsize=128)is appropriately sized - authentication systems rarely use more than a handful of different secret keys in a single process, so a cache of 128 entries provides excellent hit rates without excessive memory overhead.Trade-offs
lru_cacheis thread-safe by default✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr11397-2026-01-21T19.49.29and push.