refactor: index the database by path once

model.database.record_at and sha1_at read one cached path index with
normpath keys. They replace records_by_path of resolve.packfiles, the
manifest's own sha1 index, the per-call indexes of regionaudit,
emulatorcheck and hidden_extras, and the linear scan of held_record.
resolve_sha1 drops its by_path argument. A path spelled with ./ or a
doubled slash now finds its record everywhere; the database writes none,
so the output is the same.
This commit is contained in:
Abdessamad Derraz committed 2026-11-20 20:03:26 +01:00
1 parent 1c978f3083
commit 8bc1fcb5db
9 files changed
+55 -60

No files matched your search

+2 -7
View File
@@ -11,6 +11,7 @@ from core.hashing import compute_hashes
from core.paths import BIOS_DIR, REPO_ROOT
from core.ziptools import build_zip_contents_index
from model.alternatives import set_requirements, settle_sets, slot_set_keys
from model.database import sha1_at
from model.profiles import (
ProfileSelectionError,
entry_serves_systems,
@@ -207,7 +208,6 @@ class _EmulatorPack:
# The emulator pack walks profiles in name order and keeps the first
# file at a path, folding case: a later profile's different file there
# (zesarux pentagon.rom after speccy's PENTAGON.ROM) is not in the pack.
sha1_by_path: dict = field(init=False)
# Destination -> (file packed there, its profile, the path as written).
# Archives and data trees take paths too, before and after the loose
# files of their profile, as the pack writes them.
@@ -215,11 +215,6 @@ class _EmulatorPack:
taken: Placed = field(default_factory=lambda: Placed(True))
content_ids: dict[str, str | None] = field(default_factory=dict)
def __post_init__(self) -> None:
self.sha1_by_path = {
entry.get("path"): sha1 for sha1, entry in self.db.get("files", {}).items()
}
def pending(self, entry: dict, local_path: str | None, status: str) -> tuple[str | None, str]:
from resolve.packfiles import pending_release_asset, run_is_offline
@@ -236,7 +231,7 @@ class _EmulatorPack:
if not path:
return None
if path not in self.content_ids:
self.content_ids[path] = self.sha1_by_path.get(path) or (
self.content_ids[path] = sha1_at(self.db, path) or (
compute_hashes(path)["sha1"] if os.path.isfile(path) else None
)
return self.content_ids[path]
+3 -5
View File
@@ -11,6 +11,7 @@ from dataclasses import dataclass, field
from typing import TYPE_CHECKING, TypedDict
from core.options import reject_unknown
from model.database import sha1_at
from model.profiles import PROFILE_IDENTITY_FIELDS, entry_system_ids, lists_files
from model.systems import norm_system_id
from model.alternatives import core_file_spares, required_only_spares, set_fields
@@ -415,13 +416,10 @@ def hidden_extras(
system_ids = options.get("system_ids")
include_all = options.get("include_all", False)
required_only = options.get("required_only", False)
sha1_by_path = {
entry.get("path"): sha1 for sha1, entry in db.get("files", {}).items()
}
base_dest = config.get("base_destination", "")
taken = Placed(bool(config.get("case_insensitive_fs", False)), content={})
for dest, local in shipped.items():
taken.take(dest, sha1_by_path.get(local))
taken.take(dest, sha1_at(db, local))
prefix = detect_extras_prefix(config, base_dest)
region_prefix = declared_root(config)
hidden: dict[tuple[str, str, str], str] = {}
@@ -459,7 +457,7 @@ def hidden_extras(
local, _status = resolve_local_file(
fe, db, dest_hint=fe.get("destination", "")
)
held = (sha1_by_path.get(local) or _file_sha1(local)) if local else None
held = (sha1_at(db, local) or _file_sha1(local)) if local else None
if reason == "case_twin" and held and taken.content_at(full) == held:
continue
key = (
+2 -18
View File
@@ -9,6 +9,7 @@ from dataclasses import dataclass
from core.hashing import compute_hashes
from core.paths import declared_destination
from model.database import sha1_at
from model.declarations import OUTER_ROOTS, declared_root
from model.largefiles import asset_name, load_registered_paths, published_asset_name
from packs.extras import (
@@ -68,23 +69,6 @@ def _get_repo_path(sha1: str, db: dict) -> str:
return entry.get("path", "")
# The database the index was built from, held so its identity stays its own.
_DB_SHA1_BY_PATH: list[tuple[dict, dict[str, str]]] = []
def _db_sha1_by_path(db: dict) -> dict[str, str]:
"""Path -> sha1 of the database, built once per database."""
if _DB_SHA1_BY_PATH and _DB_SHA1_BY_PATH[0][0] is db:
return _DB_SHA1_BY_PATH[0][1]
index = {
os.path.normpath(entry["path"]): sha1
for sha1, entry in db.get("files", {}).items()
if entry.get("path")
}
_DB_SHA1_BY_PATH[:] = [(db, index)]
return index
# The hashes a manifest entry records, and each file's once verified.
_MANIFEST_HASHES = frozenset({"sha1", "sha256"})
_STABLE_HASHES: dict[tuple, dict] = {}
@@ -100,7 +84,7 @@ def _stable_hashes(local_path: str, db: dict) -> dict:
refused rather than published.
"""
stat = os.stat(local_path)
expected = _db_sha1_by_path(db).get(os.path.normpath(local_path))
expected = sha1_at(db, local_path)
# Keyed by what was checked, the sha1 this database expects there, never
# by id(db): a database freed and replaced at the same address answered
# with hashes verified against the first one.