refactor: share one derived-view cache

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 05:59:51 +01:00
1 parent 35f335d001
commit eef13547e1
8 files changed
+79 -72

No files matched your search

+2 -12
View File
@@ -119,22 +119,12 @@ def _declared_hashes(entry: dict, field_name: str) -> set[str]:
return set(parse_hash_list(entry.get(field_name)))
def _record_at(path: str, db: dict) -> dict | None:
"""The database record stored at path, from an index built once per db.
A scan of db['files'] on every call was O(calls x 19 000 files) on the
path verify --all and slots.py both take. The index is the pack
resolver's, so one run holds one copy of it.
"""
return record_at(db, path)
def content_sha1(path: str, db: dict) -> str | None:
"""The sha1 of the file at path, from the database or read from disk.
A data directory cache is not in the database, so its files are hashed.
"""
record = _record_at(path, db)
record = record_at(db, path)
if record is not None:
return record.get("sha1")
if not os.path.isfile(path):
@@ -144,7 +134,7 @@ def content_sha1(path: str, db: dict) -> str | None:
def accepted_by_all(path: str, entries: list[dict], db: dict) -> bool:
"""Whether every declaration's own hashes accept the file at path."""
record = _record_at(path, db)
record = record_at(db, path)
if record is None:
return False
for entry in entries:
+6 -14
View File
@@ -11,6 +11,7 @@ from dataclasses import dataclass
from typing import Any
from core.hashing import cut_md5_owner, md5_composite, parse_md5_list
from core.memo import DerivedView
from core.ziptools import check_inside_zip
from model.database import get_mame_clone_map
from model.profiles import CONTENT_CHECKS
@@ -41,26 +42,17 @@ from resolve.ownership import (
)
# One slot: the dict it was built from, its size then, and the folded view.
# Keyed by id() alone, a database freed and replaced by another at the same
# address got the old index back, and the case-insensitive step answered from
# a collection that no longer existed.
_casefold_index_cache: list[tuple[dict, int, dict[str, list[str]]]] = []
def _casefold_name_index(by_name: dict) -> dict[str, list[str]]:
"""Build (and cache) a casefolded view of the by_name index."""
if _casefold_index_cache:
source, size, cached = _casefold_index_cache[0]
if source is by_name and size == len(by_name):
return cached
def _fold_names(by_name: dict) -> dict[str, list[str]]:
"""A casefolded view of the by_name index."""
folded: dict[str, list[str]] = {}
for name, sha1s in by_name.items():
folded.setdefault(name.casefold(), []).extend(sha1s)
_casefold_index_cache[:] = [(by_name, len(by_name), folded)]
return folded
_casefold_name_index = DerivedView(_fold_names)
HASH_EXACT_RESOLUTION_STATUSES = frozenset(
{
"sha1_exact",
+11 -15
View File
@@ -4,6 +4,7 @@ from __future__ import annotations
import re
from core.memo import DerivedView
from model.collection import manufacturer_slug
@@ -33,26 +34,21 @@ def maker_of_relative(relative: str) -> str | None:
return parts[0] if len(parts) >= 2 else None
# One slot, keyed like the casefold view: the files dict, its size, the folders.
_maker_folders_cache: list[tuple[dict, int, frozenset[str]]] = []
def _maker_folders(db: dict) -> frozenset[str]:
"""Every maker folder the collection files a copy under."""
files_db = db.get("files", {})
if _maker_folders_cache:
source, size, folders = _maker_folders_cache[0]
if source is files_db and size == len(files_db):
return folders
bios_dir = str(db.get("bios_dir") or "bios")
folders = frozenset(
def _filed_maker_folders(files_db: dict, bios_dir: str) -> frozenset[str]:
return frozenset(
folder
for entry in files_db.values()
if (folder := maker_folder(str(entry.get("path", "")), bios_dir))
and folder not in BUCKET_FOLDERS
)
_maker_folders_cache[:] = [(files_db, len(files_db), folders)]
return folders
_maker_folders_view = DerivedView(_filed_maker_folders)
def _maker_folders(db: dict) -> frozenset[str]:
"""Every maker folder the collection files a copy under."""
return _maker_folders_view(db.get("files", {}), str(db.get("bios_dir") or "bios"))
def system_makers(file_entry: dict, db: dict) -> frozenset[str]: