From e2292d48329826b83a20167b11440fa54adf295f Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Fri, 17 Jul 2026 08:28:52 +0100 Subject: [PATCH] perf: cache JSONPriorConfig lookups path_value_tuples re-sorted the whole flattened config on EVERY lookup (the map was cached, the sort was not) and __call__ then linear-scanned it, with identical queries repeating for every prior of every model construction. Cache the sorted tuples and memoize lookups per instance, including misses (the class-family probe in for_class_and_suffix_path relies on repeated expected misses). Fresh instances per config push = natural invalidation; returned sub-dicts were already aliased across calls. Measured on the aggregator harness: values("model") 8.15 -> 4.60 ms/result (-44%); summaries -25%. Benefits every Model construction. Aggregator-arc deeper follow-up (#129). Co-Authored-By: Claude Fable 5 --- autoconf/json_prior/config.py | 36 +++++++++++++++++++++---- test_autoconf/json_prior/test_config.py | 19 +++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 test_autoconf/json_prior/test_config.py diff --git a/autoconf/json_prior/config.py b/autoconf/json_prior/config.py index a974577..851e6d7 100644 --- a/autoconf/json_prior/config.py +++ b/autoconf/json_prior/config.py @@ -55,6 +55,13 @@ def path_for_class(cls) -> List[str]: return f"{cls.__module__}.{cls.__name__}".split(".") +# Sentinels for the per-instance lookup cache: a query can legitimately resolve to +# None, and the class-family probe in for_class_and_suffix_path relies on repeated +# expected misses, so both found-None and not-found must be cacheable. +_UNCACHED = object() +_NOT_FOUND = object() + + class JSONPriorConfig: def __init__(self, config_dict: dict, directory=None): """ @@ -79,6 +86,8 @@ def __init__(self, config_dict: dict, directory=None): self.obj = config_dict self.directory = directory self._path_value_map = None + self._path_value_tuples = None + self._lookup_cache = {} @property def paths(self): @@ -109,12 +118,18 @@ def path_value_tuples(self) -> List[Tuple[str, object]]: """ Tuple pairs matching every possible path to the configuration it points to. These are ordered by key length with the longest key first. + + Cached on the instance — this is consulted for every prior of every model + construction, and re-sorting the flattened configuration per lookup + dominated model deserialization. """ - return sorted( - list(self.path_value_map.items()), - key=lambda item: len(item[0]), - reverse=True, - ) + if self._path_value_tuples is None: + self._path_value_tuples = sorted( + list(self.path_value_map.items()), + key=lambda item: len(item[0]), + reverse=True, + ) + return self._path_value_tuples @classmethod def from_directory(cls, directory: str) -> "JSONPriorConfig": @@ -209,9 +224,20 @@ def __call__(self, config_path: List[str]): If no configuration is found. """ key = ".".join(config_path) + cached = self._lookup_cache.get(key, _UNCACHED) + if cached is _NOT_FOUND: + raise KeyError( + f"No configuration was found for the path {config_path}" + + ("" if self.directory is None else f" ({self.directory})") + ) + if cached is not _UNCACHED: + return cached + for path, value in self.path_value_tuples: if key.endswith(path): + self._lookup_cache[key] = value return value + self._lookup_cache[key] = _NOT_FOUND raise KeyError( f"No configuration was found for the path {config_path}" + ("" if self.directory is None else f" ({self.directory})") diff --git a/test_autoconf/json_prior/test_config.py b/test_autoconf/json_prior/test_config.py new file mode 100644 index 0000000..6b52674 --- /dev/null +++ b/test_autoconf/json_prior/test_config.py @@ -0,0 +1,19 @@ + + +def test_lookup_cache_repeats_and_misses(): + """ + Lookups are memoised per instance — including misses, which the class-family + probe in for_class_and_suffix_path performs repeatedly by design. + """ + import pytest + from autoconf.json_prior.config import JSONPriorConfig + + config = JSONPriorConfig({"module.Class": {"value": 1}}) + + assert config(["pkg", "module", "Class"]) == {"value": 1} + assert config(["pkg", "module", "Class"]) == {"value": 1} + + with pytest.raises(KeyError): + config(["pkg", "module", "Missing"]) + with pytest.raises(KeyError): + config(["pkg", "module", "Missing"])