⚡️ Speed up function get_required_type_classes by 54%
#18
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.
📄 54% (0.54x) speedup for
get_required_type_classesinsrc/datadog_api_client/model_utils.py⏱️ Runtime :
1.52 milliseconds→990 microseconds(best of25runs)📝 Explanation and details
The optimized code achieves a 53% speedup through two key optimizations:
1. Faster type checking in
get_required_type_classes:isinstance(required_type, list/tuple/dict)withtype(required_type) is list/tuple/dictisinstance()which checks inheritance hierarchies, since we only care about exact type matchesisinstancecalls2. Early return optimization in
get_possible_classes:from_server_contextcheck to the very beginning, avoiding unnecessary list allocation whenfrom_server_context=True(which happens in 58% of calls based on profiler data)from_server_context=False, uses list concatenation[cls] + composed_model_input_classes(cls)instead ofextend()to avoid an extra list creation and resize operationPerformance characteristics:
from_server_context=Trueor when processing many non-container types, which aligns with typical API client usage patterns✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
test_pytest_testsv2test_scenarios_py_teststest_thread_py_teststest_version_py_teststest_deserialization_p__replay_test_0.py::test_src_datadog_api_client_model_utils_get_required_type_classesTo edit these changes
git checkout codeflash/optimize-get_required_type_classes-mgcu724kand push.