⚡️ Speed up method ExperimentalUIJWTToken.get_key_object_from_ui_hash_key by 26%
#443
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.
📄 26% (0.26x) speedup for
ExperimentalUIJWTToken.get_key_object_from_ui_hash_keyinlitellm/proxy/auth/auth_checks.py⏱️ Runtime :
963 microseconds→766 microseconds(best of169runs)📝 Explanation and details
The optimization applies LRU caching to the salt key retrieval using
@lru_cache(maxsize=1)on a new_cached_get_salt_key()function that wraps the original_get_salt_key()call.Key change: Instead of calling
_get_salt_key()directly indecrypt_value_helper(), it now calls_cached_get_salt_key()which caches the result after the first call.Why this provides a speedup: The line profiler shows that
_get_salt_key()consumes 99.7% of the execution time indecrypt_value_helper()(4.36 seconds out of 4.37 seconds total). This function performs expensive operations like environment variable lookups and imports fromlitellm.proxy.proxy_server. Since the salt key is static within a process lifecycle, caching eliminates this repeated overhead.Performance impact based on function references: The function is called from
_user_api_key_auth_builder()viaExperimentalUIJWTToken.get_key_object_from_ui_hash_key(), which is part of the authentication flow for UI login tokens. This authentication happens on every request that uses UI-based JWT tokens, making this a hot path where the caching provides significant value.Test case performance: The optimization shows consistent 20-35% improvements across all test scenarios, with particularly strong gains in the large-scale test cases (34% for the loop test). This indicates the optimization scales well with increased usage patterns where the same process handles multiple decryption operations, which is typical in a proxy server handling multiple concurrent requests.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-ExperimentalUIJWTToken.get_key_object_from_ui_hash_key-mhwz9ol2and push.