Fix: peak_ahead returns events in correct chronological order#3010
Merged
Fix: peak_ahead returns events in correct chronological order#3010
Conversation
|
Performance benchmarks:
|
quaquel
reviewed
Dec 24, 2025
tests/experimental/test_devs.py
Outdated
| ) | ||
| event_list.add_event(event) | ||
|
|
||
| events = event_list.peak_ahead(5) |
Member
There was a problem hiding this comment.
please go ahead and change the name as part of this PR
quaquel
reviewed
Dec 24, 2025
tests/experimental/test_devs.py
Outdated
| assert event_times == sorted(event_times), ( | ||
| f"peak_ahead should return events in chronological order, got {event_times}" | ||
| ) | ||
| assert event_times == [5.0, 8.0, 10.0, 15.0, 20.0] |
quaquel
reviewed
Dec 24, 2025
tests/experimental/test_devs.py
Outdated
| events = event_list.peak_ahead(5) | ||
| event_times = [e.time for e in events] | ||
| # Events should be in chronological order: [5.0, 8.0, 10.0, 15.0, 20.0] | ||
| assert event_times == sorted(event_times), ( |
Member
There was a problem hiding this comment.
why not test agains sorted(times)?
Contributor
Author
There was a problem hiding this comment.
Updated! Simplified to assert event_times == sorted(times)[:5] per your suggestion.
574e170 to
c59c37a
Compare
Member
|
Can you please update the PR start post following one of the templates? It looks good to me otherwise, so once updated, I'll merge it. Thanks! |
Contributor
Author
|
@quaquel Updated the PR description with the bug template. Thanks! |
quaquel
approved these changes
Dec 26, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixed
EventList.peek_ahead()returning events in wrong order and renamed from peak_ahead to peek_ahead (typo fix).Bug
Fixes #3009
peek_ahead(n) was iterating directly over the internal heap list, which is NOT sorted. A binary heap only guarantees the minimum element at index 0.
Before: Events returned in heap order e.g.,
[5, 15, 8, 25, 20](15 before 8 is wrong!)After: Events returned in chronological order
[5, 8, 10, 15, 20]Implementation
nsmallestto heapq importsheapq.nsmallest(n, valid_events)Testing
Additional Notes
Breaking change: external code using peak_ahead() needs to rename to peek_ahead().