Skip to content

Reuse parsed syntax across RunDir calls - #205

Merged
vito merged 1 commit into
vito:mainfrom
grouville:perf/run-dir-syntax-cache
Oct 27, 2026
Merged

vito merged 1 commit into
vito:mainfrom
grouville:perf/run-dir-syntax-cache

Conversation

@grouville

@grouville grouville commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

The official Go module is written in Dang. Listing its tests on dagger/dagger calls many module functions, and each call currently parses the same module source again. This remains substantial work after #204.

Add an optional, caller-owned syntax cache to RunDir, supplied through WithSyntaxCache. Files are read on every use and entries are keyed by their contents. Dang writes compiler state onto syntax nodes, so the cache keeps a private parsed tree and gives each call an independent copy with its current source paths. Imports, type checking, initialization and module functions still run for every call.

Complete command

dagger check --module go --check test --list --all
Warm edited listing Without syntax reuse With this implementation
Complete CLI command 152.11 s 86.17 s
Engine CPU during the command 927.17 s 415.55 s
Bytes allocated during the command 126.50 GB 61.06 GB

These are medians of two measured commands per version, in cache / no-cache / no-cache / cache order. Every command starts a fresh CLI after a Go source edit and a unique added test. All return the expected 4,399 rows in order and complete cleanup. Each engine completes a preparation command before timing. Telemetry and normal GC remain enabled; profiling is off.

The selection is the root Go module in dagger/dagger (test = ["."]), not every nested module. The source is ff0b029a. Both versions use engine main at 14c1942, with #14730 and #14738, and Go main at bf45651 plus Go #68. They use the exact same Dang library and CLI, including a local stdout-buffer correctness fix, eight CPUs and 40 GiB RAM. Only the native SDK's cache context is enabled or disabled. These are warm-engine results, not first-use engine timings.

Cost and implementation

The first use parses the file and prepares a private tree. On the real Go module file, the parser benchmark measured about 139 ms for parsing alone, 146 ms to parse and prepare an entry, and 7 ms for reuse. Preparation allocates about 6% more than parsing alone. These are separate parser measurements, with three samples of 20 iterations each.

The cache retains at most 128 entries with an estimated 64 MiB weight limit. Copying happens outside the cache lock. Unsupported fields or overlapping storage fall back to normal parsing.

This version copies a plain tree instead of building a graph of copy functions. A separate full-command comparison measured 85.01 → 86.13 s, about 1.3% slower than the earlier copier, with essentially unchanged engine CPU and total allocations. The simpler representation is the intended tradeoff. The cache-versus-no-cache measurements above use this final copying code.

Validation

The race checks cover source edits, paths and imports, fresh clients and changed schemas, concurrent evaluation, eviction, fallback and shared references inside a copied tree. Native SDK race checks pass with reuse enabled and disabled.

go test -race -count=1 -run '^Test(ParseBlueprint|SyntaxBlueprint|BlueprintCacheBudget|RunDir)' ./pkg/dang
DANG_BENCH_SOURCE=/path/to/go/go.dang go test ./pkg/dang -run '^$' -bench '^BenchmarkSyntaxCache$' -benchmem

The small native SDK integration supplies the cache context and needs this API before it can land.

Listing the Go test collection calls the same Dang module many times. Each call currently reparses its source files.

Let callers share an optional syntax cache through the context. Read and hash each file on every use, and keep the parsed tree before type checking. Each call receives an independent copy with its current source paths because type checking mutates syntax nodes. Imports, type checking, initialization and module functions still run for each call.

Bound the cache to 128 entries and an estimated 64 MiB. Copy outside the cache lock and fall back to parsing when a syntax layout cannot be copied safely.

Cover source changes, source paths, imports, clients and schemas, concurrent calls, eviction and shared references. Add a benchmark using the real Go module source.

Signed-off-by: Guillaume de Rouville <[email protected]>
@grouville
grouville force-pushed the perf/run-dir-syntax-cache branch from 4bf229a to 4cc48da Compare October 23, 2026 03:16
@grouville
grouville marked this pull request as ready for review October 27, 2026 00:36
@grouville

Copy link
Copy Markdown
Contributor Author

cc @vito @tiborvass

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Each call gets its own copy, the copy carries the caller's paths, and the fallback for unsupported syntax is covered by the corpus test. With no cache in the context, RunDir parses exactly as before.

GitHub hasn't run the Dagger checks on this head yet, so I ran the Go tests through Dagger at 4cc48da: 471 passed in pkg/dang and 823 in tests.

@vito
vito merged commit aaa6cfe into vito:main Oct 27, 2026
1 check passed
sipsma added a commit to grouville/dagger that referenced this pull request Nov 3, 2026
Dang moves from the merge of vito/dang#205 to the merge of vito/dang#213,
which adds ParseSourceFile. In between are the v2.2.0 and v2.3.0 releases
and vito/dang#214, which change what Dang modules see:

- The dang.toml that governs a module's source, found by walking up to the
  repository root, now sets the Dang version its source parses and
  evaluates as. A module with none keeps the newest version. A malformed
  dang.toml, which was ignored before, now fails the module's load.
- A module's own top-level declarations shadow builtins of the same name
  (vito/dang#212).
- JSON, YAML and TOML decode into Map[T] (vito/dang#214).
- The Dang language skill the engine serves describes these.

The engine still chooses a module's Dagger API version itself; the
Dagger version a dang.toml can declare doesn't change it.

Signed-off-by: Erik Sipsma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants