Skip to content

Commit 90ff220

Browse files
sipsmatonistiigi
authored andcommitted
llbsolver: Fix performance of recomputeDigests
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)
1 parent 950e06d commit 90ff220

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

client/client_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ func TestIntegration(t *testing.T) {
195195
testMountStubsDirectory,
196196
testMountStubsTimestamp,
197197
testSourcePolicy,
198+
testLLBMountPerformance,
198199
)
199200
}
200201

@@ -8945,3 +8946,31 @@ func testSourcePolicy(t *testing.T, sb integration.Sandbox) {
89458946
require.ErrorContains(t, err, sourcepolicy.ErrSourceDenied.Error())
89468947
})
89478948
}
8949+
8950+
func testLLBMountPerformance(t *testing.T, sb integration.Sandbox) {
8951+
c, err := New(sb.Context(), sb.Address())
8952+
require.NoError(t, err)
8953+
defer c.Close()
8954+
8955+
mntInput := llb.Image("busybox:latest")
8956+
st := llb.Image("busybox:latest")
8957+
var mnts []llb.State
8958+
for i := 0; i < 20; i++ {
8959+
execSt := st.Run(
8960+
llb.Args([]string{"true"}),
8961+
)
8962+
mnts = append(mnts, mntInput)
8963+
for j := range mnts {
8964+
mnts[j] = execSt.AddMount(fmt.Sprintf("/tmp/bin%d", j), mnts[j], llb.SourcePath("/bin"))
8965+
}
8966+
st = execSt.Root()
8967+
}
8968+
8969+
def, err := st.Marshal(sb.Context())
8970+
require.NoError(t, err)
8971+
8972+
timeoutCtx, cancel := context.WithTimeout(sb.Context(), time.Minute)
8973+
defer cancel()
8974+
_, err = c.Solve(timeoutCtx, def, SolveOpt{}, nil)
8975+
require.NoError(t, err)
8976+
}

solver/llbsolver/vertex.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func recomputeDigests(ctx context.Context, all map[digest.Digest]*pb.Op, visited
210210
}
211211

212212
if !mutated {
213+
visited[dgst] = dgst
213214
return dgst, nil
214215
}
215216

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

275276
for {
276277
newDgst, ok := mutatedDigests[lastDgst]
277-
if !ok {
278+
if !ok || newDgst == lastDgst {
278279
break
279280
}
280281
lastDgst = newDgst

0 commit comments

Comments
 (0)