perf: default-off in-app profiler + debug scroll jumps; verify scroll perf & clamping

internal/perf: single-goroutine profiler (no locks). When enabled by the
/storage/emulated/0/PadPerf/enable marker it records one CSV row per logic
frame (seq, ms, frame delta, page, scroll Dp, max-scroll Dp, total lines,
visible byte range), logs a rolling ~1/s summary, and on Stop reports
nearest-rank p50/p90/p99/max. Flushes per row-batch but never fsyncs per
flush (avoids periodic hitches in the logic path).

editor: PerfRecord package-level hook (nil when off) + ProbeRecord;
Logic.emitFrame() now centralizes every frame emission so the profiler sees
each logic frame exactly once, on the owner goroutine. State gains
VisibleStart/VisibleEnd so the probe can confirm shaping stays
viewport-bounded.

logic: optional debug cmd poller (off by default) watches <dir>/cmd as a
one-shot file (top/bottom/frac <0..1>/dp <int>) and the owner applies a
clamped [0,MaxScroll] jump + frame. Enables deterministic large-offset scroll
tests without pixel taps.

main: wires the profiler when the marker file exists; logs present-fps
every 2s; stops the profiler on Destroy.

docs: README package inventory (add internal/perf); architecture §11
profiler facility; spec §5 invariant 7 (scroll always clamped to
[0,maxScroll]) + §6 measured rows; development_plan v5 + Phase 6 results.

