Why my 1.17 MB player opens in under 400 ms cold: zero content providers, lazy init, and a startup window that only draws a color
Why my 1.17 MB player opens in under 400 ms cold: zero content providers, lazy init, and a startup window that only draws a color
Everyone talks about download size. Nobody talks about the first 400 ms. My player is 1.17 MB on Android, and on a 2 GB Moto G Play the difference between “opens instantly” and “opens eventually” is entirely in what runs before the first frame. I profiled cold starts with adb shell am start -W and Macrobenchmark, and the numbers matched what the Android docs say: TTID (time to initial display) is what users feel, TTFD is what they can actually use. A splash screen gets you a good TTID. It does nothing for TTFD. So I optimized both, separately.
每个人都在谈论下载大小,却没人谈论最初的 400 毫秒。我的播放器在 Android 上仅 1.17 MB,而在 2 GB 内存的 Moto G Play 上,“瞬间打开”与“最终打开”的区别完全取决于第一帧显示之前运行的内容。我使用 adb shell am start -W 和 Macrobenchmark 对冷启动进行了分析,数据与 Android 文档所述一致:TTID(初始显示时间)是用户的主观感受,而 TTFD(完全显示时间)才是用户真正能操作的时间。启动画面可以带来不错的 TTID,但对 TTFD 毫无帮助。因此,我分别对两者进行了优化。
What I cut: Zero ContentProviders. No androidx.startup, nothing auto-initializing. A 1.17 MB APK cannot afford a provider doing disk I/O on the main thread before onCreate finishes. Everything is lazy, pulled on first use. The startup window only draws a color. windowBackground on the theme, no bitmap, no inflation. First frame lands in tens of ms because there is nothing to inflate.
我砍掉了什么:零 ContentProvider。没有 androidx.startup,没有任何自动初始化的内容。一个 1.17 MB 的 APK 无法承受在 onCreate 完成前有 Provider 在主线程进行磁盘 I/O 操作。一切都是懒加载,在首次使用时才调用。启动窗口只绘制一种颜色。主题中使用 windowBackground,没有位图,没有布局填充(inflation)。第一帧在几十毫秒内就能显示,因为根本没有东西需要填充。
The video surface is created but not attached until the first file opens. SurfaceView/TextureView creation is not free on old devices, and the home screen does not need it. All JSON parsing, thumbnail decode and list binding happens after the first frame is drawn, chunked so the main thread never blocks longer than one frame. The result on the Moto G Play: cold start to first frame is around 250-350 ms, and the home list is interactive under 500 ms. That is the whole trick.
视频表面(Surface)在创建后,直到第一个文件打开前都不会被挂载。在旧设备上,SurfaceView/TextureView 的创建并非没有开销,而主屏幕并不需要它。所有的 JSON 解析、缩略图解码和列表绑定都在第一帧绘制完成后进行,并分块处理,确保主线程阻塞时间不超过一帧。在 Moto G Play 上的结果是:冷启动到第一帧显示大约需要 250-350 毫秒,主列表在 500 毫秒内即可交互。这就是全部秘诀。
There is no library magic in a 1.17 MB binary; there is just refusing to do work before the user sees anything. I also tried baseline profiles for a while and dropped them. On a binary this small the AOT compilation tax on install was not worth the jank reduction, and Play already does partial AOT on install for API 28+. Startup profiles via dex layout were the bigger win and they cost zero runtime bytes.
1.17 MB 的二进制文件中没有任何库的魔法;有的只是拒绝在用户看到任何东西之前做多余的工作。我也曾尝试过基准配置文件(baseline profiles),但后来放弃了。对于这么小的二进制文件,安装时的 AOT 编译成本并不值得为了减少卡顿而付出,况且 Google Play 已经在 API 28+ 的安装过程中进行了部分 AOT 编译。通过 dex 布局实现的启动配置文件效果更好,且不会增加任何运行时字节。
Disclosure: I build RKMovies, an ultra-lightweight 4K player client for Android and Windows (1.17 MB APK, 29.5 MB installer, hardware decode, multi-audio, WebVTT subs, no account, zero telemetry). More here: the home page of the thing, the 1.17 mb sideload link, the windows installer page.
披露:我开发了 RKMovies,这是一个适用于 Android 和 Windows 的超轻量级 4K 播放器客户端(1.17 MB APK,29.5 MB 安装程序,支持硬件解码、多音轨、WebVTT 字幕,无需账户,零遥测)。更多信息请见:该项目主页、1.17 MB 侧载链接、Windows 安装程序页面。