⚡️ Speed up function get_user_store_api_key by 222% in PR #10702 (pluggable-auth-service)
#10767
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 #10702
If you approve this dependent PR, these changes will be merged into the original PR branch
pluggable-auth-service.📄 222% (2.22x) speedup for
get_user_store_api_keyinsrc/backend/base/langflow/api/v1/store.py⏱️ Runtime :
23.2 milliseconds→7.21 milliseconds(best of16runs)📝 Explanation and details
The optimization achieves a 221% speedup by introducing service instance caching using
@lru_cache(maxsize=1)in the authentication utility layer.Key Changes:
_cached_auth_service()function with@lru_cache(maxsize=1)decoratordecrypt_api_key()to use the cached service instead of calling_auth_service()directly_auth_service()for backward compatibilityWhy This Works:
The original code was calling
_auth_service()on everydecrypt_api_key()invocation, which likely involves expensive service initialization, dependency injection, or configuration loading. Withlru_cache(maxsize=1), the auth service instance is created once and reused across all subsequent calls within the same process.Performance Impact:
Line profiler shows the critical
decrypt_api_key()call dropped from 331,121 ns per hit to 249,235 ns per hit - a 25% improvement per call. When called 1,014 times in the test scenario, this compounds to significant savings.Workload Benefits:
This optimization is particularly effective for:
get_user_store_api_key()is called repeatedly in a short timeframeThe test results confirm the optimization works correctly across all edge cases (invalid keys, empty strings, None values) while providing substantial performance gains for valid decryption scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr10702-2025-11-27T23.46.48and push.