refactor: index records by path in slots

This commit is contained in:
Abdessamad Derraz committed 2026-10-19 04:12:57 +02:00
1 parent 5ce2b44a57
commit 4e21469b3a
2 files changed
+39 -3

No files matched your search

+18 -3
View File
@@ -746,11 +746,26 @@ def _declared_hashes(entry: dict, field_name: str) -> set[str]:
}
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.
"""
files = db.get("files", {})
cached = _PATH_INDEX.get(id(files))
if cached is None or cached[0] is not files:
cached = (files, {e.get("path"): e for e in files.values()})
_PATH_INDEX[id(files)] = cached
return cached[1].get(path)
_PATH_INDEX: dict[int, tuple[dict, dict[str, dict]]] = {}
def _accepted_by_all(path: str, entries: list[dict], db: dict) -> bool:
"""Whether every declaration's own hashes accept the file at path."""
record = next(
(e for e in db.get("files", {}).values() if e.get("path") == path), None
)
record = _record_at(path, db)
if record is None:
return False
for entry in entries:
+21
View File
@@ -834,5 +834,26 @@ class SlotsJudgeWhatVerifyJudged(unittest.TestCase):
self.assertIn("--include-archived", result.stderr)
class ARecordIsFoundByItsPathOnce(unittest.TestCase):
"""_accepted_by_all walked all 19 000 records for every path asked."""
def test_the_files_are_walked_once(self):
import slots # noqa: PLC0415
class Counted(dict):
walks = 0
def values(self):
Counted.walks += 1
return super().values()
files = Counted({"a" * 40: {"path": "bios/x.bin"}, "b" * 40: {"path": "bios/y.bin"}})
db = {"files": files}
for _ in range(50):
self.assertEqual(slots._record_at("bios/y.bin", db)["path"], "bios/y.bin")
self.assertIsNone(slots._record_at("bios/z.bin", db))
self.assertEqual(Counted.walks, 1)
if __name__ == "__main__":
unittest.main()