Repository navigation
Description
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_counteraroundhand_written_documents(<checkout>)). - In a cProfile run of
tests/docs/test_docs.py::test_docs_cli_generate_and_check,hand_written_documentstook 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_documentsgives 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_argvon the repository falls by the measured share of the scan. tests/docs/test_docs.py::test_docs_cli_generate_and_checktakes 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.compileand the compiled pattern's match method, then callshand_written_documentson a tree with many Markdown files. - THEN
glob.translateruns once for each inventory glob,re.compileruns 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_matchwith a function that raisesAssertionError, then callshand_written_documentson 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_classeson a path that matches two inventory classes. - THEN The result lists both names in inventory order, and
document_classreturns 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_argvon the repository with the old and newhand_written_documentsimplementations. - 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_checkon 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.translatepattern for each class, orgit ls-filespathspecs?: Use one compiledglob.translatepattern for each class.git ls-filesneeds a Git worktree, buthand_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_INVENTORYorder inexplicit_classes. Letdocument_classreturn 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_classesanddocument_classrely 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.
- Chosen: One pattern per class in inventory order. Preserves the order that
- Does
glob.translatewithrecursive=True, include_hidden=Truematch the same full paths asPurePosixPath.full_match?: Yes for the inventory globs.PurePosixPath.full_matchuses 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_INVENTORYwithout a stale cache?: Use a cached helper keyed by the class's glob tuple, such asfunctools.lru_cacheovertuple(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.globindocuments_of_classfor non-hand-written classes?: No. The measured hot path ishand_written_documents, which callsexplicit_classesfor each file.documents_of_classcallsroot.globonce 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
dan-petty commentedon Nov 12, 2026
Both compile patterns once to cut repeated per-call work, but in different files and different commands.perf(tests): remove fixed waste in six slow tests (#4995)
devops-cli-service commentedon Nov 15, 2026
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.
devops-cli-service commentedon Nov 16, 2026
Status set to Ready at 181f85e (see proposed design in issue body).
devops-cli-service commentedon Nov 29, 2026
Released in v0.241.1: https://github.com/dan-petty/devops-cli/releases/tag/v0.241.1
devops-cli-service commentedon Nov 29, 2026
Live since 2026-11-29 00:41 UTC