Hey all, I have a bit of a technical optimization ...
# compiler
n
Hey all, I have a bit of a technical optimization question with regards to the Kotlin compiler. Are there any circumstances in which Kotlin will monomorphize generic classes? For instance, if I have a class FooX, my understanding is that usually on the JVM FooDouble will require boxing of the double argument for any method using / returning the generic type parameter. Are there any instances where Kotlin will generate a monomorphized version FooDouble behind the scenes to avoid boxing of the double result / argument? I've heard some people say that reified generics work by monomorphizing, but I don't know if that applies to classes, and if so if it's possible to guarantee this happens in certain instances. Background for this is I'm trying to eliminate some sources of boxing in some performance critical code of mine, where I use instantiations of my own generic classes.
y
Sadly it doesn't work. It only monomorphises for inline methods. The reason mainly is to maintain ABI compatibility: it can't trust that a class will stay the same between compilation and running, but that isn't a concern for inline method.
r
If they are single field classes, value classes should do this. Project Valhalla will allow for multi-field value classes in the jvm , eventually.
1
y
I think you might need to mark its methods as
inline
for this to work properly, but yes great point