Skip to content

Speed up cold start and day switching - #122

Merged
danielchalmers merged 7 commits into
mainfrom
home-notes-load
Nov 23, 2026
Merged

danielchalmers merged 7 commits into
mainfrom
home-notes-load

Conversation

@danielchalmers

@danielchalmers danielchalmers commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Three changes to what a user waits for: the database is ready sooner at launch, it no longer holds up the window while it gets ready, and the home page no longer loads every note in the journal.

This started as a benchmark of the whole app. Taps, toggles, pickers, typing and dialogs already respond within one frame, and pages open in 45 to 265 ms, so cold start and day switching were the two places worth changing.

Where cold start went on main, with the Release APK on the Android 17 emulator (medians):

Phase ms
Runtime start 130
CreateMauiApp 1,050
MAUI window, web view and Blazor boot 480
Home page day load 270
First render 270

Almost all of CreateMauiApp was EF Core warming up: about 400 ms building the model and about 350 ms compiling the first query. The migration check and category seeding themselves were only about 100 ms of work.

Database prep in the background

Migrating and seeding used to run inside CreateMauiApp, before the window existed. That work now starts on a worker thread as soon as the app is built, and the window and web view come up while it runs. Routes waits on AppDbSeeder.Ready and renders the same splash markup as index.html until it completes, so the screen doesn't change and no page can query the database early. Only pages create a DbContext, so holding the router back covers everything.

If the prep fails, the app now shows its error bar over the splash and logs the reason, where it used to crash at launch (throwing from the prep on Windows shows this). The trade is that a failed migration no longer shows up as a crash in Play vitals, but a slow migration no longer blocks the main thread either.

Adding the root component late from MainPage would have left the Blazor side untouched, but BlazorWebViewHandler throws on a late add in MAUI 11.0.10.

The database work takes longer once it shares the CPU with the window coming up (1.19 s on main, 1.51 s in the background), and the home page still ends up waiting for it, so making the prep itself shorter was worth doing as well.

A shorter prep

Building the EF model from conventions was the largest single cost, so the model is now generated ahead of time into Data/CompiledModels and EF picks it up through the assembly attribute. The model build drops from 396 ms to 72 ms. On its own that only moved home content from 2.14 s to 1.99 s, because EF defers some setup that then lands in the next thing to need it: reading the migration history went from 33 ms to 196 ms.

So the newest applied migration is now read with one plain query first, and EF is only asked for pending migrations when that isn't the newest one the app has. Nothing is remembered between launches, so a new, older or replaced database still goes down the normal path, and if the history table can't be read it falls back to EF's own check. The one case it would miss is an older migration that is unapplied while a newer one is applied, which only happens with out-of-order migrations during development.

There's no schema change in this PR (dotnet ef migrations has-pending-model-changes reports none), so updating from the current release takes the fast path. For the slow path, copies of the benchmark journal were rolled back to each of the two older schemas and launched with this build on the emulator. Both were migrated on launch, the home page opened, and all 2,348 days, 19,174 points and 16 categories came through with nothing missing or changed.

About 210 ms of deferred setup still moves into the first query during seeding. It runs on the worker thread, and the net is still well ahead.

The compiled model has to be regenerated when an entity or OnModelCreating changes. A test compares it with a freshly built model and fails with the command to run:

dotnet ef dbcontext optimize --project JournalApp --framework net11.0 --output-dir Data/CompiledModels --namespace JournalApp.CompiledModels

That needs Microsoft.EntityFrameworkCore.Design, which is referenced only by the plain net11.0 target so it doesn't ship in the app, and a design-time factory that gives the tooling the same options as the app. The factory replaces the "uncomment to enable tooling" line in OnConfiguring, so adding a migration works the same way. An EF Core major version bump will probably need a regeneration too.

The home page loading every note

The home page was pulling every note into its DbContext. GetMissingPoints decides whether to add the welcome note by reading category.Points.Count, and with lazy-loading proxies that loads the whole Notes category. It only happens in Release, because Debug passes a Random and skips that branch, which is why it never showed up in development or in the tests. New note did the same thing through note.Category.Points.Add. The check now looks at the category's collection entry and falls back to an Any() query, and a new note is added through db.Points. Soft-deleted notes still count as existing notes, same as before.

Every save on the home page runs change detection over whatever the context tracks, so with 2,354 notes tracked each day switch got slower, and the second one was slower than the first.

The first commit got that check wrong: it compared categories while enumerating tracked points, which lazy-loaded a category mid-enumeration and threw whenever the day being opened already had points. The Windows Release build caught it, and the second commit fixes it with a test that reproduces it.

Results

Cold start on a journal with 2,348 days and 2,354 notes. All three builds ran on the same emulator boot in alternating rounds, 20 cold starts each, medians from process start:

