<story><title>Why Rust helps even if you have to use a lot of `unsafe`</title><url>https://v5.chriskrycho.com/journal/unsafe/</url></story><parent_chain><item><author>flohofwoe</author><text>IMHO the one downside with sum types in a performance-oriented systems programming language (or as I prefer to call them &amp;#x27;tagged unions&amp;#x27; because that makes it clearer how they actually look like in memory) is that they are inherently cache-unfriendly, because they always take up as much space as the largest wrapped type (plus 8 bytes for the tag). When you have an array of those you need to be aware of the potential &amp;#x27;waste&amp;#x27; between array items.&lt;p&gt;(now of course an automatic transformation into a structure-of-arrays would be pretty cool, I actually wonder what Zig&amp;#x27;s MultiArrayList does with tagged unions).&lt;p&gt;PS: in general I agree though, for instance in Typescript they are fantastic, but there I usually don&amp;#x27;t care much about memory layout and performance.</text></item><item><author>tines</author><text>This is a huge deal, I find it so difficult to choose a language that doesn&amp;#x27;t have sum types and destructuring via pattern matching for the kinds of projects I like to work on (compiler&amp;#x2F;interpreter implementation).</text></item><item><author>fnfjfk</author><text>This article could be 1 sentence: “it has sum types” :)&lt;p&gt;I find programming in a language without sum types is like rock climbing in ankle weights. I just can’t model many types of data correctly, and I hate using product types for something that is clearly a sum type.&lt;p&gt;Even if Rust didn’t make any safety guarantees over other languages (although sum types &lt;i&gt;provide&lt;/i&gt; safety guarantees through exhaustiveness!) I would still think Rust helps a lot because of that.&lt;p&gt;I guess Swift and Kotlin are basically “it has sum types” languages too, although they both come with baggage (Apple-only in practice, JVM).</text></item></parent_chain><comment><author>CodesInChaos</author><text>The overhead isn&amp;#x27;t always 8 bytes. When the type uses the naive representation, the overhead will generally match the alignment. For example an Option&amp;lt;u8&amp;gt; occupies 2 bytes, 1 of which is for the tag.&lt;p&gt;And if the type has some illegal values, these can sometimes be used to discriminate the type. When that&amp;#x27;s possible, the union will have the same size as the largest underlying type with no overhead for a tag. For example Option&amp;lt;&amp;amp;T&amp;gt; matches the size of &amp;amp;T, since it uses the illegal null value to represent None.</text></comment>
<story><title>Why Rust helps even if you have to use a lot of `unsafe`</title><url>https://v5.chriskrycho.com/journal/unsafe/</url></story><parent_chain><item><author>flohofwoe</author><text>IMHO the one downside with sum types in a performance-oriented systems programming language (or as I prefer to call them &amp;#x27;tagged unions&amp;#x27; because that makes it clearer how they actually look like in memory) is that they are inherently cache-unfriendly, because they always take up as much space as the largest wrapped type (plus 8 bytes for the tag). When you have an array of those you need to be aware of the potential &amp;#x27;waste&amp;#x27; between array items.&lt;p&gt;(now of course an automatic transformation into a structure-of-arrays would be pretty cool, I actually wonder what Zig&amp;#x27;s MultiArrayList does with tagged unions).&lt;p&gt;PS: in general I agree though, for instance in Typescript they are fantastic, but there I usually don&amp;#x27;t care much about memory layout and performance.</text></item><item><author>tines</author><text>This is a huge deal, I find it so difficult to choose a language that doesn&amp;#x27;t have sum types and destructuring via pattern matching for the kinds of projects I like to work on (compiler&amp;#x2F;interpreter implementation).</text></item><item><author>fnfjfk</author><text>This article could be 1 sentence: “it has sum types” :)&lt;p&gt;I find programming in a language without sum types is like rock climbing in ankle weights. I just can’t model many types of data correctly, and I hate using product types for something that is clearly a sum type.&lt;p&gt;Even if Rust didn’t make any safety guarantees over other languages (although sum types &lt;i&gt;provide&lt;/i&gt; safety guarantees through exhaustiveness!) I would still think Rust helps a lot because of that.&lt;p&gt;I guess Swift and Kotlin are basically “it has sum types” languages too, although they both come with baggage (Apple-only in practice, JVM).</text></item></parent_chain><comment><author>jackmott42</author><text>Rust sum types do not take up that much extra space, in fact often the overhead is completely compiled away.  Lots of hard work to keep the wasted memory to a minimum, and they are not pointers, so there isn&amp;#x27;t an extra memory hop or anything.</text></comment>