From 63dfcbc5ddd8eaed8892499556b19c15d34d4fb2 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Wed, 2 Sep 2026 17:08:54 -0400 Subject: [PATCH] perf(scan): stop re-reading title ids a rom already carries Extraction is a native parse of every ROM file, and it rode along on every HASHES and COMPLETE rebuild, writing back the value already stored. With the binding shipped by default that landed on every rescan of every library. A rescan now re-reads only when the rom has no id yet, when the scan is COMPLETE, or on the Switch family, where the same parse is what settles the per-file categories. Co-Authored-By: Claude Opus 5 (1M context) --- backend/endpoints/sockets/scan.py | 24 ++++++++++++- backend/tests/endpoints/sockets/test_scan.py | 36 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/backend/endpoints/sockets/scan.py b/backend/endpoints/sockets/scan.py index 81f6218a5..157f565a4 100644 --- a/backend/endpoints/sockets/scan.py +++ b/backend/endpoints/sockets/scan.py @@ -76,6 +76,7 @@ from utils.audio_tags import remove_persisted_cover from utils.context import initialize_context from utils.gamelist_exporter import GamelistExporter from utils.pegasus_exporter import PegasusExporter +from utils.switch import SWITCH_PLATFORM_SLUGS STOP_SCAN_FLAG: Final = "scan:stop" @@ -328,6 +329,26 @@ def _should_get_rom_files( ) +def _should_extract_title_ids(scan_type: ScanType, rom: Rom) -> bool: + """Decide if a rescan should re-read title ids out of a rom's binaries. + + Extraction is a native parse of every ROM file, so it is not repeated for a + rom that already carries an id. A complete rescan refreshes it regardless, + and the Switch family always re-reads because the same parse is what + settles its per-file categories. + + Args: + scan_type (ScanType): Type of scan to be performed. + rom (Rom): The existing rom being rescanned. + """ + + return bool( + scan_type == ScanType.COMPLETE + or not rom.title_id + or rom.platform_slug in SWITCH_PLATFORM_SLUGS + ) + + def _should_reparse_tags( scan_type: ScanType, rom: Rom, @@ -547,7 +568,8 @@ async def _identify_rom( rom, fs_rom, calculate_hashes=calculate_hashes, - extract_title_ids=extract_title_ids, + extract_title_ids=extract_title_ids + and _should_extract_title_ids(scan_type, rom), embed_title_ids=embed_title_ids, ) # Keep the in-memory rom's name matching the renamed file. diff --git a/backend/tests/endpoints/sockets/test_scan.py b/backend/tests/endpoints/sockets/test_scan.py index c8e6f3725..43d18bd58 100644 --- a/backend/tests/endpoints/sockets/test_scan.py +++ b/backend/tests/endpoints/sockets/test_scan.py @@ -1,5 +1,6 @@ from itertools import count from types import SimpleNamespace +from typing import cast from unittest.mock import AsyncMock, MagicMock, Mock import pytest @@ -11,6 +12,7 @@ from endpoints.sockets.scan import ( ScanStats, _identify_rom, _scan_selected_roms, + _should_extract_title_ids, _should_reparse_tags, reject_unauthorized_scan, scan_handler, @@ -510,6 +512,40 @@ class TestShouldReparseTags: assert _should_reparse_tags(ScanType.QUICK, rom, [rom.id + 99]) is False +class TestShouldExtractTitleIds: + """Which rescans pay for another native parse of a rom's binaries.""" + + @staticmethod + def _rom(platform_slug: str, title_id: str | None) -> Rom: + # Built untyped and cast, since `platform_slug` is a read-only property. + rom = Mock(spec=Rom) + rom.title_id = title_id + rom.platform_slug = platform_slug + return cast(Rom, rom) + + def test_a_stored_id_is_not_re_read(self): + rom = self._rom("psp", "ULUS-10041") + + assert _should_extract_title_ids(ScanType.QUICK, rom) is False + assert _should_extract_title_ids(ScanType.HASHES, rom) is False + + def test_a_rom_without_an_id_is_read(self): + assert _should_extract_title_ids(ScanType.QUICK, self._rom("psp", None)) is True + + def test_a_complete_rescan_always_re_reads(self): + rom = self._rom("psp", "ULUS-10041") + + assert _should_extract_title_ids(ScanType.COMPLETE, rom) is True + + @pytest.mark.parametrize("platform_slug", ["switch", "switch-2"]) + def test_switch_always_re_reads(self, platform_slug: str): + """The same parse settles the per-file categories, which a rebuild would + otherwise take from the folder names instead.""" + rom = self._rom(platform_slug, "0100ABCD12340000") + + assert _should_extract_title_ids(ScanType.QUICK, rom) is True + + class TestIdentifyRomTagReparse: """A complete rescan re-reads filename tags onto an existing entry.