Skip to content

perf: cache JSONPriorConfig lookups (model deserialization -44%) - #130

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/prior-config-cache
Jul 17, 2026
Merged

perf: cache JSONPriorConfig lookups (model deserialization -44%)#130
Jammy2211 merged 1 commit into
mainfrom
feature/prior-config-cache

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

The aggregator arc's recorded "deeper follow-up" (#129): cProfile showed 77% of per-result summary/model deserialization is Model.__init__ building default priors from prior config, and most of that was two defects in JSONPriorConfig:

  1. path_value_tuples re-sorted the entire flattened config dict on every lookup — the flattened map was cached, the sort was not.
  2. __call__ then linear-scanned the tuples, with identical queries repeating for every prior of every model construction (thousands of times when aggregating a catalogue).

Fix: cache the sorted tuples and memoize lookups per instance — including misses, which the class-family probe in for_class_and_suffix_path performs repeatedly by design (a _NOT_FOUND sentinel; found-None is also representable). Invalidation is structural: config pushes construct fresh JSONPriorConfig instances, and returned sub-dicts were already aliased across repeated calls, so no new sharing is introduced.

Measured (100 mock results, like-for-like load, best of 3):

  • values("model"): 8.15 → 4.60 ms/result (−44%)
  • values("samples_summary"): 8.73 → 6.58 ms/result (−25%; includes sample-json costs the cache doesn't touch)

This benefits every Model/Collection construction (search startup included), not just the aggregator.

API Changes

None — internal caching only; lookup results and exception behaviour are unchanged.

Test Plan

  • pytest test_autoconf — 147 passed (+ new cache regression test: repeated hits and repeated misses)
  • Full downstream pytest test_autofit/ — 1495 passed, 1 skipped
  • Before/after measurement above via the aggregator profiling harness

Generated by the PyAutoLabs agent workflow.

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 <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Jul 17, 2026
@Jammy2211
Jammy2211 merged commit 1497a04 into main Jul 17, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/prior-config-cache branch July 17, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pending-release PR queued for the next release build

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant