⚡ perf: Optimize type reflection during dataclass decoding#339
⚡ perf: Optimize type reflection during dataclass decoding#339
Conversation
The `typing.get_type_hints()` function can be extremely slow to run continuously in a loop when decoding repetitive API responses back to dataclass targets. Adding a `@functools.lru_cache()` drastically reduces the overhead since the classes generally remain static per execution. Co-authored-by: abn <165325+abn@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
for more information, see https://pre-commit.ci
Review Summary by QodoCache get_type_hints to optimize dataclass decoding performance
WalkthroughsDescription• Added LRU cache wrapper for get_type_hints() calls • Reduces reflection overhead during dataclass decoding • Achieves ~60% performance improvement on repeated decoding • Includes benchmark script demonstrating performance gains Diagramflowchart LR
A["Dataclass Decoding"] --> B["get_type_hints Call"]
B --> C["LRU Cache Check"]
C -->|Cache Hit| D["Return Cached Types"]
C -->|Cache Miss| E["Compute Type Hints"]
E --> F["Store in Cache"]
F --> D
D --> G["Decode Fields"]
File Changes1. src/aiographql/client/codec.py
|
Code Review by Qodo
1. Benchmark fails Ruff hooks
|
💡 What: Added an LRU-cached wrapper function
_get_type_hints_cachedaroundget_type_hints(target_type)insidesrc/aiographql/client/codec.py.🎯 Why: The codebase relied on calling
get_type_hints(target_type)for every element it decoded into a dataclass. This caused an excessive amount of reflection overhead, particularly when iterating and deserializing arrays of objects.📊 Measured Improvement:
100,000objects using the previous codec).100,000objects with the cache patch applied).PR created automatically by Jules for task 16241414663598073856 started by @abn