https://kotlinlang.org logo
#compose
Title
# compose
v

vide

10/11/2023, 3:23 PM
How heavy is reading from a static composition local? Is it a blazing fast constant time operation, or does it do some tree searching to find the nearest provider? 🤔
👀 1
s

shikasd

10/11/2023, 4:02 PM
Depends if it is cached (usually it is), but I'd expect something like a map lookup
gratitude thank you 1
r

romainguy

10/11/2023, 4:11 PM
No, it’s way more expensive than a map lookup
On the same device, a Map read is ~60ns, a CompositionLocal read is ~4,000ns
👆 1
Derived state is the only thing that’s more expensive
s

shikasd

10/11/2023, 4:13 PM
Static composition local is not a state, so it is cheaper
Assuming it is inside composition and cached, it should be quite cheap, unless I am missing something
r

romainguy

10/11/2023, 4:15 PM
Ah right sorry I missed the “static” bit
s

shikasd

10/11/2023, 4:16 PM
tbh, Leland's benchmark indicates that static composition local is still expensive, but I didn't look on what exactly it measures
v

vide

10/11/2023, 4:26 PM
The context here is that I need to determine if I'm inside a preview or not many times in every single composable, I'll just cache the value in a global variable instead to avoid uncertainty in performance cost 😅
s

Stylianos Gakis

10/11/2023, 5:59 PM
Are you checking if you're in a preview in your production composable? Why?
v

vide

10/11/2023, 6:00 PM
I was prototyping the viability of a thing. It's a long story! However, I abandoned the idea already 😂