⚡️ Speed up function select_last_detection by 342%
#777
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.
📄 342% (3.42x) speedup for
select_last_detectionininference/core/workflows/core_steps/common/query_language/operations/detections/base.py⏱️ Runtime :
84.4 microseconds→19.1 microseconds(best of50runs)📝 Explanation and details
The optimization replaces
deepcopy(detections)withdetections.copy()when handling empty detections, delivering a 342% speedup (84.4μs → 19.1μs).Key optimization:
deepcopy()is extremely expensive because it recursively traverses and copies all nested objects, even for empty containers. The line profiler showsdeepcopy()consuming 73.7% of execution time (155,713 ns across 7 calls = ~22,244 ns per call), whiledetections.copy()takes only 10% of total time (~898 ns per call).Why this works: For empty
sv.Detectionsobjects, a shallow copy via.copy()produces identical behavior todeepcopy()since there are no elements to deep-copy. Thesv.Detectionsclass (which inherits list-like behavior) supports.copy()method that creates a new empty instance efficiently.Performance impact by test case:
This optimization is particularly valuable when
select_last_detection()frequently encounters empty detection sets, which appears common based on the test coverage emphasizing empty cases. The 7 out of 37 calls hitting the empty path in profiling suggests this scenario occurs regularly in real workloads.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-select_last_detection-miqhuaxuand push.