Skip to content

perf: drop background tabs' back/forward cache and ask before sending a form again - #114

Merged
rgxdev merged 3 commits into
mainfrom
quavon/reduce-tab-memory-use
Nov 24, 2026
Merged

rgxdev merged 3 commits into
mainfrom
quavon/reduce-tab-memory-use

Conversation

@rgxdev

@rgxdev rgxdev commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Closes #

What changed

Tarn reached about 4.4 GB with many tabs open. Measured on a running Tarn: the shell 374 MB, WebKit's GPU process 321 MB, and 8 WebContent processes at about 3.6 GB. Sleeping a tab already frees its process: in a throwaway build with live tabs capped at 3, 10 tabs left 4 processes. Without sleeping tabs any sooner, this PR cuts the memory WebKit keeps for pages a tab has already left:

  • Back/forward cache drop (macOS, iOS). A live tab that has been off screen for engine::lifecycle::BACK_FORWARD_IDLE (60 s) calls WebKit's private _clearBackForwardCache once per stay off screen. The call is guarded by respondsToSelector, like the existing _webProcessIdentifier use. This ends the suspended WebContent processes WebKit keeps for earlier pages after cross-site navigations. The shown page, its form input and its history stay; Back loads the earlier page again. engine::lifecycle::BackForwardTrim makes the decision (shared core, unit-tested); macOS calls it from the minute tick, and mobile from a minute timer through a new dropBackForwardCache web-view command. Measured with one WKWebView after 5 cross-site loads: 4 WebContent processes / 586 MB before, 2 / 377 MB after.
  • Asking before sending a form again (macOS, iOS). With the cache dropped, WebKit reloads a POST result on Back, Forward or Reload as FormResubmitted, and allowing that sends the form again. A harness confirmed it: the server logged 2 POSTs when allowed and 1 when cancelled. That decision now waits for a new PromptKind::ResendForm prompt (" would get your form again"), with Send Again (danger colour, click-guarded) and Cancel (default). The prompt comes after every gate, Shield, S10 and level check. Only the user's own history step asks. A page's history.back() or a swipe cancels the load and shows nav_policy::RESEND_STOPPED, so a page cannot make Tarn ask about another site's form. Chromium tabs (Windows) show Chromium's own resubmission page; Android's WebView declines resubmission by default.

An earlier attempt changed the sleep policy (6 live tabs, a 5-minute budget, a 768 MB per-tab ceiling). It was dropped because it partly reversed the gentler sleeping from #73 and could lose typed input.

How it was tested

Checks run, and checks not run (and why):

  • Root: cargo fmt --all --check, cargo clippy --all-targets --locked -- -D warnings, cargo test --locked: pass.
  • mobile/: fmt, clippy, test: pass. cargo clippy --target aarch64-apple-ios-sim --all-targets --locked -- -D warnings: pass.
  • New tests: engine::lifecycle tests for BackForwardTrim (once per stay, the split, a tab left between two checks, a tab that slept and woke), mobile/app/tests/features.rs (split panes stay, a tab left between checks waits a minute), and the browser_core::prompts text.
  • WebKit behaviour checked with standalone Swift harnesses against the system WebKit: the cache drop saves memory, canGoBack stays true, Back after the drop sends BackForward then FormResubmitted, and Reload of a POST result sends FormResubmitted directly.
  • Smoke run of a throwaway Tarn (TARN_DATA_DIR in a temp folder) with 3 tabs past two ticks: no crash.
  • Not run: linux/ (WebKitGTK/GTK not installed on this Mac), mobile/android/build.sh (needs the nsh packager), Windows (no MSVC). CI covers these. The resend prompt itself was not clicked through in the GPUI app; its WebKit inputs were verified with the harness.

Clients covered (the others get a docs/port-backlog.md entry):

  • Cache drop: macOS and iOS. Real gaps documented: CEF (docs/windows-plan.md §8), WebKitGTK (docs/nsh-plan.md), Android WebView (docs/mobile-plan.md). None of them has a per-view back/forward cache flush.
  • Resend prompt: macOS and iOS. Linux still sends the form again silently (unchanged behaviour); it has a docs/port-backlog.md entry. A maintainer may want this treated as a protection that lands everywhere (AGENTS rule 8). Linux drops no cache, so this change makes the Linux case no more frequent.

Protected paths (.github/CODEOWNERS), dependencies, pins or checks changed, and why:

  • docs/: mobile-plan.md, nsh-plan.md, windows-plan.md (gaps), port-backlog.md (Linux entry). No dependency, pin, lockfile or CI changes.

Threat model: S1 (no page-to-shell bridge: none added; the prompt is shell UI driven by WebKit's navigation delegate), S5/S10 (every navigation check runs before the prompt; accepting runs the same steps as an allowed load), S21 (each new unsafe states its invariant). The held WebKit decision is answered exactly once on every path: confirm, cancel, page-started, a dropped prompt (which declines), or a closed tab.

Anything done differently from the guidelines in AGENTS.md or CONTRIBUTING.md, and why:

  • Uses WebKit SPI (_clearBackForwardCache), the same pattern as the existing _webProcessIdentifier and _hideFindUI calls; it does nothing where the selector is missing.

  • Known limits: iframe form resubmissions still pass silently (unchanged behaviour). Mobile prompts have no click-guard delay (applies to all mobile prompts, unchanged).

  • I read and understand every line and sign it off as its author (git commit -s)

  • Any AI agent used kept the hard rules in AGENTS.md

  • No secrets, credentials or personal data in the diff

A WebKit tab left in the background for a minute ends the web processes
WebKit keeps for its earlier pages after cross-site navigations. The shown
page, its form input and its history stay; Back loads the earlier page
again. No tab sleeps any sooner.

One WKWebView after five cross-site loads went from 4 web processes and
586 MB to 2 and 377 MB. CEF, WebKitGTK and Android WebView have no
per-view call for it (documented gaps); iOS is in the port backlog.

Signed-off-by: rgxdev <hello@d-aaron.dev>
…gain

Back/forward cache:
- engine::lifecycle::BackForwardTrim decides which live tabs drop their
  cache: once per stay off screen, after BACK_FORWARD_IDLE (60 s). Shells
  stamp the tabs they leave on a switch, so a tab viewed between two
  checks waits a full minute.
- macOS uses it instead of the inline idle check; iOS sends a new
  dropBackForwardCache command from a minute timer, and Android ignores
  it (its WebView has no such call).

Form resubmission:
- With the cache dropped, WebKit loads a POST result again on Back,
  Forward or Reload as FormResubmitted and would send the form again.
  On macOS and iOS that load now waits for a Tarn prompt (Send Again or
  Cancel, Cancel by default) after every Shield and network check.
- Only the user's own history step asks; a page's history.back() or a
  swipe cancels the load and shows a notice.
- Linux, which still sends it silently, is in the port backlog.

Signed-off-by: rgxdev <hello@d-aaron.dev>
@rgxdev
rgxdev requested review from a team as code owners November 24, 2026 22:40
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

MadebyLeopold
MadebyLeopold previously approved these changes Nov 24, 2026
…ory-use

Signed-off-by: rgxdev <hello@d-aaron.dev>

# Conflicts:
#	docs/port-backlog.md
@rgxdev
rgxdev merged commit 475d864 into main Nov 24, 2026
11 checks passed
@rgxdev
rgxdev deleted the quavon/reduce-tab-memory-use branch November 24, 2026 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants