Repository navigation
Conversation
sipsma
force-pushed
the
sipsma/engine-allocation-cuts
branch
from
October 30, 2026 19:02
9096fbf to
b3b980e
Compare
poolsCopy passed the opened *os.File to io.CopyBuffer, which hands the copy to the file's WriteTo when it has one. A hash has no fast path to take, so WriteTo fell back to a copy with its own new buffer for every file, and the pooled 32 KiB buffer went unused. Hiding WriteTo makes CopyBuffer use the pooled buffer, as #14949 did for ReadFrom in zip extraction. Signed-off-by: Erik Sipsma <[email protected]>
flate decompresses from an io.ByteReader, and wraps any reader that isn't one, such as a zip entry's section of the archive, in a new 4 KiB bufio.Reader. extractZip opened every entry that way. It now registers its own deflate decompressor on the archive's reader: the zip package's pooled one, with the bufio.Reader it reads through pooled too. Signed-off-by: Erik Sipsma <[email protected]>
The censor stepped through its trie once for every byte it read, and each step, and each return to the root, allocated a new TrieIter. Trie iterators are now values, with the zero TrieIter for no node, so stepping allocates nothing. The censor keeps its current node and its last match by value. Signed-off-by: Erik Sipsma <[email protected]>
Clone and Delegate copied the scope's encoded metadata. It's never modified once the scope is made, so clones now share it. Signed-off-by: Erik Sipsma <[email protected]>
Result's Type, Unwrap and DerefValue, and ObjectResult's DerefValue, loaded the result's whole payload state to read its value, and loadPayloadState copies the snapshot owner links every time: most of the bytes cloneSnapshotRefLinks allocated in a build. They now read the value alone through loadSelf, like loadResultCall reads the call. Signed-off-by: Erik Sipsma <[email protected]>
forwardIO gave the container runtime's process its stdout and stderr writers as they were. They aren't files, so os/exec copied each stream with io.Copy, which allocated a 32 KiB buffer for each stream of every container run. The writers now get a ReadFrom that copies through a pooled buffer, which io.Copy uses; nil and *os.File writers are left for os/exec to handle as before. Signed-off-by: Erik Sipsma <[email protected]>
…pt entry Signed-off-by: Erik Sipsma <[email protected]>
sync.Pool may hand out a fresh decompressor instead of the one the corrupt entry broke, and drops some under the race detector, so the test could pass without reusing one. It now retries until the reader that failed comes back from the pool, and fails if it never does. Signed-off-by: Erik Sipsma <[email protected]>
Acquiring and releasing a ref, and removing a record, built a map of log fields, and a logger entry around them, for trace logs that are usually off. Each now checks the level first. Signed-off-by: Erik Sipsma <[email protected]>
…r call Every call through the namespace wrappers for containerd's snapshotter, content store and lease manager attached the namespace again, and attaching it copies the context's gRPC and ttrpc metadata. AttachLease and attachOwnerContent make such a call for every snapshot and blob in a chain. The wrappers now leave a context that already carries their namespace as it is, and those two walks attach it once at the start. Signed-off-by: Erik Sipsma <[email protected]>
…hed everywhere Review found that namespaces.Namespace also reads incoming gRPC metadata, so the wrappers could skip WithNamespace for a server-side context whose outgoing headers carry no namespace, and a proxied snapshotter would get none; replaced outgoing metadata had the same effect. There's now one WithNamespace in engine/snapshots, used by every wrapper, that skips only when the context value and both outgoing headers, gRPC and ttrpc, name the namespace. A test covers bare, other-namespace, incoming-only and replaced-header contexts. go.mod now requires ttrpc directly. Signed-off-by: Erik Sipsma <[email protected]>
completePartKeys captured the row's persisted record, encoding each object's payload to JSON, then copied the record and decoded each payload again only to read its parts' states: 468 MiB of allocations in a cold first build of dagger's CLI, 7.5% of the engine's. An object can now describe its parts from its live value (PersistedPartsDescriber). File, Directory and Container build the payload their encoders build, under the same locks, and run their family's part mapping and routing on it, skipping only the JSON. The row is held and refused as a capture holds and refuses it, and its output versions are recorded and checked the same way. An object that can't describe itself (an acquired container output, a File or Directory with a transfer pending) and an undecoded row take the record's path, as before. One difference: a container built with no platform encodes its platform as "unknown", which doesn't decode, so the record's path fell back to the parts the row's gate settled. Its live description reports its actual parts. Every container the engine builds has a platform. Signed-off-by: Erik Sipsma <[email protected]>
Describing a container's parts from its live value (completePartKeys) encoded the container's recipe to JSON, then decoded it again for each part it routed, only to read a field or two. In a cold first build of dagger's CLI that was about 54 MiB of allocations, most of it withExec's recipe. A withExec recipe now gives routing what it reads from its encoding, with its encoder's checks and failures, without encoding it. Any other recipe is still encoded, and a router decodes what it reads from the recipe's JSON once rather than once per part. Signed-off-by: Erik Sipsma <[email protected]>
The schema digest was the hash of the whole schema's JSON encoding: every type, each object again as a possible type and as an implementation, and every description. Each session builds its schemas' digests anew, so every invocation paid for it. A one-line edit of dagger's CLI allocated 112 MiB for it, 7% of what the engine allocated, and a cold first build as much. schemaDigest now writes each part of the schema straight into the hash, in a fixed order, every value with its length or a presence flag first. It covers every field the JSON encoding held, except positions, which the encoding left out too; a nil list or map hashes as an empty one, since both describe the same schema. Each definition is hashed once, and its hash stands for it wherever it appears. A test fills every field of every part of a schema and checks that changing any one changes the digest and changing a position doesn't, so a field gqlparser adds fails it until it's hashed. The digests change, so calls scoped to a schema get new cache keys once. Signed-off-by: Erik Sipsma <[email protected]>
The namespace wrappers skipped re-attaching the namespace only when the context carried it in its value and on both outgoing headers, gRPC and ttrpc. Checking the gRPC header copies the context's whole outgoing metadata, so every call through the wrappers on an already-namespaced context paid that copy, about 21 MiB of a cold build of dagger's CLI, and a context whose headers had been replaced was attached again. The engine's containerd stores run in process and read the namespace from the context value. The headers matter only to a proxied snapshotter, the one store reached over gRPC, so its client is now wrapped to put the namespace on each call's headers, as containerd's own daemon does for its proxy plugins. WithNamespace checks only the value. go.mod takes ttrpc back as an indirect requirement. Signed-off-by: Erik Sipsma <[email protected]>
…server The test now builds the proxy through newSnapshotter and reads the header a snapshots server receives, so it also fails if the proxy isn't wrapped. The WithNamespace comment says what it checks: what namespaces.Namespace finds, incoming headers included, which is what the in-process stores read. Signed-off-by: Erik Sipsma <[email protected]>
Describing a row's complete parts from its live value took over the one use of the revision captureRowRecord returned, so it returns only the record and its error now. Signed-off-by: Erik Sipsma <[email protected]>
sipsma
force-pushed
the
sipsma/engine-allocation-cuts
branch
from
October 30, 2026 20:58
b3b980e to
b171c5e
Compare
Contributor
Author
|
Combined into #15007, which carries these commits unchanged with the rest of the engine performance batch. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Profiling the engine through a cold first build and the edits of dagger's CLI shows it allocating about 4 GiB per first build at the default GC target, and garbage collection is a large share of its CPU. Much of that allocation is the engine redoing work it has already done:
This PR removes those in four areas, each change its own commit. Measured separately and added up, they save roughly 3 engine CPU-s per cold first build of dagger's CLI, out of about 35. One-line edits get 0.1 s faster. Behavior is unchanged except for the two points under What changes for users.
1. Complete parts from the live value (2 commits)
The engine reports which of a result's parts are complete, on each cache-state span attribute and in result-state queries.
completePartKeysworked that out by capturing the result's persisted record, which encoded each object's payload to JSON and copied the record. Then it decoded each payload again, only to read its parts' states. The answer is cached, but each lazy operation that completes changes a part, so most calls found the cache out of date for a real reason. In a cold first build this was 468 MiB, a tenth of everything the engine allocated.The first commit adds an optional interface in dagql,
PersistedPartsDescriber, for an object to describe its parts from its live value.The second commit removes what was left. Describing a container's parts still encoded its recipe to JSON, and decoded it once per part, to read a single field: for a withExec recipe, whether it replays a volatile exec's cache hit.
2. The schema digest without JSON (1 commit)
The schema digest scopes per-schema calls in dagql's cache keys. It was the hash of the whole schema's JSON encoding: every type, each object again as a possible type and as an implementation, and every description, grown into one buffer. Each session builds its schemas' digests anew, so every
daggerinvocation paid for it while loading its module, before running any calls. A one-line edit of dagger's CLI spent 112 MiB on it, 7% of all the engine allocated.schemaDigestnow writes each part of the schema straight into the hash, in a fixed order.3. Snapshot namespaces and trace log fields (5 commits)
Every call through the engine's containerd wrappers (snapshotter, content store, leases, metadata) called
namespaces.WithNamespace. That copies the context's outgoing gRPC and ttrpc metadata, even when the caller had attached the same namespace already.What changes:
WithNamespace: engine/snapshots now has one, used by every wrapper, and a snapshot walk attaches the namespace once rather than once per call.namespaces.Namespacedoesn't already find it, which is what the engine's in-process containerd stores (snapshots, leases, content) read. Checking the outgoing headers too would copy the context's whole outgoing metadata on every call, so it doesn't.proxySnapshotterPath. It needs the namespace on each call's headers, so its client is wrapped to put it there, as containerd's own daemon does for proxy plugins. The wrapper covers every context-taking snapshotter method, and passesCleanupthrough, which containerd's metadata store calls after garbage collection.The snapshot cache also built its trace log fields on every call. It now builds them only when trace logs are on.
4. Small cuts in hot paths (6 commits, plus 2 test commits)
Each replaces an allocation made per file, per stream or per read with a pooled or shared one:
bufio.Readers and the zip package's pooled decompressor;What changes for users
Numbers
These come from building dagger's CLI with dagger/go's per-package Go builds, where each package compiles in its own exec.
Tests
The core, dagql, engine, engine/contenthash, engine/engineutil, engine/snapshots and engine/server tests pass on main.