You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The JVM doesn't have higher-order functions, so Kotlin must generate a class (a "SAM type") with the lambda's code in a single method. This doesn't matter too much if Kotlin can generate a singleton object, but in this case it can't, as CsvReaderContext() is captured in the closure. So, every time this function is invoked the lambda's class must be instantiated with CsvReaderContext() in a field, invoked, then garbage collected right after. (Correct me if I'm wrong)
*Small correction: this is a bad example. Looking at the bytecode, the reader context is passed as a method parameter.
I'm not sure how this works on other platforms, but on the JVM this impacts performance.
To avoid this, Kotlin provides inline functions, which inline the function's bytecode into where it's used. This mitigates the performance issues above at the expense of bytecode size being larger. If internal types or functions are used, you can add the @PublishedApi annotation to allow them to be accessed by the function, which makes whatever it's applied to public in the bytecode but obeyed by Kotlin.
Thanks for the reply. I forked the repository, but couldn't get the open functions to work with the tests when inlined ("Wrong bytecode generated" error). Admittedly, my experience with tests is limited though.
@NightEule5
I tried too and got an error.
Though, Inline function makes code performance better, but funcitons like csvReader , open are not often called. So the performance affect is very small.
I noticed that functions using lambdas aren't utilizing Kotlin's
inline
keyword. This could have avoidable performance impacts.Take, for example, this function:
The JVM doesn't have higher-order functions, so Kotlin must generate a class (a "SAM type") with the lambda's code in a single method. This doesn't matter too much if Kotlin can generate a singleton
object
, but in this case it can't, asCsvReaderContext()
is captured in the closure. So, every time this function is invoked the lambda's class must be instantiated withCsvReaderContext()
in a field, invoked, then garbage collected right after. (Correct me if I'm wrong)*Small correction: this is a bad example. Looking at the bytecode, the reader context is passed as a method parameter.
I'm not sure how this works on other platforms, but on the JVM this impacts performance.
To avoid this, Kotlin provides
inline
functions, which inline the function's bytecode into where it's used. This mitigates the performance issues above at the expense of bytecode size being larger. Ifinternal
types or functions are used, you can add the@PublishedApi
annotation to allow them to be accessed by the function, which makes whatever it's applied to public in the bytecode but obeyed by Kotlin.A more impactful example would be the
open
functions inCsvReader.kt
andCsvWriter.kt
Now, whether this is that big of a deal in this case is debatable.
The text was updated successfully, but these errors were encountered: