Skip to content

engine: allocate less as each exec starts - #14986

Closed
sipsma wants to merge 4 commits into
mainfrom
sipsma/exec-start-allocations
Closed

sipsma wants to merge 4 commits into
mainfrom
sipsma/exec-start-allocations

Conversation

@sipsma

@sipsma sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Each exec the engine starts allocated about 80 KB before running, mostly in three places. Over the ~1,400 execs of a cold build of dagger's own CLI with per-package Go builds, that was about 90 MiB of garbage for the GC to collect. This removes most of it, with no change in behavior:

  • The OCI spec's encoding (takes effect on Go 1.27). On Go 1.27, encoding/json is built on json v2, and json.Encoder encodes into a buffer from a pool shared with every other encode in the process. That pool drops large buffers after a few small encodes, so each ~12 KB spec grew a new buffer, about 37 KB per exec. writeSpec now encodes into a pooled buffer of its own with json v2's MarshalWrite and encoding/json's own options, so config.json is byte-identical. Builds before Go 1.27, as today's are until chore: bump to go 1.27 #14954, keep json.Encoder, whose own pool keeps its buffers there.
  • The user lookup. An exec whose user isn't a numeric uid:gid, which includes every exec with no user set, parsed the image's /etc/passwd and /etc/group. GetUser now keeps each result by a hash of the user name and both files' contents, so later execs of the same image only read the files. Results and errors are unchanged. At most 1 MiB of each file is read into memory; a longer file is parsed as before, streaming, and isn't cached.
  • The "starting container" log fields are set with one WithFields, and the exec's args are formatted once for both of its log lines. The log text is unchanged.

On a cold build of dagger's CLI with a Go 1.27 engine (16-CPU arm64 machine, 4 runs per arm), allocations fall from 151 to about 100 MiB at the container's start, from 33 to 3 MiB in the user lookup, and from 19 to 11 MiB in the log entries. Engine CPU fell from 35.3 to 34.9 s and GC CPU from 8.7 to 8.3 s, within run-to-run spread at that count. Wall time and peak memory didn't change. On Go 1.26 the spec change has no effect, so the saving there is about 37 MiB.

New unit tests check that config.json matches json.Encoder's output byte for byte; that GetUser gives the same results and errors as before across changed, missing and very long files; that the log text is unchanged; and that a cached lookup allocates less than half of what parsing does.

On Go 1.27, encoding/json runs on json v2, and its Encoder encodes into
a buffer from a pool that every json.Marshal in the engine shares. The
pool drops a large buffer after a few small encodes, so each exec's
spec, about 12 KB, grew a new buffer from scratch: about 37 KB allocated
per exec, 51-53 MiB over a cold build of dagger's CLI.

writeSpec encodes into a buffer of its own pool with json v2's
MarshalWrite and encoding/json's options, so config.json is unchanged
byte for byte. Builds before Go 1.27 or without json v2 keep the
Encoder, whose own pool there keeps its buffers.

Signed-off-by: Erik Sipsma <[email protected]>
An exec whose user isn't a numeric uid:gid, which includes every exec
with no user set, parsed the image's /etc/passwd and /etc/group: 33-38
MiB allocated over a cold build of dagger's CLI, the same files parsed
again for each of its execs.

GetUser now reads both files into a reused buffer and keeps each result
by a hash of the user name and both files' contents, so a later exec
of the same image only reads the files. Results, errors included, are
the same as before for every set of files; a read error isn't cached.

Signed-off-by: Erik Sipsma <[email protected]>
The fields were added one at a time, each WithField copying the entry's
whole field map, and the exec's args were formatted for each of the two
entries, "starting container" and "container done". Setting the fields
with one WithFields and formatting args once, as the text formatter
would, leaves the engine's log text unchanged.

Signed-off-by: Erik Sipsma <[email protected]>
The previous commit read the whole of /etc/passwd and /etc/group into
memory, where the parser streams them: an image with a very large file
made the engine allocate all of it. GetUser now reads at most 1 MiB;
the rest of a longer file is parsed from the file, as before, and its
result isn't cached.

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

sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #15007, which carries these commits unchanged as part of the engine performance batch.

@sipsma sipsma closed this Oct 30, 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