A profile switch runs one full-library query per remembered browse node #37

Closed
opened 2026-09-18 08:47:46 +02:00 by alexander · 0 comments
Owner

Migrated from GitHub issue #129. Forgejo issue: #37. GitHub source content synchronized on 2026-09-19. Migrated issue references below use Forgejo numbers where an equivalent issue exists; historical references without a Forgejo counterpart remain explicit GitHub links.

Found by review on 67a14fb. Not reported from a car, and not a blocker for GitHub #78 — but GitHub #78 makes it materially more likely to be hit, so it is worth recording now rather than after somebody notices a slow account switch.

The mechanism

AutoLibrary.remember() records every series/<id> and author/<id> node the browse tree has ever handed a head unit, into emittedNodes. The set is never cleared, deliberately: an id dropped from it is a car still showing the previous profile's books, which is product priority 4. That part is correct and should stay.

PlaybackService.observeBrowseTreeInvalidation() then does this on every profile switch:

auto.browsableParents().forEach { parentId ->
    current.notifyChildrenChanged(parentId, auto.children(parentId, nowPlaying()).size, null)
}

It calls children() purely to obtain a count. And AutoLibrary.books() is:

private suspend fun books(): List<Book> {
    val profileId = profiles.activeProfileId() ?: return emptyList()
    return library.observeAccessibleBooks(profileId).first()
}

No caching between calls. observeAccessibleBooks returns BookWithRelations — authors, series memberships, tracks and chapters — for every visible book.

So: one full accessible-library read per remembered node, per profile switch. A library with a thousand authors turns one account switch into a thousand full-library queries, which also delays the very notifications meant to clear the previous profile's cached titles.

Why GitHub #78 makes it worse

Before GitHub #78, Series and Authors lived one level inside Library, so a driver had to go Library → Series → a series to grow emittedNodes at all. GitHub #78 promotes both to the browse root at the owner's request, so drilling into them is now a top-level action and the set will fill much faster in ordinary use.

The change did not create the defect and does not need to fix it, but it removes the friction that was keeping it small.

What a fix looks like

The count is the only thing needed, so the expensive part is avoidable:

  1. One snapshot per invalidation sweep. Read the accessible-book list once, pass it through the count calculation for every remembered parent. This is what roadmap item BW-AUTO-01 ("one library snapshot per sweep") already describes.
  2. Or do not compute counts at all. notifyChildrenChanged's count is advisory — the browser re-queries on notification. Verify that against Media3 before relying on it; if a plausible count suffices, the whole query disappears.

Option 1 is the safe one. Option 2 is cheaper still but needs the Media3 behaviour checked rather than assumed — this branch has a poor record with assumed platform behaviour.

Acceptance

  • A profile switch with N remembered dynamic nodes performs O(1) library reads, not O(N).
  • Every remembered parent is still notified — no id is dropped to save work, because a missed id is another profile's books on a car screen.
  • A test that fails if the per-node query returns.
  • AutoLibrary.remember() / browsableParents(), PlaybackService.observeBrowseTreeInvalidation()
  • Roadmap BW-AUTO-01
  • #10 (browse invalidation/snapshot follow-up) — likely the same fix
> Migrated from [GitHub issue #129](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/issues/129). Forgejo issue: #37. GitHub source content synchronized on 2026-09-19. Migrated issue references below use Forgejo numbers where an equivalent issue exists; historical references without a Forgejo counterpart remain explicit GitHub links. Found by review on `67a14fb`. Not reported from a car, and **not a blocker for [GitHub #78](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/issues/78)** — but [GitHub #78](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/issues/78) makes it materially more likely to be hit, so it is worth recording now rather than after somebody notices a slow account switch. ## The mechanism `AutoLibrary.remember()` records every `series/<id>` and `author/<id>` node the browse tree has ever handed a head unit, into `emittedNodes`. The set is **never cleared**, deliberately: an id dropped from it is a car still showing the previous profile's books, which is product priority 4. That part is correct and should stay. `PlaybackService.observeBrowseTreeInvalidation()` then does this on every profile switch: ```kotlin auto.browsableParents().forEach { parentId -> current.notifyChildrenChanged(parentId, auto.children(parentId, nowPlaying()).size, null) } ``` It calls `children()` purely to obtain a **count**. And `AutoLibrary.books()` is: ```kotlin private suspend fun books(): List<Book> { val profileId = profiles.activeProfileId() ?: return emptyList() return library.observeAccessibleBooks(profileId).first() } ``` No caching between calls. `observeAccessibleBooks` returns `BookWithRelations` — authors, series memberships, tracks *and* chapters — for every visible book. So: **one full accessible-library read per remembered node, per profile switch.** A library with a thousand authors turns one account switch into a thousand full-library queries, which also delays the very notifications meant to clear the previous profile's cached titles. ## Why [GitHub #78](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/issues/78) makes it worse Before [GitHub #78](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/issues/78), Series and Authors lived one level inside Library, so a driver had to go Library → Series → *a series* to grow `emittedNodes` at all. [GitHub #78](https://github.com/AlexanderWilhelmsenBerg/Audiobookshelf-Manager-Claude/issues/78) promotes both to the browse root at the owner's request, so drilling into them is now a top-level action and the set will fill much faster in ordinary use. The change did not create the defect and does not need to fix it, but it removes the friction that was keeping it small. ## What a fix looks like The count is the only thing needed, so the expensive part is avoidable: 1. **One snapshot per invalidation sweep.** Read the accessible-book list once, pass it through the count calculation for every remembered parent. This is what roadmap item BW-AUTO-01 ("one library snapshot per sweep") already describes. 2. **Or do not compute counts at all.** `notifyChildrenChanged`'s count is advisory — the browser re-queries on notification. Verify that against Media3 before relying on it; if a plausible count suffices, the whole query disappears. Option 1 is the safe one. Option 2 is cheaper still but needs the Media3 behaviour checked rather than assumed — this branch has a poor record with assumed platform behaviour. ## Acceptance - A profile switch with N remembered dynamic nodes performs O(1) library reads, not O(N). - Every remembered parent is still notified — no id is dropped to save work, because a missed id is another profile's books on a car screen. - A test that fails if the per-node query returns. ## Related - `AutoLibrary.remember()` / `browsableParents()`, `PlaybackService.observeBrowseTreeInvalidation()` - Roadmap BW-AUTO-01 - #10 (browse invalidation/snapshot follow-up) — likely the same fix
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
alexander/Audiobookshelf-Manager-Claude#37
No description provided.