Skip to content

perf(ai): make the ast graph walk and call search faster - #5039

Merged
dan-petty merged 2 commits into
mainfrom
perf/4662-faster-ast-graph
Nov 12, 2026
Merged

dan-petty merged 2 commits into
mainfrom
perf/4662-faster-ast-graph

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

devops ai ast graph used most of its time in two places. The link step made a new call pattern for each pair of a symbol body and a name, so each re.search missed the re cache. The file walk ran Path.rglob one time for each extension and went into .git, .venv and the other ignored folders.

Now the link step runs the call regex only when the body holds the name. The pattern holds the name as written, so the edges do not change. One os.scandir walk skips each ignored folder and keeps the order of Path.rglob, so --max-files chooses the same files. The graph of this repository has the same files, nodes and edges as before.

The dry-run test of the command builds the graph of src/devops_cli/ai/ast, not of the whole repository.

Closes #4662

Change: openspec/changes/archive/2026-11-12-4662-faster-ast-graph/

The link step of `devops ai ast graph` compiled a new call pattern for
each pair of a symbol body and a name, and missed the `re` cache at each
call. Now it runs the regex only when the body holds the name. The
pattern holds the name as written, so the result does not change.

The file walk ran `Path.rglob` once for each extension and went into
`.git`, `.venv` and the other ignored folders. Now one `os.scandir` walk
skips each ignored folder and keeps the order of `Path.rglob`, so
`--max-files` chooses the same files.

The dry-run test of the command builds the graph of a small folder.
@dan-petty dan-petty added type/infra Type infra: the build, CI, images or cluster scope/ai AI review pipeline, personas, prompts, LLM client, or RAG labels Nov 12, 2026
@dan-petty

Copy link
Copy Markdown
Owner Author

Measurements (before and after)

All runs used CPython 3.14.10 on the shared development host, which had a high load (load average 7 to 50). The old graph.py from origin/main and the new one ran in one process on the same tree, one after the other, so they had the same load.

Graph of this repository (CodeGraphBuilder(max_files=100).build(), worktree of this branch, load average 10):

Build File walk Whole build Files Nodes Edges Output hash
Old 10.78 s 159.08 s 100 1,889 6 21eefa5bde23
New 4.80 s 8.82 s 100 1,889 6 21eefa5bde23

The hash covers the file list, the node keys and the edges in order, so the output is the same. On the main checkout, the new build took 8.68 s for 100 files, 1,893 nodes and 6 edges at load average 11. The issue measured the old build there at 17.9 to 19.8 s on a quieter host. I stopped the old build on the main checkout after 21 minutes at load average 49.

Link step alone (same 100 files): old, about 72 s; one compiled pattern for each name (functools.lru_cache), 4.8 s; substring test before the regex (this change), 0.25 to 0.29 s. Only 191 pairs get to the regex.

What is left of the new build: the walk asks the ignore index about each of 3,719 folders (about 4.5 s at this load, mostly pathlib.relative_to in core/gitignore.py), and Tree-sitter parses 100 Markdown files (about 3.5 to 4.4 s at this load).

Dry-run test (tests/commands/test_ai_local_dry_run.py::test_local_ai_commands_run_cleanly[ai ast graph-ast_graph_cmd-argv1], no coverage):

Run Time
Before, in CI (audit) 15.06 s
Before, local (audit) 19.3 s
After, local, load average 17 1.22 s

In the same after-run, ai --help took 0.88 s and ai repomap took 1.91 s. Under the profiler, the graph command took 0.63 s of the 2.66 s of the case; the rest is the start of the Typer command tree, which each case of the test pays.

Checks run locally: ruff check, ruff format --check, mypy src/devops_cli/ai/ast/graph.py, devops openspec check, and devops ci test on the 7 files that cover the graph (182 passed).

Fail-to-pass

