Skip to content

Fast first game-code build (-O0), upgraded to -O1 from the played session - #467

Open
maxi000q wants to merge 3 commits into
mamcjr:mainfrom
maxi000q:fast-build-upgrade
Open

maxi000q wants to merge 3 commits into
mamcjr:mainfrom
maxi000q:fast-build-upgrade

Conversation

@maxi000q

@maxi000q maxi000q commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What

The first setup on a phone builds the game code at -O0 - minutes instead of the best part of an hour on a mid-range phone. The library it leaves is playable but a little slower, and the start screen rebuilds it in the background: with the app's own shipped hot list (gamebuild.c load_hot_for - the same lists the hot/cold build is made with), the cold marking, the memory floors and the jobs formula, everything a full build does. No fnprof anywhere: the session list was only found through LARECOMP_FNPROF_OUT in the process that pressed Play, and fnprof_frame latches its static from early - a new player would have stayed on -O0 forever - and it cost a hash probe and an atomic per function entry, and a hitch every 1800 frames.

The rebuild runs beside the menu: PLAY stays on, the status line shows the percent with a Later button beside it, and either one stops the build for now - the game gets the phone to itself, the work folder stays, and the next title screen goes on from where it stopped (the .gamebuild resume machinery, as for a build the app was closed on). A fresh start screen offers it again, an in-game "Title screen" reboot included; LARECOMP_NO_UPGRADE=1 keeps the fast library as it is. An update over the same files (a new GAMELIB_ID) with no library to play on builds fast too - minutes to play again, then the upgrade as for any fast build; the desktop is unchanged.

The seven points, and where each lives now

  1. The compile error - g_upgrade_busy (extern, then static) is gone, together with the play-blocking it existed for.
  2. The upgrade never ran - it doesn't key on the loaded library anymore: gamelib_fast_present (gamelib.c) reads the -fast marker from the .id on disk, without loading anything - boot() loads the library only after the start screen. The upgrade itself is a plain full build: load_hot_for, mark_cold, the floors. fnprof is out of the tree entirely.
  3. build_stamp carries the mode ("-fast"): a fast build killed half-way is never resumed as a full one (the restart path builds fast whenever there is no library to play on, so it goes on as it began), and a full build never goes on from fast objects - the mixed library with a complete .id can't happen now.
  4. remove(lib) before rename - dropped; the rename replaces the old file in one atomic step and the .id lands after it, of the build that just finished.
  5. Jobs and memory - the upgrade and both builds use the same RAM formula (launcher.mm build_jobs); fast mode keeps the 1536/1792 MB floors of may_start_another and the mark_cold scan - with no hot list loaded nothing is marked cold, but the giant files are still found, and they build alone (Unit.alone), as on main.
  6. Play - never blocked: the upgrade is a background thread (GameBuildOpts.stopped, checked between units and before the link), stopped and joined by PLAY, the Later button, a game-files delete, or a setup starting - always resumable, never a failure.
  7. Text - the four new strings ("Upgrading the game code (X%)", "Later", the postponed note, the failure note) are in all 11 languages, without "-O1".

Where it touches

  • runtime/gamebuild.{c,h} - opts->fast, opts->keep_current, opts->stopped; the mode in the stamp; the floors and the giant-file scan kept in fast mode.
  • runtime/gamelib.{c,h} - the -fast id loads like any other; gamelib_fast_present().
  • runtime/ui/launcher.mm - the background upgrade (offer / thread / postpone), the status line's percent and Later, the fast choice on each setup path.
  • runtime/ui/i18n_*.inc - the four strings, all 11 languages.

Measurements

On the device this was built for (Helio G95, 8 GB): the first import's game-code build was ~90 minutes at -O1 (2 compilers, the memory formula's answer there); at -O0 it is minutes, playable at once, and the -O1 rebuild finishes beside the menu afterwards. The -O0 vs -O1 difference of the generated code itself is ~10-15% of frame time on this SoC: the fast library is playable, the full one is worth having. A timed fast-vs-full comparison against main's current hot/cold build, from the same phone, I'll post in the comments.

