Skip to content

perf: prepare inferred directories for fresh evaluations - #211

Closed
grouville wants to merge 2 commits into
vito:mainfrom
grouville:perf/prepared-directory-20261029
Closed

grouville wants to merge 2 commits into
vito:mainfrom
grouville:perf/prepared-directory-20261029

Conversation

@grouville

@grouville grouville commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

The native Dang SDK evaluates the same module source for many function calls. Each call currently creates a fresh inferred program, even when the source and imported GraphQL schema are unchanged. Small collection getters make that repeated compiler work visible.

I added PrepareDir and PreparedDirectory.Evaluate so an embedder can finish parsing and whole-directory type checking once, then evaluate that program with fresh runtime state for each call. Existing RunDir callers keep their current behavior.

Preparation completes interface linking and effect checking before the program can be shared. Every evaluation still creates fresh values, closures, import bindings and services, and reruns module initializers. GraphQL clients come from the current call. A changed import schema is rejected. The prepared program retains no client or caller context.

Scalar hooks, static namespaces and native Dang imports still keep runtime state in their type graphs. PrepareDir returns ErrUnpreparableDirectory for those declarations, allowing the embedder to use RunDir before executing any user code. An evaluation failure must be returned without retrying through RunDir, because that could repeat effects.

The motivating measurement is Dagger's full Go test listing:

dagger check --module go --check test --list --all

With only prepared compilation enabled in the engine, the median fell from 25.793s to 19.926s: 5.87 seconds saved, 23% lower latency. Engine CPU fell from 119.505s to 76.350s (36%). The CLI, Go module source and public API were unchanged.

Case Warm runs, seconds
Existing engine, before 25.781, 25.805
Prepared compilation 19.926, 19.437, 20.108
Existing engine, return 28.782, 28.073

This measures this API together with the Dagger engine adapter, dagger/dagger#14953. Adding an unused API to Dang alone does not speed up existing callers. It does not include the separate Go discovery index, bulk listing or telemetry batching changes. Controls drifted, so the headline uses the conservative before comparison rather than the larger pooled result.

The benchmark uses collection engine #14802 at 97a8621c57 privately combined with main 5c4cdd243b, Dang aaa6cfeacd, and dagger/go#84 at eecde50722. This patch is ported onto Dang main 10fb92d. Measurements use a warmed 8-CPU engine and a fresh CLI after an ordinary source edit plus a unique added test on every invocation. All 4,706 expected tests, order, full normalized stdout, the added test and successful exit through normal cleanup are verified. Preparation runs are retained separately. wcprof and CPU/allocation profiles were captured.

Validation covers current clients under concurrent evaluation, initializer execution, copy-on-write behavior, interfaces, closures, private members across files, error locations, changed schemas/source, retries and unsupported declarations. The corpus check runs ordinary evaluation and three concurrent prepared evaluations for each of 229 eligible pure language programs; imported integration fixtures and unsupported runtime type declarations have separate tests. Actual Dagger check execution and the Go catalogue/manual-operation regression also passed on the benchmark version.

On current Dang main, the full go test ./pkg/dang -count=1 suite passes (12.658s), and go test -race ./pkg/dang -run '^TestPreparedDirectory' -count=1 passes (28.819s), including those 229 programs. The Dagger adapter's current-main concurrency/session-release race test also passes.

The full Dang race suite fails the existing TestFormat/TestNestedOneLineBlocksFormatQuickly five-second timing limit. The same focused formatter race test also fails on unchanged main 10fb92d (30.165s total). I have kept that failure visible and left the formatter and its test unchanged. This draft needs review of the API/fallback boundary before the engine can pin an upstream version.

An independent repeat using the same frozen engine and published Go module refs reproduced the compiler benefit: candidate 20.124 / 19.443 / 20.317s; controls 26.570 / 28.518s before and 26.018 / 25.426s after. Median latency is 20.124s versus 25.722s for the faster control block (22% lower), or 26.294s pooling controls (23% lower). Engine CPU is 75.20s versus 120.52s. Every expected output check passed. A separate comparison on today's main plus the current collection prerequisites is in progress; this repeat is not claimed as that current-main result.

Current-code combined check (2026-10-29): main 4342720d00 plus collection branch 636dd96749, with Go #84 at 1572fd4634, measured 15.505s median after adding the open bulk-listing, prepared-compilation and Go-index changes. Runs were 15.505 / 15.255 / 15.679s. Baseline runs were 28.339 / 26.209s before and 24.745 / 26.245s after. Using the faster control block gives 25.495s → 15.505s, 39% lower latency; pooled controls give 41%. Engine CPU is 116.32s → 35.73s.

That is the combined stack, not this PR's standalone contribution. Engine/CLI code is current; the workload is the same fixed 4,706-test root-Go fixture in all arms, with a fresh source edit and unique added test each time. Complete ordered stdout and normal cleanup pass. All arms also contain the identical small table-output buffering fix, prepared separately after the unmodified current CLI silently dropped stdout fragments. That shared prerequisite is not attributed to these performance changes. Telemetry batching and discovery-only scanning are absent from this combined result.

@sipsma

sipsma commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

This continues in #250, rebased onto current main with fixes from review. Guillaume's two commits are there unchanged, with him as author.

sipsma added a commit to grouville/dagger that referenced this pull request Nov 3, 2026
The engine pinned Guillaume's fork of Dang for vito/dang#211, which adds
PrepareDir and PreparedDirectory.Evaluate. vito/dang#250 carried that
change onto Dang main and merged, so Dang now moves to its merge commit
and the replace of the fork goes away.

Since the merge of vito/dang#213 that Dang was on before, Dang's type
checker became stricter (vito/dang#230 to #235, #246 and #249): programs
it accepted that could fail at runtime are now type errors, so a module
that loaded before may now fail to load. Among them, operators need
non-null operands, Map.get is typed as nullable (vito/dang#215), and a
decode result must be given a type before it is used. Function
parameters match by name, so a positional call through an interface
binds in the interface's order. YAML << merge keys are resolved when
decoding (vito/dang#217), and malformed syntax is reported instead of
panicking the parser (vito/dang#243). The release note describes what
module authors will see.

Signed-off-by: Erik Sipsma <[email protected]>
@grouville grouville closed this Nov 3, 2026
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.

2 participants