From c3a67d4727b0cb8a9ee887790187d2dc9aa15a16 Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Mon, 7 Oct 2024 11:57:29 -0500 Subject: [PATCH] move compute cache key function to its own function Signed-off-by: Jonathan A. Sternberg --- solver/edge.go | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/solver/edge.go b/solver/edge.go index 6910187f0453e..2ab876c9be048 100644 --- a/solver/edge.go +++ b/solver/edge.go @@ -783,19 +783,9 @@ func (e *edge) createInputRequests(desiredState edgeStatusType, f *pipeFactory, } else { debugSchedulerSkipInputRequestBasedOnDepState(e, dep, desiredStateDep) } + // initialize function to compute cache key based on dependency result - if dep.state == edgeStatusComplete && dep.slowCacheReq == nil && (e.slowCacheFunc(dep) != nil || e.preprocessFunc(dep) != nil) && e.cacheMap != nil { - pfn := e.preprocessFunc(dep) - fn := e.slowCacheFunc(dep) - res := dep.result - func(pfn PreprocessFunc, fn ResultBasedCacheFunc, res Result, index Index) { - dep.slowCacheReq = f.NewFuncRequest(func(ctx context.Context) (interface{}, error) { - v, err := e.op.CalcSlowCache(ctx, index, pfn, fn, res) - return v, errors.Wrap(err, "failed to compute cache key") - }) - }(pfn, fn, res, dep.index) - addedNew = true - } + addedNew = e.computeCacheKeyFromDep(dep, f) || addedNew } return addedNew } @@ -872,6 +862,26 @@ func (e *edge) createOutgoingRequest(dep *dep, desiredStateDep edgeStatusType, f return true } +func (e *edge) computeCacheKeyFromDep(dep *dep, f *pipeFactory) (addedNew bool) { + if dep.state != edgeStatusComplete || dep.slowCacheReq != nil || e.cacheMap == nil { + return false + } + + pfn := e.preprocessFunc(dep) + fn := e.slowCacheFunc(dep) + if pfn == nil && fn == nil { + return false + } + + res := dep.result + index := dep.index + dep.slowCacheReq = f.NewFuncRequest(func(ctx context.Context) (interface{}, error) { + v, err := e.op.CalcSlowCache(ctx, index, pfn, fn, res) + return v, errors.Wrap(err, "failed to compute cache key") + }) + return true +} + // execIfPossible creates a request for getting the edge result if there is // enough state func (e *edge) execIfPossible(f *pipeFactory) bool {