Kairy 课本
继续阅读
第 43 讲前端工程约 16 分钟

Swift 133 文件原生探索复盘与 Owner 长期演进指南

为什么花了大精力写出纯 SwiftUI + WKWebView 两套 macOS 原生客户端(~133 个 .swift 文件),最后却果断冻结回归 Electron 单栈?

kairy-macKAIRY-0067KAIRY-0068WebKitProMotionBUG-0178Owner Playbook
心智模型

用实测数据止损沉没成本:为什么暂停 Swift 并回归 Electron 单栈

很多人认为“原生 Swift 一定比 Electron 快、省内存、体验好”。Kairy 团队没有停留在口头争论上,而是真刀真枪搭建了 Swift 客户端、注入桥接脚本与自测压测台:

  • 路线一(KAIRY-0068 WKWebView 外壳):用 Swift 原生窗口、⌥Space 快聊与桥接层(含 WebKitProMotion.swift 反射关掉 60fps 锁)承载 Electron Web UI(文档状态:Paused · 2026-11-18)。
  • 路线二与实仓演进(KAIRY-0067):曾做出无 WebKit 的纯原生探路并在 46 字符 / 29 消息种子自测录得 103.8–108.5 FPS;但据 KAIRY-0068/README.md:10 记录,kairy-mac 随后收敛为共用 Web UI 的 WebKit 外壳,全目录共计 133 个 .swift 文件、26,850 行。

然而当 Owner 亲自在真机日常对话(315 条消息)里对比两套客户端时,一个无法回避的结论浮出水面:在复杂排版、GenUI、Spaces 双 Blob 与高频更新面前,双前端栈的维护成本远高于收益,且 Electron 单栈在关键路径上已取得扎实实测效果。

32.1 315 条消息高刷屏历史压测账本(KAIRY-0068 实证数据)

Kairy 架构深度长课 EP07 · 分支 F:桌面渲染工艺、GenUI 四防线、双 Blob 沙箱与单栈收敛本章定位 47:1054:35
倍速
当前 00:00 · 开场动效:Kairy 架构深度长课 · EP07

2026-11-18,团队在 Apple ProMotion 高刷屏(每帧预算 8.3ms)上针对 315 条消息 历史基准,对比了 Swift WKWebView 版与 Chromium/Electron 版:

