-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Noticeable upfront cost of Dispatchers.Default #4051
Comments
…Worker` init During the creation of `CoroutineScheduler.Worker`, we need to initialize its embedded random number generator. To avoid additional class-loading costs (#4051), `rngState` is now directly initialized from the current nanoTime (that is used as seed). Also, a potential bug is fixed, where `nextInt` will always return zero due to incorrect initialization of rngState with zero.
Worth noting that this code path is noticeable during our IDE startup, when Additional validation:
Can be run as
The resulting flamegraph: Flamegraph![bytecodes.svg](https://github.com/Kotlin/kotlinx.coroutines/assets/2780116/7117041e-a2e8-426c-80a4-57ea5e819478)The same results w.r.t. relative cost |
…id state (Kotlin#4052) A potential bug is fixed, where `nextInt` would always return zero due to incorrect initialization of rngState with zero. This could happen during the creation of a worker with thread id = `1595972770` (JDK >=8), or unpredictable if fallback thread-local random is used (android SDK <34 or JDK <7), approximate probability is 2.4E-10 Also, this slightly optimizes the performance of coroutines initialization. During the creation of `CoroutineScheduler.Worker`, we need to initialize its embedded random number generator. To avoid additional class-loading costs (Kotlin#4051), `rngState` is now directly initialized from the current nanoTime (that is used as seed). Fixes Kotlin#4051 Co-authored-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
Describe the problem
Creation of the
CoroutineScheduler
requires initialization ofkotlin.random.Random
, which, in the case ofkotlin-stdlib-jdk8
is used, requires dynamic class-loading ofandroid.os.BUNDLE
(see: https://youtrack.jetbrains.com/issue/KT-44089)However,
Dispatchers.Default
is often used early in the application runtime when startup logic uses coroutines, and additional class-loading delays are unfortunate.See github: "main" /Dispatchers.Default/ for examples.
Profiler result
Provide a Reproducer
build.gradle.kts
The text was updated successfully, but these errors were encountered: