Data classes optimization

It is possible to treat data classes with only immutable members as structs.
Doing this would increase performance a lot since you don’t need to heap allocate every simple read-only data class.
Also LLVM could optimize way more things so this should seriously be considered.

2 Likes

data class is an identity based class, you can write data1 === data2, so it is not possible to pass them by value, but only by reference.
Maybe you are looking for value class.

2 Likes

For JVM, this could be implemented only once values classes of the Valhalla project are enabled in JDKs. As far as I know, this is still in progress (Project Valhalla).

Can’t say for other platforms.

also not only immutable things can be treated like structs… the question is the fixed sized fields and how you allocate deallocate memory to them. Nothing to do with immutability

yes but it is easier to treat them as structs

But they only work for one value, no?

Yes, but this restriction can be relaxed in the future.
Valhalla is in the roadmap.