hex/ios/HEXKeyboard
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Ryan Vogel 6ab53fff1e
iOS keyboard: open Hex instead of being killed when short of memory (#2045)
* iOS: open Hex instead of dictating in the keyboard when it is short of memory

Testers on 4 GB iPhones (12, 12 mini, 13) saw in-keyboard dictation in other
apps stop about a second after tapping Dictate and iOS switch to the system
keyboard: iOS ends a keyboard extension that passes its memory limit (Apple:
"The memory limits vary from model to model"; reported at ~60-70 MB by
KeyboardKit, ActiveHard 77 MB by fcitx5-ios, 90 MB on a 4 GB iPad by
dictus-ios #202). Starting creates the capture web view, loads its page, starts
WebKit's capture and mounts the recording screen's Metal scope all at once.

What changed
- KeyboardLocalDictation.hasMemoryToStart: os_proc_available_memory() must
  leave 32 MB for the first dictation (web view, page, WebKit's first use,
  recording screen, Metal device and pipeline) and 16 MB once the web view is
  loaded (capture and recording screen only). 0 (the simulator) is enough.
- KeyboardViewController.syncLocalDictationAvailability, on every bridge poll:
  while short, viewModel.onLocalDictateTap is nil, so Dictate, the bar's Hex
  mark and the full keyboard's Hex key take their existing cold start (open
  Hex, record there, return), as before iOS 26. A hold stays a tap there, so
  push to talk sends no stray stop. With room, nothing changes. The probe log
  notes each switch, at most every 5 s.
- A web view whose WebContent process ended now also leaves the keyboard's
  view; before, it stayed behind (2 pt, alpha 0.02) next to the new one each
  later dictation made.
- KeyboardProbeLog.memory() adds the process's peak footprint
  (ledger_phys_footprint_peak), so a tester's keyboard-capture-probe.log shows
  what the limit saw: [footprint, peak, headroom].
- README: the guard, and its proof limits.

Tried and left out, after measuring
- Releasing the capture web view after each dictation (and on memory
  warnings): the keyboard's footprint stayed (31.9 -> 31.8 MB; WebKit/JSC state
  and allocator pages stay in the process, malloc_zone_pressure_relief returned
  16 KB) while the next start's spike grew (+1.9 vs +0.9 MB) and it waited for
  the page again (~1.5 s). The web view stays loaded, as before.
- Dropping the text checker or lexicon while dictating: the full keyboard rests
  lower than the dictation bar in the simulator (18.3-21.0 vs 21.4-24.3 MB), so
  nothing showed it helps.

Measured (simulator iPhone 13, iOS 27.2, Release; a temporary, uncommitted
probe drove the keyboard's own Dictate and Done paths with a host field
focused, logged phys_footprint at each step and every 100 ms, with footprint(1)
sampling from outside; 3 runs each, MB):
                      bar before  bar after   full before full after
  keyboard shown      21.6-24.2   21.4-24.3   18.5-21.0   18.3-20.9
  web view created    25.1-27.7   24.9-27.9   22.1-24.7   22.1-24.7
  web view loaded     27.5-30.0   27.5-30.5   25.5-28.7   25.8-28.6
  capture started     28.5-30.8   28.1-31.3   26.7-29.6   26.8-29.4
  max 3 s after tap   28.8-31.2   28.5-31.7   28.9-31.4   28.9-32.1
  stop                27.8-31.8   27.7-31.0   26.0-28.6   26.1-28.6
  transcriber made    27.9-31.8   27.8-31.0   26.1-28.7   26.2-28.8
  lifetime peak       31.5-32.4   28.9-32.3   29.8-32.6   30.0-32.6
A second dictation adds 0.5-1.3 MB (bar) and 4.2-4.8 MB (full keyboard, which
rebuilds its recording panel). With a simulated limit 24 MB above the footprint
at the tap, both keyboards now hand off and stay at 21.2 / 18.3 MB instead of
rising 7-13 MB; with 40 MB they dictate in the keyboard, twice. The simulator
counts no GPU memory and transcription fails there ("Audio format is not
supported"); a macOS stand-in put the Metal device and pipeline at ~3.5 MB and
the scope's drawables at ~4.4 MB, which the thresholds allow for.

Verified: xcodebuild test on the iPhone 18 Pro simulator, 212 tests pass (1 skipped as
before);
the Release generic iOS build with CODE_SIGNING_ALLOWED=NO succeeds. Not
verified on a physical iPhone: the real limit, GPU memory, and whether 32/16 MB
is the right line; the probe log's peak and headroom will show it.

* iOS keyboard: need 24 MB, not 16, before a second in-keyboard dictation

Leaving the full keyboard's new recording strip briefly peaked 7 MB above recording (35.5 MB in a simulated iPhone 13), and a second dictation now costs up to 8 MB, so 16 MB left little margin.

Compile-checked with a generic-iOS build.
2026-11-20 14:33:16 +00:00
..
CleanupTagger iOS: clean up what I say with an on-device tagger (experiment) (#2023) 2026-11-20 01:29:41 +00:00
ContainingAppOpener.swift Open Hex from the iPhone keyboard and show live keyboard setup (#599) 2026-10-31 09:56:02 -07:00
HEXKeyboard.entitlements Set up Hex Mobile internal TestFlight releases (#1997) 2026-11-19 11:48:49 -05:00
Info.plist iOS: dictate from the keyboard without opening Hex, plus suggestions, autocorrect, and Paste (#1999) 2026-11-19 12:20:12 -05:00
KeyboardLocalDictation.swift iOS keyboard: open Hex instead of being killed when short of memory (#2045) 2026-11-20 14:33:16 +00:00
KeyboardViewController.swift iOS keyboard: open Hex instead of being killed when short of memory (#2045) 2026-11-20 14:33:16 +00:00