⚡️ Speed up method PathDeviationAnalyticsBlockV2.run by 17%
#634
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.
📄 17% (0.17x) speedup for
PathDeviationAnalyticsBlockV2.runininference/core/workflows/core_steps/analytics/path_deviation/v2.py⏱️ Runtime :
69.1 microseconds→59.1 microseconds(best of40runs)📝 Explanation and details
The optimized code achieves a 17% speedup through several key performance optimizations:
1. Reduced Dictionary Lookups
object_paths[video_id]asobject_paths_videoto avoid repeated dictionary lookups in the detection loopPATH_DEVIATION_KEY_IN_SV_DETECTIONSasoutput_keyto eliminate string constant lookups2. Memory-Efficient Array Construction
np.array(obj_path)withnp.fromiter(obj_path, dtype=np.float64).reshape(-1, 2)for faster conversion from list of tuples to numpy arraynp.ascontiguousarray()to ensure C-contiguous memory layout for faster access patterns during computation3. Optimized Distance Matrix Operations
np.ones() * -1tonp.full(-1.0)for more efficient matrix initializationnp.float64dtype throughout to avoid type conversion overhead4. Inlined Critical Path Operations
_compute_distance()to eliminate function call overhead in the hot recursive pathmin()operation with explicit comparisons to avoid Python builtin overhead5. Enhanced Edge Case Handling
float("inf")to prevent unnecessary computationThe optimizations are particularly effective for workloads with many tracked objects (as seen in test cases with multiple detections), where the reduced dictionary lookups and memory-efficient array operations compound. The 17% improvement comes primarily from eliminating repeated lookups and optimizing the memory-intensive Fréchet distance computation.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-PathDeviationAnalyticsBlockV2.run-mhbxqndeand push.