Skip to content

perf(docs): match the inventory globs with one pattern for each class #4979

Done

Description

@dan-petty

Problem or Motivation

hand_written_documents in src/devops_cli/docs/inventory.py calls explicit_classes for each tracked Markdown file. explicit_classes calls PurePosixPath.full_match for each glob of CONST_DOCS_INVENTORY until one matches. The repository has 4,763 tracked Markdown files and 22 globs (measured), so one scan makes up to about 100,000 glob matches, and check_documented_argv scans one time for each call.

  • One scan took 12.7 s on a host at a load average of 38 on 16 CPUs (measured with time.perf_counter around hand_written_documents(<checkout>)).
  • In a cProfile run of tests/docs/test_docs.py::test_docs_cli_generate_and_check, hand_written_documents took 65.0 s of 158.0 s: 2 calls and 248,754 calls of the glob generator (measured).

devops docs check, the docs row of devops ci and each test that runs the documented argv check pay this cost. The number of Markdown files grows with each archived OpenSpec change.

Found while building #4706, which removes the duplicate scans in the tests but does not change src/.

Proposed Solution & Architectural Design

Match each class with one compiled pattern, not one full_match call for each glob. The standard library gives the pattern: glob.translate(pattern, recursive=True, include_hidden=True) (Python 3.13 and later). Join the patterns of a class into one regular expression, compile it one time, and keep the inventory order. Alternatively, read the Markdown files of a class with one git ls-files call and pathspecs.

Target Subsystem

Documentation inventory (src/devops_cli/docs/inventory.py).

Existing Tool Considered

The standard library glob.translate and re, or the pathspec support of git ls-files. No new dependency.

Acceptance Criteria

  • hand_written_documents gives the same list as before for the repository (a test compares the two on the real tree or on a fixture tree).
  • One scan of the repository makes one regular expression match for each file and class, or fewer.
  • The time of check_documented_argv on the repository falls by the measured share of the scan.
  • tests/docs/test_docs.py::test_docs_cli_generate_and_check takes less time than the 5.51 s that perf(tests): remove fixed waste in six slow tests #4706 gives for it. This criterion moves here from perf(tests): remove fixed waste in six slow tests #4706.

Dependencies

None.

Source: #4706

Alternatives check of 2026-11-15: not-applicable.
Class: perf, no search.
Tool: none found.

Proposed design

Problem statement

The scan repeats glob translation for each Markdown file and each inventory glob. The repository has 4,763 tracked Markdown files and 22 globs, so one scan makes about 100,000 glob matches. The change compiles one matcher for each class and reuses it. devops docs check, the docs row and the tests then stop paying the repeated cost.

Acceptance criteria

Requirement: Same hand-written file list

hand_written_documents SHALL return the same sorted list of Markdown paths as the current PurePosixPath.full_match implementation for the repository and for a fixture tree.

Scenario: Compare old and new on a fixture tree

  • WHEN The test builds a fixture tree with files that match each inventory class, hidden files and hand-written files, and runs both implementations.
  • THEN The two sorted lists are equal.

Requirement: One compiled pattern and one match per class

explicit_classes SHALL compile one regular expression for each class of CONST_DOCS_INVENTORY and SHALL make at most one match call for each file and class in one scan.

Scenario: Count compilations and matches in one scan

  • WHEN A test spies on glob.translate, re.compile and the compiled pattern's match method, then calls hand_written_documents on a tree with many Markdown files.
  • THEN glob.translate runs once for each inventory glob, re.compile runs once for each class, and the match method runs at most once for each file and class.

Requirement: No per-file full_match

hand_written_documents MUST NOT call PurePosixPath.full_match for each Markdown file.

Scenario: Forbid full_match in the hot path

  • WHEN A test replaces PurePosixPath.full_match with a function that raises AssertionError, then calls hand_written_documents on a fixture tree.
  • THEN The call returns the expected list and does not raise.

Requirement: Inventory order and first class

explicit_classes SHALL return matching class names in CONST_DOCS_INVENTORY order, and document_class SHALL return the first name.

Scenario: A path matches two classes

  • WHEN A test calls explicit_classes on a path that matches two inventory classes.
  • THEN The result lists both names in inventory order, and document_class returns the first name.

Requirement: Benchmark shows lower scan time

A benchmark SHALL show that check_documented_argv on the repository takes less time after the change than before it on the same runner.

Scenario: Record the before and after time

  • WHEN The benchmark runs check_documented_argv on the repository with the old and new hand_written_documents implementations.
  • THEN The elapsed time after the change is lower than the elapsed time before the change.

