swift - 为什么使用结构会增加应用程序的二进制大小?

标签 swift struct compilation filesize

GitHub 上有一个相当受欢迎的 repo,叫做 Bohdan Orlov 的 iOS 技能矩阵。
在那里,在“内存”行的“中间”列中,它指出

Structs increase binary size

我第一次看到这个说法是在半年多以前,从那以后我就一直在想这是否属实,如果是,为什么会这样?它与结构在函数调用中被复制这一事实有什么关系吗?

BohdanOrlov/ios-skills-matrix

最佳答案

来自 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 也会发生同样的事情。

如果是这样,原因是在某些情况下您的结构对象没有在运行时初始化,而是存储在二进制文件中。

希望对您有所帮助。

关于swift - 为什么使用结构会增加应用程序的二进制大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52372423/

相关文章:

c++ - C++ 中的正则表达式编译错误

ios - 在 7.1 设备 swift xcode6 beta 3 上调试时出错

iOS : Connected to wifi but no internet access

ios - 医疗领域应用的自定义意图

c - 读取结构内部的字符时有什么问题吗?

c - 将结构指针数组传递给函数

c - 解释编译代码结构和静态分配

ios - Swift 后台执行

c - 嵌套结构初始化是否取决于范围?

android - 有没有办法在 Eclipse 中编译 GnuTLS?