⚡️ Speed up function are_events_compatible by 176%
#795
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.
📄 176% (1.76x) speedup for
are_events_compatibleininference/core/interfaces/stream/watchdog.py⏱️ Runtime :
1.02 milliseconds→371 microseconds(best of47runs)📝 Explanation and details
The optimized code achieves a 175% speedup by eliminating expensive operations and reducing memory allocations through three key optimizations:
1. Early Exit Strategy
any(e is None for e in events)which scans the entire list even when the first element is Noneif not eventsfor empty lists and checksevents[0] is Noneimmediately, avoiding unnecessary iteration2. Single-Pass Validation
any()generator +frame_idslist) and performs two complete list traversalsforloop that checks both None values and frame_id equality simultaneously, eliminating the need for intermediate collections3. Reduced Memory Pressure
frame_idslist containing all frame_id values before comparisonfirst_idand compares directly during iteration, avoiding list allocation entirelyPerformance Impact by Test Case:
Function Usage Context:
Based on
compute_events_latency()calling this function with just 2 events, this optimization is particularly valuable since it's likely called frequently in event processing pipelines where latency calculations are performance-critical.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
inference/unit_tests/core/interfaces/stream/test_watchdog.py::test_are_events_compatible_when_empty_event_giveninference/unit_tests/core/interfaces/stream/test_watchdog.py::test_are_events_compatible_when_events_related_to_different_frames_giveninference/unit_tests/core/interfaces/stream/test_watchdog.py::test_are_events_compatible_when_events_related_to_the_same_frame_giveninference/unit_tests/core/interfaces/stream/test_watchdog.py::test_are_events_compatible_when_no_events_given🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-are_events_compatible-miqqaqcgand push.