Requirement: Docs test time below baseline

tests/docs/test_docs.py::test_docs_cli_generate_and_check SHALL take less than 5.51 s on the reference runner after the change.

Scenario: Measure the docs test

  • WHEN The benchmark runs tests/docs/test_docs.py::test_docs_cli_generate_and_check on the reference runner.
  • THEN The elapsed time is less than 5.51 s.

Existing Tool Considered

The alternatives check is not-applicable for this perf item. The Python standard library glob.translate and re cover the need. They lack a single matcher over the inventory's class order, so the change adds a small cached join of the translated patterns. No new dependency.

Key questions

  • Which approach matches the inventory globs: one compiled glob.translate pattern for each class, or git ls-files pathspecs?: Use one compiled glob.translate pattern for each class. git ls-files needs a Git worktree, but hand_written_documents(root) must work on a fixture tree. (best practice) [sources: src/devops_cli/docs/inventory.py:37, tests/docs/test_docs_inventory.py]
    • Chosen: Compiled glob.translate patterns per class. Uses the standard library, keeps the root parameter, and adds no subprocess.
    • Rejected: git ls-files with pathspecs. Needs a Git worktree and cannot serve the fixture roots that the tests use.
    • Rejected: Keep PurePosixPath.full_match per glob. Repeats glob translation for each file and keeps the measured cost.
  • How does the new matcher keep the inventory order and the first matching class?: Compile the globs of each class into one pattern. Keep CONST_DOCS_INVENTORY order in explicit_classes. Let document_class return the first name. A path that matches two classes still returns both names in inventory order. (best practice) [sources: src/devops_cli/docs/inventory.py:15, src/devops_cli/docs/inventory.py:25, src/devops_cli/config/constants.py:494]
    • Chosen: One pattern per class in inventory order. Preserves the order that explicit_classes and document_class rely on.
    • Rejected: One combined pattern for all classes. Needs extra logic to map a match back to a class and does not improve the repeated translation.
  • Does glob.translate with recursive=True, include_hidden=True match the same full paths as PurePosixPath.full_match?: Yes for the inventory globs. PurePosixPath.full_match uses the same glob translation for recursive patterns and hidden files. Wrap each translated pattern in a non-capturing group before joining. Each branch then keeps its own end anchor. [sources: src/devops_cli/docs/inventory.py:21]
  • Where should the compiled patterns live so a test can change CONST_DOCS_INVENTORY without a stale cache?: Use a cached helper keyed by the class's glob tuple, such as functools.lru_cache over tuple(globs). The helper compiles one pattern for each unique glob set. It returns that pattern for every file. (best practice) [sources: src/devops_cli/docs/inventory.py, tests/docs/test_docs_inventory.py]
    • Chosen: Cached helper keyed by the glob tuple. Compiles once for each unique glob set and stays correct when a test replaces the inventory.
    • Rejected: Module-level compiled map at import. Compiles once but ignores a test or runtime change to CONST_DOCS_INVENTORY.
  • Should the change also replace root.glob in documents_of_class for non-hand-written classes?: No. The measured hot path is hand_written_documents, which calls explicit_classes for each file. documents_of_class calls root.glob once for each glob, not once for each file. This change leaves it as it is. (best practice) [sources: src/devops_cli/docs/inventory.py:30, src/devops_cli/docs/inventory.py:37]
    • Chosen: Keep root.glob in documents_of_class. The function makes one glob call for each class and is not the measured hot path.
    • Rejected: Replace root.glob with the compiled matcher. Adds scope and can change directory and file matching behavior without a measured gain.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    on Nov 12, 2026
  2. added
    type/infra
    scope/docs
    status/new
    value/medium
    effort/medium
    on Nov 12, 2026
  3. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor
  4. added
    priority/p2-medium
    on Nov 12, 2026
  5. added
    priority/p2-medium
    and removed
    priority/p2-medium
    on Nov 12, 2026
  6. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Class: perf. The check makes no search and no model call for an item of this class.

    Tool: none found

    Checked on 2026-11-15. A person decides what this means for the item. The check changes no label and closes no issue.

  7. added
    status/ready
    and removed
    status/new
    on Nov 16, 2026
  8. devops-cli-service commented on Nov 16, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 181f85e (see proposed design in issue body).

  9. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 28, 2026
  10. removed
    status/in-review
    owner/agent
    on Nov 28, 2026
  11. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-29 00:41 UTC

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

    effort/mediumpriority/p2-mediumscope/docssource/agenttype/infravalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions