[swift-users] Memory used by enums

Daniel Eggert danieleggert at me.com
Tue Apr 5 11:10:20 CDT 2016


On 05 Apr 2016, at 01:57, Jonathan Hull via swift-users <swift-users at swift.org> wrote:
> 
> I had a quick question on the memory used by enums with associated values in the current implementation.  If I have an enum like the following:
> 
> enum MyEnum {
> 	case mostCommonlyUsed
> 	case somewhatCommon (Int,Int)
> 	case prettyRare (Int,Int,Int,Int,Int)
> }
> 
> 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?

  1> enum MyEnum { 
  2.     case mostCommonlyUsed 
  3.     case somewhatCommon (Int,Int) 
  4.     case prettyRare (Int,Int,Int,Int,Int) 
  5. }
  6> sizeof(MyEnum)
$R0: Int = 41
  7> strideof(MyEnum)
$R1: Int = 48

It depends on what you're doing. The pure "size" is 41 bytes, but the alignment of these would be 48 bytes.

Those numbers are very platform dependent -- these are on 64 bit OS X.

/Daniel



More information about the swift-users mailing list