# Value types vs allocated types and SoA in Haxe

**URL:** https://community.haxe.org/t/value-types-vs-allocated-types-and-soa-in-haxe/3361
**Category:** Haxe
**Created:** [November 28, 2021, 5:21pm UTC](https://community.haxe.org/t/value-types-vs-allocated-types-and-soa-in-haxe/3361 "2021-11-28T17:21:31Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![javidcf](https://community.haxe.org/user_avatar/community.haxe.org/javidcf/32/1776_2.png) [@javidcf](https://community.haxe.org/u/javidcf)
#### Post date: [November 28, 2021, 5:21pm UTC](https://community.haxe.org/t/value-types-vs-allocated-types-and-soa-in-haxe/3361/1 "2021-11-28T17:21:31Z")

</div>

I am sure this must be properly addressed somewhere, but I just cannot find where.

I am starting to experiment with Haxe and one of the things about which I have not been able to find clarity is whether it is possible to create “value” types. By this I mean a type that is created on the stack and that is generally passed around by copy like an integer or a float, as opposed to being allocated on the heap and passed around by reference. Importantly, a collection of a value type (e.g. an array) would store the values themselves, not references to the objects. A typical example would be things like Vector2 or Vector3 in video games. I thought that [anonymous structures](https://haxe.org/manual/types-anonymous-structure.html) might be something like that, but according to the manual their performance is actually worse than for classes, so I suppose they are implemented as a sort of dictionary on static targets.

My impression is that this is not possible in Haxe, but I have not found explicit documentation about it. I know there are inline constructors that do something like that, but I’m not sure it is quite that (e.g. what happens if I have an array of a type with an inline constructor?).

I suspect this is partially, or totally, due to the multi-target nature of the language, so things might in principle work differently on different targets. But it seems a relatively important aspect. For example, there is this idea of [Structure of Arrays (SoA)](https://en.wikipedia.org/wiki/AoS_and_SoA) that some games or engines implement to gain some performance from better cache locality (it is frequently associated with ECS designs, although one doesn’t necessarily imply the other). But this only makes sense if you can store value types in your arrays, not references. So, is it possible at all to have a SoA design in Haxe (for anything else than boolean, integer or float values)?

---

<div class="post-metadata">

### Author: ![Apprentice-Alchemist](https://community.haxe.org/user_avatar/community.haxe.org/apprentice-alchemist/32/1324_2.png) [@Apprentice-Alchemist](https://community.haxe.org/u/Apprentice-Alchemist)
#### Post date: [November 28, 2021, 5:35pm UTC](https://community.haxe.org/t/value-types-vs-allocated-types-and-soa-in-haxe/3361/2 "2021-11-28T17:35:26Z")

</div>

Value types are not supported in haxe at the moment (many of Haxe’s targets do not support them: JS, Java, Python, Lua, etc.)  
When targetting C# there is the `@:struct` meta to generate C# structs.  
Value types could also be implemented for hashlink or hxcpp, but are not at the moment.  
(They might already be possible on hxcpp, but I’m not sure.)

---

<div class="post-metadata">

### Author: ![RealyUniqueName](https://community.haxe.org/user_avatar/community.haxe.org/realyuniquename/32/10_2.png) [@RealyUniqueName](https://community.haxe.org/u/RealyUniqueName)
#### Post date: [November 28, 2021, 6:47pm UTC](https://community.haxe.org/t/value-types-vs-allocated-types-and-soa-in-haxe/3361/3 "2021-11-28T18:47:31Z")

</div>

`@:struct` also works for hashlink.

---

<div class="post-metadata">

### Author: ![haxiomic](https://community.haxe.org/user_avatar/community.haxe.org/haxiomic/32/75_2.png) [@haxiomic](https://community.haxe.org/u/haxiomic)
#### Post date: [November 28, 2021, 8:31pm UTC](https://community.haxe.org/t/value-types-vs-allocated-types-and-soa-in-haxe/3361/4 "2021-11-28T20:31:17Z")

</div>

Something I wonder if we could explore in static targets would be marking variables that escape to an outer scope, any variable that doesn’t can be stack allocated and doesn’t need to touch the GC (giving you the perf benefits of value types, hopefully faster since no copying is required)

This wouldn’t help with SoAs, for those you could use an abstract to make a struct over some arbitrary byte buffer, that way you know exactly where your memory is and how it’s arrange

@RealyUniqueName when the analyser runs does it already determine function purity somewhere?

---

<div class="post-metadata">

### Author: ![Apprentice-Alchemist](https://community.haxe.org/user_avatar/community.haxe.org/apprentice-alchemist/32/1324_2.png) [@Apprentice-Alchemist](https://community.haxe.org/u/Apprentice-Alchemist)
#### Post date: [November 30, 2021, 12:44pm UTC](https://community.haxe.org/t/value-types-vs-allocated-types-and-soa-in-haxe/3361/5 "2021-11-30T12:44:26Z")

</div>

`@:struct` on hashlink will not generate a pass-by-value struct.  
Simply remove the extra `hl_type *t` field from classes.

---

<div class="post-metadata">

### Author: ![haxiomic](https://community.haxe.org/user_avatar/community.haxe.org/haxiomic/32/75_2.png) [@haxiomic](https://community.haxe.org/u/haxiomic)
#### Post date: [December 1, 2021, 1:02am UTC](https://community.haxe.org/t/value-types-vs-allocated-types-and-soa-in-haxe/3361/6 "2021-12-01T01:02:00Z")

</div>

@javidcf to expand on implementing SoA and AoS with abstracts over plain byte buffers I’ve created a macro that does this for you, given a structure

Demo: [Try Haxe !](https://try.haxe.org/?module-b=ObjectPool#2C917d0d)

For example, to create a SoA object pool you can do:

```haxe
typedef Player = ObjectPool.SoA<'Player', {
	x: Float,
	y: Float,
	age: Int,
	eyeColor: EyeColor,
	alive: Bool,
	velocity: {
		x: Float,
		y: Float,
		q: {a: Int, b: Int},
	},
}>;

```

You can create instances with `new Player()`. This returns simply and `Int`, corresponding to the index in the SoA arrays

In AoS mode, nested structures are flattened so you have just one big chunk of bytes corresponding to each instance

Implementation is here:

> <https://gist.github.com/haxiomic/568e381f65716ddf977c2e1a46234e05>

---

<div class="post-metadata">

### Author: ![cambiata](https://community.haxe.org/user_avatar/community.haxe.org/cambiata/32/61_2.png) [@cambiata](https://community.haxe.org/u/cambiata)
#### Post date: [December 2, 2021, 7:14am UTC](https://community.haxe.org/t/value-types-vs-allocated-types-and-soa-in-haxe/3361/7 "2021-12-02T07:14:05Z")

</div>

Really cool! Thanks, George!

---

<div class="post-metadata">

### Author: ![alex\_dja](https://community.haxe.org/user_avatar/community.haxe.org/alex_dja/32/294_2.png) [@alex\_dja](https://community.haxe.org/u/alex_dja)
#### Post date: [December 3, 2021, 4:27pm UTC](https://community.haxe.org/t/value-types-vs-allocated-types-and-soa-in-haxe/3361/8 "2021-12-03T16:27:20Z")

</div>

Yeah, George, really impressive example👍! It would be nice to have it in the haxe cookbook:)
