# Data classes optimization

**URL:** <https://discuss.kotlinlang.org/t/data-classes-optimization/28421>\
**Category:** Native\
**Created:** [April 7, 2024, 2:05pm UTC](https://discuss.kotlinlang.org/t/data-classes-optimization/28421 "2024-04-07T14:05:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Nutz](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Alexander\_Nutz](https://discuss.kotlinlang.org/u/Alexander_Nutz)\
**Post date:** [April 7, 2024, 2:05pm UTC](https://discuss.kotlinlang.org/t/data-classes-optimization/28421/1 "2024-04-07T14:05:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [April 9, 2024, 3:53pm UTC](https://discuss.kotlinlang.org/t/data-classes-optimization/28421/2 "2024-04-09T15:53:30Z")

</div>

`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`.

---

<div class="post-metadata">

**Author:** ![psota.j](https://avatars.discourse-cdn.com/v4/letter/p/4da419/32.png) [@psota.j](https://discuss.kotlinlang.org/u/psota.j)\
**Post date:** [April 13, 2024, 7:22am UTC](https://discuss.kotlinlang.org/t/data-classes-optimization/28421/3 "2024-04-13T07:22:35Z")

</div>

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](https://openjdk.org/projects/valhalla/)).

Can’t say for other platforms.

---

<div class="post-metadata">

**Author:** ![vach](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vach/32/6909_2.png) [@vach](https://discuss.kotlinlang.org/u/vach)\
**Post date:** [April 14, 2024, 11:30am UTC](https://discuss.kotlinlang.org/t/data-classes-optimization/28421/4 "2024-04-14T11:30:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alexander\_Nutz](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Alexander\_Nutz](https://discuss.kotlinlang.org/u/Alexander_Nutz)\
**Post date:** [April 27, 2024, 8:03am UTC](https://discuss.kotlinlang.org/t/data-classes-optimization/28421/6 "2024-04-27T08:03:44Z")

</div>

yes but it is easier to treat them as structs

---

<div class="post-metadata">

**Author:** ![Alexander\_Nutz](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Alexander\_Nutz](https://discuss.kotlinlang.org/u/Alexander_Nutz)\
**Post date:** [April 27, 2024, 8:03am UTC](https://discuss.kotlinlang.org/t/data-classes-optimization/28421/7 "2024-04-27T08:03:57Z")

</div>

But they only work for one value, no?

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [April 27, 2024, 9:33pm UTC](https://discuss.kotlinlang.org/t/data-classes-optimization/28421/8 "2024-04-27T21:33:04Z")

</div>

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