Skip to content

Open and close the lyrics within 50 ms - #715

Merged
isaaclins merged 14 commits into
mainfrom
fix/674-lyrics-toggle
Nov 26, 2026
Merged

isaaclins merged 14 commits into
mainfrom
fix/674-lyrics-toggle

Conversation

@isaaclins

@isaaclins isaaclins commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Fixes #674. ⌥⌘L now opens or closes the lyrics within the 50 ms budget. Before this it took 115 ms to open and 190 ms to close.

How it looks

Nothing looks different. The lyrics and the page underneath end up exactly as before; they just show up sooner. Screenshot mode, after the change:

Dark Light
Lyrics open lyrics-open-dark.png lyrics-open-light.png
Lyrics closed again lyrics-closed-dark.png lyrics-closed-light.png

Times

scripts/latency-test.sh --only lyrics.toggle, Release build, my Mac mini M4, 60 Hz, 8 tries per run (4 opens, 4 closes), in ms with one frame included, load average 3-7:

median checked opening closing
main (from docs/latency.md) 114 123
this branch, run 1 36.7 47.6 35-52 32-38
this branch, run 2 38.3 39.6 39-40 29-38
this branch, run 3 33.7 35.8 33-39 28-36
this branch, run 4 39.1 44.2 38-44 30-52
this branch, run 5 37.4 38.4 36-38 37-40

