ios

谈谈Swift中的枚举内存布局

2019-12-14  本文已影响0人  lkkwxy

在掘金上看到从 汇编 到 Swift 枚举内存 的惊鸿一瞥之后,作者分析了几种不同枚举的内存布局,但是我感觉覆盖的不够全面,算是对作者那篇文章的一个补充。建议先看下作者的文章,作者的结论如下:

关联值枚举:
最大字节数之和 额外 + 1
最后一个字节 存放 case 类型
非关联值枚举:
内存 占用 1个字节
内存中 以下标数 为值,依次累加

疑问

不知道你看完之后,有没有我同样的疑问?

  1. 普通枚举时,内存占用一个字节,而一个字节最多只能从0到255,那么当case的选项超出256个时,会怎样
  2. 若关联值得类型是协议,结构体,类或其他枚举呢?这个时候内存占用是怎么样的
  3. 如果是递归枚举呢?

答案

func test(){
     enum TestEnum {
       case testCase1
       case testCase2
    }
   var testEnum = TestEnum.testCase1
   show(val: &testEnum)
   testEnum = .testCase2
   show(val: &testEnum)
}
image
//测试case过多时
func test1(){
    var testEnum = MoreCaseEnum.case257
    show(val: &testEnum)
}
image
struct TestStruct: TestProtocol {
    var testPropetry1 = 10
    var testPropetry2 = 11
    var testPropetry3 = 12
    var testPropetry4 = 13
    var testPropetry5 = 14
}
func test2() {
    enum TestStructEnum {
        case testCase1
        case testCase2(TestStruct)
        case testCase3
    }
    var testEnum = TestStructEnum.testCase1
    show(val: &testEnum)
    testEnum = .testCase2(TestStruct())
    show(val: &testEnum)
    testEnum = .testCase3
    show(val: &testEnum)
}
image
//测试关联值的类型是class
func test3() {
    enum TestClassEnum {
        case testCase1
        case testCase2(TestClass)
        case testCase3
    }
    var testEnum = TestClassEnum.testCase1
    show(val: &testEnum)
    testEnum = .testCase2(TestClass())
    show(val: &testEnum)
    testEnum = .testCase3
    show(val: &testEnum)
}
image
func test4() {
    enum TestClassOtherEnum {
        case testCase1
        case testCase2(TestClass)
        case testCase3(Bool)
    }
    var testEnum = TestClassOtherEnum.testCase1
    show(val: &testEnum)
    testEnum = .testCase2(TestClass())
    show(val: &testEnum)
    testEnum = .testCase3(true)
    show(val: &testEnum)
}
image
func test5() {
    enum TestEnum {
        case testCase1
        case testCase2
    }
    enum TestSamllEnum {
        case testCase1
        case testCase2(TestEnum)
        case testCase3(Bool)
    }
    var testEnum = TestSamllEnum.testCase1
    show(val: &testEnum)
    testEnum = .testCase2(.testCase2)
    show(val: &testEnum)
    testEnum = .testCase3(true)
    show(val: &testEnum)
}
image
func test6() {
    enum TestProtocolEnum {
        case testCase1
        case testCase2(TestProtocol)
        case testCase3
    }
    var testEnum = TestProtocolEnum.testCase1
    show(val: &testEnum)
    testEnum = .testCase2(TestClass())
    show(val: &testEnum)
    testEnum = .testCase2(TestStruct())
    show(val: &testEnum)
    testEnum = .testCase3
    show(val: &testEnum)
}
image
func test7() {
    indirect enum TestIndirectEnum {
        case testCase1
        case testCase2(TestIndirectEnum)
        case testCase3
    }
    var testEnum = TestIndirectEnum.testCase1
    show(val: &testEnum)
    testEnum = .testCase2(.testCase3)
    show(val: &testEnum)
    testEnum = .testCase3
    show(val: &testEnum)
}
image

Other

以上所有的结论都是测试并总结出来,不能保证绝对的正确性,仅供参考,测试demo

参考链接

上一篇 下一篇

猜你喜欢

热点阅读