Provide more efficient ClockMark implementation
Do not use default 'plus' operator implementation, that creates a wrapping AdjustedClockMark instance, create instance of the same type instead.
This commit is contained in:
@@ -18,10 +18,12 @@ public expect object MonoClock : Clock
|
|||||||
public abstract class AbstractLongClock(protected val unit: DurationUnit) : Clock {
|
public abstract class AbstractLongClock(protected val unit: DurationUnit) : Clock {
|
||||||
protected abstract fun read(): Long
|
protected abstract fun read(): Long
|
||||||
|
|
||||||
override fun mark(): ClockMark = object : ClockMark() {
|
private class LongClockMark(val startedAt: Long, val clock: AbstractLongClock, val offset: Duration) : ClockMark() {
|
||||||
val startedAt = read()
|
override fun elapsed(): Duration = (clock.read() - startedAt).toDuration(clock.unit) - offset
|
||||||
override fun elapsed(): Duration = (read() - startedAt).toDuration(this@AbstractLongClock.unit)
|
override fun plus(duration: Duration): ClockMark = LongClockMark(startedAt, clock, offset + duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun mark(): ClockMark = LongClockMark(read(), this, Duration.ZERO)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,10 +32,12 @@ public abstract class AbstractLongClock(protected val unit: DurationUnit) : Cloc
|
|||||||
public abstract class AbstractDoubleClock(protected val unit: DurationUnit) : Clock {
|
public abstract class AbstractDoubleClock(protected val unit: DurationUnit) : Clock {
|
||||||
protected abstract fun read(): Double
|
protected abstract fun read(): Double
|
||||||
|
|
||||||
override fun mark(): ClockMark = object : ClockMark() {
|
private class DoubleClockMark(val startedAt: Double, val clock: AbstractDoubleClock, val offset: Duration) : ClockMark() {
|
||||||
val startedAt = read()
|
override fun elapsed(): Duration = (clock.read() - startedAt).toDuration(clock.unit) - offset
|
||||||
override fun elapsed(): Duration = (read() - startedAt).toDuration(this@AbstractDoubleClock.unit)
|
override fun plus(duration: Duration): ClockMark = DoubleClockMark(startedAt, clock, offset + duration)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun mark(): ClockMark = DoubleClockMark(read(), this, Duration.ZERO)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user