历史实测场景(315 条消息 @ ProMotion)Swift (WKWebView) 实测Electron (Chromium) 实测根因拆解与结论
Profile 面板展开87 fps,p95 16–17 ms(8ms 与 16ms 交替掉帧)历史测试录得 120 fps,p95 9 ms(含 1 帧 25ms)面板宽度从 0 扩到 25rem 时,WebKit 每帧对整条 315 行对话重新 Layout,而 Chromium 明显更扛重排。
Profile 面板关闭出现 74–99 ms 单帧巨型尖峰无明显掉帧尖峰Safari Web Inspector 定位到是 WebKit 同步销毁面板内 6 个在线状态视频解码器所致(后续 BridgeCompositorScript 延迟释放虽改善至 116.6 FPS,但维护极其脆弱)。
发送气泡起飞出现 52–64 ms 卡顿帧曾现 9 帧 33ms(后经 BUG-0159 压至 10 次重绘消平)未修 BUG-0159 前的 639 次全树重绘在 WebKit JSC 上放大了近一倍。
纯原生路线对比(KAIRY-0067 / 0068)早期纯 SwiftUI 46 字符种子自测 103.8–108.5 FPS(p95 17.5–18ms,未跑 315 条复杂 GenUI/Spaces)单栈完整支撑 26 符号 GenUI + Spaces 双 Blob + KairyUI 契约,240 轮发送 96ms双栈需要重写 GenUI、Markdown、Spaces 隔离窗、设计系统与两套 E2E,维护债呈指数爆炸。
MARKDOWNKAIRY-0068 真实测量日志节选(2026-11-18 高刷屏对照,历史基准)docs/engineering/work-items/KAIRY-0068/README.md · 23-35
23 - **Measured (2026-11-18)**, 315 synthetic messages (the owner's conversation has 315). Swift ran isolated with the self-test (`KAIRY_MAC_SUPPORT_DIR`); Electron ran in the e2e harness (`tests/e2e/electron/motion-frames.spec.mjs`, not committed). A ProMotion high-refresh screen [历史摘录], so 8.3 ms per frame:
24
25 | | Swift (WebKit) | Electron (Chromium) |
26 |---|---|---|
27 | Profile opens | 87 fps, p95 16–17 ms (frames alternate 8 and 16 ms) | 120 fps, p95 9 ms, one 25 ms frame |2
28 | Profile closes | one frame of 74–99 ms | — |
29 | Sent bubble | one frame of 52–64 ms | nine frames of 33 ms |
30 | Scroll, typing | high refresh rate | — |
31
32 - **Without the injected compositor script, Swift got worse**: the bubble at 12 fps, more long frames on open and close. The global wrappers are not the cause, and the earlier guess was wrong.1
33 - **The profile's width animation is the cause of the uneven opening**: the slot grows from 0 to 25rem, so WebKit lays out the whole conversation every frame. With the slot at 25rem from the start (only the slide and fade), the opening is 118 fps with a p95 of 9 ms in Swift. This changes the motion design (the conversation narrows at once), so it waits for the owner's choice.
34 - **Sending (fixed, BUG-0159 second part)**: every send rendered every message row twice, 639 renders for 315 messages; now 10. This was Electron's 33 ms frames, and is heavier in WebKit.
35 - The self-test's bubble benchmark (52 ms) does not exercise this. It injects a state without the in-flight flag, so its number did not move. A real send in the Swift app is still to be measured.

32.2 技术遗产精读:WebKitProMotion.swift 如何解锁 WKWebView 高刷限流

虽然 kairy-mac/ 已经暂停迭代,但它沉淀的多个精巧工程模块仍留在仓库里作为宝贵遗产。其中最具代表性的是 WebKitProMotion.swift——苹果默认在所有第三方 WKWebView 中偷偷开启了 PreferPageRenderingUpdatesNear60FPSEnabled 把刷新率锁在 60fps。Kairy 通过反射查找 WKPreferences 私有特性列表并调用 _setEnabled:forFeature: 将其关闭,从而解锁原生 ProMotion 刷新率:

SWIFT关闭 WKPreferences 60fps 节流锁解锁 ProMotion 高刷支持kairy-mac/Sources/KairyUI/WebHost/WebKitProMotion.swift · 13-33
13 @discardableResult
14 public static func enableNativeRefreshRate(on preferences: WKPreferences) -> Bool {
15 guard let feature = find60FPSFeature() else { return false }1
16
17 let setSel = NSSelectorFromString("_setEnabled:forFeature:")2
18 if preferences.responds(to: setSel) {
19 let imp = preferences.method(for: setSel)
20 typealias SetFn = @convention(c) (AnyObject, Selector, ObjCBool, AnyObject) -> Void
21 unsafeBitCast(imp, to: SetFn.self)(preferences, setSel, ObjCBool(false), feature)
22 return isNativeRefreshRateEnabled(on: preferences)
23 }
24
25 let expSetSel = NSSelectorFromString("_setEnabled:forExperimentalFeature:")
26 if preferences.responds(to: expSetSel) {
27 let imp = preferences.method(for: expSetSel)
28 typealias SetFn = @convention(c) (AnyObject, Selector, ObjCBool, AnyObject) -> Void
29 unsafeBitCast(imp, to: SetFn.self)(preferences, expSetSel, ObjCBool(false), feature)
30 return isNativeRefreshRateEnabled(on: preferences)
31 }
32 return false
33 }

32.3 毕业总结:Owner 长期演进与日常验收操作手册

走完 7 大模块,你已经掌握了 Kairy 各个核心子系统的关键脉络。作为项目的唯一 Owner,日常指挥 AI 编程代理与守住架构不腐化的黄金闭环清单如下(更多当前待拍板清单请前往 决策台 (#decisions) 查看):

真实踩坑:本讲关联的 Bug 复盘

Owner 审阅清单

  1. 当前还有 DEC-01(ActionGuard 是否从 observe 切为 enforce)与 DEC-02(KAIRY-0081..0098 收口提交与全量门禁)等 7 项重要决策待你拍板,点击进入 决策台 (#decisions) 逐项审批。
  2. 保留 kairy-mac/ 源码在仓库内作参考但不主动向它同步新功能;未来只有当 Electron 单栈出现无法绕过的系统级硬瓶颈时,才依据实测数据评估重启原生前端。
  3. 每次大型重构后,先跑受影响模块的单测,里程碑收口时跑一次全量门禁并在真实桌面端亲自聊天验收,再关闭工作项。

自测:合上书能答出来吗

进度只存在这台电脑的浏览器里。