Skip to content

feat: move gitIgnore loading into dagOp - #10853

Merged
TomChv merged 5 commits into
dagger:mainfrom
TomChv:feat/optimize-git-ignore-loading
Aug 11, 2025
Merged

TomChv merged 5 commits into
dagger:mainfrom
TomChv:feat/optimize-git-ignore-loading

Conversation

@TomChv

@TomChv TomChv commented Aug 7, 2025 •

Copy link
Copy Markdown
Member

Optimize performances by moving the gitIgnore loading into a dagOpWrapper internal operation so it's cached
more efficiently.
Also removed the dag.Select to read file in favour of a mount + fs.Walkdir.

This PR follows up #10736 and apply this comment: #10736 (comment)

Benchmarks

Screenshot 2025-08-07 at 17 46 27

Specific load git ignore benchmark: #10853 (comment)
GitIgnoreFor specific benchmarks: #10853 (comment)

Optimize performances by moving the gitIgnore loading into a dagOpWrapper
internal operation so it's cached
more efficiently.

Signed-off-by: Tom Chauveau <[email protected]>
@TomChv
TomChv requested a review from jedevc August 7, 2025 14:29
@TomChv TomChv self-assigned this Aug 7, 2025
@TomChv

TomChv commented Aug 7, 2025

Copy link
Copy Markdown
Member Author

As asked by @helderco, I also did some benchmark specially on how long it takes to load the modules git ignore:

Code snippet
		start := time.Now()

		// Load .gitignore patterns before loading the module so they can be used in the `Include` argument
		// instead of Exclude so we get the right behaviour.
		// We only include the .gitignore from the module source path and above up to the context directory.
		ignorePatterns, err := loadDirectoryGitIgnorePatterns(ctx, src.Local.ContextDirectoryPath, filepath.Join(src.Local.ContextDirectoryPath, moduleSourcePath))
		if err != nil {
			return fmt.Errorf("failed to load .gitignore patterns: %w", err)
		}

		rebasedIgnorePatterns, err := rebaseGitIgnorePatterns(src.Local.ContextDirectoryPath, src.Local.ContextDirectoryPath, ignorePatterns)
		if err != nil {
			return fmt.Errorf("failed to rebase .gitignore patterns: %w", err)
		}

		end := time.Since(start)

		slog.Debug("moduleSource .gitignore patterns", "duration", end.String(), "duration_ms", end.Milliseconds(), "duration_seconds", end.Seconds(), "duration_ns", end.Nanoseconds())
Screenshot 2025-08-07 at 18 31 51

@helderco

helderco commented Aug 7, 2025

Copy link
Copy Markdown
Contributor

Hmmm... how is 1000 .gitignore better than our dagger/dagger? I suggest you run something like dagger -M -c 'host | directory . | glob "**" to rule out other complications with module loading. If you isolate the measurement to just the part that's figuring out all the patterns to add to the exclude, it should be faster on dagger/dagger than on the repo with 500 .gitignore files even.

@TomChv

TomChv commented Aug 8, 2025

Copy link
Copy Markdown
Member Author

Hmmm... how is 1000 .gitignore better than our dagger/dagger? I suggest you run something like dagger -M -c 'host | directory . | glob "**" to rule out other complications with module loading. If you isolate the measurement to just the part that's figuring out all the patterns to add to the exclude, it should be faster on dagger/dagger than on the repo with 500 .gitignore files even.

My theory is that the time it takes to get the cache is slower on dagger than for 1000 .gitignore that are not in nested directories.
What I'm testing there is how long it takes to loadGitIgnore during moduleLoading, not inside host.Directory.__gitIgnoreFor

I'll do some benchmark specially inside gitIgnoreFor to see what we got

@TomChv

TomChv commented Aug 8, 2025

Copy link
Copy Markdown
Member Author

So I did another benchmark where I isolated only the gitIgnoreFor operation and compute 2 things:

  • The time it takes to read/process a file (excluding the append to the list)
  • The total time of the function

See snippets below for the code

Screenshot 2025-08-08 at 17 45 18

We observe that the dagger repo has a slightly longer overhead but it may be cause by the large number of patterns in all these gitignore.
The 1000 and 500 test case only have 1 line in the .gitignore while average on dagger repo is 4.

The result also may vary depending on my computer, I had my fan running and noticed it takes slightly longer than when they are turned off, but overall it's quite fast, even for a large number of files.

Code implementation
func (dir *Directory) GitIgnoreFor(ctx context.Context) ([]string, error) {
	result := []string{}
	var timeByReadFile []time.Duration

	start := time.Now()

	_, err := execInMount(ctx, dir, func(root string) error {
		resolvedDir, err := containerdfs.RootPath(root, dir.Dir)
		if err != nil {
			return err
		}

		return filepath.WalkDir(resolvedDir, func(path string, d fs.DirEntry, prevErr error) error {
			if prevErr != nil {
				return prevErr
			}

			if d.Name() != ".gitignore" {
				return nil
			}

			startFile := time.Now()
			content, err := os.ReadFile(path)
			if err != nil {
				return err
			}

			relPath, err := filepath.Rel(resolvedDir, path)
			if err != nil {
				return err
			}

			patterns := parseGitIgnore(string(content), filepath.Dir(relPath))
			endFile := time.Since(startFile)
			timeByReadFile = append(timeByReadFile, endFile)

			result = append(result, patterns...)

			return nil
		})
	})
	if err != nil {
		if errors.Is(err, errEmptyResultRef) {
			// empty directory, i.e. llb.Scratch()
			return []string{}, nil
		}
		return nil, err
	}

	end := time.Since(start)

	var total time.Duration
	for _, t := range timeByReadFile {
		total += t
	}

	slog.Info("GitIgnoreFor",
		"dir_name", dir.Dir,
		"duration", end,
		"duration_ms", end.Milliseconds(),
		"avg_by_file_ns", total.Nanoseconds()/int64(len(timeByReadFile)),
		"avg_by_file_ms", total.Milliseconds()/int64(len(timeByReadFile)))

	return result, nil
}

Comment thread core/schema/host.go Resolved
Comment thread core/directory.go Resolved
Comment thread core/schema/host.go Resolved
Comment thread core/schema/directory.go Resolved
Signed-off-by: Tom Chauveau <[email protected]>
Comment thread core/directory.go Resolved
Signed-off-by: Tom Chauveau <[email protected]>
@TomChv
TomChv requested a review from jedevc August 11, 2025 12:43
@TomChv
TomChv merged commit ea5f91d into dagger:main Aug 11, 2025
jedevc added a commit to jedevc/dagger that referenced this pull request Aug 18, 2025
jedevc added a commit to jedevc/dagger that referenced this pull request Aug 18, 2025
jedevc added a commit to jedevc/dagger that referenced this pull request Aug 18, 2025
jedevc pushed a commit to jedevc/dagger that referenced this pull request Aug 20, 2025
Optimize performances by moving the gitIgnore loading into a dagOpWrapper
internal operation so it's cached more efficiently.
Remove multiple selection in favor on walkdir
Fix host | directory . issue on rebase pattern

Signed-off-by: Tom Chauveau <[email protected]>
jedevc pushed a commit to jedevc/dagger that referenced this pull request Aug 21, 2025
Optimize performances by moving the gitIgnore loading into a dagOpWrapper
internal operation so it's cached more efficiently.
Remove multiple selection in favor on walkdir
Fix host | directory . issue on rebase pattern

Signed-off-by: Tom Chauveau <[email protected]>
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.

3 participants