Skip to content

Commit

Permalink
llbsolver: Fix performance of recomputeDigests
Browse files Browse the repository at this point in the history
Before this, in the case where nothing was mutated the visited memo
would never be updated, thus causing exponential complexity.

Now the memo is updated even when nothing is mutated, just setting old
and new to be the same digest.

Signed-off-by: Erik Sipsma <[email protected]>
(cherry picked from commit ea69a59)
  • Loading branch information
sipsma authored and tonistiigi committed Mar 22, 2023
1 parent 950e06d commit 90ff220
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 29 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func TestIntegration(t *testing.T) {
testMountStubsDirectory,
testMountStubsTimestamp,
testSourcePolicy,
testLLBMountPerformance,
)
}

Expand Down Expand Up @@ -8945,3 +8946,31 @@ func testSourcePolicy(t *testing.T, sb integration.Sandbox) {
require.ErrorContains(t, err, sourcepolicy.ErrSourceDenied.Error())
})
}

func testLLBMountPerformance(t *testing.T, sb integration.Sandbox) {
c, err := New(sb.Context(), sb.Address())
require.NoError(t, err)
defer c.Close()

mntInput := llb.Image("busybox:latest")
st := llb.Image("busybox:latest")
var mnts []llb.State
for i := 0; i < 20; i++ {
execSt := st.Run(
llb.Args([]string{"true"}),
)
mnts = append(mnts, mntInput)
for j := range mnts {
mnts[j] = execSt.AddMount(fmt.Sprintf("/tmp/bin%d", j), mnts[j], llb.SourcePath("/bin"))
}
st = execSt.Root()
}

def, err := st.Marshal(sb.Context())
require.NoError(t, err)

timeoutCtx, cancel := context.WithTimeout(sb.Context(), time.Minute)
defer cancel()
_, err = c.Solve(timeoutCtx, def, SolveOpt{}, nil)
require.NoError(t, err)
}
3 changes: 2 additions & 1 deletion solver/llbsolver/vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func recomputeDigests(ctx context.Context, all map[digest.Digest]*pb.Op, visited
}

if !mutated {
visited[dgst] = dgst
return dgst, nil
}

Expand Down Expand Up @@ -274,7 +275,7 @@ func loadLLB(ctx context.Context, def *pb.Definition, polEngine SourcePolicyEval

for {
newDgst, ok := mutatedDigests[lastDgst]
if !ok {
if !ok || newDgst == lastDgst {
break
}
lastDgst = newDgst
Expand Down

0 comments on commit 90ff220

Please sign in to comment.