Which high level / safe languages have unboxed composite types?
<p>Most languages have some kind of composite type, e.g. structs or classes. In most cases in higher level languages these are implemented as a pointer to a separately allocated block of memory that contains the composite's data. Say we have a type Foo:</p>

<pre><code>struct Foo {
    int bar;
    float baz;
}
</code></pre>

<p>In C, if we create an array of Foos the array itself will contain the bar and baz values contiguously in a single block of memory. If we define another struct that contains a Foo the bar and baz of it will also be directly embedded into the containing struct.</p>

<p>In many higher level languages such as Java or Python, if we were to create an array or list of Foos (which would have to be classes or something else), the underlying memory block would contain pointers to separately allocated instances of the Foos.</p>

<p>For some usage patterns involving large datasets, this can be a huge overhead compared to direct embedding. There is memory overhead because each separately allocated object usually has a header and there are the pointers themselves. Then there is a big speed cost due to extra cache line misses and the much worse locality of the data, which can be over a hundred times.</p>

<p>Most (or all) low level / system programming languages allow such embedding without boxing, but most of them are unsafe (i.e. require manual memory management and have undefined behavior). Most high level languages require boxing.</p>

<p>For large dataset processing tasks I would like to use a language that doesn't require boxing for composite data types, but without the programmer time overhead of low level systems languages. So far I'm only aware of C#/.NET and to some extent Go that can do that, but they have their own limitations. Do any other options exist?</p>

<p>The ones I am aware of:</p>

<p>Unboxed composites &amp; safe:</p>

<ul>
<li>C# has structs that behave as above and is safe. Any language on .NET can have unboxed composites if they choose to, I expect F# to also have unboxed composites but I don't know the details. This combines nicely with generics so you don't need to know the type of the thing you're embedding when defining the containing type.</li>
<li>Go probably has unboxed embedding of structs, but I'm not sure about the details. Interfaces are always boxed, so due to no generics it only works if you know the exact type of the struct you're embedding.</li>
<li>Rust is a bit of an edge case, it's a low level system language that has unboxed composites, but it is safe. Memory management is partially automatic, partially manual but you need to show to the compiler that what you are doing is correct so there can't be memory bugs.</li>
</ul>

<p>Unsafe:</p>

<ul>
<li>Low level languages such as C, C++, assembler, etc all have unboxed composites but require manual memory management and have undefined behavior.</li>
<li>D is also an edge case. The core language includes unsafe features, but there is a safe subset that you explicitly can opt in to, however afaiui you cannot write a complete program in safe D.</li>
</ul>

<p>Require boxing:</p>

<ul>
<li>Python, Ruby, PHP, most scripting languages in general.</li>
<li>Java and JVM languages. On the JVM the only composition feature are classes and arrays which are always boxed. Though there's a <a href="http://openjdk.java.net/projects/valhalla/" rel="nofollow">project</a> to change this.</li>
<li>OCaml only has unboxed ints and types that are represented as ints on the memory level. Anything else is boxed, though there are some exceptions for floats. (That surprised me a bit since OCaml is known as a very fast compiled language.)</li>
<li>Haskell, though there are unboxed types as a limited special case.</li>
</ul>

<p>(Many of these boxing languages have libraries or extensions that allow using unboxed data, but that is usually a special purpose feature that doesn't mix well with the rest of the language.)</p>

<p>Which other languages are available in the safe + unboxed composites category?</p>