identity-compare-immutable-setting

Installation
SKILL.md

One immutable value, compared by identity

A consumer running per audio buffer or per frame needs the current setting on every pass, and needs to know cheaply whether it changed since last time. Two @Volatile fields plus a numeric diff gives you the worst of both: N comparisons on the hot path, and a window where the consumer sees the new value of one field beside the old value of the other.

Bundle the setting instead, hand the consumer a supplier, and compare the reference:

/** One setting, as a single immutable value. */
data class EqualizerCurve(val bandsDb: List<Float>, val preampDb: Float) {
    val isFlat: Boolean = preampDb == 0f && bandsDb.all { it == 0f }
    companion object { val FLAT = EqualizerCurve(emptyList(), 0f) }
}

class Consumer(private val setting: () -> EqualizerCurve) {
    private var applied: EqualizerCurve? = null
Installs
2
GitHub Stars
35
First Seen
6 days ago
identity-compare-immutable-setting — maxrave-dev/kotlin-footguns