uv run devops ci fail-to-pass --base origin/main (base 87ae9eefa): 1 failed on base (1 failed and 55 passed in tests/ai/ast/test_treesitter_engine.py, tests/commands/test_ai_local_dry_run.py; 0 errored). The test that fails on the base is tests/ai/ast/test_treesitter_engine.py::test_code_graph_lists_no_ignored_folder: a direct run on the base showed that the old walk lists .git, vendor and vendor/pkg. The other new test, test_code_graph_links_no_name_that_a_body_holds_but_does_not_call, passes on the base as it must, because the link step keeps its result.

@dan-petty

Copy link
Copy Markdown
Owner Author

CI result of this branch

All checks of 8482e462d pass: 19 succeeded and 4 were skipped. The shards ran in 129.12 s, 220.29 s, 181.97 s and 124.41 s (pytest time).

The dry-run case of ai ast graph is no longer among the 10 slowest tests of any shard. The 10th-slowest test of the shards took 2.04 s to 3.09 s, so the case took less than 3.09 s in CI, against 15.06 s before. CI shards print no other per-test time.

Follow-up for the cost that stays in the ignore index: #5041.

@dan-petty
dan-petty marked this pull request as ready for review November 12, 2026 11:33
@dan-petty
dan-petty merged commit 3b25cc6 into main Nov 12, 2026
23 checks passed
@dan-petty
dan-petty deleted the perf/4662-faster-ast-graph branch November 12, 2026 12:12
dan-petty pushed a commit that referenced this pull request Nov 12, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.118.0](v0.117.2...v0.118.0)
(2026-11-12)


### Features

* **ci:** devops ci stops its rows when timeout ends it
([#5031](#5031))
([1fa4e9c](1fa4e9c))
* **roadmap:** keep the stage gate problems as a list with a code
([#5022](#5022))
([5819627](5819627))
* **roadmap:** log one result line for each roadmap job attempt
([#4966](#4966))
([2b6fe3f](2b6fe3f))
* **telemetry:** record each CLI usage error by name and kind
([#4973](#4973))
([a179fd9](a179fd9))


### Bug Fixes

* **ai:** chat loads each persona prompt and challenger gets tools
([#5037](#5037))
([c40b0ad](c40b0ad))
* **ai:** keep the HTTP status of a Claude reply after retries
([#4976](#4976))
([5bf9466](5bf9466))
* **deps:** keep Renovate uv bumps green and groups on Monday
([#4987](#4987))
([b19e153](b19e153))
* **k8s:** raise the limits that 7 d of use shows are too low
([#5030](#5030))
([028c62b](028c62b))
* **roadmap:** a job posts no comment that repeats a recent one
([#4978](#4978))
([fff903a](fff903a))
* **service:** alert when GitHub cannot deliver a webhook
([#5045](#5045))
([2bfbfcb](2bfbfcb))
* **service:** delete the stream route that sends canned verified text
([#5038](#5038))
([e9d51b5](e9d51b5))
* **telemetry:** keep one upstream status series per service
([#4986](#4986))
([3e4ca56](3e4ca56))
* **tests:** remove the runner variable in the test floor
([#4988](#4988))
([093aa61](093aa61))


### Performance Improvements

* **ai:** make the ast graph walk and call search faster
([#5039](#5039))
([3b25cc6](3b25cc6)),
closes [#4662](#4662)
* **ci:** build the devcontainer only on its own inputs
([#4977](#4977))
([c5b15ac](c5b15ac))
* **ci:** test each merge once, on the push to main
([#5043](#5043))
([c6992b1](c6992b1))
* **tests:** remove fixed waste in six slow tests
([#4995](#4995))
([1c7044e](1c7044e))


### Code Refactoring

* **ai:** delete devops ai pipeline and persona leftovers
([#5044](#5044))
([d5fefde](d5fefde)),
closes [#4827](#4827)
* **openspec:** split big capabilities into nested ones
([#4981](#4981))
([dfb09bd](dfb09bd))
* **pr:** devops pr update acts only on a failed check
([#4990](#4990))
([aa4fa1a](aa4fa1a))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope/ai AI review pipeline, personas, prompts, LLM client, or RAG type/infra Type infra: the build, CI, images or cluster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(ai): compile one call pattern per symbol in ast graph

1 participant