Repository navigation
Conversation
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]>
Contributor
Author
|
Superseded by #15007, which carries these commits unchanged as part 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.
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:
encoding/jsonis built on json v2, andjson.Encoderencodes 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.writeSpecnow encodes into a pooled buffer of its own with json v2'sMarshalWriteandencoding/json's own options, soconfig.jsonis byte-identical. Builds before Go 1.27, as today's are until chore: bump to go 1.27 #14954, keepjson.Encoder, whose own pool keeps its buffers there.uid:gid, which includes every exec with no user set, parsed the image's/etc/passwdand/etc/group.GetUsernow 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.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.jsonmatchesjson.Encoder's output byte for byte; thatGetUsergives 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.