Could somebody please shed light on the point of t...
# compose
m
Could somebody please shed light on the point of the
StateObjectImpl.recordReadIn
method?
Copy code
internal fun recordReadIn(reader: ReaderKind) {
    do {
        val old = ReaderKind(readerKind.get())
        if (old.isReadIn(reader)) return

        val new = old.withReadIn(reader)
    } while (!readerKind.compareAndSet(old.mask, new.mask))
}
I can tell that it makes it so that
StateObjectImpl.isReadIn(reader: ReaderKind): Boolean
returns
true
if called with the same
ReaderKind
argument, but what purpose does that serve?
Copy code
if (value is StateObjectImpl) {
    value.recordReadIn(ReaderKind.Composition)
}
I’ve located this
if
statement in
(CompositionImpl as ControlledComposition).recordReadOf
, but again, what’s the point of it? Compose seems to work fine with my own implementations of
StateObject
, and when they’re read inside a composition, they’re also passed to the aforementioned
recordReadOf
method. Since they only extend
StateObject
, the conditional is skipped, but everything still works the same way it does with subtypes of
StateObjectImpl
.
c
ReaderKind
is an internal optimization that allows the
Recompose
and the
SnapshotStateObserver
to quickly ignore objects that have where not read in composition or in a snapshot state observer. This, for example, allows the
Recomposer
's apply observer to avoid waking the recomposer's coroutine when it received a change that was only read by layout or draw, for example (and vis versa). Waking the recompose unnecessarily was causing frames to jank in some cases.
m
So just an optimisation, not any kind of central business logic. I see, thanks!