Skip to content

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

Closed

Description

@Jammy2211

Overview

autonerves re-parses the same YAML/JSON config files many times per script run: every Config.push/register invalidates the lazily-built config dict and prior config, and the next access re-reads every file from disk (308 parses per run in the /ci_speedup audit, ~1–2 s of fixed overhead on every PyAuto script). This adds a process-level parse cache keyed on path + mtime + size so each file is parsed at most once, while returning results identical to today's.

Plan

  • Add a small parse cache module to autonerves: parse a config file once, keyed on its absolute path and file stat (mtime, size), and hand callers a private deep copy so mutation can never leak between callers.
  • Route the three config-loading paths through it: YAMLConfig, JSONPriorConfig.from_directory (JSON + YAML priors) and Config.logging_config.
  • Edited files re-parse automatically (stat changes), so behaviour on disk edits is unchanged.
  • Tests: deep-compare parsed config (full Config.dict and prior configs) with and without the cache over every packaged config directory in the repo; a parse-count hook proves each file is parsed at most once; an mtime change invalidates.

Tier: notify — merge mode: auto-merge on green. Batch 2026-11-08-am contract applies: the session ends at PR-open, merge is the human's /prm.

Detailed implementation plan

Affected Repositories

  • PyAutoNerves (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoNerves main clean

Suggested branch: feature/config-yaml-parse-cache

Implementation Steps

  1. New autonerves/parse_cache.py: load_yaml(path), load_json(path), clear(). Cache {(abspath, kind): (mtime_ns, size, parsed)}; on a hit with matching stat return copy.deepcopy(parsed); on a miss open + parse via yaml.safe_load / json.load (looked up at call time, so hooks still see each real parse). Parse errors propagate and are not cached.
  2. directory_config.YAMLConfig.__init__ → parse_cache.load_yaml.
  3. json_prior/config.JSONPriorConfig.from_directory → cache loaders (same suffix order, same rglob walk, same keys).
  4. conf.Config.logging_config → parse_cache.load_yaml.
  5. Tests in test_autonerves/test_parse_cache.py.

Key Files

  • autonerves/parse_cache.py — new
  • autonerves/directory_config.py, autonerves/json_prior/config.py, autonerves/conf.py

Original Prompt

Click to expand starting prompt

Source: PyAutoMind/draft/refactor/autonerves/config_yaml_parse_cache.md (Type: refactor, Autonomy: safe, Consequence: notify).

Witness: The parsed config objects compare equal before and after (deep-compare test over every packaged config directory), YAML parses per script run drop from 308 to at most one per distinct directory (counted by a parse hook), and visualization.py's run is faster by at least 0.8 s.

Found by /ci_speedup (2026-09-10) while profiling the slowest CI smoke scripts.

autonerves parses 308 YAML files on every script run — 186 of them inside json_prior/config.py:from_directory, reached from only 5 call sites. That is roughly 1.5–2s of every PyAuto script's runtime, in CI and on a laptop.

Because it is per-process fixed overhead, it does not show up as any one script's problem — it is a flat tax on the whole scripts lane, and on every tutorial and workspace example a user runs.

A library-level cache (parse once per directory, keyed on path + mtime) would pay back across the entire lane rather than one script. Behaviour-preserving: the parsed config must be identical, which is the acceptance test.

Measured on autogalaxy_workspace_test scripts/imaging/visualization/visualization.py, where it is 1.0s of a 26.8s run — but the same 308 parses happen in every script profiled during that audit.

Activity

  1. Jammy2211 commented on Nov 8, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Merged in #212 (merge commit 9b204c9). Each config YAML/JSON file is now parsed once per process: in the simulated register sequence, parses went from 468 to 148 and config time from 1.41 s to 0.56 s. The unittest legs (3.12, 3.13 and no-jax) were green.

    This is a library change, so it is pending the next release. The end-to-end visualization.py timing has not been measured yet.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions