为什么使用结构会增加应用程序的二进制大小?
Why does usage of structs increase application's binary size?
GitHub 上有一个相当受欢迎的回购,叫做 iOS 技能矩阵,作者是 Bohdan Orlov。
在那里,在 "Memory" 行,"Middle" 列中,它指出
Structs increase binary size
半年多前第一次看到这个说法,一直在想是不是真的,如果是,为什么会这样?它与结构在函数调用中被复制的事实有什么关系吗?
来自 here(不是关于 Swift,但可能是答案)
In C, static structs that are not zero-initialized or uninitialized (i.e. that are statically to something else than zero) increase the binary size (they go into the data segment, i.e. even if you only initialize one field of a struct, the binary contains a full image of the full struct)
来自 here(不仅关于结构,而且可能也很有趣)
Structs can increase your binary size. If you have structs into lists they are created on the stack and they can increase your binary size.
Optionals usage will increase your binary size as well. You will be using optionals, but the thing you don't know is that the compiler has to do a lot of things; It has to do checking, it has to do unwrapping. So even though it's just a one-liner for you with a question mark, you get a lot of size in your binary.
Generic specialization is another problem that we encountered. Whenever you use generics, if you want your generics to be fast, the compiler will specialize them and give you quite a bit of binary size increase, as well.
第一句话是关于 C 的,对我来说很有意义。我想 Swift.
也会发生同样的事情
如果是这样,原因是在某些情况下,您的结构对象未在运行时初始化,而是存储在二进制文件中。
希望对您有所帮助。
GitHub 上有一个相当受欢迎的回购,叫做 iOS 技能矩阵,作者是 Bohdan Orlov。
在那里,在 "Memory" 行,"Middle" 列中,它指出
Structs increase binary size
半年多前第一次看到这个说法,一直在想是不是真的,如果是,为什么会这样?它与结构在函数调用中被复制的事实有什么关系吗?
来自 here(不是关于 Swift,但可能是答案)
In C, static structs that are not zero-initialized or uninitialized (i.e. that are statically to something else than zero) increase the binary size (they go into the data segment, i.e. even if you only initialize one field of a struct, the binary contains a full image of the full struct)
来自 here(不仅关于结构,而且可能也很有趣)
Structs can increase your binary size. If you have structs into lists they are created on the stack and they can increase your binary size.
Optionals usage will increase your binary size as well. You will be using optionals, but the thing you don't know is that the compiler has to do a lot of things; It has to do checking, it has to do unwrapping. So even though it's just a one-liner for you with a question mark, you get a lot of size in your binary.
Generic specialization is another problem that we encountered. Whenever you use generics, if you want your generics to be fast, the compiler will specialize them and give you quite a bit of binary size increase, as well.
第一句话是关于 C 的,对我来说很有意义。我想 Swift.
也会发生同样的事情如果是这样,原因是在某些情况下,您的结构对象未在运行时初始化,而是存储在二进制文件中。
希望对您有所帮助。