Skip to content

Commit 2285a3e

Browse files
authored
[nix-cache] Prevent checking cache twice per package (#2055)
## Summary Fixes bug described here #2054 A better (but a bit more involved) solution is to parallelize by output. Currently we parallelize by meta-output (which includes `__default_output__`). This has two downsides: * `__default_output__` can be multiple outputs, in which case we don't parallelize them at all. * We still do redundant work when `__default_output__` is multiple outputs. (once as `__default_output__` and once for each individual outout that forms part of defaults). ## How was it tested?
1 parent b23231a commit 2285a3e

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

internal/devpkg/narinfo_cache.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"slices"
9+
"strings"
810
"sync"
911
"time"
1012

@@ -107,13 +109,30 @@ func (p *Package) fetchNarInfoStatusOnce(output string) (bool, error) {
107109
type inCacheFunc func() (bool, error)
108110
f, ok := narInfoStatusFnCache.Load(p.Raw)
109111
if !ok {
110-
key := fmt.Sprintf("%s^%s", p.Raw, output)
111112
f = inCacheFunc(sync.OnceValues(func() (bool, error) { return p.fetchNarInfoStatus(output) }))
112-
f, _ = narInfoStatusFnCache.LoadOrStore(key, f)
113+
f, _ = narInfoStatusFnCache.LoadOrStore(p.keyForOutput(output), f)
113114
}
114115
return f.(inCacheFunc)()
115116
}
116117

118+
func (p *Package) keyForOutput(output string) string {
119+
if output == useDefaultOutput {
120+
sysInfo, err := p.sysInfoIfExists()
121+
// let's be super safe to always avoid empty key.
122+
if err == nil && sysInfo != nil && len(sysInfo.DefaultOutputs()) > 0 {
123+
names := make([]string, len(sysInfo.DefaultOutputs()))
124+
for i, o := range sysInfo.DefaultOutputs() {
125+
names[i] = o.Name
126+
}
127+
slices.Sort(names)
128+
output = strings.Join(names, ",")
129+
}
130+
}
131+
fmt.Println("output: ", output)
132+
133+
return fmt.Sprintf("%s^%s", p.Raw, output)
134+
}
135+
117136
// fetchNarInfoStatus fetches the cache status for the package. It returns
118137
// true if cache exists, false otherwise.
119138
// NOTE: This function always performs an HTTP request and should not be called

0 commit comments

Comments
 (0)