Skip to content

perf: cache config YAML/JSON parsing — one parse per file per process - #212

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/config-yaml-parse-cache
Nov 8, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/config-yaml-parse-cache

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Closes #211.

What

Config.push / Config.register reset the lazily-built config dict and prior config, so after every library import the next access re-read every config file from disk. The new autonerves/parse_cache.py parses each file once per process:

  • key: absolute path + parser kind; entry reused only while (st_mtime_ns, st_size) match, so editing a file re-parses it as before
  • returns copy.deepcopy of the cached object, so a caller mutating a loaded config can never affect a later load
  • parse errors (missing file, bad YAML) propagate unchanged and are not cached
  • parsing still goes through yaml.safe_load / json.load, looked up at call time

Routed through the three config-loading paths: YAMLConfig.__init__, JSONPriorConfig.from_directory (JSON + YAML priors) and Config.logging_config. No public API change: the import yaml lines stay in place, so no module attributes are removed. The one-off general.yaml reads in __init__.py and workspace.py, and site_config, are left alone because they run once per process anyway.

Witness

  • Behaviour-preserving: test_cached_parse_deep_equals_fresh_parse builds Config.dict, JSONPriorConfig.from_directory(<dir>/priors).obj and logging_config from fresh parses, then from the cache (cold and warm), and deep-compares them over every packaged config directory in the repo (config, config_flip, default, site). I also ran the same comparison by hand, outside CI, over the shipped and test configs of PyAutoFit, PyAutoArray, PyAutoGalaxy and PyAutoLens (8 directories). All 8 were deep-equal.
  • Parse count: a yaml.safe_load hook counted parses. In a simulated register sequence (galaxy test config + push autofit → autoarray → autogalaxy → autolens → autolens test config, with dict and prior_config read after each push), parses went from 468 (148 distinct files, up to 8× each) to 148 (each file exactly once). In the repo, test_each_file_parsed_once_across_pushes checks that no file is parsed more than once.
  • Time: in that simulation the config work went from 1.41 s to 0.56 s (−0.85 s). I did not run visualization.py itself because the workspace isn't in this session, so the end-to-end ≥0.8 s figure should be confirmed on a real script run.

Tests

python3 -m pytest -q -n auto: 301 passed, 1 skipped (the base had 291 passed; the 10 new ones are in test_autonerves/test_parse_cache.py, covering deep-compare, parse once, independent copies, mtime invalidation, missing file not cached, and json/yaml kinds kept separate).

Tier: notify. Batch 2026-11-08-am: the session ends at PR-open, and merge is the human's /prm.


Generated by Claude Code

…#211)

Config.push/register invalidate the lazily-built config dict and prior
config, so every library import re-read every config file from disk.
autonerves.parse_cache parses each file once, keyed on absolute path and
(mtime_ns, size), and returns a deep copy so results equal a fresh parse.
Routed through YAMLConfig, JSONPriorConfig.from_directory and
Config.logging_config.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016nhQ8NkMVDS9RLqr6FRXye
@Jammy2211
Jammy2211 merged commit 9b204c9 into main Nov 8, 2026
3 checks passed
@Jammy2211
Jammy2211 deleted the feature/config-yaml-parse-cache branch November 8, 2026 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: cache config YAML/JSON parsing (one parse per file per process)

2 participants