Skip to content

perf(core): evaluate ignore rules with no Path.relative_to per parent #5041

Open

Description

@dan-petty

Problem or Motivation

GitignoreIndex.is_ignored (src/devops_cli/core/gitignore.py:132) evaluates every parent folder of a path from the repository root at each call. For each parent and each ignore file in scope, _match calls Path.relative_to, and _relative calls it one more time. In Python 3.14, Path.relative_to walks Path.parents and makes a new Path for each parent, so each call is slow.

Measured on this repository after #4662: the code graph walk asks about 3,719 folders and takes about 4.5 s at load average 10. A cProfile run of the walk gave 23.7 s in total under the profiler: 18.8 s in is_ignored, 8.5 s of that in pathlib.relative_to, and 5.0 s in the Sequence.__contains__ of Path.parents. The same index serves the RAG indexer walk (ai/rag/indexer.py:748) and the file watcher (watchers/file_watcher.py:59), which also ask about each folder.

Proposed Solution & Architectural Design

  • Make the relative path once with os.path string operations, and give each ignore file its base as a string prefix, not as a Path.relative_to call.
  • Optional: let a walk ask about a folder whose parent it already found not ignored, so that is_ignored checks only the last part.

Risk: Low. The ignore rules and pathspec stay the same. The contract tests of the index keep the verdicts.

Target Subsystem

Core (src/devops_cli/core/gitignore.py).

Existing Tool Considered

pathspec already gives git's wildmatch rules, and it stays. git check-ignore --stdin gives the verdicts of git itself, but it starts a process, and the index exists to avoid that (39.5 ms for each file when it spawned git). The standard library os.path covers the change. Cost: about 20,000 tokens and 1 hour.

Acceptance Criteria

  • The walk of the code graph over this repository asks the same folders and gives the same files, in less than half of its time before the change (measured in one process, old and new code one after the other).
  • The tests of core/gitignore.py pass with no change to their verdicts.

Dependencies

None.

Source: #5039

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

Proposed design

Problem statement

The ignore index evaluates each parent folder of a path from the repository root at each call. For each parent and each ignore file in scope, _match calls Path.relative_to, and _relative calls it once more. In Python 3.14 each call builds a new Path for each parent, so the call is slow. A profile of the code graph walk records 18.8 s in is_ignored, and 8.5 s of that in pathlib.relative_to. The change computes the relative path once with os.path string operations. Every ignore verdict stays the same.

Acceptance criteria

Requirement: String paths in the ignore evaluation

GitignoreIndex.is_ignored MUST compute the path relative to the repository root with os.path string operations and MUST NOT call Path.relative_to for each parent folder.

Scenario: A walk makes no Path.relative_to call for a parent

  • WHEN A test walks a fixture tree of 500 folders through is_ignored and counts each call to pathlib.Path.relative_to.
  • THEN The count is zero and each is_ignored call returns a bool.

Requirement: Ignore verdicts stay the same

The index MUST return the same ignore verdict for each path and is_dir value as the implementation before the change.

Scenario: The index contract tests pass with no change

  • WHEN tests/core/test_gitignore_evaluation.py runs against the new implementation with no change to its expectations.
  • THEN Every test in the file passes.

Requirement: The folder walk keeps its file set

The folder walk that calls is_ignored MUST ask the same folders and MUST return the same files as before the change.

Scenario: A walk of a fixture repository returns the recorded file list

  • WHEN The walk runs over a fixture repository with nested .gitignore files and one ignored folder.
  • THEN The returned file list equals the list recorded before the change.

Requirement: The folder walk time halves

The folder walk that calls is_ignored MUST take less than half of the time it took before the change, measured in one process on the same tree.

Scenario: A benchmark walk on a fixture tree is at least twice as fast

  • WHEN A benchmark runs the walk over a fixture tree of 3,000 folders with the new code and with a reference copy of the old loop in one process.
  • THEN The new code takes less than half of the time of the reference copy.

Existing Tool Considered

The alternatives check of 2026-11-15 recorded not-applicable for this perf item and ran no search, so it names no tool. pathspec stays as the matcher: it covers git's wildmatch rules in the process and lacks no rule this change needs. git check-ignore --stdin gives git's own verdicts, but it starts a process (39.5 ms for each file), and the index exists to avoid that process. The change builds its own code: about 20,000 tokens and 1 hour to build, keep and fix it.

