*When is it proper to use `@NonRestartableComposab...
# compose
s
When is it proper to use
@NonRestartableComposable
?
I watched a talk on Compose 2 months back, where the speaker was talking about under the hood of Compose. The speaker was very nervous, but clearly had a plethora of knowledge. I think he worked at Google? Anyways, I remember him distinctly mentioning that we should be using
@NonRestartableComposable
in our Root screens, and it was a micro optimization we could take advantage of. I know it's probably not really necessary, but I wanted to watch again and refresh myself on it, but can't find the video.
z
I think the docs are pretty clear:
This annotation can be applied to Composable functions in order to prevent code from being generated which allow this function's execution to be skipped or restarted. This may be desirable for small functions which just directly call another composable function and have very little machinery in them directly, and are unlikely to be invalidated themselves.
I’m not sure what video you’re talking about unfortunately
s
Thanks for quoting the docs. I had read that. Maybe I'm misremembering, but I remember him specifically recommending this be applied to the root screen for almost all apps, which doesn't really make sense to me.
z
Doesn’t make sense to me either, especially as a general rule of thumb. That screen is not unlikely to have state reads in it.
s
Found the talk in my browser history. He talks about non restarable composables a few times. Here is where he first brings it up

https://youtu.be/h1xTtTl0k7Q?si=KR3nN5opXD7Oz14k&t=1475

Going to watch it again for him talking about it on Root Composables.
h
Is the annotation also needed if you use an inline function?
s
I'm not sure it is ever "needed". I think it's a micro optimization.
h
Yes, it’s an optimization but I don’t know if the optimization (the annotation) is needed if you already use an inline function.
z
Shouldn’t have any effect on inline functions - they’re never restartable on their own anyway
👍 1
s
I don't think making root non-restartable is going to be a significant improvement, it is more appropriate for functions that are frequently called with different parameters since you never skip that function. It'll prevent allocation of restart scopes, potentially making composition cheaper. With OptimizeNonSkippingGroups compiler flag, you won't even get a group.
👍 1
s
I think the docs are pretty clear:
i'd like to argue against this. i don't think the doc is clear enough, and i'm saying this someone who thinks he's quite proficient with compose. it took me a while to understand what the docs meant, especially these lines:
This may be desirable for small functions which just directly call another composable function and have very little machinery in them directly, and are unlikely to be invalidated themselves
it would have helped if the doc contained an example to clarify what the term "machinery" meant in this context
true story 1
491 Views