⚡️ Speed up function decrypt_api_key by 51% in PR #11376 (fix-encryption-key-logging)
#11380
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 #11376
If you approve this dependent PR, these changes will be merged into the original PR branch
fix-encryption-key-logging.📄 51% (0.51x) speedup for
decrypt_api_keyinsrc/backend/base/langflow/services/auth/utils.py⏱️ Runtime :
7.43 milliseconds→4.92 milliseconds(best of12runs)📝 Explanation and details
The optimized code achieves a 51% speedup by introducing memoization via
@lru_cacheto avoid redundant Fernet object creation.Key Optimization
The core change extracts Fernet creation into a cached helper function
_fernet_from_secret():Why This Works
Looking at the line profiler data for
get_fernet():Original version (9.7ms total):
ensure_valid_key(): 6.77ms (69.6%)Fernet()construction: 2.36ms (24.3%)Optimized version (1.0ms total):
_fernet_from_secret(): 0.41ms (40.3%)The cache eliminates ~90% of get_fernet() execution time by reusing previously created Fernet objects when called with the same secret key, rather than re-executing the expensive
ensure_valid_key()(which involves random number generation, base64 encoding) and Fernet initialization on every call.Impact on decrypt_api_key()
Since
decrypt_api_key()callsget_fernet()at the start:get_fernet()(35.9% of function time)get_fernet()(9.3% of function time)This reduces
decrypt_api_key()from 29.9ms to 20.9ms overall.Test Case Performance
The optimization particularly benefits workloads with:
test_many_sequential_decryptions_stabilitywith 200 iterations)For single-call scenarios, the speedup is minimal, but the cache has negligible overhead (simple dictionary lookup).
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr11376-2026-01-20T20.59.12and push.