-
Notifications
You must be signed in to change notification settings - Fork 2
Terminal Performance
AceGuru-mjh edited this page Oct 1, 2026
·
4 revisions
规格来源 ATR 2.0 Final Spec §49;v1 的 7 项指标全部达成。 来源
docs/PERF.md。
| # | 指标 | 目标 | 实现要点 |
|---|---|---|---|
| ① | PTY → EventBus 延迟 | < 20 ms |
PtyOutputPump 读 8KB chunks 非阻塞,直发 SharedFlow,无中间队列 |
| ② |
ProcessExited 事件 |
< 100 ms | SessionManager exit watcher 每 100ms 轮询 nativeIsAlive + nativeWaitExit 阻塞立即返回 |
| ③ | 语义快照 | < 10 ms |
SemanticStateReducer 持 MutableStateFlow<TerminalSemanticState>,observe(SEMANTIC) 读 .value,O(1) |
| ④ | 增量 observe | 0 重复字节 | 游标契约 prev.endCursor == next.startCursor
|
| ⑤ | UI 连续输出 | 无主线程阻塞 / OOM / ANR |
yes 跑 60s 监控 |
| ⑥ | RingBuffer 默认容量 | 256 KB |
RingTerminalBuffer.DEFAULT_CAPACITY = 256 * 1024,可配 64KB–4MB |
| ⑦ | 并发订阅者 | ≥ 8 | SharedFlow extraBufferCapacity = 1024,每订阅者独立游标 |
只有 PtyOutputPumpImpl 调 nativeRead —— 无竞争、无重复读。泵跑在 Dispatchers.IO:
每次读 → 恰好一个 OutputProduced 事件 → RingBuffer append → VT feed → reducer 更新 → bus emit,全在 IO 调度器。
WaitEngineImpl.await() 订阅 EventBus Flow + withTimeoutOrNull,
没有 sleep+read 循环,唤醒延迟 = O(事件到达) 而非 O(轮询间隔)。
SemanticStateReducer.onEvent() 模式匹配 12 种事件类型,只更新受影响字段:
OutputProduced → session.cursor + input.state;ProcessExited → foregroundJob + lastExitCode。无全量重算。
getSince(cursor) 返回 [cursor, cursor + available);越界(cursor < oldestCursor)返回 overrun = true + 空字节 —— 从不静默丢弃。
InputManagerImpl 每 session 一个 Channel<WriteOp>(无限容量),单写协程顺序调 nativeWrite;
InputControlState(TAKEOVER)仲裁是入队前的廉价 StateFlow 检查。
| 对象 | 策略 |
|---|---|
| RingBuffer | 默认 256KB,溢出淘汰最旧字节,EventLog 引用变 overrun
|
| EventLog | 每 session 内存 ArrayList,tail() 取最后 N;持久化只写元数据 + 最后 100 条事件
|
| VT100Emulator | 固定 rows × cols 单元数组,resize 复用 |
| 项 | 问题 | 何时重要 |
|---|---|---|
EventLog.query(afterCursor) |
O(n) 线性扫描 | >100K 事件时(v1 主路径是 tail(N),可接受) |
VT100Emulator.scrollUp() |
每行 System.arraycopy,O(rows×cols) |
高频滚动(如 yes)是瓶颈;环形行索引可降到 O(cols) |
EventBus.subscribe(afterCursor) |
回放过老游标可能很大(有 maxEvents 上限) |
极端回溯 |
| exit watcher | 每 session 一个协程 | >50 session 建议共享 watcher(v1 典型 1–3 session) |
./gradlew :platform:terminal:test
./gradlew :app:assembleDebug
# 插桩延迟测试(需模拟器)
adb shell am instrument -w \
-e class com.apex.agent.platform.terminal.PerfTest \
com.apex.agent.test/androidx.test.runner.AndroidJUnitRunner-
MCP 生态总览
新 - (沙箱 MCP · 官方 Hub · 逆向 Host · 门控语义)
- 终端运行时
- 终端 API 契约
- SDK 边界
- Termux 能力矩阵
- Ubuntu rootfs 供给
- Ubuntu 生命周期
- PRoot 二进制溯源
- VT100/ANSI 模拟器
- 终端性能
- 终端迁移
- 原生层 C++/JNI