swift枚举(二)

1,001 阅读3分钟

swift枚举(一)

No-payload enums 布局比较简单,也好理解,接下来看看 Single-payload enums

Single-payload enums

	enum IFLEnum {
	    case test_one(Bool)
	    case test_two
	    case test_three
	    case test_four
	}
	print(MemoryLayout<IFLEnum>.size)
	print(MemoryLayout<IFLEnum>.stride)

打印

1

1

	enum IFLEnum {
	    case test_one(Int)
	    case test_two
	    case test_three
	    case test_four
	}
	print(MemoryLayout<IFLEnum>.size)
	print(MemoryLayout<IFLEnum>.stride)

打印

9

16

为什么都是单个负载,Swift结构占用的内存大小却不一致

Swift中 Single-payload enums 会使用负载类型中的额外空间来记录没有负载的case值

首先 Bool 类型是1字节,也就是UInt8, 所以当前能表达256种case

对于Bool来说,只需要抵用低位的0, 1这两种情况,其余的空间就可以用来表示没有负载的值

而对于Int来说,其实系统没办法推算当前的负载所要使用的位数,也就意味着当前Int的负载是没有额外空间的,就需要额外开辟空间存储剩余的没有负载的case,也就是 8+1 = 9字节,对齐方式就是 Int的整数倍 16字节

image.png

Single-payload - Bool, 5个变量 是按照 1字节去对齐的, 大小也均为1字节

image.png

Single-payload - Bool, 5个变量 是按照 16字节去对齐的, 大小也均为16字节的前9个字节

小端模式读取

0x0a =========> 10

0x14 =========> 20

第三行 0x01 00 00 00 00 00 00 00 00

最后两行 高位也都是 0x01

高位1字节01 标识 未负载case, 剩余3个变量 低位 分别是 0 1 2

多个负载 Mutil-payload enums

	enum IFLEnum {
	    case test_one(Bool)
	    case test_two
	    case test_three(Bool)
	    case test_four
	}
	print(MemoryLayout<IFLEnum>.size)
	print(MemoryLayout<IFLEnum>.stride)

打印

1

1

image.png

6个枚举变量在内存中的存储 : 01 00 80 41 40 81 大小1字节,1字节对齐

image.png

用更多成员枚举 对照测试打印

发现多负载 枚举 的规律

有负载case,始终用低位 0标识false 1标识 true, 其余位中选择一位标识哪个case

其余的case 按照成员case顺序, 低位 顺序0,接着1,高位偶数阶递增

这个规律需要进一步 swift源码探究验证

多负载枚举大小

	enum IFLEnum {
	    case test_one(Bool)
	    case test_two
	    case test_three(Int)
	    case test_four
	    case test_five
	    case test_six
	    case test_seven
	    case test_eight
	    case test_nine
	}
	print(MemoryLayout<IFLEnum>.size)
	print(MemoryLayout<IFLEnum>.stride)

打印

9

16

Int 占8字节,Bool 1字节低位 0或1标识,占用1位, UInt8 能表示256种case,其余空间可分配给剩下的case使用,总共需要9个字节

对齐按照最大占用字节的倍数 8 * 2 = 16字节 对齐

这就解答了 上一篇 swift枚举(一) 中的一个遗留问题

	enum IFLEnum {
	    case circle(Double)
	    case rectangle(Int, Int)
	    case triangle(Int, Int, Int)
	    case none
	}
	print(MemoryLayout<IFLEnum>.size)
	print(MemoryLayout<IFLEnum>.stride)

结果

25 ---- triangle负载 3个Int 也就是3个8字节,rectangle负载 两个Int,复用3个Int负载 ,circle 也是8字节,3个Int负载空间完全也可以复用 其余1字节 能表示256种case

32 ---- 最大占用字节数 为8字节, 负载最大是3个8字节,再加上 剩余1字节 补齐8字节对齐

特殊情况

	enum IFLEnum {
	    case one
	}
	print(MemoryLayout<IFLEnum>.size)
	print(MemoryLayout<IFLEnum>.stride)

结果

0

1

由于不需要用任何东西来区分 唯一case one,所以打印 IFLEnum结构大小时你会发现是0

# swift枚举(三)-Optional