Skip to content

contenthash: don't walk the parent directory to checksum a file - #14555

Merged
sipsma merged 2 commits into
dagger:mainfrom
sipsma:contenthash-file-no-walk
Oct 8, 2026
Merged

sipsma merged 2 commits into
dagger:mainfrom
sipsma:contenthash-file-no-walk

Conversation

@sipsma

@sipsma sipsma commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Problem

Reading a file from an evaluated directory (Directory.file, and so Container.file) gives the file a content digest. To checksum a path that is not yet in a snapshot's content-hash cache, the cache walked the path's parent directory recursively. For a file at the root of a container filesystem, such as Container.file("/stamp") on an exec output, that is a walk of the whole filesystem: about 36,000 entries for a Go toolchain image, once per exec snapshot.

On a workload that runs one exec per Go package (yq, 263 packages) and reads /stamp from each, this was the largest own-time item in the engine's wall-clock profile. Probes inside the resolver showed 145 s of its 167 s (summed across concurrent calls) in that walk. Hashing the file bytes took 0.27 s.

Fix

When the resolved path exists and is not a directory, record only that path in the cache instead of walking its parent. The record, and so the digest, is the same one the walk produced. Directories, missing paths and errors keep the existing walk. Directory records are still only added by walks, so later directory, sibling and pattern checksums behave as before.

Before / after

Cold gcexp replay --concurrency 16 of yq on a dev engine (wcprof), main vs. this change:

main this PR
Directory.file own time, summed 190.0 s 10.9 s
Directory.file p50 255 ms 15 ms
Directory.file max 1.57 s 163 ms
replay total 1m20.3s 1m06.1s

Each side ran once on a shared host. Unrelated profile classes varied by up to 2x between the two runs, so the whole-replay delta is approximate; the drop for Directory.file is far larger than that noise.

Tests

TestChecksumFileDoesNotWalkParent in engine/contenthash fails before the change (/stamp: scanned 108 entries) and passes after. It also checks that file, symlink, directory and root digests match those of a full walk, and that missing paths still error. It runs in test-split/test-packages.

@grouville
grouville self-requested a review October 8, 2026 05:37
sipsma added 2 commits October 8, 2026 07:51
Checksumming a path that is not yet in a ref's cache context walked the
path's parent directory recursively. For a file at the root of a
container filesystem, such as Container.file("/stamp") on an exec
output, that is a walk of the whole filesystem: about 36,000 entries
for a Go toolchain image, once per exec snapshot.

When the resolved path is not a directory, record only that path. The
record and its digest are the same ones the walk produced. Directory
records are still only added by walks, so a directory in the tree still
has all of its contents, and later directory or sibling checksums scan
as before.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the contenthash-file-no-walk branch from 3140b4d to 5299a26 Compare October 8, 2026 08:08
@sipsma
sipsma merged commit ea21cf4 into dagger:main Oct 8, 2026
93 checks passed
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.

2 participants