When using `LazyVerticalGrid` etc, suppose we want...
# compose
m
When using
LazyVerticalGrid
etc, suppose we want the modifier for all items in that grid to use
Modifier.size(123.dp, 456.dp)
and build on top of that. Is it a valid optimisation to reuse the same
Modifier.size(123.dp, 456.dp)
instance, or is there some gotcha regarding this?
Copy code
val cellSizeModifier = remember {
    Modifier.size(cellWidthDp, cellHeightDp)
}
LazyVerticalGrid(...
e
Yes it's valid to reuse the same object. Modifiers themselves are stateless and are not the "actual implementations", Modifier.Node is. So even with the same modifier instance, it will create different nodes when applied to different layout nodes. (At least that's how I think it works, need citation from a compose runtime engr đŸ˜¬)
Oh wait i just noticed you are remembering the instance across recompositions, not just applying to multiple nodes. This case I'm not so sure of. (Also it might fall into premature optimisation land, best to profile and see if recreating the modifier on recomposition is such a big performance issue)
m
Thanks! Yes, though being stateless I think is not enough by itself because there could be references to those items stored somewhere, such that those references are not expected to reused (perhaps like with `Span`s in Android’s
Spannable
world).