Verified: go build/vet + full -race green. On-device 10 MB file:
logic-frame cadence flat across offsets 0.02->1.0 (no large-offset
degradation), visible byte range <=4.3 KB at every offset, clamping exact
across 2->130,955-line files, PSS plateaus ~250 MB (bounded, no leak);
profiler overhead negligible.
This commit is contained in:
gmp
2026-08-16 16:53:28 -04:00
parent 5271092c21
commit be48ad8157
10 changed files with 627 additions and 46 deletions
+12
View File
@@ -9,6 +9,18 @@ line-by-line code — so they stay true as the implementation evolves.
| [`architecture.md`](./architecture.md) | How it works: single-owner concurrency model, channel topology, Frame handoff contract, ownership rules, editor/browser/render internals. |
| [`development_plan.md`](./development_plan.md) | The active plan: completed phases, remaining work, and the on-device observation loop. |
## Package inventory
| Package | Role |
|---------|------|
| `internal/editor` | Chunked buffer, line index, virtualized viewport, IME ops, autosave, logic goroutine, state, Frame |
| `internal/browser` | Directory browsing, search, sort, pagination, browser state machine |
| `internal/ui` | Element tree, frame layout, renderer (op-based), IME wiring, gestures |
| `internal/perf` | Default-off performance profiler (per-logic-frame cadence + scroll state → CSV) |
| `internal/io/pool` | Worker pool (8 workers, 2 priority lanes) + file/dir tasks |
| `internal/test/e2e` | Harness driving the real `Logic` + `Inspect` |
| `cmd/pad` | `main.go` (entry), `impl_android.go` (base path) |
## Documentation policy
1. **Docs describe invariants and contracts, not code lines.** If a document
+29
View File
@@ -304,3 +304,32 @@ Only the visible byte range is shaped and drawn each frame:
(`development_plan.md` Phase 4).
- Dead task types (`ReadChunk`, `SaveState`, `SaveUndo`, …) and unused element
types are candidates for removal in the same round.
## 11. Performance profiler (default-off, `internal/perf`)
Pad ships a built-in profiler that is **off by default** and costs nothing when
off. It is the in-app frame-timing tool (see `development_plan.md` §9: `gfxinfo`
cannot measure this app because it renders into a `SurfaceView`).
- **Enable** by creating the marker file
`/storage/emulated/0/PadPerf/enable` before launch. `main.go` then creates a
`perf.Profiler` that writes `logic_frames.csv` (one row per logic frame: seq,
ms-since-start, frame delta, page, scroll Dp, max-scroll Dp, total lines,
visible byte range) and logs a rolling ~1 s `PERF` summary. `DestroyEvent`
stops it (final flush + `PERF stopped` summary with p50/p90/p99/max).
- **Hook**: `internal/editor.PerfRecord` is a package-level func, set by
`main.go` only when enabled. `Logic.emitFrame` calls it on the owner
goroutine with a `ProbeRecord` **before** sending the frame. When disabled it
is `nil` and the per-frame cost is a single nil check.
- **Debug scroll jumps** (for testing, off by default): with the profiler on,
`main.go` also polls `/storage/emulated/0/PadPerf/cmd` (a one-shot file
consumed on read). `top`, `bottom`, `frac <0..1>`, and `dp <int>` jump the
editor's `ScrollOffset` (clamped to `[0, MaxScroll]`) and emit a frame. This
lets a test drive large-offset scrolls deterministically without pixel taps.
- The profiler is owned by the goroutine that creates it and is single-goroutine
(no locks). It does **not** `Sync()` the CSV per flush (only per row batch) to
avoid periodic fsync hitches in the logic path.
- **Measured** (emulator, 10 MB file, 2026-08): logic-frame cadence is flat
across scroll offsets 0.02→1.0 (no large-offset degradation); the visible byte
range stays ≤ ~4.3 KB (0.04% of the file); PSS plateaus ~250 MB (bounded
high-water mark, no leak). See `development_plan.md` Phase 6.
+43 -4
View File
@@ -1,7 +1,7 @@
# Development Plan: reach a lean, usable Android text editor
Status: v4, 2026-08-16 (Phases 0–3 complete; doc reorganization done). Written
against the **live** repo `/home/gmp/pad`. v1 (the widget-rebuild plan) is
Status: v5, 2026-08-16 (Phases 0–3 + doc reorg + Phase 6 scroll-perf/clamping
verification complete). Written against the **live** repo `/home/gmp/pad`. v1 (the widget-rebuild plan) is
superseded — see §12 for why. Doc reorganization (2026-08-16): the over-detailed
docs (`*_implementation_plan.md`, `touch.md`, `element_model.md`,
`layout_rendering.md`, `virtual_scroll_render_optimization.md`,
@@ -11,7 +11,10 @@ adds the doc policy + build/install recipe. See `doc/README.md`.
Phases 0–3 done: single-owner no-lock architecture, Android IME wiring, on-device
IME validation (passing), viewport-on-open fix, chunked-buffer drift fix, the
whole-file shaper memory-leak fix, a measured 50 MB size limit, and the IME
rapid-commit desync fix (snippet/selection dedup). Remaining: real-device
rapid-commit desync fix (snippet/selection dedup). Phase 6 (2026-08-16) added a
default-off in-app performance profiler and verified: scroll does not degrade at
large offsets (10 MB file), scroll clamping is exact across 2→130,955-line
files, and memory plateaus ~250 MB (no leak). Remaining: real-device
swipe/autocorrect sign-off (the emulator's AOSP/Gboard keyboard is a proxy).
## 1. Decision summary (updated)
@@ -239,7 +242,43 @@ but are not needed for a usable v1.
- ✓ Build/install recipe (2026-08-16: in `doc/README.md`; machine-local script
`/tmp/build_pad.sh`).
- ☐ In-repo device test checklist (the adb tap/swipe/IME sequences used in
Phases 2–3 are in this plan's phase notes but not a standalone checklist).
Phases 2–3 and Phase 6 are in this plan's phase notes but not a standalone
checklist).
### Phase 6 — scroll performance & clamping verification — DONE (2026-08-16)
Goal: (a) confirm scroll does not degrade at large offsets in a large file,
(b) confirm scroll clamping is correct across file sizes. Enabled the default-off
in-app profiler (architecture.md §11) and drove deterministic `frac` scroll
jumps + real swipes on the emulator.
**Scroll performance at large offsets (10 MB file, 130,955 lines):** swept
`frac` 0.02→1.0 with swipes. Logic-frame cadence is **flat across all offsets**
(p50 ~38–44 ms, p90 ~51–75 ms, no trend up at 0.9/0.98/1.0) — **no large-offset
degradation**. The visible byte range stays **≤ 4,274 B (0.04% of the file)** at
every offset, confirming the shaper never lays out the whole file. `ScreenRecord`
and `gfxinfo` were ruled out as metrics (downsampled / View-layer-only, since Pad
renders into a `SurfaceView`); the in-app profiler is the instrument.
**Scroll clamping (6 sizes, 2→130,955 lines):** for each, `top` and `bottom`
commands set the offset to exactly 0 and to exactly `maxScroll`; **no frame ever
exceeded `maxScroll` or went negative**. Sub-viewport files (`tiny_1line` 2 lines,
`small_10` 11 lines) correctly have `maxScroll = 0` and cannot scroll; larger
files' `maxScroll` scales correctly with content. No blank viewport at any size
(content verified on-screen).
**Memory (bonus):** PSS **plateaus ~250 MB** for the 10 MB file under sustained
scroll (bounded high-water mark from the shaper glyph cache + Go heap; grows
~13 MB over the first ~30 scrolls then flat) — **no leak**, well under the 2.5 GB
OOM line. The profiler's own overhead is negligible (same plateau with it off).
**Test-harness gotcha:** the 390×844 Dp window is **letterboxed** on the
1080×2400 screen (~28 px left / ~133 px top offset), so on-screen tap
coordinates are offset from the naive 1:1 Dp→px map. The editor back-arrow hits
at ~`(86, 264)` px, not the glyph's apparent 1:1 position. Browser file rows
(newest-first) start ~y=520 px, ~134 px apart. Verify every open via the exact
logcat line `Logic: OpenFileChan /storage/emulated/0/Notes/<file>`, not a loose
`OpenFileChan` match.
## 6. File-size decision (re-framed)
+6 -2
View File
@@ -119,15 +119,19 @@ Full details, channel topology, and ownership rules: [`architecture.md`](./archi
owner-dispatches-a-task pattern.
6. **Logic work stays < 16 ms.** Anything that can block or scan more than the
viewport goes to the worker pool.
7. **Scroll offset is always clamped to `[0, maxScroll]`,** where
`maxScroll = contentHeight − viewportHeight` floored at 0. A file whose
content fits the viewport has `maxScroll = 0` and cannot scroll. Verified
on-device across 2→130,955 lines (`development_plan.md` Phase 6).
## 6. Performance expectations (validated on-device)
| Operation | Target | Measured (10 MB file, Android 35 emulator) |
|---|---|---|
| Open file | instant feel | ~120 ms (stat + read + line index) |
| Scroll | 60 fps | flat ~240 MB RSS, no growth, no OOM |
| Scroll | 60 fps | logic-frame cadence flat across offsets 0.02→1.0 — **no large-offset degradation**; present fps is emulator-limited (~14–27 on the software-rendered emulator, not a Pad quality metric) |
| Type | responsive | single IME path; rapid commits land cleanly |
| Memory | bounded | ~150 MB PSS / ~230 MB RSS at 10 MB file, flat |
| Memory | bounded | PSS plateaus ~250 MB at a 10 MB file (bounded high-water mark, no growth under sustained scroll; no OOM) |
## 7. Deferred / not implemented (explicit non-goals for v1)