⚡️ Speed up method AsyncV1SocketClient._handle_json_message by 315%#16
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization introduces **TypeAdapter caching** using `functools.lru_cache` to eliminate the expensive repeated creation of `pydantic.TypeAdapter` objects. **Key change:** - Added `_get_type_adapter()` function with `@functools.lru_cache(maxsize=32)` that caches TypeAdapter instances by type - Replaced direct `pydantic.TypeAdapter(type_)` calls with cached `_get_type_adapter(type_)` calls **Why this speeds up the code:** The line profiler shows that `pydantic.TypeAdapter(type_)` construction was taking **76.3% of execution time** (10.06ms out of 13.19ms total). TypeAdapter creation involves significant internal setup including type analysis, validation schema generation, and metadata processing. By caching these expensive objects, subsequent calls with the same type reuse the pre-built adapter instead of recreating it. **Performance impact:** - Original: 10.06ms spent on TypeAdapter creation (76.3% of total time) - Optimized: 3.39ms spent on cached adapter retrieval (53.1% of total time) - **67% reduction** in TypeAdapter overhead, leading to overall **315% speedup** This optimization is particularly effective for WebSocket message handling scenarios where the same message types (like `V1SocketClientResponse`) are parsed repeatedly, as shown in the test cases. The LRU cache with maxsize=32 provides excellent hit rates for typical usage patterns while preventing unbounded memory growth.
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.
📄 315% (3.15x) speedup for
AsyncV1SocketClient._handle_json_messageinsrc/deepgram/agent/v1/socket_client.py⏱️ Runtime :
3.79 milliseconds→913 microseconds(best of65runs)📝 Explanation and details
The optimization introduces TypeAdapter caching using
functools.lru_cacheto eliminate the expensive repeated creation ofpydantic.TypeAdapterobjects.Key change:
_get_type_adapter()function with@functools.lru_cache(maxsize=32)that caches TypeAdapter instances by typepydantic.TypeAdapter(type_)calls with cached_get_type_adapter(type_)callsWhy this speeds up the code:
The line profiler shows that
pydantic.TypeAdapter(type_)construction was taking 76.3% of execution time (10.06ms out of 13.19ms total). TypeAdapter creation involves significant internal setup including type analysis, validation schema generation, and metadata processing. By caching these expensive objects, subsequent calls with the same type reuse the pre-built adapter instead of recreating it.Performance impact:
This optimization is particularly effective for WebSocket message handling scenarios where the same message types (like
V1SocketClientResponse) are parsed repeatedly, as shown in the test cases. The LRU cache with maxsize=32 provides excellent hit rates for typical usage patterns while preventing unbounded memory growth.✅ Correctness verification report:
⏪ Replay Tests and Runtime
test_pytest_testsintegrationstest_integration_scenarios_py_testsunittest_core_utils_py_testsutilstest_htt__replay_test_0.py::test_deepgram_agent_v1_socket_client_AsyncV1SocketClient__handle_json_messageTo edit these changes
git checkout codeflash/optimize-AsyncV1SocketClient._handle_json_message-mh4gblr2and push.