From 2321468a67692400d47898cd6637af419bf7602c Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Thu, 16 Jul 2026 18:37:27 -0400 Subject: [PATCH 1/2] perf(roms): make full rom id index opt-out on GET /api/roms The home rails ("recently added" / "continue playing") request 15 cards but the endpoint always computed rom_id_index, the complete ordered list of every matching rom id that backs gallery virtual scroll. On large libraries this ran a full-table scan and shipped hundreds of KB of ids to render 15 cards. Add a with_rom_id_index query param (defaults to true, mirroring the existing with_char_index / with_filter_values flags). When false, skip building the index and serve the page directly via offset/limit off the sort index, with a lightweight count for total. The home rail fetches pass false. Fixes #3770 Generated-By: PostHog Code Task-Id: 45dbad45-bd7c-4cf3-8e8f-105f174f4386 --- backend/endpoints/roms/__init__.py | 52 +++++++++++++++++------- backend/handler/database/roms_handler.py | 20 +++++++++ backend/tests/endpoints/roms/test_rom.py | 25 ++++++++++++ frontend/src/services/api/rom.ts | 2 + frontend/src/services/cache/api.ts | 4 ++ 5 files changed, 88 insertions(+), 15 deletions(-) diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index 4df859e1a..5d59e5080 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -307,6 +307,16 @@ def get_roms( with_filter_values: Annotated[ bool, Query(description="Whether to return filter values.") ] = True, + with_rom_id_index: Annotated[ + bool, + Query( + description=( + "Whether to return the full ordered rom id index that backs" + " virtual scroll. Disable it for small rails that only need a" + " page, to skip the full-library scan." + ) + ), + ] = True, search_term: Annotated[ str | None, Query(description="Search term to filter roms."), @@ -729,14 +739,18 @@ def get_roms( filter_values = RomFiltersDict(**query_filters) # The full ordered id list backs virtual scroll, so it's computed over the - # whole result set on every request. Memoise the unscoped library scan (same - # key scheme as the other sidecars); scoped/searched sets stay live. - rom_id_index_cache_key = build_unscoped_sidecar_cache_key( - request.user.id, order_by, order_dir, group_by_meta_id, is_unscoped - ) - rom_id_index = db_rom_handler.get_rom_id_index( - query=query, cache_key=rom_id_index_cache_key - ) + # whole result set. Callers that only need a page (e.g. the home rails) opt + # out with with_rom_id_index=false and avoid the full-library scan. + rom_id_index: list[int] = [] + if with_rom_id_index: + # Memoise the unscoped library scan (same key scheme as the other + # sidecars); scoped/searched sets stay live. + rom_id_index_cache_key = build_unscoped_sidecar_cache_key( + request.user.id, order_by, order_dir, group_by_meta_id, is_unscoped + ) + rom_id_index = db_rom_handler.get_rom_id_index( + query=query, cache_key=rom_id_index_cache_key + ) # Hydrate the requested page and its additional data with sync_session.begin() as session: @@ -779,14 +793,22 @@ def get_roms( ] params = resolve_params() - total = len(rom_id_index) - page_ids = list(rom_id_index[params.offset : params.offset + params.limit]) - if page_ids: - page_rows = session.scalars(query.where(Rom.id.in_(page_ids))).all() - rows_by_id = {rom.id: rom for rom in page_rows} - page_items = [rows_by_id[i] for i in page_ids if i in rows_by_id] + if with_rom_id_index: + total = len(rom_id_index) + page_ids = list(rom_id_index[params.offset : params.offset + params.limit]) + if page_ids: + page_rows = session.scalars(query.where(Rom.id.in_(page_ids))).all() + rows_by_id = {rom.id: rom for rom in page_rows} + page_items = [rows_by_id[i] for i in page_ids if i in rows_by_id] + else: + page_items = [] else: - page_items = [] + # Let the database serve the page from the sort index instead of + # walking the whole primary key to build a full id list. + page_items = list( + session.scalars(query.offset(params.offset).limit(params.limit)).all() + ) + total = db_rom_handler.get_rom_count(query=query, session=session) return CustomLimitOffsetPage.create( _transform(page_items), diff --git a/backend/handler/database/roms_handler.py b/backend/handler/database/roms_handler.py index 04c9c62a0..5ae921925 100644 --- a/backend/handler/database/roms_handler.py +++ b/backend/handler/database/roms_handler.py @@ -1347,6 +1347,26 @@ class DBRomsHandler(DBBaseHandler): _store_versioned_cache(redis_key, version, ids) return ids + @begin_session + def get_rom_count( + self, + query: Query, + *, + session: Session = None, # type: ignore + ) -> int: + """Count matching roms without materialising their ids. + + For callers that need a page and its total but not the full ordered id + list (e.g. the home rails), so the database can serve the page from the + sort index instead of scanning the whole library. + """ + return ( + session.scalar( + select(func.count()).select_from(query.order_by(None).subquery()) + ) + or 0 + ) + @begin_session def get_roms_by_fs_name( self, diff --git a/backend/tests/endpoints/roms/test_rom.py b/backend/tests/endpoints/roms/test_rom.py index eb3f5f95b..dc456dbe0 100644 --- a/backend/tests/endpoints/roms/test_rom.py +++ b/backend/tests/endpoints/roms/test_rom.py @@ -148,6 +148,31 @@ def test_get_all_roms( assert items[0]["sibling_roms"] == [] +def test_get_roms_without_rom_id_index( + client: TestClient, access_token: str, rom: Rom, platform: Platform +): + response = client.get( + "/api/roms", + headers={"Authorization": f"Bearer {access_token}"}, + params={ + "platform_id": platform.id, + "limit": 15, + "with_rom_id_index": False, + }, + ) + assert response.status_code == status.HTTP_200_OK + + body = response.json() + + # The page and total stay correct, but the full id index is not built. + assert body["total"] == 1 + assert body["rom_id_index"] == [] + + items = body["items"] + assert len(items) == 1 + assert items[0]["id"] == rom.id + + def test_get_roms_filter_by_metadata_providers( client: TestClient, access_token: str, rom: Rom, platform: Platform ): diff --git a/frontend/src/services/api/rom.ts b/frontend/src/services/api/rom.ts index 119463b6b..75db86b96 100644 --- a/frontend/src/services/api/rom.ts +++ b/frontend/src/services/api/rom.ts @@ -355,6 +355,7 @@ async function getRecentRoms() { limit: RECENT_ROMS_LIMIT, with_char_index: false, with_filter_values: false, + with_rom_id_index: false, }, }); } @@ -367,6 +368,7 @@ async function getRecentPlayedRoms() { limit: RECENT_PLAYED_ROMS_LIMIT, with_char_index: false, with_filter_values: false, + with_rom_id_index: false, last_played: true, }, }); diff --git a/frontend/src/services/cache/api.ts b/frontend/src/services/cache/api.ts index 9807e1557..4b0655854 100644 --- a/frontend/src/services/cache/api.ts +++ b/frontend/src/services/cache/api.ts @@ -170,6 +170,7 @@ class CachedApiService { limit: 15, with_char_index: false, with_filter_values: false, + with_rom_id_index: false, }); return cacheService.request(config, onBackgroundUpdate); @@ -184,6 +185,7 @@ class CachedApiService { limit: 15, with_char_index: false, with_filter_values: false, + with_rom_id_index: false, last_played: true, }); @@ -202,6 +204,7 @@ class CachedApiService { limit: 15, with_char_index: false, with_filter_values: false, + with_rom_id_index: false, }); } @@ -212,6 +215,7 @@ class CachedApiService { limit: 15, with_char_index: false, with_filter_values: false, + with_rom_id_index: false, last_played: true, }); } From bc602972c7778a326308fc7c60376339b8e8f8dc Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Thu, 16 Jul 2026 21:00:57 -0400 Subject: [PATCH 2/2] cleanup --- backend/endpoints/roms/__init__.py | 4 +--- backend/handler/database/roms_handler.py | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index a0c3bacb6..49336e286 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -311,9 +311,7 @@ def get_roms( bool, Query( description=( - "Whether to return the full ordered rom id index that backs" - " virtual scroll. Disable it for small rails that only need a" - " page, to skip the full-library scan." + "Whether to return the full ordered rom id index that backs virtual scroll." ) ), ] = True, diff --git a/backend/handler/database/roms_handler.py b/backend/handler/database/roms_handler.py index ba230a609..772b769fb 100644 --- a/backend/handler/database/roms_handler.py +++ b/backend/handler/database/roms_handler.py @@ -1412,8 +1412,8 @@ class DBRomsHandler(DBBaseHandler): ) -> int: """Count matching roms without materialising their ids. - For callers that need a page and its total but not the full ordered id - list (e.g. the home rails), so the database can serve the page from the + For callers that need a page and its total but not the full + ordered id list, so the database can serve the page from the sort index instead of scanning the whole library. """ return (