…sion

The first setup on a phone builds the game code in minutes instead of the
best part of an hour: everything at -O0, the cold-function scan skipped,
memory floors halved (512/768 MB) for the same avail_mb check. The library
it leaves is playable but slower, so the first play session records the
functions the player's own game entered (fnprof, the project's own
profiler), and the next start screen rebuilds the game code at -O1 with
those functions hot and the rest optnone, in the background.

The four things that made the old version unsafe, as asked (PR mamcjr#417
review):

- A distinct marker: a fast library's id is "<GAMELIB_ID>-fast", in the .id
  file and the tables it exports (gamelib_is_fast). It is what says an
  upgrade is pending, so the state survives restarts; with the same id a
  fast library would look finished and never be rebuilt.
- The upgrade keeps the current library (GameBuildOpts.keep_current): the
  old build removed it and its .id first - a failed build, or Play during
  it, left no game code. The new library is swapped in by the final rename,
  whole; until then the one that loaded is what runs.
- The state is on disk, not in memory: the marker plus the session's
  fnprof file (rewritten every 1800 frames while playing, so a session
  stopped short keeps its counts). An app closed mid-session or
  mid-upgrade picks up where it was - record again, or rebuild again.
- Play is blocked while the upgrade goes (check_game_files: "Upgrading
  the game code"), and the compiler count stays what the memory formula
  gives (jobs/2 for the background build) - no raised cap.

An update over the same files (a new GAMELIB_ID) still builds the full
library as before; the desktop is unchanged. LARECOMP_NO_UPGRADE=1 keeps
the fast library.
@mamcjr

mamcjr commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks for redoing it. A few blockers still:

  1. It doesn't compile: extern std::atomic<bool> g_upgrade_busy and then static (launcher.mm ~230 / ~479).
  2. The upgrade never runs: gamelib_is_fast() is false on the first start-screen pass because gamelib_load only runs in boot(), after launcher_run. The session list is found only through LARECOMP_FNPROF_OUT in the process that pressed Play, and fnprof_frame latches its static from early. New players would stay on -O0. Upgrading from the hot list the app already ships (load_hot_for) would avoid fnprof altogether; fnprof also costs a hash probe and an atomic per function entry, and a hitch every 1800 frames.
  3. build_stamp doesn't include fast mode, so a fast build killed half-way is resumed as a full one and its .id says complete.
  4. remove(lib) before rename (gamebuild.c ~868): a kill between them leaves no library; rename alone replaces it atomically.
  5. Jobs: the upgrade uses cores/2, not the RAM formula, and fast mode lowers the memory floors and skips mark_cold, so giant files no longer build alone. That brings back the low-memory kills on 4 GB phones (The recompiling process issues #25/Crashed at 81% Snapdragon 888 #47/poco x7 pro 8/256GB #51).
  6. Play blocked: sending the start screen to SETUP for a build of tens of minutes, with no cancel, isn't good; it should be a background build the player can postpone.
  7. Text: the new strings need all 11 languages, without "-O1".

A build-time comparison against main's current hot/cold build would also help.

… postponeable

fnprof is gone - the session list was found only through LARECOMP_FNPROF_OUT in
the process that pressed Play, and fnprof_frame latches its static from early, so
new players stayed on -O0; it also cost a hash probe and an atomic per function
entry, and a hitch every 1800 frames. The upgrade builds from the app's own
shipped hot list instead (load_hot_for), like any full build. It runs beside the
menu now - PLAY stays on - with the percent and a Later button on the status
line: PLAY and Later both stop it (GameBuildOpts.stopped), resumable from the
work folder, and the thread is joined before the window is handed to the game.

The extern/static mismatch of g_upgrade_busy is gone with it. The build stamp
carries the mode, so a fast build killed half-way is neither resumed as a full
one nor marked complete. remove(lib) before the final rename is dropped: the
rename replaces it in one step. The upgrade uses the RAM formula for its
compilers like both other builds, and fast mode keeps the memory floors and the
giant-file scan (mark_cold: Unit.alone), so giant files build alone on 4 GB
phones as on main. The four new strings are in all 11 languages, without -O1.
@maxi000q

maxi000q commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you, all seven are real, and the "never runs" one is embarrassing: I tested the upgrade by playing first and coming back, which is exactly the case that works, and never once tried a fresh install straight to the start screen. Pushed as 581ac62; the shape of it changed quite a bit:

  1. The compile error is gone with g_upgrade_busy itself, see 6.
  2. The upgrade never runs: fnprof is out of the tree entirely (the env block at PLAY, session_hot_list, the LARECOMP_GAMEBUILD_HOT override, all of it). The trigger is the marker on disk now: gamelib_fast_present (gamelib.c) reads the -fast id without loading anything, so it's true on the first start-screen pass, before boot() ever loads the library. The upgrade itself is a plain full build load_hot_for with the app's shipped list, mark_cold, everything, so new players get it the first time they stand at the menu, and the per-entry hash probe, the atomic and the every-1800-frames hitch are simply gone.
  3. build_stamp carries the mode ("-fast" in it), and the restart path that a killed first build lands on builds fast whenever there is no library to play on, so it goes on as it began, and a full build can never pick up -O0 objects or leave a .id that says complete over them.
  4. remove(lib) before the rename is dropped; the rename replaces it in one step, as you said.
  5. Jobs and memory: one build_jobs (the RAM formula) for both builds and the upgrade, and fast mode keeps the 1536/1792 MB floors and the mark_cold scan: with no hot list loaded nothing gets marked cold, but the giant files are found and build alone again (Unit.alone). The 4 GB phones get main's behaviour back.
  6. Play is never blocked now. The upgrade runs beside the menu: the status line shows Upgrading the game code (X%) with a Later button beside it, and both Later and PLAY stop it (GameBuildOpts.stopped), checked between units and before the link; the compilers are ended, the work folder stays, the thread is joined before the window is handed over, and the next title screen (a "Title screen" reboot too) goes on from where it stopped. A setup starting or the game files being deleted stops it the same way.
  7. Text: four new strings (Upgrading the game code (X%), Later, a postponed note, a failure note), all in the 11 languages, none saying "-O1".

@mamcjr

mamcjr commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks, this is much closer: fnprof gone, the hot list, build_stamp, rename, one job formula, mark_cold, the "Later" button and the 11 languages are all right. Three things left:

  1. It doesn't compile: launcher.mm (482, 488, 530, 2863) calls SDL_GetTicks64(), which SDL3 renamed to SDL_GetTicks().
  2. The upgrade runs again after a return to the title screen: gamelib.c:132 returns the cached g_fast once the tables are loaded, and gamelib_load runs once per process. If the upgrade finishes during a later title screen, every following one in that process still sees "fast" and starts a full rebuild from scratch (.gamebuild was already deleted). Please check the marker on disk each time.
  3. A freeze while linking: pressing PLAY or Later during the link makes upgrade_postpone call pthread_join on the UI thread (launcher.mm:527) and wait for the whole link (minutes), and Android may show "app not responding". The link needs to be stoppable, or the join shouldn't block the UI.

Minor: the "postponed" note says it resumes when "the app is opened", but it resumes at the next title screen. And the build-time comparison against main is still welcome.

…ch time, a stoppable link

- launcher.mm called SDL_GetTicks64(), which SDL3 renamed to SDL_GetTicks(): four
  call sites and a comment, now the name the rest of the app uses.
- gamelib_fast_present() returned the cached g_fast once the tables stood loaded,
  so after an upgrade finished during a later title screen every following one
  saw "fast" and started a full rebuild from scratch (.gamebuild already gone).
  The marker on disk is read every call now; g_fast is out entirely, the library
  that stands loaded never answers for what is on the disk.
- PLAY or Later while the link ran made upgrade_postpone() join the upgrade
  thread on the UI thread and wait the link out: minutes of ANR. The link is
  stoppable now (wait_for checks GameBuildOpts.stopped): the linker is ended at
  once, the objects stay, the next title screen links again from them, and the
  join is over in a moment. A stop there is "postponed", not a failure.
- The postponed note said the upgrade goes on "the next time the app is opened";
  it goes on at the next title screen. All 11 languages.
@maxi000q

maxi000q commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Thank you, all three are real and all three are in, pushed as f587227:

  1. The compile error: SDL_GetTicks64() is SDL_GetTicks() now, the four call sites and the comment beside them. Sloppy of me, the rest of the app has been calling the SDL3 name all along.
  2. The cached g_fast: the g_tables shortcut is gone, and g_fast with it, the whole cache. gamelib_fast_present reads the marker on disk on every call now, so a title screen that comes up after the upgrade finished sees the full library's .id and offers nothing, in the same process or the next one.
  3. The freeze while linking: the link is stoppable now. wait_for takes the opts and asks GameBuildOpts.stopped while it polls, and a stop ends the linker at once: the objects stay, the next title screen links again from them, and the join in upgrade_postpone is over in a moment instead of the link's minutes, so no app-not-responding. A stop there comes back as "postponed", not a failure, same as a stop between units.

The note says "it resumes at the next title screen" now, in English and in all 11 translations (each in its own word for the title screen, the one the Quit menu already uses).

@mamcjr mamcjr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing all the earlier points in f587227. Before deciding whether this should be the default, my concerns:

  1. It's the default on every Android phone, the POCO included, on the first setup and after every update that changes GAMELIB_ID. There's no setting or variable to ask for the full build directly (LARECOMP_NO_UPGRADE only stops the upgrade). On a fast phone the full build takes about 10-15 minutes, so a player there trades that for several sessions on an -O0 library (zig's -O1 is clang's -O2, so the gap is large).
  2. The upgrade only moves while the player stays at the title screen, and each resume reruns the generator from scratch (the stop kills the generators), which is minutes before any unit compiles. A player who taps PLAY within a minute stays on -O0 indefinitely. After each release, CPU-bound frames would drop on mid-range phones until then.
  3. The build-time comparison against main's current hot/cold build was asked for twice; the ~90 min at -O1 on the G95 isn't tied to a main build. Could you post both on the same phone?

Would you consider the fast build only below a RAM or core threshold (where the full build really takes an hour), or as a choice at setup? The thread safety looks right to me (g_upg_lock across pthread_join, the stoppable link).

@mamcjr

mamcjr commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thanks for keeping at this. One new bug, and the points from 11-24 are still open:

  • Bug: launcher.mm:449 static std::atomic<float> g_upgrade_frac; starts at 0.0f, not -1. The status line (2833-2841) shows the upgrade whenever it's ≥ 0, so every start screen with a normal full library shows "Upgrading the game code (0%)" and a Later button that does nothing, instead of "Game files ready.". Fix: static std::atomic<float> g_upgrade_frac{-1};, like g_build_frac{-1} at line 367. (Minor: upgrade_postpone sets "postponed" even when the upgrade already finished.)
  • Still open from 11-24:
    1. Fast is the default on every phone and after every update, with no way to force the full build. Please make it a choice (or only below a RAM / core threshold) plus a setting or env that forces the full build.
    2. The upgrade only moves while the player stays at the title screen, and each resume reruns the generator.
    3. The timed comparison (fast vs full vs main's hot/cold build, same phone) is still missing, and so is a real -O0 vs -O1 fps measurement on a CPU-bound scene: main's hot functions are at -O2, and fast mode puts them at -O0 too, so "10-15%" is likely low.

The zig -O0 = Debug point is fine: -O0 comes after GAMELIB_CFLAGS, whose -fno-sanitize=undefined still wins, and the mode is in the stamp.

Note: today's 0.5.1 changed GAMELIB_ID (tools/patches.py), so please rebase before measuring.

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