main background prep only this PR
Native splash gone 1.85 s 0.77 s 0.75 s
Home content rendered 2.73 s 2.37 s 1.97 s
Preparing and seeding the database 1.19 s 1.51 s 1.15 s
Day load during startup 301 ms 184 ms 184 ms
Points tracked after opening the app 2,380 26 26

The emulator ran about 25% slower than usual on that boot for every build (main measured 2.19 s the day before), so read the columns against each other.

Day switching right after launch, from tap to the frame after the last DOM change:

main this PR
First previous-day tap 211 ms 61 ms
Second previous-day tap 377 ms 27 ms
Back to home from Calendar 98 ms 52 ms

These two columns come from different emulator boots, and the one used for this PR was the slower of the two. On the same boot, the compiled model made no difference to day switching.

For a typical journal: the startup changes don't depend on how much is in it, so expect about a quarter off the time to home content, with the splash handing over roughly a second sooner. The day switching gain only shows up with thousands of notes. A small journal wasn't run on the emulator with the final build.

On a real phone, a Galaxy S23 on Android 17, with arm64 Release builds of main and this PR installed side by side under separate application ids, each with a new, nearly empty journal (8 cold starts each, medians from process start):

main this PR
Native splash gone 1.47 s 0.53 s
Home content rendered 2.03 s 1.69 s
Preparing and seeding the database 0.95 s 0.98 s

The ranges were tight (1.98 to 2.08 s for main, 1.65 to 1.92 s for this PR), and an earlier run of 10 cold starts of this PR on its own gave 1.67 s. That is one round per build, main first; a second round stopped when the phone's screen turned off.

So on the phone the time to content drops by about 340 ms, or 17%, and the splash hands over almost a second sooner. The database work takes about as long as on main there, so the phone numbers show the background prep paying off but not how much the compiled model and migration check add on top. On the emulator the background-only build spent 1.51 s on that work and this PR 1.15 s, but a background-only build hasn't been run on the phone to see whether the same holds. These were fresh installs, not upgrades of a real journal.

On Windows the Release build goes from 2.06 s to about 1.6 s to home content, all of it from the background prep. The compiled model makes no measurable difference there, because the Windows build isn't precompiled and the generated code still has to be jitted.

A fresh install on the emulator also migrates, seeds and opens, and the Debug build opens with the sample days seeding in the background.

Measured and left out:

  • Seeding the categories with one query and one save: the seeding phase took 432 ms against 421 ms, because the first query's compile cost dominates either way.
  • Dropping lazy-loading proxies: about 50 ms, not worth the refactor.
  • TieredPGO=false: within run-to-run noise.
  • Trends renders each chart three times over about 330 ms when it opens. The sampled geometry didn't change between renders, so it's wasted work more than visible jank, and it's untouched here.

New tests cover the splash staying up until the database is ready, the home page opening straight away when it already is, a failed prep reaching the error handling without opening a page, the compiled model matching the entities, the design-time factory matching the app's options, migrations being applied to a new database and to one that is behind without losing its data, an up-to-date database being left alone, the welcome note check not tracking existing notes, the check working on a day that already has points, saved and soft-deleted notes suppressing the welcome note, and the New note flow.

The welcome note check read category.Points.Count, which lazy-loaded all of the Notes category's points into the home page's context, and New note did the same through note.Category.Points. Both now ask the database instead, so the context only tracks the day being shown.
Comparing each tracked point's category while enumerating them lazy-loaded categories mid-enumeration, which threw "Collection was modified" in Release builds whenever the day being opened already had points. The check now reads the category's own collection entry and falls back to a query, so nothing is loaded along the way.
Migrating and seeding took about half of startup and ran before the window existed. They now run on a worker thread while the window and web view come up, and the root component keeps the splash on screen until the database is ready, so no page can query it early.
@danielchalmers danielchalmers changed the title Stop loading every note when opening a day Speed up cold start and day switching Nov 23, 2026
Building the model from conventions was the largest single cost in getting the database ready at launch. The generated model in Data/CompiledModels replaces that work, and EF finds it through the assembly attribute.

The model has to be regenerated whenever an entity or OnModelCreating changes, and a test fails with the command to run when it is out of date. The design-time factory gives the tooling the same options as the app, and the design package is only referenced by the plain net11.0 target so it doesn't ship.
Asking EF for pending migrations sets up its whole migration pipeline, which with the compiled model became the slowest part of preparing the database. The newest applied migration is now read with one plain query first, and EF is only asked when it isn't the newest one the app has.
@danielchalmers
danielchalmers merged commit 023a3b9 into main Nov 23, 2026
1 check passed
@danielchalmers
danielchalmers deleted the home-notes-load branch November 23, 2026 14:45
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.

1 participant