Make TimeMark returned by TimeSource.Monotonic a value class

This value class wraps Long on JVM and Native thus reducing allocations
in time measurement scenarios when the default monotonic time source is
statically known.

KT-46132
This commit is contained in:
Ilya Gorbunov
2022-01-25 00:47:21 +03:00
committed by Space
parent 5b45ea9bc9
commit fede70d0d5
11 changed files with 281 additions and 49 deletions
@@ -5,9 +5,24 @@
package kotlin.time
import kotlin.time.Duration.Companion.nanoseconds
@SinceKotlin("1.3")
@ExperimentalTime
internal actual object MonotonicTimeSource : AbstractLongTimeSource(unit = DurationUnit.NANOSECONDS), TimeSource { // TODO: interface should not be required here
override fun read(): Long = System.nanoTime()
internal actual object MonotonicTimeSource : TimeSource {
private fun read(): Long = System.nanoTime()
override fun toString(): String = "TimeSource(System.nanoTime())"
}
actual override fun markNow(): DefaultTimeMark = DefaultTimeMark(read())
actual fun elapsedFrom(timeMark: DefaultTimeMark): Duration = (read() - timeMark.reading).nanoseconds
// may have questionable contract
actual fun adjustReading(timeMark: DefaultTimeMark, duration: Duration): DefaultTimeMark =
DefaultTimeMark(timeMark.reading + duration.inWholeNanoseconds)
}
@Suppress("ACTUAL_WITHOUT_EXPECT") // visibility
internal actual typealias DefaultTimeMarkReading = Long