<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 4, 2016, at 4:57 PM, Jonathan Hull via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">If this enum is going to be used by tens/hundreds of thousands of structs, am I actually saving any space by breaking out the rarer cases which store more data or is the footprint just equal to the largest case?</span><br style="font-family: Alegreya-Regular; font-size: 15px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote></div><br class=""><div class="">Enums are implemented a lot like C unions, so the size is equal to that of the largest case, plus a byte for the tag.</div><div class=""><br class=""></div><div class="">It’s interesting that the size in Daniel’s example is 41 bytes. That implies that the tag is placed at the <i class="">end</i> of the data, because putting a 1-byte field at the beginning would require 7 extra bytes of padding before the first Int. But the stride still needs to be 48 bytes to preserve the 8-byte-alignment of subsequent values.</div><div class=""><br class=""></div><div class="">This implies that you might be able to make an enum use up to 7 fewer bytes in practice, if you can make its largest case be one byte smaller than a multiple of a power of 2. Obviously not always attainable, but if you’re really groveling for space improvements you might be able to squeeze out that extra byte.</div><div class=""><br class=""></div><div class="">—Jens</div></body></html>