Record write-once lazy builds against repeated reads and report the cache state.

write-once lazy A coordination report can record how a write-once value is shared. A Kotlin `by lazy` delegate runs its initializer the first time the value is read and caches the result, so repeated reads reuse the single build. With no reads the slot is unset, with one read it is set, and with repeated reads it is locked to the one cached build. The status line summarizes whether the slot stayed unset, set, or locked after the recorded reads, while the build counter confirms the initializer ran at most once.

Lazy Value Coordination Report

reads
LazyValueCoordinationReport.kt
Replay: real traced execution (multi-file project)
fun main() {
    val reads = 1
    var builds = 0
    val cached: Int by lazy {
        builds++
        7
    }
    var served = 0

    for (i in 0 until reads) {
        if (cached > 0) {
            served++
        }
    }

    val status = if (builds == 0) {
        "unset"
    } else if (served <= 1) {
        "set"
    } else {
        "locked"
    }

    println("reads=$reads served=$served builds=$builds $status")
}
fun main() {
    val reads = 0
    var builds = 0
    val cached: Int by lazy {
        builds++
        7
    }
    var served = 0

    for (i in 0 until reads) {
        if (cached > 0) {
            served++
        }
    }

    val status = if (builds == 0) {
        "unset"
    } else if (served <= 1) {
        "set"
    } else {
        "locked"
    }

    println("reads=$reads served=$served builds=$builds $status")
}
fun main() {
    val reads = 2
    var builds = 0
    val cached: Int by lazy {
        builds++
        7
    }
    var served = 0

    for (i in 0 until reads) {
        if (cached > 0) {
            served++
        }
    }

    val status = if (builds == 0) {
        "unset"
    } else if (served <= 1) {
        "set"
    } else {
        "locked"
    }

    println("reads=$reads served=$served builds=$builds $status")
}
  1. reads ← 1, builds ← 0, served ← 0

    1fun main() {2    val reads→ 1 = 1 //@reads=0, 23    var builds→ 0 = 04    val cached: Int by lazy {5        builds++6        77    }8    var served→ 0 = 0
  2. for (i in 0 until reads)

    10for (i0 in 0 until reads1) {11    if (cached > 0) {
  3. if (cached > 0)

    10for (i in 0 until reads) {11    if (cached7 > 0) {12        served0++13    }14}
  4. status ← set

    16    val status→ set = if (builds1 == 0) {17        "unset"18    } else if (served1 <= 1) {19        "set"20    } else {21        "locked"22    }2324    println("reads=$reads1 served=$served1 builds=$builds1 $statusset")25}
    outputreads=1 served=1 builds=1 set
  1. reads ← 0, builds ← 0, served ← 0, status ← unset

    1fun main() {2    val reads→ 0 = 03    var builds→ 0 = 04    val cached: Int by lazy {5        builds++6        77    }8    var served→ 0 = 0910    for (i in 0 until reads) {11        if (cached > 0) {12            served++13        }14    }1516    val status→ unset = if (builds0 == 0) {17        "unset"18    } else if (served0 <= 1) {19        "set"20    } else {21        "locked"22    }2324    println("reads=$reads0 served=$served0 builds=$builds0 $statusunset")25}
    outputreads=0 served=0 builds=0 unset
  1. reads ← 2, builds ← 0, served ← 0

    1fun main() {2    val reads→ 2 = 23    var builds→ 0 = 04    val cached: Int by lazy {5        builds++6        77    }8    var served→ 0 = 0
  2. for (i in 0 until reads)

    pass 1 of 2
    10for (i0 in 0 until reads2) {11    if (cached > 0) {
  3. if (cached > 0)

    pass 1 of 2
    10for (i in 0 until reads) {11    if (cached7 > 0) {12        served0++13    }14}
  4. for (i in 0 until reads)

    pass 2 of 2
    10for (i1 in 0 until reads2) {11    if (cached > 0) {
  5. if (cached > 0)

    pass 2 of 2
    10for (i in 0 until reads) {11    if (cached7 > 0) {12        served1++13    }14}
  6. status ← locked

    16    val status→ locked = if (builds1 == 0) {17        "unset"18    } else if (served2 <= 1) {19        "set"20    } else {21        "locked"22    }2324    println("reads=$reads2 served=$served2 builds=$builds1 $statuslocked")25}
    outputreads=2 served=2 builds=1 locked