Skip to content

perf: match "*.ext" include patterns without a regexp - #14980

Closed
sipsma wants to merge 2 commits into
mainfrom
sipsma/host-read-glob-match
Closed

sipsma wants to merge 2 commits into
mainfrom
sipsma/host-read-glob-match

Conversation

@sipsma

@sipsma sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Every host.directory read walks the client's tree in the CLI and checks each path against each include pattern. Patterns such as **/*.go or sdk/go/**/*.go each compiled to a regular expression, so a read with many patterns spent most of its time in Go's regexp backtracker, on the single goroutine that walks the tree.

This shows up in any module that reads the workspace with many include patterns. For example, building dagger's own CLI with the dagger.io/go module reads the workspace twice per build, the second time with about 110 patterns. In a CPU profile of an unchanged rebuild, the CLI spent 0.60 s walking, 0.45 s of it in regexp matching. An execution trace showed the walk wasn't waiting on the engine, so all of that was wall time.

What changes

  • util/patternmatcher: a pattern made of literal text, one * or **/*, then literal text within one path element (*.go, dir/*.go, dir/**/*.go) is matched with prefix and suffix comparisons instead of a regexp. It matches exactly the paths its regexp matched. A test checks this against the regexps, including the edge cases: zero directories under **/, separators, newlines, and regexp metacharacters in the literal parts. A pattern containing ^ keeps its regexp, where ^ is an anchor.
  • Patterns are compiled when they're created rather than on first match. The CLI matches against one pattern from several goroutines (the walk and the file senders), and the lazy compile wasn't safe for that.
  • internal/fsutil's filter, used by both the CLI's walk and the engine's mirror walk, now uses util/patternmatcher (dagger's fork) instead of github.com/moby/patternmatcher.
  • go-client-dev uploads util/patternmatcher, which internal/fsutil now imports, so dagger generate's Go client generator still finds it.

Measured

Building dagger's CLI with dagger.io/go (go | packages | get cmd/dagger | binary) on a 16-CPU Graviton4 machine, 5 runs per side, alternating:

Build Before After Difference
Unchanged rebuild 1.98 s 1.56 s −0.42 s
Build after editing the CLI's package 7.92 s 7.46 s −0.46 s
Build after editing a package seven levels below the CLI 9.40 s 9.13 s −0.27 s

Every unchanged rebuild with this change was faster than every one without it. The two workspace reads of an unchanged rebuild went from 0.68 s to 0.26 s.

A host directory read walks the whole client tree in the CLI and matches
every path against every include pattern. Patterns such as "**/*.go" or
"sdk/go/**/*.go" each compiled to a regexp, and dagger/go's source read
of dagger's own tree passes about 110 of them. Matching them was most of
the CLI's CPU in each read, about 0.45 s per build, and the walk runs on
one goroutine, so it was wall time too.

A pattern made of literal text, one "*" or "**/*", then literal text
within one path element now matches with prefix and suffix comparisons.
It matches exactly the paths its regexp did. A "^" keeps a pattern on
its regexp, where it is an anchor.

The fsutil filter, which both the CLI's walk and the engine's mirror
walk use, now uses this package rather than the upstream one it was
forked from. Patterns compile when they are created rather than on first
match, since the CLI matches against them from several goroutines, so a
match can no longer fail.

Building dagger's CLI with dagger/go, an unchanged rebuild goes from
1.98 s to 1.56 s, and an edit is 0.27 to 0.46 s faster.

Signed-off-by: Erik Sipsma <[email protected]>
Without it, go generate in sdk/go cannot find the package and fails
resolving the module graph.

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

sipsma commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Folded into #15007, which carries these two commits unchanged.

@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