-
Notifications
You must be signed in to change notification settings - Fork 51
Sorted timestamps optimization #507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sorted timestamps optimization #507
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great!
@@ -308,6 +308,67 @@ access(all) contract FlowCallbackScheduler { | |||
} | |||
} | |||
|
|||
/// SortedTimestamps maintains a sorted array of timestamps for efficient processing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of this is pretty generic. I wish we could just put this in an ArrayUtils contract that is separate from this so we don't have all this sorting stuff cluttering up this contract, but that might not really be worth putting together
} | ||
insertIndex = i + 1 | ||
} | ||
self.timestamps.insert(at: insertIndex, timestamp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wow, I didn't even realize this was possible. This is great!
} | ||
|
||
/// Get all timestamps that are in the past (less than or equal to current timestamp) | ||
access(all) fun past(current: UFix64): [UFix64] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
access(all) fun past(current: UFix64): [UFix64] { | |
access(all) fun getPast(current: UFix64): [UFix64] { |
|
||
/// Check if there are any timestamps that need processing | ||
/// Returns true if processing is needed, false for early exit | ||
access(all) fun check(current: UFix64): Bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
access(all) fun check(current: UFix64): Bool { | |
access(all) fun checkIfTimestampsNeedProcessing(current: UFix64): Bool { |
Closes: #502
Summary
Optimizes the
process()
function inFlowCallbackScheduler
by replacing expensive map filtering with efficient sorted timestamp management, significantly reducing computation when called frequently with no pending work.Changes:
SortedTimestamps
struct: Encapsulates sorted timestamp logic with clean API (add(), remove(), past(), check()
)check()
method enables immediate return when no callbacks need processingpast()
method returns only timestamps needing processing without filtering entire mapPerformance Impact
Testing
Added isolated unit tests for
SortedTimestamp
s struct.