Decompress function runtime tarballs once when loading#127
Draft
negz wants to merge 1 commit into
Draft
Conversation
fce17b8 to
7dfdc78
Compare
PR crossplane#24 added support for gzipped function runtime image tarballs by streaming each one through gzip.NewReader directly into go-containerregistry's tarball.Image, writing no temporary files. go-containerregistry calls the tarball.Opener it's given once per layer, plus once each for the manifest and config. Because the gzip opener re-opened and re-decompressed the whole file from the start on every call, loading a single image decompressed it once per layer. Nix's dockerTools emits one layer per store path, so a typical function image has ~50 layers and was fully gunzipped ~54 times. Computing the image digest then re-reads every layer again. With functions built concurrently, a project with a dozen multi-arch functions spent over ten minutes pegging every core in this loop. This change decompresses each gzipped tarball once into a temporary file and serves every opener call from that plain tar, turning ~54 full decompressions per image into one. The temporary files back the returned images lazily, so they must outlive Build; the builder now creates them under a per-build temporary directory and exposes a Close method that removes the directory once the caller has finished consuming the images. NewBuilder returns the concrete *realBuilder so callers can defer Close, and the build, run, and render entry points do so after they have written, sideloaded, or loaded the images. On a project with twelve functions built for amd64 and arm64, loading all twenty-four images drops from over ten minutes to roughly eighty seconds. Signed-off-by: Nic Cope <nicc@rk0n.org>
7dfdc78 to
a35fbd6
Compare
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.
Description of your changes
#24 added support for gzipped function runtime image tarballs by streaming each one through
gzip.NewReaderdirectly into go-containerregistry'starball.Image.go-containerregistry calls the
tarball.Openerit's given once per layer, plus once each for the manifest and config. Because the gzip opener re-opened and re-decompressed the whole file from the start on every call, loading a single image decompressed it once per layer.My project uses Nix's
dockerTools, which emits one layer per store path, so a typical function image has ~50 layers and was fully gunzipped ~54 times. Computing the image digest then re-reads every layer again. With functions built concurrently, a project with a dozen multi-arch functions spent over ten minutes pegging every core in this loop.This PR decompresses each gzipped tarball once into a temporary file and serves every opener call from that plain tar. The temporary files back the returned images lazily, so they must outlive
Build; the builder now creates them under a per-build temporary directory and exposes aClosemethod that removes the directory once the caller has finished consuming the images.NewBuilderreturns the concrete*realBuilderso callers candefer Close, and the build, run, and render entry points do so after they have written, sideloaded, or loaded the images.On a project with twelve functions built for amd64 and arm64, loading all twenty-four images drops from over ten minutes to roughly eighty seconds.
This follows on from #21 and #24, which introduced pre-built and gzipped function runtime tarball support respectively.
Fixes #
I have:
./nix.sh flake checkto ensure this PR is ready for review.Linked a PR or a docs tracking issue to document this change.Addedbackport release-x.ylabels to auto-backport this PR.