⚡️ Speed up function compute_events_latency by 253%
#794
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.
📄 253% (2.53x) speedup for
compute_events_latencyininference/core/interfaces/stream/watchdog.py⏱️ Runtime :
302 microseconds→85.6 microseconds(best of45runs)📝 Explanation and details
The optimized code achieves a 252% speedup by eliminating function call overhead and reducing unnecessary operations in the critical path.
Key optimizations:
Inlined compatibility check in
compute_events_latency: The original code calledare_events_compatible()which created a list and performed complex checks. The optimized version directly checks if either event is None or if frame_ids differ, eliminating function call overhead and list creation.Early-exit optimization in
are_events_compatible: Instead of usingany()with a generator expression and building a completeframe_idslist, the optimized version uses explicit loops that returnFalseimmediately upon finding the first None or mismatched frame_id.Performance impact by test case:
compute_events_latencyavoid the function call entirely when events are NoneHot path impact: Based on the function reference showing
compute_events_latencyis called within_generate_report()for latency monitoring, this optimization will improve the performance of stream processing pipelines where latency measurements are computed frequently. The 252% speedup means latency monitoring operations that previously took ~300μs now complete in ~85μs, reducing overhead in real-time video processing workflows.The optimizations preserve all original behavior while dramatically reducing computational overhead through smarter control flow and elimination of unnecessary operations.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
inference/unit_tests/core/interfaces/stream/test_watchdog.py::test_compute_events_latency_when_events_are_compatibleinference/unit_tests/core/interfaces/stream/test_watchdog.py::test_compute_events_latency_when_events_are_not_compatible🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-compute_events_latency-miqpyqt3and push.