When using collection API in kotlin (map, filter, ...
# android
y
When using collection API in kotlin (map, filter, ...) I think using method reference is better for performance because using lambdas creates anonymous class objects. On a related note, I asked chatgpt, and he said that with the optimization of lambda object creation in kotlin 1.8, there is not much difference in performance, does anyone know if this is true? (I read the 1.8 release note but couldn't find anything related. Is ChatGPT lying to me?)
e
the collections operators are
inline
which means the lambdas are inlined and not created as anonymous class objects
that's been the case since before 1.0
ChatGPT making up shit again
😅 2
there was a change - experimental in 1.5, production in 2.0 - to use invokedynamic lambdas in cases where the object does need to be created, https://youtrack.jetbrains.com/issue/KTLC-10
invokedynamic lambdas in smaller/fewer classes but longer class initialization, and not much difference at runtime after (but again, that only impacts the cases when they aren't inlined, so irrelevant to collections)
also lambdametafactory gets desugared into classes by d8 for most android apps anyways so there's really no difference as far as android is concerned
y
ohhhhh... thank you for very details response!
s
lambdametafactory gets desugared
does this still happen if minsdk is 26+ (which seems to be the lowest version that supports
invokedynamic
)?
e
invokedynamic bytecode is added since API 26. LambdaMetaFactory is not, so you can't use it at runtime, but ART should recognize the bootstrap call and replace it, so the typical lambda usage should work. I haven't checked what d8 chooses to do.