Skip to content

perf: match .dockerignore patterns once per path when listing docker dependencies - #10220

Open
John98Zakaria wants to merge 2 commits into
GoogleContainerTools:mainfrom
John98Zakaria:dockerignore-faster-dependency-listing
Open

John98Zakaria wants to merge 2 commits into
GoogleContainerTools:mainfrom
John98Zakaria:dockerignore-faster-dependency-listing

Conversation

@John98Zakaria

Copy link
Copy Markdown

Description

Listing the dependencies of a docker artifact (docker.WalkWorkspace, used for tagPolicy: inputDigest, the build cache and file watching) is slow for Dockerfiles with many COPY instructions and a .dockerignore with many patterns. A CPU profile shows the time in NewDockerIgnorePredicate → patternmatcher.MatchesOrParentMatches → regexp matching:

  • MatchesOrParentMatches matches every pattern against the path and against each of its parent directories, for every file that is not ignored.
  • That repeats for every COPY source that contains the file, for example COPY svc/src followed by COPY svc.

This PR changes two things:

  • The dockerignore predicate remembers which patterns match each directory, so a path below it is only matched itself. The result is the same as MatchesOrParentMatches: the last pattern that matches a path or one of its parents decides.
  • WalkWorkspace skips a directory that it already listed for an earlier COPY source.

Measured on a synthetic workspace with 38,793 files (30,500 of them ignored), 151 COPY sources and 45 patterns, 5 runs each on 4 vCPUs:

Measurement Before After
BenchmarkWalkWorkspace (included) 1.88–2.04 s 0.23–0.26 s
skaffold build --dry-run -q with inputDigest 1.97–2.12 s 0.46–0.56 s

The inputDigest tag of the synthetic artifact is the same before and after.

Tests, in pkg/skaffold/docker/dockerignore_test.go:

  • Negated patterns: seven cases check the reported files and which ignored directories are not read.
  • A randomized test compares 300 pattern lists with patternmatcher.MatchesOrParentMatches, and checks that no skipped directory contains a path that is not ignored.
  • Overlapping COPY sources, in both orders. This test also passes without the change.

I ran the tests of pkg/skaffold/docker/..., lint, graph, sync and build/custom locally, not the full suite or the linters.

User facing changes

One bug fix changes the reported dependencies. An ignored directory was skipped unless a negated pattern had the directory as a literal prefix, so a negated pattern with wildcards did not prevent skipping.

Before: with **/node_modules and !**/*.md, app/node_modules/pkg/README.md is missing from the dependencies, although Docker sends it to the daemon.

After: the file is a dependency. An ignored directory is skipped unless a negated pattern can match below it. For repositories with such patterns, inputDigest tags change once.

Follow-up Work

  • patternmatcher.MatchesOrParentMatches and MatchesUsingParentResults, which BuildKit uses, appear to disagree for some lists with negated patterns. This PR keeps the behaviour of MatchesOrParentMatches.
  • GetDependenciesByDockerCopyFromTo (skaffold lint) creates a predicate for each COPY instruction, so it gains from the cheaper matching but not from skipping listed directories.

…dependencies

Listing the dependencies of a docker artifact matched every pattern
against every path and all of its parent directories, and did so again
for every COPY source that contains the path.

The dockerignore predicate now remembers which patterns match a
directory, so a path below it is only matched itself, and remembers its
results. WalkWorkspace no longer walks a directory again that it listed
for an earlier COPY source.

An ignored directory is now skipped unless a negated pattern can match
below it. Before, a negated pattern with wildcards, such as !**/*.md,
did not prevent skipping, so files that Docker sends to the daemon were
missing from the dependencies.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1db16-b0fa-71dc-b4b6-090d7f504eb4
…ults

WalkWorkspace now skips a subdirectory that an earlier COPY source
already listed, in either order of the sources. That makes the
predicate's per-file result cache unnecessary, so it is removed, along
with the cached skip decision and the mutex: every caller creates its
own predicate and walks sequentially.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1db16-b0fa-71dc-b4b6-090d7f504eb4
@google-cla

google-cla Bot commented Nov 14, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@John98Zakaria
John98Zakaria marked this pull request as ready for review November 14, 2026 18:14
@John98Zakaria
John98Zakaria requested a review from a team as a code owner November 14, 2026 18:14
@John98Zakaria
John98Zakaria marked this pull request as draft November 14, 2026 23:25
@John98Zakaria
John98Zakaria force-pushed the dockerignore-faster-dependency-listing branch from 033227a to ec663e4 Compare November 14, 2026 23:27
@John98Zakaria
John98Zakaria marked this pull request as ready for review November 15, 2026 00:41

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant