-
Notifications
You must be signed in to change notification settings - Fork 73
adds support for swift-concurrency Duration and async closures for Timer measurements #135
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
Open
AndriusA
wants to merge
1
commit into
apple:main
Choose a base branch
from
AndriusA:aa/concurrency-duration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -24,6 +24,7 @@ extension Timer { | |||||||
| /// - dimensions: The dimensions for the Timer. | ||||||||
| /// - body: Closure to run & record. | ||||||||
| @inlinable | ||||||||
| @available(*, deprecated, message: "Please use non-static version on an already created Timer") | ||||||||
| public static func measure<T>(label: String, dimensions: [(String, String)] = [], body: @escaping () throws -> T) rethrows -> T { | ||||||||
| let timer = Timer(label: label, dimensions: dimensions) | ||||||||
| let start = DispatchTime.now().uptimeNanoseconds | ||||||||
|
|
@@ -74,3 +75,88 @@ extension Timer { | |||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| #if (os(macOS) && swift(>=5.7.1)) || (!os(macOS) && swift(>=5.7)) | ||||||||
| extension Timer { | ||||||||
| /// Convenience for recording a duration based on Duration. | ||||||||
| /// | ||||||||
| /// - parameters: | ||||||||
| /// - duration: The duration to record. | ||||||||
| @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) | ||||||||
| public func record(duration: Duration) { | ||||||||
| self.recordNanoseconds(duration.nanosecondsClamped) | ||||||||
| } | ||||||||
|
|
||||||||
| /// Convenience for recording a duration since Instant using provided Clock | ||||||||
| /// | ||||||||
| /// - parameters: | ||||||||
| /// - instant: The instant to measure duration since | ||||||||
| /// - clock: The clock to measure duration with | ||||||||
| @inlinable | ||||||||
| @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) | ||||||||
| public func recordDurationSince<C: Clock>( | ||||||||
| instant: C.Instant, | ||||||||
| clock: C = ContinuousClock.continuous | ||||||||
| ) where C.Duration == Duration { | ||||||||
| self.record(duration: instant.duration(to: clock.now)) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) | ||||||||
| internal extension Swift.Duration { | ||||||||
| /// The duration represented as nanoseconds, clamped to maximum expressible value. | ||||||||
| var nanosecondsClamped: Int64 { | ||||||||
| let components = self.components | ||||||||
|
|
||||||||
| let secondsComponentNanos = components.seconds.multipliedReportingOverflow(by: 1_000_000_000) | ||||||||
| let attosCompononentNanos = components.attoseconds / 1_000_000_000 | ||||||||
| let combinedNanos = secondsComponentNanos.partialValue.addingReportingOverflow(attosCompononentNanos) | ||||||||
|
|
||||||||
| guard | ||||||||
| !secondsComponentNanos.overflow, | ||||||||
| !combinedNanos.overflow | ||||||||
| else { | ||||||||
|
Comment on lines
+117
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| return .max | ||||||||
| } | ||||||||
|
|
||||||||
| return combinedNanos.partialValue | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| extension Timer { | ||||||||
| /// Convenience for measuring duration of a closure | ||||||||
| /// | ||||||||
| /// - parameters: | ||||||||
| /// - body: Closure to run & record. | ||||||||
| @inlinable | ||||||||
| @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) | ||||||||
| public func measure<T>( | ||||||||
| body: @escaping () throws -> T | ||||||||
| ) rethrows -> T { | ||||||||
| let start = ContinuousClock.continuous.now | ||||||||
| defer { | ||||||||
| self.recordDurationSince(instant: start, clock: ContinuousClock.continuous) | ||||||||
| } | ||||||||
| return try body() | ||||||||
| } | ||||||||
|
|
||||||||
| /// Convenience for measuring duration of an async closure with a provided clock | ||||||||
| /// | ||||||||
| /// - parameters: | ||||||||
| /// - clock: The clock to measure closure duration with | ||||||||
| /// - body: Closure to run & record. | ||||||||
| @inlinable | ||||||||
| @available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) | ||||||||
| public func measure<T, C: Clock>( | ||||||||
| clock: C = ContinuousClock.continuous, | ||||||||
| body: @escaping () async throws -> T | ||||||||
| ) async rethrows -> T where C.Duration == Duration { | ||||||||
| let start = clock.now | ||||||||
| defer { | ||||||||
| self.recordDurationSince(instant: start, clock: clock) | ||||||||
| } | ||||||||
| return try await body() | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| #endif // (os(macOS) && swift(>=5.7.1)) || (!os(macOS) && swift(>=5.7)) | ||||||||
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
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.
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.
maybe this value could be guarded into a static constant since this never mutates and is being used in both
secondsComponentNanos&attosCompononentNanos