⚡️ Speed up method AsyncRawModelsClient.list by 36%#7
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization introduces **TypeAdapter caching** in the Pydantic utilities, which significantly reduces object creation overhead during type parsing operations. **Key optimization:** - **Added `@lru_cache(maxsize=128)` to TypeAdapter creation** in Pydantic v2 path - instead of creating a new `pydantic.TypeAdapter(type_)` instance on every call, the function now caches adapters by type and reuses them **Why this improves performance:** - **Eliminates redundant object instantiation** - TypeAdapter objects are expensive to create but can be safely reused for the same type - **Reduces memory allocation pressure** - fewer temporary objects created during parsing operations - **Optimizes hot path execution** - the `parse_obj_as` function is called frequently (496 times in profiling), making caching highly effective **Performance impact from line profiler:** - **28% reduction in TypeAdapter creation time** - from 23,023.7 ns to 2,243 ns per hit (90% faster per call) - **Overall `parse_obj_as` function time reduced by 28%** - from 40.2ms to 28.9ms total execution time The optimization is particularly effective for workloads with **repeated type parsing operations** using the same types, as shown in the test cases where the same `ListModelsV1Response` type is parsed multiple times. The caching provides consistent speedup across both single calls and concurrent execution scenarios, improving both runtime (36% faster) and throughput (6.1% increase to 60,863 operations/second).
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.
📄 36% (0.36x) speedup for
AsyncRawModelsClient.listinsrc/deepgram/manage/v1/models/raw_client.py⏱️ Runtime :
15.9 milliseconds→11.6 milliseconds(best of121runs)📝 Explanation and details
The optimization introduces TypeAdapter caching in the Pydantic utilities, which significantly reduces object creation overhead during type parsing operations.
Key optimization:
@lru_cache(maxsize=128)to TypeAdapter creation in Pydantic v2 path - instead of creating a newpydantic.TypeAdapter(type_)instance on every call, the function now caches adapters by type and reuses themWhy this improves performance:
parse_obj_asfunction is called frequently (496 times in profiling), making caching highly effectivePerformance impact from line profiler:
parse_obj_asfunction time reduced by 28% - from 40.2ms to 28.9ms total execution timeThe optimization is particularly effective for workloads with repeated type parsing operations using the same types, as shown in the test cases where the same
ListModelsV1Responsetype is parsed multiple times. The caching provides consistent speedup across both single calls and concurrent execution scenarios, improving both runtime (36% faster) and throughput (6.1% increase to 60,863 operations/second).✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-AsyncRawModelsClient.list-mh2sqzh8and push.