Skip to content

Performance regression in WithMountedDirectory with owner parameter #11556

Closed

Description

@grouville

Problem

Container.WithMountedDirectory with the owner parameter seems to face a performance regression since v0.19.6 (time of its dagop-ification).

A user reported mounting a directory with 100.000 files going from ~4 seconds to ~3+ minutes

Root cause

My current debugging seems to isolate one culprit:

  • Commit 27af6d5f4 changed from LLB-based chown (single operation) to Directory.Chown which uses filepath.WalkDir + os.Lchown per file (O(n) syscalls):
  // directory.go:1757
  filepath.WalkDir(chownPath, func(path string, d fs.DirEntry, err error) error {
      os.Lchown(path, ownership.UID, ownership.GID)  // One syscall per file!
  })

How to repro

dagger init --sdk=go # in both 0.18.5 vs 0.19.8

Copy this repro:

package main

import (
	"context"
	"fmt"
	"time"

	"dagger/chown-perf-repro/internal/dagger"
)

type ChownPerfRepro struct{}

func (m *ChownPerfRepro) Run(ctx context.Context, fileCount int) (string, error) {
	if fileCount == 0 {
		fileCount = 1000
	}

	dir := dag.Container().From("alpine").
		WithExec([]string{"sh", "-c", fmt.Sprintf(
			"mkdir -p /src && for i in $(seq 1 %d); do echo $i > /src/file$i.txt; done", fileCount)}).
		Directory("/src")

	start := time.Now()
	_, err := dag.Container().From("alpine").
		WithMountedDirectory("/mnt", dir, dagger.ContainerWithMountedDirectoryOpts{
			Owner: "1000:1000",
		}).
		WithExec([]string{"true"}).
		Sync(ctx)
	if err != nil {
		return "", err
	}
	withOwner := time.Since(start)

	start = time.Now()
	_, err = dag.Container().From("alpine").
		WithMountedDirectory("/mnt", dir).
		WithExec([]string{"true"}).
		Sync(ctx)
	if err != nil {
		return "", err
	}
	withoutOwner := time.Since(start)

	ratio := float64(withOwner) / float64(withoutOwner)
	return fmt.Sprintf("files=%d with_owner=%s without_owner=%s ratio=%.1fx",
		fileCount, withOwner, withoutOwner, ratio), nil
}

Output

0.18.5

$ chown-perf-repro2 cd /tmp/chown-perf-repro2 && /tmp/dagger_0_18_5 call run --file-count=100000
✔ connect 0.5s
✔ load module 0.8s
✔ parsing command line arguments 0.0s

✔ chownPerfRepro: ChownPerfRepro! 0.0s
✔ .run(fileCount: 100000): String! 26.0s

files=100000 with_owner=24.69771825s without_owner=784.128631ms ratio=31.5x

Setup tracing at https://dagger.cloud/traces/setup. To hide set DAGGER_NO_NAG=1

A new release of dagger is available: v0.18.5 → v0.19.8
To upgrade, see https://docs.dagger.io/install
https://github.com/dagger/dagger/releases/tag/v0.19.8
➜  chown-perf-repro2 dagger call call run --file-count=100000
✔ connect 10.1s
✔ load module: . 11.8s
✘ parsing command line arguments 0.0s ERROR
! unknown command "call" for "dagger call"

Setup tracing at https://dagger.cloud/traces/setup. To hide set DAGGER_NO_NAG=1

0.19.8

$  chown-perf-repro2 dagger call run --file-count=100000
✔ connect 0.5s
✔ load module: . 0.7s
✔ parsing command line arguments 0.0s

✔ chownPerfRepro: ChownPerfRepro! 0.0s
✔ .run(fileCount: 100000): String! 3m38s

files=100000 with_owner=3m30.378930021s without_owner=970.462954ms ratio=216.8x

Setup tracing at https://dagger.cloud/traces/setup. To hide set DAGGER_NO_NAG=1

Explored solution

Instead of chowning at mount definition time, chown at exec time using fscopy.Copy

Activity

  1. grouville commented on Dec 11, 2025

    @grouville
    MemberAuthor

    I have a PR pending, shipping it in a bit

  2. self-assigned this
    on Dec 11, 2025
  3. sipsma commented on Dec 30, 2025

    @sipsma
    Contributor

    Fixed via #11559

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions