⚡️ Speed up method AuthService.encrypt_api_key by 12% in PR #11639 (docs-chat-refactor-and-screenshots)#11643
Closed
codeflash-ai[bot] wants to merge 6 commits into
Closed
Conversation
The optimized code achieves an **11% speedup** by introducing **instance-level caching** for the `Fernet` object used in encryption operations. ## Key Optimization: Fernet Instance Caching The original code creates a new `Fernet` instance on every call to `_get_fernet()`, which is invoked for each `encrypt_api_key()` operation. The line profiler shows that `Fernet(valid_key)` construction takes **60.7% of the time** in `_get_fernet()` (1.12ms out of 1.85ms per call). The optimized version caches both the secret key and the constructed `Fernet` instance as instance attributes (`_cached_secret_key` and `_cached_fernet`). When `_get_fernet()` is called, it checks if: 1. A cached Fernet instance exists 2. The current secret key matches the cached secret key If both conditions are true (which happens in 104 out of 111 calls based on the profiler), it returns the cached instance, avoiding expensive re-initialization. ## Performance Impact From the line profiler data: - **Original**: `_get_fernet()` takes 1.85ms total per call - **Optimized**: `_get_fernet()` takes only 0.58ms total (68% reduction) - Cache hits (104/111 calls) execute only the fast path with string comparison and cached return - Cache misses (7/111 calls) still pay the full cost but also populate the cache The optimization reduces `_get_fernet()` overhead in `encrypt_api_key()` from **20%** to **10.4%** of total runtime. ## Test Case Performance The optimization is most effective for workloads with: - **Repeated encryption calls** with the same secret key (e.g., `test_encrypt_many_different_api_keys`, `test_encrypt_api_key_repeated_calls_consistency`) - the cache persists across multiple API key encryptions - **Large-scale operations** (`test_large_scale_encrypt_many_api_keys_performance_and_correctness` with 100 keys) - amortizes the one-time initialization cost The cache correctly invalidates when the secret key changes, maintaining correctness while capturing the common case where the secret key remains constant across multiple encryption operations.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## docs-1.8-release #11643 +/- ##
===================================================
Coverage ? 35.21%
===================================================
Files ? 1521
Lines ? 72930
Branches ? 10936
===================================================
Hits ? 25679
Misses ? 45856
Partials ? 1395
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Base automatically changed from
docs-chat-refactor-and-screenshots
to
docs-1.8-release
February 10, 2026 16:03
Contributor
Author
|
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 #11639
If you approve this dependent PR, these changes will be merged into the original PR branch
docs-chat-refactor-and-screenshots.📄 12% (0.12x) speedup for
AuthService.encrypt_api_keyinsrc/backend/base/langflow/services/auth/service.py⏱️ Runtime :
2.66 milliseconds→2.38 milliseconds(best of57runs)📝 Explanation and details
The optimized code achieves an 11% speedup by introducing instance-level caching for the
Fernetobject used in encryption operations.Key Optimization: Fernet Instance Caching
The original code creates a new
Fernetinstance on every call to_get_fernet(), which is invoked for eachencrypt_api_key()operation. The line profiler shows thatFernet(valid_key)construction takes 60.7% of the time in_get_fernet()(1.12ms out of 1.85ms per call).The optimized version caches both the secret key and the constructed
Fernetinstance as instance attributes (_cached_secret_keyand_cached_fernet). When_get_fernet()is called, it checks if:If both conditions are true (which happens in 104 out of 111 calls based on the profiler), it returns the cached instance, avoiding expensive re-initialization.
Performance Impact
From the line profiler data:
_get_fernet()takes 1.85ms total per call_get_fernet()takes only 0.58ms total (68% reduction)The optimization reduces
_get_fernet()overhead inencrypt_api_key()from 20% to 10.4% of total runtime.Test Case Performance
The optimization is most effective for workloads with:
test_encrypt_many_different_api_keys,test_encrypt_api_key_repeated_calls_consistency) - the cache persists across multiple API key encryptionstest_large_scale_encrypt_many_api_keys_performance_and_correctnesswith 100 keys) - amortizes the one-time initialization costThe cache correctly invalidates when the secret key changes, maintaining correctness while capturing the common case where the secret key remains constant across multiple encryption operations.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr11639-2026-02-07T00.39.04and push.