Key questions

  • Where does the cost of is_ignored come from?: GitignoreIndex.is_ignored (src/devops_cli/core/gitignore.py:132) walks each parent folder of the target from the repository root. For each parent and each ignore file in scope, _match (src/devops_cli/core/gitignore.py:182) calls Path.relative_to, and _relative (src/devops_cli/core/gitignore.py:119) calls it once more. [sources: src/devops_cli/core/gitignore.py:119, src/devops_cli/core/gitignore.py:132, src/devops_cli/core/gitignore.py:178, src/devops_cli/core/gitignore.py:182]
  • Which path operation replaces Path.relative_to for each parent?: Compute the path relative to the repository root once with os.path.relpath and pass str values to pathspec. Compare each ignore file's base with a string prefix test. Build no Path object for a parent. (best practice) [sources: src/devops_cli/core/gitignore.py:119, src/devops_cli/core/gitignore.py:132]
    • Chosen: os.path string operations. os.path.relpath returns a str, builds no Path for each parent, and pathspec accepts str paths. The standard library holds it.
    • Rejected: Build the Path.parents tuple once for each target. It still builds a Path for each parent and keeps the Sequence.__contains__ scan of Path.parents, which the profile shows at 5.0 s.
    • Rejected: Cache the verdict of each parent folder. A cache needs an invalidation rule for each change of a .gitignore file. The string path removes the cost with no cache.
  • Does the change keep pathspec as the matcher?: Keep pathspec. It gives git's wildmatch rules in the process and stays the matcher of the index. Do not call git check-ignore --stdin for each path. (best practice) [sources: src/devops_cli/core/gitignore.py, src/devops_cli/ai/rag/indexer.py]
    • Chosen: Keep pathspec. It gives git's wildmatch rules in the process, and the change touches only the path handling around it.
    • Rejected: Shell out to git check-ignore --stdin. It starts a process for each ask. The body records 39.5 ms for each file when the index spawned git, and the index exists to avoid that.
  • Does is_ignored get a parameter that says the parent folder is not ignored?: No. Keep the call shape of the walk in this change. The string path removes the measured cost, and the parameter changes the contract of is_ignored for each caller, such as src/devops_cli/core/repo.py:548. (best practice) [sources: src/devops_cli/core/gitignore.py:132, src/devops_cli/core/repo.py:548]
    • Chosen: Keep the current call shape. The string path removes the cost that the profile shows, and the acceptance criteria name no new parameter. Each caller keeps its code.
    • Rejected: Add a parent-verdict parameter to is_ignored. It changes the index contract and makes each walker carry the scope of the parent. That is a second change with its own correctness risk.
  • How does the test hold the performance requirement?: The required check counts each Path.relative_to call in a walk of a fixture tree and asserts the count is zero. A benchmark test runs the walk over a fixture tree of 3,000 folders with the new code and with a reference copy of the old loop in one process, and asserts the new code takes less than half of the time. (best practice) [sources: tests/core/test_gitignore_evaluation.py]
    • Chosen: Call count plus a benchmark ratio. The call count is stable on any machine, and the benchmark states the time target of the issue and runs offline.
    • Rejected: Wall-clock ratio alone. One wall-clock ratio is noisy on a shared runner and gives a false failure.
    • Rejected: A cProfile run of the walk. A profile run is heavy, and its numbers move with the load average of the machine.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

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

    @dan-petty
    OwnerAuthor
    • Scope: none. The title has the scope core, and .github/labels.yml declares no scope/core label. Add the label to the file to give the scope to this item.
    • Related: perf(ai): compile one call pattern per symbol in ast graph #4662, which asks for a different change. Both are perf changes in the same project, but #4662 targets regex compilation in ast graph while this targets path resolution in gitignore.py.
  3. added
    priority/p2-medium
    on Nov 12, 2026
  4. 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.

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

    @devops-cli-service
    Contributor

    Status set to Ready at 9cc974b (see proposed design in issue body).

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/lowpriority/p2-mediumsource/agentstatus/readytype/infravalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions