今日头条 iOS 安装包大小优化—— 新阶段、新实践

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"前言"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"今日头条 iOS 端从 2016 年起就关注到了安装包大小的问题,并启动了包大小优化。2017 年,我们将当时的经验发表为技术文章 "},{"type":"link","attrs":{"href":"http:\/\/mp.weixin.qq.com\/s?__biz=MzI1MzYzMjE0MQ==&mid=2247484366&idx=1&sn=5a2d0e981c733e9eaec274b835600e67&chksm=e9d0c82cdea7413ada372bb936541c2a49de664b38daa6137c179ab6177231fa8b00ddf9acbc&scene=21#wechat_redirect","title":"","type":null},"content":[{"type":"text","text":"《干货|今日头条iOS端安装包大小优化—思路与实践》"}]},{"type":"text","text":"[1]。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如今三年过去了。今日头条在继续探索包大小优化时实践了更多思路,包括构建配置、图片压缩、__TEXT 段迁移、二进制段压缩等。这些优化项在业务入侵较少的前提下给今日头条带来了显著的包大小收益。同时,整个业界在包大小优化上也产出了更多方案。因此我们更新文章,期待与大家共同交流包大小优化这件事。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/55\/556c02e01f7d3dfb857a453236bfdb0d.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"表格:今日头条落地的优化项和收益一览"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"一、安装包的构成"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当我们通过构建,获得了一个经过了 App Slicing 后的 ipa 文件后,将其用 zip 解压缩方式解压,进入 .app 文件后,我们就可以直观地看到安装包中的内容。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/7e\/7e4c84b09b7760e1a884d576bfc922ee.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一个安装包,往往包含资源与 iOS 上的可执行文件 Mach-O 文件两部分,资源又可以分为 Asset Catalog 的构建产物 Assets.car 文件和其他资源。其中 Assets.car 文件和 Mach-O 文件,是我们投入较多精力优化的部分。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.1、Assets.car 文件"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Assets.car 文件是工程中 Asset Catalog 的构建产物。Xcode 工具链中的 actool 负责构建 Assets.car。在构建 Assets.car 的过程中,actool 会按照一定策略选取编码算法,对其中的 png 图片重新编码。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/24\/2472e24da8164f8fbb1ff9a02791e47a.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"图:Asset Catalog"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.2、Mach-O 文件"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Mach-O 文件是 iOS 上的可执行文件,它是由代码源文件经过编译和静态链接获得。经过 App Slicing 之后的 Mach-O 文件往往仅包含单个架构。使用 MachOView 等工具,我们可以直观了解 Mach-O 中包含的内容。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/da\/da1318b8fb4378fb6caa4d1a60a8e94f.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同时,Link Map 文件能更进一步帮助我们分析 Mach-O 文件的构成。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/4c\/4c077d1ec82d86c83eae64fcc36ed603.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 Build Settings 中打开 "},{"type":"codeinline","content":[{"type":"text","text":"LD_GENERATE_MAP_FILE"}]},{"type":"text","text":" 开关,构建 App 的过程中就会生成一个名叫 Link Map 的 txt 文件,它能展示每个段、每个节、每个函数在 Mach-O 中的分布和大小。这些信息是包大小优化中经常使用的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/e7\/e75dd3d03700855b8f18d05121aec630.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"二、资源大小优化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"“压缩资源”往往是最容易被联想到的包大小优化方案,但实际操作起来,却也包含技巧。今日头条在资源优化上做了诸多尝试。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.1、使用合适的资源压缩配置"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"今日头条目前最低支持的 iOS 系统版本为 iOS 9。然而,大部分 Pod 库的 Podspec 文件中指定的"},{"type":"codeinline","content":[{"type":"text","text":"deployment_target"}]},{"type":"text","text":"(最低支持版本)由于未及时修改,依然还是 iOS 8,这就导致了这些 Pod 库中指定的 "},{"type":"codeinline","content":[{"type":"text","text":"resource_bundles"}]},{"type":"text","text":" 在构建出 Assets.car 时,是以 iOS 8 为最低支持版本的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们通过实验发现:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1、将 Pod 库和主工程的最低支持版本从 iOS 8.0 提升成 iOS 9.0"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2、开启 Pod 库和主工程 Xcode Build Settings 中的 "},{"type":"codeinline","content":[{"type":"text","text":"ASSETCATALOG_COMPILER_OPTIMIZATION space"}]},{"type":"text","text":" 选项"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/1e\/1e6e3f04fb195c3e4ee553a9c2015f02.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这两项设置可以改变 actool 构建 Assets.car 时选取的编码压缩算法,减小包大小。我们可以使用 "},{"type":"codeinline","content":[{"type":"text","text":"xcrun assetutil --info Assets.car"}]},{"type":"text","text":" 命令检查 Assets.car 中每张图片使用的编码压缩算法。在今日头条环境下,整理的结果如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/3c\/3c6225887f06c0a8c46124392794a110.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由于 Assets.car 中 png 图片的编码压缩算法得到了改变,这两项配置在今日头条落地时获得了 2.31MB 的包大小收益。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.2、使用 RGB with palette 压缩图片"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在今日头条投入包大小优化的早期,我们曾尝试对 Asset Catalog 中的 png 图片做无损压缩,但实践后发现,虽然放入 Asset Catalog 的图片大小有了明显减小,但是构建的产物的大小却几乎没有变化。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"经过探究,我们发现,Xcode 中,构建 Asset Catalog 的工具 actool 会首先对 Asset Catalog 中的 png 图片进行解码,得到 Bitmap 数据,然后再运用 actool 的编码压缩算法进行编码压缩处理。无损压缩通过变换图片的编码压缩算法减少大小,但是不会改变 Bitmap 数据。对于 actool 来说,它接收的输入没有改变,所以无损压缩无法优化 Assets.car 的大小。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那是否有其他的压缩方式能优化 Assets.car 的大小呢?我们猜测对图片做合适的有损压缩是一个思路。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"于是我们尝试了 RGB with palette 编码方式[2]。RGB with palette 编码的得到的字节流首先维护了一个颜色数组。颜色数组每个成员用 RGBA 四个分量维护一个颜色。图像中的每个像素点则存储颜色数组的下标代表该点的颜色。颜色数组维护的颜色种类和数量由图片决定,同时可以人为的限制颜色数组维护颜色的种类的上限,默认为最大值 256 种。这种编码方式正如它的名字:palette(调色板)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/14\/14dea1c78c1b7509b71cc6e549d630d5.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"App 中大部分图片虽然使用了很多种类的颜色,但这些颜色中大多数都非常接近,从视觉上很难分辨,比如大量扁平风格的 icon。这种类型的图片非常适合用 palette 编码且减少颜色数组大小的方式来进行有损压缩,既能减少颜色数量实现有损压缩,也能保证保留的颜色贴近原始图片,使得经过有损压缩后的也看起来质量无损。我们在今日头条上落地,获得了 3.15MB 包大小收益。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在具体执行中,我们使用了 ImageOptim 工具改变图片的编码方式为 RGB with palette :"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"imageoptim -Q --no-imageoptim --imagealpha --number-of-colors 16 --quality 40-80 .\/1.png\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其中 "},{"type":"codeinline","content":[{"type":"text","text":"--number-of-colors"}]},{"type":"text","text":" 控制颜色数组维护颜色的数量;"},{"type":"codeinline","content":[{"type":"text","text":"--quality"}]},{"type":"text","text":" 控制图片的质量变为原来的百分比。我们的经验表明,当 "},{"type":"codeinline","content":[{"type":"text","text":"--number-of-colors"}]},{"type":"text","text":" 从 16 开始向上调整,"},{"type":"codeinline","content":[{"type":"text","text":"--quality"}]},{"type":"text","text":" 维持 40-80,能够在显著减少包大小的同时维持肉眼看不到的质量变化。经过 UI 同学的像素眼审查,确认优化前后的图片看起来无差别。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/1f\/1f0db5a62314eb1b6a723d57b19a9358.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.3、Assets.car 合并"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"今日头条使用 CocoaPods 进行组件集成,各个组件携带的 Asset Catalog 文件以 Podspec 中 resource_bundles 的方式引入,最终会以 Bundle 下的 Assets.car 文件的形式体现在安装包内。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以 7.9.4 版本为例,安装包内有 106 个 Bundle 包含 Assets.car 文件:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/a3\/a307256c3da27136a8036ba7a98ec154.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Assets.car 文件本质上是 BOM 文件,同时,Xcode 在使用 actool 构建 Assets.car 文件时,也会自带一些优化操作,比如:将若干张小图片自动合并为一张 Packed Image。因此,将若干个 Assets.car 合并,可以减少重复的 BOM Block,也可以最大化享受到 actool 自带的优化效果。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在构建的过程中,今日头条通过在 Build Phases 中加入脚本,将多个库中 Asset Catalog 中的图片合并到一个 Asset Catalog 中,再经 actool 构建成 Assets.car 产物。这一优化产生了 2.1MB 的包大小收益。同时,从理论上分析,这一优化也可以减少运行时 Assets.car 的解析操作,对图片读取的响应耗时有正向收益。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2.4、文本文件压缩"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了占比最大的图片资源,今日头条安装包内还有不少文本文件资源,如 JSON 文件、HTML 文件等。这些文本文件的压缩也能带来包大小优化效果。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"今日头条落地的文本文件压缩方案由三部分组成:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1、压缩阶段:在 Build Phase 中添加脚本,构建期间对白名单内的文本文件做 zip 压缩;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2、解压阶段:在 App 启动阶段,在异步线程中进行解压操作,将解压产物存放到沙盒中;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"3、读取阶段:在 App 运行时,hook 读取这些文件的方法,将读取路径从 Bundle 改为沙盒中的对应路径;"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这一方案能在业务入侵较少的前提下完成压缩优化。我们首先将这一方案应用在了 Lottie 动画的 JSON 文件上,产生了 400KB 的包大小收益。后续这一方案也可以进一步拓展,应用在更多类型的文件上。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"三、Mach-O 文件优化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在资源优化的同时,我们也关注到,Mach-O 文件始终占据了今日头条安装包 80% 左右的体积。Mach-O 文件的优化必不可少。下面我们以时间顺序,介绍我们落地的 Mach-O 文件优化项。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.1、使用 -Oz 编译参数"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Oz 是 Xcode 11 新增的编译优化选项。WWDC 2019 《What's New in Clang and LLVM》[3] 中对 Oz 有过介绍。Oz 的核心原理是对重复的连续机器指令外联成函数进行复用,和“内联函数”的原理正好相反。因此,开启 Oz,能减小二进制的大小,但同时理论上会带来执行效率的额外消耗。对性能(CPU)敏感的代码使用需要评估。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"苹果给的参考数据是 4.5% 的包体积收益。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们在评估了执行效率、堆栈解析、稳定性和编译速度后,对大部分源代码开启了 Oz 编译,包体积减小 4MB 以上。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.2、使用链接时优化 LTO"}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/7e\/7ecd0ac913b3b267202100cada720389.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Link-Time Optimization 链接时优化,是 Xcode 自带的一个编译\/链接参数。根据 WWDC 2016 《What's New in LLVM》[4]的介绍,LTO 对包大小和运行效率都有正向影响。今日头条在编译和链接中均开启 Incremental LTO 后,包体积减小 6.5MB。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.3、修正 Exported Symbols 配置"}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/cb\/cbdec87d887d1474efbfdad5f028b2cf.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Xcode Build Settings 中的 "},{"type":"codeinline","content":[{"type":"text","text":"EXPORTED_SYMBOLS_FILE"}]},{"type":"text","text":" 配置,控制着 Mach-O 中 "},{"type":"codeinline","content":[{"type":"text","text":"__LINKEDIT"}]},{"type":"text","text":" 段中 Export Info 的信息。动态链接器 dyld 在做符号绑定时,会读取被绑定的动态库或可执行文件的 Export Info 信息,得到一个符号对应的实际调用地址。如果正在被绑定的符号,在目标动态库的 Export Info 中缺失,dyld 则会抛出异常,表现为 App 崩溃。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"虽然从原理上看,Export Info 中的信息不可或缺。但是,对于一个 Mach-O 文件来说,并非所有的符号都是需要暴露给其他动态库或可执行文件的。理想情况下,私有的符号应该在编码时就应该以 "},{"type":"codeinline","content":[{"type":"text","text":"__attribute__((visibility(hidden)))"}]},{"type":"text","text":" 修饰。但在历史代码难以逐个添加修饰符的情况下,Exported Symbols 配置给了工程一个维护公有符号白名单的机会。如果填写了有效的 "},{"type":"codeinline","content":[{"type":"text","text":"EXPORTED_SYMBOLS_FILE"}]},{"type":"text","text":" 配置,动态库或者可执行文件会在静态链接时去掉白名单以外的符号,起到缩减包大小、增加逆向难度的作用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"今日头条在使用 Exported Symbols 配置后,包大小减少了 2.1MB。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.4、属性动态化"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"属性是 OC 中最常见的概念之一。然而,一个属性并没有我们想象的这么小。通过分析 Mach-O 文件,我们发现,一个属性可以分为三个部分:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(1)成员变量部分:成员变量本质是一个大小 32B 的结构体,结构体中三个指针(Offset、Name、Type)指向的内容的大小分别为 8B、10B、10B,其中 Name、Type 指针指向的内容的大小和成员变量的类型、名字长度相关。总大小大约 60B。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"@interface presentViewController ()\n@property (nonatomic,strong) UIImageView *imageView;\n@property (nonatomic,strong) UIButton *button;\n@property (nonatomic,strong) NSString *name;\n@end\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/c7\/c72401a301bcd99a80da0330b276e067.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(2)自动生成的 set\/get 方法部分:set\/get 方法本质是一个大小 24B 的结构体,结构体包含三个指针 Name、Type、Implementation,指向的内容大小大概为 10B、10B、20B。一个方法大小大概是64B,set、get 两个方法就是 128B。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/07\/078b56ec962f84d6273982a859f0cd78.webp","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(3)property 部分:property 的本质仍然是个结构体,大小是 16B,结构体中两个指针指向内容的大小分别大概是 10B、10B,和属性的名字和类型相关。总大小大概 36B。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/ca\/ca21df4d45e5ba740c5376a007c6d8cd.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"即一个属性占用的包大小大约为 224B。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果我们用 "},{"type":"codeinline","content":[{"type":"text","text":"@dynamic"}]},{"type":"text","text":" 修饰一个属性,不生成成员变量、get\/set 方法,则一个属性可以由 224B 减少到 36B,即仅包含 property 部分的大小。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同时,代码中存在大量通过脚本自动生成的 "},{"type":"codeinline","content":[{"type":"text","text":"JSONModel"}]},{"type":"text","text":" 子类,这些子类往往拥有大量属性。这里也就存在着包大小优化空间。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"于是我们通过修改生成 "},{"type":"codeinline","content":[{"type":"text","text":"JSONModel"}]},{"type":"text","text":" 子类的脚本,实现了:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1、属性全部使用 "},{"type":"codeinline","content":[{"type":"text","text":"@dynamic"}]},{"type":"text","text":" 修饰,基础变量额外生成 IVAR"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2、所有 "},{"type":"codeinline","content":[{"type":"text","text":"JSONModel"}]},{"type":"text","text":" 的子类继承自新的父类,新的父类实现 "},{"type":"codeinline","content":[{"type":"text","text":"resolveInstanceMethod"}]},{"type":"text","text":",在该方法中用 "},{"type":"codeinline","content":[{"type":"text","text":"class_addMethod"}]},{"type":"text","text":" 统一为属性添加 get\/set 方法。对象类型的属性使用关联对象的方式存取,基础类型的属性使用额外生成的 IVAR 存取。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这一优化获得了 800KB 的包大小收益,并且评估对读写的性能影响损耗可以接受。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.5、__TEXT 段迁移"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"安装包经过压缩后的 Download Size 若超过 200 MB,在蜂窝网络下载 App 就会受到限制,这对新增会有较大影响。在 2020 年下半年,我们探索实践了 __TEXT 段迁移技术:在链接阶段使用 "},{"type":"codeinline","content":[{"type":"text","text":"-rename_section"}]},{"type":"text","text":" 选项将 "},{"type":"codeinline","content":[{"type":"text","text":"__TEXT,__text"}]},{"type":"text","text":" 迁移到 "},{"type":"codeinline","content":[{"type":"text","text":"__BD_TEXT"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":"__text"}]},{"type":"text","text":",减少苹果对可执行文件的加密范围,提升可执行文件的压缩效率,从而减少 Download Size。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用该方案我们最终减少了 60 MB 的 Download Size 以及 2 MB 的 Install Size。详细的原理可以参考:"},{"type":"link","attrs":{"href":"http:\/\/mp.weixin.qq.com\/s?__biz=MzI1MzYzMjE0MQ==&mid=2247487459&idx=1&sn=3dd9276f5af78ca5a377adec37e3e916&chksm=e9d0c401dea74d17e9f1bdd5ea764cc0cd7e845c6ebadde752d36608306b09e762a1681c7252&scene=21#wechat_redirect","title":"","type":null},"content":[{"type":"text","text":"《今日头条优化实践:iOS 包大小二进制优化,一行代码减少 60 MB 下载大小》"}]},{"type":"text","text":"[5]。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.6、二进制段压缩"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Mach-O 文件占据了 Install Size 中很大一部分比例,但并不是文件中的每个段\/节在程序启动的第一时间都要被用到。可以在构建过程中将 Mach-O 文件中的这部分段\/节压缩,然后只要在这些段被使用到之前将其解压到内存中,就能达到了减少包大小的效果,同时也能保证程序正常运行。由于苹果的一些限制,我们目前只压缩了 "},{"type":"codeinline","content":[{"type":"text","text":"__TEXT,__gcc_except_tab"}]},{"type":"text","text":" 与 "},{"type":"codeinline","content":[{"type":"text","text":"__TEXT,__objc_methtype"}]},{"type":"text","text":"两个节,然后在 "},{"type":"codeinline","content":[{"type":"text","text":"_dyld_register_func_for_add_image"}]},{"type":"text","text":" 的回调中对它进行解压。该方案累计优化了 3.5 MB Install Size。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"四、总结"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在以上优化项落地的同时,我们还与业务协作,通过挖掘无用代码、无用资源等手段,进一步优化着安装包大小。使得今日头条在高速的业务迭代下,包大小仍能保持稳定。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"参考资料"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[1] "},{"type":"link","attrs":{"href":"http:\/\/mp.weixin.qq.com\/s?__biz=MzI1MzYzMjE0MQ==&mid=2247484366&idx=1&sn=5a2d0e981c733e9eaec274b835600e67&chksm=e9d0c82cdea7413ada372bb936541c2a49de664b38daa6137c179ab6177231fa8b00ddf9acbc&scene=21#wechat_redirect","title":"","type":null},"content":[{"type":"text","text":"干货|今日头条iOS端安装包大小优化—思路与实践 "}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[2] Palette Images"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"http:\/\/www.manifold.net\/doc\/mfd9\/palette_images.htm"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[3] WWDC 2019 What's New in Clang and LLVM"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"https:\/\/developer.apple.com\/videos\/play\/wwdc2019\/409\/"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[4] WWDC 2016 What's New in LLVM"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"https:\/\/developer.apple.com\/videos\/play\/wwdc2016\/405\/"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"[5] "},{"type":"link","attrs":{"href":"https:\/\/www.infoq.cn\/article\/XUJL32hTDKYqAKz0hkMM","title":"","type":null},"content":[{"type":"text","text":"今日头条优化实践:iOS 包大小二进制优化,一行代码减少 60 MB 下载大小"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文转载自:字节跳动技术团队(ID:outiaotechblog)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"原文链接"},{"type":"text","text":":"},{"type":"link","attrs":{"href":"https:\/\/mp.weixin.qq.com\/s\/oyqAa8wKdioI5ZDG5LjkfA","title":"xxx","type":null},"content":[{"type":"text","text":"今日头条 iOS 安装包大小优化 - 新阶段、新实践"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章