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) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-09-02 17:08:54 -04:00
parent e19394e969
commit 63dfcbc5dd
2 changed files with 59 additions and 1 deletions

View File

@@ -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.

View File

@@ -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.