Repository navigation
Speed up cold start and day switching - #122
Merged
Merged
Conversation
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
CreateMauiAppAlmost all of
CreateMauiAppwas 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.Routeswaits onAppDbSeeder.Readyand renders the same splash markup asindex.htmluntil 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
MainPagewould have left the Blazor side untouched, butBlazorWebViewHandlerthrows 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/CompiledModelsand 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-changesreports 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
OnModelCreatingchanges. A test compares it with a freshly built model and fails with the command to run:That needs
Microsoft.EntityFrameworkCore.Design, which is referenced only by the plainnet11.0target 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 inOnConfiguring, 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.
GetMissingPointsdecides whether to add the welcome note by readingcategory.Points.Count, and with lazy-loading proxies that loads the whole Notes category. It only happens in Release, because Debug passes aRandomand skips that branch, which is why it never showed up in development or in the tests. New note did the same thing throughnote.Category.Points.Add. The check now looks at the category's collection entry and falls back to anAny()query, and a new note is added throughdb.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:
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:
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):
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:
TieredPGO=false: within run-to-run noise.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.