Browse Source

Fix preciseDelay: delay() is proven to be inaccurate upto 2ms (#5173)

Noticed that in preciseDelay we may spent more than 1.5ms to expected.
pull/5175/head v1.8.0-alpha01
Nikita Lipsky 1 year ago committed by GitHub
parent
commit
f999cb09fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 13
      benchmarks/multiplatform/src/commonMain/kotlin/MeasureComposable.kt

13
benchmarks/multiplatform/src/commonMain/kotlin/MeasureComposable.kt

@ -8,7 +8,6 @@ import kotlin.time.Duration @@ -8,7 +8,6 @@ import kotlin.time.Duration
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.ExperimentalTime
import kotlinx.coroutines.*
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.TimeSource.Monotonic.markNow
import kotlin.time.measureTime
@ -24,15 +23,17 @@ expect fun runGC() @@ -24,15 +23,17 @@ expect fun runGC()
suspend inline fun preciseDelay(duration: Duration) {
val liveDelay: Duration
if (duration.inWholeMilliseconds > 1) {
val delayMillis = duration.inWholeMilliseconds - 1
if (duration.inWholeMilliseconds > 2) {
//experiments have shown that for precise delay we should do live delay at least 2 ms
val delayMillis = duration.inWholeMilliseconds - 2
val delayStart = markNow()
delay(delayMillis)
liveDelay = duration - delayMillis.milliseconds
liveDelay = duration - delayStart.elapsedNow()
} else {
liveDelay = duration
}
val start = markNow()
while (start.elapsedNow() < liveDelay){}
val liveDelayStart = markNow()
while (liveDelayStart.elapsedNow() < liveDelay){}
}
@OptIn(ExperimentalTime::class, InternalComposeUiApi::class)

Loading…
Cancel
Save