In my composable I have this ```val sizeOfSomeSort...
# compose
c
In my composable I have this
Copy code
val sizeOfSomeSortaNedtedThing =
    myScreenState.listOf20Items.flatMap { it.subListOf10Items }.flatMap { it.anotherSubListOf10Items }
        .filter { it.isDone }.size
Should I try to have this remembered? Should I just move this into myScreenState as a top level field thats calculated before-hand?
f
Profile to determine if it's an issue then take any necessary action
s
At the same time, wrapping this into a remember is so simple that you might as well do it.
Copy code
val sizeOfSomeSortaNedtedThing = remember(myScreenState.listOf20Items) {
    myScreenState.listOf20Items.flatMap { it.subListOf10Items }.flatMap { it.anotherSubListOf10Items }
        .filter { it.isDone }.size
}
f
At the same time, a
remember
has a cost associated to it, so you should not be remembering stuff willy-nilly
t
What sort of cost? Memory, cache key recomputation?
f
t
So it looks like you can replace a calculation with a cache key computation and equality check, at the expense of storing a potentially redundant/unused copy of the data (depending on your use case).
(and the cache is of size 1)
c
@Stylianos Gakis any reason why you chose to
remember(myScreenState.listOf20Items)
? and not just have an empty remember
s
Yes, if
listOf20Items
changes your
remember
will not re-run and it will simply keep the result of whatever the first result it ever got was.
So, maybe you asking this question maybe hints towards what the rest of the folks are saying. Maybe it’s a better idea not to do anything special with it unless it’s a real performance issue. Since if you do try to do something with it (like remembering it) you risk of the having correctness bugs if you do it wrong as opposed to have some potentially non-impactful performance issues. And having correctness problems is much much worse in this case then
c
Gotcha. Cool. I've never used a remember with an arg hence why I asked. 😄
s
That sounds a tiny bit concerning 😄 Either you’re not using remember a whole lot, or there’s a good chance some of your remembers are resulting in you showing some stale data 👀
c
Yeah, I mean i only use remember when i have stateful composables which is infrequent
but i just go by the examples that compose has
but yeah. 4 apps shipped to prod. no perf or staleness issues here. so im doing something right. hahahah
over 1 million happy-ily installed users 👌
t
So far I've only encountered one example in the docs that shows
remember
being used with keys. :-/