Skip to content

Android: save commits and mounts copy only what changed (#571) - #733

Open
vhoxha08 wants to merge 2 commits into
igawa6:mainfrom
vhoxha08:perf/save-commit-delta
Open

vhoxha08 wants to merge 2 commits into
igawa6:mainfrom
vhoxha08:perf/save-commit-delta

Conversation

@vhoxha08

Copy link
Copy Markdown

What and why

Fixes #571. Likely fixes #565. Depends on #732 (it builds on its in-place save journal; until that is merged this PR also shows its commit).

On Android, a save commit copied the whole work folder into <id>.new, synced every file, renamed every file into the save folder and synced every file again; a mount deleted <id>.work and copied the whole save. On shared storage (FUSE: a picked save folder under /storage/emulated/0 or on an SD card) every one of those operations is slow, so saves with many files take seconds. Breath of the Wild (509 files, 469 of them pictures) took 10.2-10.8 s per commit on an Odin 2 Portal; #565's log shows 2.7-3.3 s per commit on an SD card, the length of the sound gap after a teleport (BotW saves on arrival). On the Portal the delayed sound after fast travel, and Compendium entries not shown as saved, went away with this change.

#571: a changed save file kept its old modification time, so Syncthing (which skips files whose size and time did not change) never synced it. Cause: on shared storage, Android's media provider puts the old file's time back about two seconds after a file is renamed over it, and the journal's stage 2 renamed each new file over the old one.

The change (plat_android.c, hle_fs.c, SaveStore.java):

  • A commit puts into <id>.new only the files that differ from the committed save, plus a list of every path of the new version (.dm-keep). A file differs when its type, size, mode, attributes or contents differ; times are not trusted (they can be stale on shared storage).
  • The journal keeps its markers and roll-forward rules. With the list, stage 1 deletes what the list does not name and stage 2 moves in the changed files; without it, nothing changes. After stage 2 only the folders are synced (the moved files were synced in <id>.new).
  • Copied files get the commit's time, and stage 2 deletes the old file before renaming the new one in, so a changed file keeps its new time; untouched files keep theirs. A stop between the two is finished from .pend2 like any stage 2.
  • SaveStore.recover reads the same list and moves files the same way.
  • A mount brings an existing <id>.work up to date (copying only what differs) instead of copying the whole save again.
  • Apple: unchanged (clonefile / RENAME_SWAP).

Checked

  • Python tests: python3 -m unittest test_android_saves (in dm/tools/tests) passes, with a new delta swap test (an unchanged file keeps its inode) and three new stopped-commit states (delta_pend1, delta_pend2_partial, delta_done) that the host's plat_swap_recover and SaveStore.recover finish identically.
  • NDK 28.2.13676358 syntax check of plat_android.c and hle_fs.c with the host's flags: no errors, no warnings.
  • On the phone, outside the game: the code built with the NDK and run as the app on /storage/emulated/0, with a copy of the BotW save, one slot changed and its old times put back on the changed files: a commit took 1.1 s (21 s the old way), a mount refresh 0.9 s (5.8 s); the folders ended identical; five seconds later the changed files still had the commit's time, the others their own.
  • Phone runs on a test app (-PnxApp=peanuts -PnxAppId=peanutstest), save folder on /storage/emulated/0: The Legend of Zelda: Breath of the Wild 1.9.0 (01007EF00011E000), loaded and saved twice: 7 commits of 0.63-0.81 s (10.2-10.8 s with 0.1.8 on the same folder), the save identical to .work, the changed files carrying the save's time ten seconds later, no crash.
  • GPU drivers: Turnip T30 (PurpleVK 26.3.0). Not run on the system driver.
  • Phone, chip, Android version: AYN Odin 2 Portal, Snapdragon 8 Gen 2, Android 13.
  • Not checked: Android 15 (where [BUG] BOTW save files retain old modification timestamps after successful commits, breaking Syncthing synchronization #571 was reported), an SD card save folder (BOTW teleport sound skip #565's setup), other games' saves, lint_c.py tidy.

Docs

  • The finding is in its game note or its docs/reference/<subsystem>.md entry, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • No game files, keys or data extracted from a game in the change or the attachments
  • Source only: no binaries, nothing under dm/third_party/, no change to dmsh_chain.lock; the branch starts from the current main (on top of Android: a save commit updates the save folder in place (#485) #732)

vhoxha08 and others added 2 commits November 24, 2026 21:45
A commit replaced the whole save folder: it swapped <id>.new and <id> with
renameat2(RENAME_EXCHANGE), which gives <id> a new inode. Sync tools such as
Syncthing watch the folder with inotify, so their watch stayed on the replaced
folder and they missed every save until their next full rescan (hourly by
default).

plat_rename_swap no longer swaps folders. Every commit now moves the new files
into the existing folder one by one, using the crash-safe journal that shared
storage already used. The folder stays the same, so sync tools see each new
file. A commit cut short by a crash is finished at the next start, so the save
is never left half old, half new.

Checked: a new test, test_android_saves.py HostSwap, commits a version with a
removed file and a new subfolder and checks that the folder keeps its inode,
that it ends up with exactly the new files, and that no marker stays. It
passes with the fix and fails on the old code. The whole test_android_saves.py
passes, including the launcher's JVM tests (JDK 25). plat_android.c and
hle_fs.c pass an NDK 28.2.13676358 syntax check with the host's flags
(Link's Awakening profile), with no warnings. On an Odin 2 Portal (Snapdragon
8 Gen 2, Android 13), the journal code built with the NDK ran on f2fs
(/data/local/tmp, as app-private storage) and on /sdcard, with an inotify
watch on the save folder. With RENAME_EXCHANGE the folder got a new inode and
the watch saw only IN_MOVE_SELF. With the fix the inode stays and the watch
sees each delete, the new subfolder and each file moved in.

Not checked: game runs on the phone (system driver, Turnip) with Syncthing,
and lint_c.py tidy (no clang-tidy here).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On Android a commit copied the whole work folder into <id>.new, synced
every file, renamed every file into the save folder and synced every file
again. A mount deleted <id>.work and copied the whole save. On shared
storage (FUSE) this is slow for saves with many files: Breath of the Wild
(509 files, 469 of them pictures) took 10 s per commit in the game.

A changed save file also kept its old modification time (igawa6#571), so
Syncthing, which skips files whose size and time did not change, never
synced it. On shared storage, Android's media provider puts the old
file's time back about two seconds after a file is renamed over it, and
the journal's stage 2 renamed each new file over the old one.

A commit now puts into <id>.new only the files that differ from the
committed save, and a list of every path of the new version (.dm-keep).
A file differs when its type, size, mode, attributes or contents differ;
times are not trusted. The copied files get the commit's time, and stage
2 deletes the old file before renaming the new one in (SaveStore too), so
a changed file keeps its new time; untouched files keep theirs. A stop
between the two is finished from .pend2 like any stage 2. The journal keeps its markers and its roll-forward rules: with
the list, stage 1 deletes what the list does not name and stage 2 moves
in the changed files; without it nothing changes. After stage 2 only the
folders are synced (the moved files were synced in <id>.new).
SaveStore.recover reads the same list. A mount brings an existing
<id>.work up to date instead of copying the save again.

Checked: test_android_saves.py (the new delta swap test and three delta
recovery states, host and launcher alike) passes; an NDK syntax check of
plat_android.c and hle_fs.c. On an Odin 2 Portal, as the app on
/storage/emulated/0 with a copy of the BotW save, one slot changed and
its old times put back on the changed files: a commit took 1.1 s (21 s
the old way) and a mount refresh 0.9 s (5.8 s), each leaving the folders
identical; five seconds later the changed files still had the commit's
time, the others their own. Not checked in the game with this version (an earlier one that
compared times committed a BotW save in 0.27 s; 0.1.8 takes 10.2-10.8 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

[BUG] BOTW save files retain old modification timestamps after successful commits, breaking Syncthing synchronization BOTW teleport sound skip

1 participant