Skip to content

core/sdk/dang/v2: parse a module's source once when it loads - #14998

Closed
sipsma wants to merge 4 commits into
mainfrom
sipsma/dang-module-load
Closed

sipsma wants to merge 4 commits into
mainfrom
sipsma/dang-module-load

Conversation

@sipsma

@sipsma sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Loading a Dang module parses its source three times: once for the module's own type names, once in DeclareDir, and once more to put its object directives back on its types. Since #14802, the engine keeps parsed Dang source in a syntax cache, but only DeclareDir's parse goes through it. So on a fresh engine, every module still pays for two extra parses before its first call.

The engine's own two reads now go through the same cache with dang.ParseSourceFile (vito/dang#213), at the module's Dang version. A module's source is parsed once for all three.

What it saves, by a Go benchmark on 16 Graviton4 cores:

File Uncached parse Copy from the cache
dagger/go's Go module (go.dang, 47 KB) 147 ms, 65 MB 6.4 ms, 2.3 MB
dagger/go's gomod module (35 KB) 85 ms, 38 MB 3.6 ms, 1.3 MB

That's about 0.44 CPU-s and 200 MB of allocations off a fresh engine's first load of dagger/go. On a warm engine, or one that keeps module definitions in its persistent cache, the load doesn't run, so nothing changes there.

The type-name memo now also keys on the Dang version. The names' parse now follows the module's dang.toml. Without the version in the key, fixing only a broken dang.toml kept the names found before. A dang.toml that can't be read isn't remembered.

The Dang move changes more than caching

ParseSourceFile is on vito/dang main, past the v2.2.0 and v2.3.0 releases, so this moves Dang there. These come with it, and reach every Dang module:

  • The governing dang.toml sets the Dang version. That's the nearest dang.toml, walking up to the repository root, so it can be in a parent directory. It sets the version the source parses and runs as. A module with none keeps the newest version. A malformed dang.toml, which was ignored before, now fails the module's load.
  • Builtins can be shadowed. A module's own top-level declarations shadow builtins of the same name (Version-gate reserved words; let modules shadow builtins vito/dang#212).
  • Map decoding. JSON, YAML and TOML decode into Map[T] (Decode JSON/YAML/TOML into Map[T] vito/dang#214).
  • The Dang language skill that the engine serves describes these.

The engine still chooses a module's Dagger API version itself. A Dagger version declared in dang.toml doesn't change it. #14958 moves Dang to v2.3.0 with engine-side version handling, so whichever lands second rebases its go.mod.

Tests

go build of the engine, CLI and core/..., and go test -race ./core/sdk/dang/..., pass. A new test covers repairing a broken dang.toml without changing the source. ParseSourceFile's own tests are in vito/dang.

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]>
Loading a Dang module parses its source three times: once for its declared
type names, once in DeclareDir, and once more to put its object directives
back on its types. Only DeclareDir's parse went through the syntax cache,
so on a fresh engine every module paid for two extra parses before its
first call. For dagger/go's Go module each costs 147 ms and 65 MB of
allocations, against 6 ms and 2 MB to copy the file from the cache.

The engine's own two reads now parse through the same cache with
dang.ParseSourceFile, at the module's Dang version, so a module's source
is parsed once for all three.

Signed-off-by: Erik Sipsma <[email protected]>
Their parse now follows the module's dang.toml, so the names depend on the
Dang version as well as the source: a module whose dang.toml was broken,
or that changed only its version, kept the names found before. The memo
key now includes the version, and a dang.toml that can't be read is not
remembered.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma

sipsma commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of #14953, which carries these four commits (parsing a Dang module's source once at load, through Dang's syntax cache) together with the Dang pin they need.

@sipsma sipsma 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.

1 participant