From 4e21469b3a0f466b079845f5a10acd99ab379057 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Mon, 19 Oct 2026 04:12:57 +0200 Subject: [PATCH] refactor: index records by path in slots --- scripts/slots.py | 21 ++++++++++++++++++--- tests/test_slots.py | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/scripts/slots.py b/scripts/slots.py index 5f7e260f..a3a9e3d8 100644 --- a/scripts/slots.py +++ b/scripts/slots.py @@ -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: diff --git a/tests/test_slots.py b/tests/test_slots.py index e6dcbfb3..0313be89 100644 --- a/tests/test_slots.py +++ b/tests/test_slots.py @@ -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()