⚡️ Speed up method AsyncRawModelsClient.list by 36%
#7
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.