⚡️ Speed up function wrap_in_list by 12%
#797
Open
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.
📄 12% (0.12x) speedup for
wrap_in_listininference/core/interfaces/stream/utils.py⏱️ Runtime :
44.5 microseconds→39.8 microseconds(best of44runs)📝 Explanation and details
The optimization replaces
issubclass(type(element), list)withisinstance(element, list), resulting in an 11% speedup.Key optimization: The original code uses
issubclass(type(element), list)to check if an element is a list, which involves two function calls:type()to get the object's type, thenissubclass()to check inheritance. The optimized version usesisinstance(element, list), which is a single, more direct type check operation.Why it's faster:
isinstance()is implemented more efficiently in Python's C code and avoids the overhead of thetype()+issubclass()chain. The line profiler shows the condition check time reduced from 107,350ns to 84,656ns (21% faster on that line alone).Function usage context: This function is called in hot paths within the inference pipeline - specifically in
render_boxes(),send_predictions(), andactive_learning_sink()methods that process video frames and predictions. Since these methods are called frequently during video processing, the 11% improvement compounds significantly.Test case performance: The optimization shows consistent improvements across most test cases, with particularly strong gains for list inputs (24-41% faster) where the isinstance check immediately returns True. Non-list inputs see 7-17% improvements. A few edge cases with dict/bool inputs show slight regressions (1-14% slower), but these are likely within measurement noise and outweighed by the overall gains.
Behavioral preservation: Both approaches correctly handle list subclasses and maintain identical functionality -
isinstance()properly recognizes subclasses oflistjust like the originalissubclass(type(x), list)approach.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
inference/unit_tests/core/interfaces/stream/test_utils.py::test_wrap_in_list_when_list_providedinference/unit_tests/core/interfaces/stream/test_utils.py::test_wrap_in_list_when_single_element_provided🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-wrap_in_list-miqsx5lrand push.