Skip to content

Commit fac4117

Browse files
authored
[perf] memoize cache setup status (#2100)
## Summary TSIA This still means an extra ~50-100ms because we check the status but this is probably fine. ## How was it tested? The `defer debug.FunctionTimer().End()` call in Status only prints once.
1 parent 56af312 commit fac4117

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

internal/devbox/providers/nixcache/nixcache.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"go.jetpack.io/pkg/filecache"
2424
)
2525

26-
var cachedCredentials = goutil.OnceWithContext(
26+
var cachedCredentials = goutil.OnceValuesWithContext(
2727
func(ctx context.Context) (AWSCredentials, error) {
2828
// Adding version to caches to avoid conflicts if we want to update the schema
2929
// or while working on dev.
@@ -85,7 +85,7 @@ func Caches(
8585
return resp.GetCaches(), nil
8686
}
8787

88-
var cachedReadCaches = goutil.OnceWithContext(
88+
var cachedReadCaches = goutil.OnceValuesWithContext(
8989
func(ctx context.Context) ([]*nixv1alpha1.NixBinCache, error) {
9090
caches, err := Caches(ctx)
9191
if err != nil {
@@ -102,7 +102,7 @@ func CachedReadCaches(ctx context.Context) ([]*nixv1alpha1.NixBinCache, error) {
102102
}
103103

104104
func DisableReadCaches() {
105-
cachedReadCaches = goutil.OnceWithContext(
105+
cachedReadCaches = goutil.OnceValuesWithContext(
106106
func(ctx context.Context) ([]*nixv1alpha1.NixBinCache, error) {
107107
return nil, nil
108108
},

internal/devpkg/narinfo_cache.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"go.jetpack.io/devbox/internal/boxcli/featureflag"
1717
"go.jetpack.io/devbox/internal/debug"
1818
"go.jetpack.io/devbox/internal/devbox/providers/nixcache"
19+
"go.jetpack.io/devbox/internal/goutil"
1920
"go.jetpack.io/devbox/internal/lock"
2021
"go.jetpack.io/devbox/internal/nix"
2122
"golang.org/x/sync/errgroup"
@@ -339,9 +340,11 @@ func fetchNarInfoStatusFromS3(
339340
return fetch.(func() (bool, error))()
340341
}
341342

343+
var nixCacheIsConfigured = goutil.OnceValueWithContext(nixcache.IsConfigured)
344+
342345
func readCaches(ctx context.Context) ([]string, error) {
343346
cacheURIs := []string{binaryCache}
344-
if !nixcache.IsConfigured(ctx) {
347+
if !nixCacheIsConfigured.Do(ctx) {
345348
return cacheURIs, nil
346349
}
347350

internal/goutil/sync.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,35 @@ import (
55
"sync"
66
)
77

8-
type once[T any] struct {
8+
type onceValue[T any] struct {
9+
once sync.Once
10+
fn func(context.Context) T
11+
result T
12+
}
13+
14+
func OnceValueWithContext[T any](fn func(context.Context) T) *onceValue[T] {
15+
return &onceValue[T]{fn: fn}
16+
}
17+
18+
func (o *onceValue[T]) Do(ctx context.Context) T {
19+
o.once.Do(func() {
20+
o.result = o.fn(ctx)
21+
})
22+
return o.result
23+
}
24+
25+
type onceValues[T any] struct {
926
once sync.Once
1027
fn func(context.Context) (T, error)
1128
result T
1229
err error
1330
}
1431

15-
func OnceWithContext[T any](fn func(context.Context) (T, error)) *once[T] {
16-
return &once[T]{fn: fn}
32+
func OnceValuesWithContext[T any](fn func(context.Context) (T, error)) *onceValues[T] {
33+
return &onceValues[T]{fn: fn}
1734
}
1835

19-
func (o *once[T]) Do(ctx context.Context) (T, error) {
36+
func (o *onceValues[T]) Do(ctx context.Context) (T, error) {
2037
o.once.Do(func() {
2138
o.result, o.err = o.fn(ctx)
2239
})

internal/setup/setup.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const (
9494

9595
// Status returns the status of a setup task.
9696
func Status(ctx context.Context, key string, task Task) TaskStatus {
97+
defer debug.FunctionTimer().End()
9798
state := loadState(key)
9899
switch {
99100
case isSudo(key):

0 commit comments

Comments
 (0)