Timed back to back with main at 0.8.73 on the last commit: this branch 39.8 / 39.5 ms median, 43.0 / 43.2 checked; main 145 / 150 median, 170 / 178 checked. Play/pause is the same on both (see the comments; its creep since 0.8.68 is #726).

The first open is as quick as the others, since the lyrics are already built by then. lyrics.toggle is off the known list in scripts/latency-baseline.json, its ci time there is 48 ms (from 547) after this branch's CI run measured 37-73 ms, and docs/latency.md has the new time.

What was slow

Profiles of the press-to-first-frame window showed the main thread busy the whole time, and very little of that was the lyrics themselves:

  • The whole window ran again on every toggle. Whether the lyrics were open was an @Published on LyricsOverlayController. PlaylistBrowserView held it as an @EnvironmentObject, and the scene host passed every change on to RootView. So each ⌥⌘L rebuilt the sidebar, the page under the lyrics with every song row on screen, the player bar and the menu bar.
  • Closing rebuilt the page from scratch. While the lyrics were open, the page was swapped for an empty view, so closing built it and all its rows again. That was about 38 ms of re-measuring row heights alone. A scroll-restore path existed only to put the rebuilt playlist back in place.
  • A new view graph on every open. The lyrics' focus container and its NSHostingView were created and added to the window each time.
  • The lyrics themselves were built on every open. Even with all of the above gone, opening took 51-72 ms: a Time Profiler trace of 40 opens shows the first frame almost all in NSHostingView.layout() building every lyric line, its text, the focus modifiers and the AppKit views inside.
  • The toolbar, the menus and the bar's Lyrics buttons cost about 30 ms of the first frame, and none of them are what you look at when the lyrics open.
  • Moving the keyboard reran the song list. Its @FocusState lived in TrackListView, so the lyrics taking the keyboard ran the list body and every row again.

The change

  • Whether the lyrics are open is its own small object, LyricsPresentationState. Only the lyrics layer and the page's fade observe it. The controller itself publishes nothing, and RootView no longer hears about it.
  • The page stays built under the lyrics. It fades out with them and comes back at once, still scrolled where it was. The scroll-restore path (pendingScrollRestoreTrackID, the first-visible-row tracking in TrackListView, the anchor helper) is gone.
  • The lyrics, with their container, are built a second after the window shows (or on the first ⌥⌘L, if sooner) and kept. Closed, the container is hidden, so it takes no clicks, keys or VoiceOver, and the lyrics are paused (lyricsAreShowing in the environment): their position TimelineView stops, and they load no lyrics or queue and take no keyboard focus. Opened again, they start on the current line and take the keyboard. With the lyrics closed the app uses the same CPU (about 2%) as before they were first opened.
  • The toolbar items, the window's name, the bar's Lyrics buttons, the menus, switching the hidden page off for the keyboard and VoiceOver, and the keyboard handoff into the lyrics now follow one frame after the lyrics, using a second LyricsPresentationState set right after Core Animation commits. They used to change in the same frame.
  • TrackListView keeps its focus state in a small modifier.

Tests

  • WindowResizeCostTests.testOpeningAndClosingTheLyricsReRendersNothingElse hosts the real browser on a 500-song playlist, then opens and closes the lyrics twice. It fails if the browser, sidebar, page column, page, player bar or any song row runs its body again. It also checks that the window's name and the toolbar still switch, that the lyrics are built before they first open, and that the same lyrics, not just their container, come back on every open and are hidden once closed. On the code before this change it fails on every toggle.
  • LyricsOverlayControllerTests.testTheFrameAroundTheLyricsFollowsOneFrameLater checks the toolbar state follows one frame later, and that an open and close within one frame doesn't swap the toolbar twice.
  • make test: 2559 tests, 0 failures. scripts/check-periphery.sh (periphery 2.21.2) reports only SpotiglassSettingsView.columnVisibility, which main reports too.

What I couldn't check

  • Moving through the lyrics with the arrow and page keys after reopening: the key presses I can send from a script didn't scroll them on this branch's first commit either, so I couldn't check it that way. Each open puts the keyboard focus back in the lyrics, as before.
  • Keeping the lyrics built costs a little memory, and the backdrop still loads each new song's artwork while they are closed. With them closed the app uses about 2% CPU, as before they were first opened.
  • I measured in screenshot mode only. The lyrics views are the same for Apple Music.
  • Search opens in about 45 ms: a page change announces only what changed #701 also touches LyricsOverlayController (it publishes only on a real change, which this does too) and the player bar. Whichever lands second needs a small merge.

@isaaclins isaaclins added bug Something isn't working performance Speed / latency / responsiveness priority:high Fix before next release labels Nov 25, 2026

@isaaclins isaaclins left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I ran it on my Mac mini (Release, scripts/latency-test.sh --only lyrics.toggle, load average around 4-5, so a fairly quiet Mac). Closing is well inside the budget, opening isn't yet, so lyrics.toggle still fails the gate:

run median checked opening closing
1 53.5 ms 68.2 ms 64-70 ms 37-43 ms
2 48.7 ms 52.8 ms 51-72 ms 34-47 ms

The 37-40 ms estimate for a quiet Mac holds for closing but not for opening. Everything else checks out: make build, make test (2559 tests, 0 failures) and the periphery scan (only SpotiglassSettingsView.columnVisibility, as on main).


var body: some View {
ZStack {
if isShowing {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Opening still builds the lyrics from nothing on every ⌥⌘L. In a Time Profiler trace of 40 opens, the first frame of an open is almost all NSHostingView.layout() building this view: the lazy stack of lines, their text, the focus modifiers and the AppKit views inside. That's what keeps opening at 50-70 ms.

Since the container is now kept, the lyrics inside it can be kept too: built once, invisible and paused while closed (no TimelineView ticks, no lyrics or queue loading, no keyboard focus), shown again on ⌥⌘L, and re-centred on the current line when they come back.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Fixed in f0080d4. The lyrics are now built a second after the window shows and kept. Closed, they are invisible and paused: their position TimelineView stops (lyricsAreShowing), they load no lyrics or queue and don't take the keyboard. Opening shows them again, back on the current line and with the keyboard in them.

Three runs of scripts/latency-test.sh --only lyrics.toggle on my Mac mini (Release, load average 5-7):

run median checked opening closing
1 36.7 ms 47.6 ms 35-52 ms 32-38 ms
2 38.3 ms 39.6 ms 39-40 ms 29-38 ms
3 33.7 ms 35.8 ms 33-39 ms 28-36 ms

The first open is 36-39 ms too, since the lyrics are already built. With the lyrics closed the app takes the same CPU (about 2%) as before they were ever opened. Open it's about 32%, all of it the lyrics playing.

testOpeningAndClosingTheLyricsReRendersNothingElse now also checks that the lyrics are built before they first open and that the same lyrics view comes back on every open. On the previous commit it fails on both.

Screenshot mode, dark, open, and closed again at the same place in the 1000-song playlist:

dark-open.png
dark-closed.png

Light:

light-open.png
light-closed.png

Comment thread docs/latency.md Outdated
| `sidebar.toggle` | ⌘S hides or shows the sidebar | 81 | 83 | #672 |
| `queue.toggle` | ⌥⌘Q opens or closes the queue | 72 | 77 | #673 |
| `lyrics.toggle` | ⌥⌘L opens or closes the lyrics | 114 | 123 | #674 |
| `lyrics.toggle` | ⌥⌘L opens or closes the lyrics | 46 | 50-52 | fixed in #674 (was 114 / 123)† |

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

This row needs the times measured on a quiet Mac once opening is fixed: 50-52 ms checked is over the budget it says is met.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Updated in f0080d4 with those three runs: 34-38 ms median, 36-48 ms checked.

@isaaclins

Copy link
Copy Markdown
Owner Author

Another look at the kept lyrics, fixed in fe9c827:

  • A right click while the lyrics close. Their timing-hold view only stood down once the fade had ended, so for 0.3 s after closing, a right click on a song under them started a hold instead of the song's menu. Holds now start only while the lyrics are open, and closing ends one in progress. testClosingLyricsLeaveARightClickToThePage covers it.
  • What closed lyrics still load. The comments said nothing; the blurred backdrop does still load each song's artwork, which keeps it ready for the next open. The comments say so now.
  • The keyboard after closing. I checked it in the Release build: with a song selected, opening and closing the lyrics leaves the keyboard in the song list, and Page Down scrolls it.

Two more runs of scripts/latency-test.sh --only lyrics.toggle on this commit: median 39.1 / 37.4 ms, checked 44.2 / 38.4 ms. make test: 2560 tests, 0 failures.

@isaaclins

Copy link
Copy Markdown
Owner Author

Another look, fixed in fb4610e:

  • Play and pause rebuilt the kept lyrics. Their position reader swapped a timeline for a still view whenever playback started or stopped, which built every lyric line again, now with the lyrics closed too. It is one timeline now, paused while the song is or the lyrics are closed. Timed back to back with main (0.8.73), play/pause is the same as there: Space 46.7 / 46.8 ms here and 47.0 / 44.8 ms on main, the button 46.1 / 47.9 and 46.7 / 44.9, Space with the 1000-song playlist 50.0 / 45.4 and 42.8 / 44.0. Those have all crept up from the 36-38 ms in docs/latency.md on main too, which is Play/pause has slowed to 45-47 ms, and with a 1000-song playlist open it goes over 50 ms #726.
  • Nothing tested what closed lyrics load. testClosedLyricsLoadNothingAndOpeningAgainLoadsTheSongNow changes the song with the lyrics closed and checks nothing is fetched, then opens them and checks they fetch the song playing then. Without the check in the lyrics' task it fails: both songs get fetched while closed. Where the keyboard goes on reopening isn't visible to a test (SwiftUI keeps that focus inside its hosting view); in the Release build each open puts it back into the lyrics, as before.
  • Four browser tests had no settings store. The lyrics, now built a second after the window shows, need it as they do in the app, and PlayPauseRenderScopeTests crashed when it ran longer than that. Those hosts now get the store the app always has.
  • The lyrics.toggle row in docs/latency.md now has single numbers like the others (37 / 40 ms, the middle of five runs), and the after-this-frame helper's comment says it can also run when a menu's run loop ends.

lyrics.toggle on this commit: median 39.8 / 39.5 ms, checked 43.0 / 43.2 ms (main: 145 / 150 ms, checked 170 / 178). make test: 2561 tests, 0 failures.

@isaaclins

Copy link
Copy Markdown
Owner Author

CI failed testOpeningAndClosingTheLyricsReRendersNothingElse on this branch's first commit and again after my rebase: opening the lyrics ran the player bar's body once. It didn't fail on my Mac, even in a full CI-style run (unsigned, with coverage).

Fixed in 32d7a3b: PlaybackControlsView held the lyrics' open state as an @Binding built from a getter. SwiftUI reads such a binding again whenever it updates the view, and its value flips on every ⌥⌘L, so any other update of the bar after a toggle ran the whole bar. The bar only writes that state, and its Lyrics buttons read it through their own observer, so it's a plain Binding value now. If the test fails again it lists everything else that re-rendered in that round.

The other CI failure, SettingsWindowChromeTests, fails on other branches too and is #714. Rebased on 0.8.74; make test: 2591 tests, 0 failures.

@isaaclins

Copy link
Copy Markdown
Owner Author

On how much room lyrics.toggle leaves under 50 ms: I profiled 40 more opens on this commit. The first frame of an open is now 20-29 ms of main-thread work, down from 35-54, and what's left is SwiftUI laying out the lyric lines on screen as they move to the current position, spread thin with nothing big left to take out. Unhiding the lyrics' container is about 3 ms of it, and that hiding is what keeps closed lyrics out of reach of clicks, keys and VoiceOver.

Every Mac run of scripts/latency-test.sh --only lyrics.toggle on this branch's last commits stayed under 50 ms checked (seven runs: 35.8-48.7 ms), and the checked times' median is 40 ms. Four of the seven also met the 40 ms bar #678 sets for moving an entry off the known list. CI's own reference for it is now 48 ms. If a later Mac run does land over 50, it'll fail loudly rather than drift.

@isaaclins

Copy link
Copy Markdown
Owner Author

CI caught the bar once more on the second open, and this time nothing else rendered with it: not its parent, so the player didn't change. Its one other input was the openSettings action from the environment, which the window passes down again when the keyboard moves into the lyrics. Fixed in 2b30793: only the reconnect button reads it now. While a test counts renders, the bar also prints what made it run, so if CI still catches one it'll say why.

Rebased on 0.8.79; make test: 2643 tests, 0 failures.

@isaaclins

Copy link
Copy Markdown
Owner Author

The pre-merge CI run on 40a55a6 is green: unit tests, interaction latency (Release) and static audits, https://github.com/isaaclins/wax/actions/runs/45750825086. The same branch merged with today's main (0.8.86) passes make test on my Mac: 2667 tests, 0 failures. The frame after ⌥⌘L is #752, and the after-this-frame helpers that now exist three times are #759.

@isaaclins
isaaclins force-pushed the fix/674-lyrics-toggle branch 3 times, most recently from 6fe218f to 03cb0ef Compare November 26, 2026 11:43
@isaaclins

Copy link
Copy Markdown
Owner Author

Rebased on 0.8.94. Two things came up with what main gained:

Also: building the lyrics ahead is now only on for the app's window (buildsLyricsAhead), so browser tests without a settings store no longer crash once the lyrics build; and #673's queue row in docs/latency.md is back. make test: 2716 tests, 0 failures. The frame after ⌥⌘L is #752, the duplicated after-this-frame helpers #759.

@isaaclins
isaaclins force-pushed the fix/674-lyrics-toggle branch 2 times, most recently from 99a9923 to b08445b Compare November 26, 2026 14:34
⌥⌘L took about 115 ms to open the lyrics and 190 ms to close them (#674).
Every toggle ran the whole browser again: the sidebar, the page under the
lyrics with every song row on screen, the player bar and the menu bar.
Closing was slowest because the page had been swapped for an empty view and
was built again from scratch.

- Whether the lyrics are open is its own small published object. Only the
  lyrics layer, the toolbar, the window's name, the bar's Lyrics buttons and
  the menus observe it; the browser and the scene host no longer re-render.
- The page stays built under the lyrics, faded out and out of reach of the
  keyboard and VoiceOver, so closing shows it again at once where it was.
  The scroll-restore path that put a rebuilt playlist back in place is gone.
- The lyrics' container and hosting view are built once and kept, hidden
  while closed, instead of a new view graph on every open.
- The toolbar, the window's name, the bar's Lyrics buttons, the menus, the
  page's disabled state and the keyboard handoff follow one frame after the
  lyrics themselves, so they don't hold back the frame that answers the key.
- The song list keeps its focus state in a small modifier, so a move of the
  keyboard no longer runs the list and its rows again.
…ws them

Opening still took 51-72 ms on a quiet Mac: each ⌥⌘L built the lyrics from nothing, every line,
its text and the views inside, and that was most of the first frame. The lyrics are now built a
second after the window shows and kept. Closed, they are invisible and paused: their position
timeline stops, they load no lyrics or queue and take no keyboard focus. Opened again, they start
on the current line and take the keyboard, as before. Opening now takes 33-52 ms, closing 28-38.
The kept lyrics hid their timing-hold view only once their fade had ended, so for 0.3 s after
closing, a right click on a song under them started a timing hold instead of the song's menu.
Holds now start only while the lyrics are open, and closing them ends one in progress. The
comments on closed lyrics now say what they still load: the backdrop's artwork.
… load

The lyrics' position reader swapped a timeline for a still view whenever playback started or
stopped, which built every line again. With the lyrics now kept while closed, that happened on
every play and pause even with them closed. It is one timeline now, paused while the song is or
the lyrics are closed.

A new test checks that closed lyrics fetch nothing when the song changes and that opening them
loads the song playing then. Four browser tests now get the settings store the app always has:
the lyrics built a second after the window shows need it, and one of them crashed without it.
The bar held the lyrics' open state as an @binding built from a getter. SwiftUI reads such a
binding again whenever it updates the view, and its value changes on every ⌥⌘L, so any other
update of the bar after a toggle ran the whole bar again. On CI that showed up as one bar render
in testOpeningAndClosingTheLyricsReRendersNothingElse. The bar only writes the state; its Lyrics
buttons read it through their own observer. The test now also says what else re-rendered.
The Interaction latency job on this branch measured 37-73 ms for ⌥⌘L, median 48. With the old
547 ms reference CI would only have caught it getting slower than about a second.
CI still caught one render of the bar when the lyrics opened a second time, with nothing else
rendering: not its parent, so not the player. The bar's one other input was the openSettings
action from the environment, which the window passes down again when the keyboard moves into the
lyrics. Only the reconnect button needs it, so it reads it itself now.

While a test counts renders, the bar now prints what made it run, so the next render CI catches
says why.
The kept lyrics' timing-hold monitor still looked at every scroll and Esc in the window while they
were closed. Both only matter during a hold, and closing the lyrics ends one, so the monitor now
lets them pass untouched while the lyrics are closed.
main removed AppKitTestSupport.settleRunLoopOnly() (#746). This test is synchronous, so the plain
settle() is the same barrier.
…e note

Building the lyrics a second after the window shows needs the settings store the app always
provides. Browser tests that host the window without one crashed once the lyrics built
(SidebarToggleRenderScopeTests on CI), and each new such test would again. Building ahead is now
the controller's buildsLyricsAhead, on for the app's window and for the test that checks it;
everywhere else the lyrics build on their first open, as before.

docs/latency.md: #673's queue row and note were overwritten when both used †; the lyrics note is ‡.
…stay built

Since #599 a song whose lyrics failed for want of a connection waits for it and loads again when
the network comes back; leaving the window ended that wait (onDisappear). The kept lyrics never
leave it, so after closing them the wait went on and the song loaded behind them. Closing now
ends it too. testClosingLyricsStopsTheirWaitForTheConnection fails without the change.
#701's test watched LyricsOverlayController.isPresented, which this branch moved into
presentation (what the lyrics observe) and chromePresentation (what the menus follow).
#701 compared a copy of it taken when the browser ran. The browser no longer runs when the lyrics
open or close, so that copy went stale and a later page change rebuilt the bar for it. The bar's
Lyrics buttons follow the lyrics through lyricsPresentation.
@isaaclins
isaaclins force-pushed the fix/674-lyrics-toggle branch from b08445b to b5db122 Compare November 26, 2026 15:04
@isaaclins
isaaclins merged commit b152ae1 into main Nov 26, 2026
6 checks passed
@isaaclins
isaaclins deleted the fix/674-lyrics-toggle branch November 26, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working performance Speed / latency / responsiveness priority:high Fix before next release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Opening or closing the lyrics with ⌥⌘L takes about 140 ms

1 participant