Skip to content

Commit ec6dd55

Browse files
authored
Merge pull request moby#5055 from tonistiigi/immutable-radix-v2
update immutable-radix and simplelru to v2
2 parents 8f32370 + a0773d1 commit ec6dd55

File tree

22 files changed

+647
-439
lines changed

22 files changed

+647
-439
lines changed

cache/contenthash/checksum.go

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"strings"
1212
"sync"
1313

14-
iradix "github.com/hashicorp/go-immutable-radix"
15-
"github.com/hashicorp/golang-lru/simplelru"
14+
iradix "github.com/hashicorp/go-immutable-radix/v2"
15+
simplelru "github.com/hashicorp/golang-lru/v2/simplelru"
1616
"github.com/moby/buildkit/cache"
1717
"github.com/moby/buildkit/session"
1818
"github.com/moby/buildkit/snapshot"
@@ -31,7 +31,7 @@ var defaultManagerOnce sync.Once
3131

3232
func getDefaultManager() *cacheManager {
3333
defaultManagerOnce.Do(func() {
34-
lru, _ := simplelru.NewLRU(20, nil) // error is impossible on positive size
34+
lru, _ := simplelru.NewLRU[string, *cacheContext](20, nil) // error is impossible on positive size
3535
defaultManager = &cacheManager{lru: lru, locker: locker.New()}
3636
})
3737
return defaultManager
@@ -85,7 +85,7 @@ type includedPath struct {
8585

8686
type cacheManager struct {
8787
locker *locker.Locker
88-
lru *simplelru.LRU
88+
lru *simplelru.LRU[string, *cacheContext]
8989
lruMu sync.Mutex
9090
}
9191

@@ -110,10 +110,10 @@ func (cm *cacheManager) GetCacheContext(ctx context.Context, md cache.RefMetadat
110110
cm.lruMu.Unlock()
111111
if ok {
112112
cm.locker.Unlock(md.ID())
113-
v.(*cacheContext).mu.Lock() // locking is required because multiple ImmutableRefs can reach this code; however none of them use the linkMap.
114-
v.(*cacheContext).linkMap = map[string][][]byte{}
115-
v.(*cacheContext).mu.Unlock()
116-
return v.(*cacheContext), nil
113+
v.mu.Lock() // locking is required because multiple ImmutableRefs can reach this code; however none of them use the linkMap.
114+
v.linkMap = map[string][][]byte{}
115+
v.mu.Unlock()
116+
return v, nil
117117
}
118118
cc, err := newCacheContext(md)
119119
if err != nil {
@@ -159,12 +159,12 @@ func (cm *cacheManager) clearCacheContext(id string) {
159159
type cacheContext struct {
160160
mu sync.RWMutex
161161
md cacheMetadata
162-
tree *iradix.Tree
162+
tree *iradix.Tree[*CacheRecord]
163163
dirty bool // needs to be persisted to disk
164164

165165
// used in HandleChange
166-
txn *iradix.Txn
167-
node *iradix.Node
166+
txn *iradix.Txn[*CacheRecord]
167+
node *iradix.Node[*CacheRecord]
168168
dirtyMap map[string]struct{}
169169
linkMap map[string][][]byte
170170
}
@@ -224,7 +224,7 @@ func (m *mount) clean() error {
224224
func newCacheContext(md cache.RefMetadata) (*cacheContext, error) {
225225
cc := &cacheContext{
226226
md: cacheMetadata{md},
227-
tree: iradix.New(),
227+
tree: iradix.New[*CacheRecord](),
228228
dirtyMap: map[string]struct{}{},
229229
linkMap: map[string][][]byte{},
230230
}
@@ -263,10 +263,10 @@ func (cc *cacheContext) save() error {
263263

264264
var l CacheRecords
265265
node := cc.tree.Root()
266-
node.Walk(func(k []byte, v interface{}) bool {
266+
node.Walk(func(k []byte, v *CacheRecord) bool {
267267
l.Paths = append(l.Paths, &CacheRecordWithPath{
268268
Path: string(k),
269-
Record: v.(*CacheRecord),
269+
Record: v,
270270
})
271271
return false
272272
})
@@ -294,7 +294,7 @@ func (cc *cacheContext) HandleChange(kind fsutil.ChangeKind, p string, fi os.Fil
294294

295295
deleteDir := func(cr *CacheRecord) {
296296
if cr.Type == CacheRecordTypeDir {
297-
cc.node.WalkPrefix(append(k, 0), func(k []byte, v interface{}) bool {
297+
cc.node.WalkPrefix(append(k, 0), func(k []byte, v *CacheRecord) bool {
298298
cc.txn.Delete(k)
299299
return false
300300
})
@@ -322,7 +322,7 @@ func (cc *cacheContext) HandleChange(kind fsutil.ChangeKind, p string, fi os.Fil
322322
if kind == fsutil.ChangeKindDelete {
323323
v, ok := cc.txn.Delete(k)
324324
if ok {
325-
deleteDir(v.(*CacheRecord))
325+
deleteDir(v)
326326
}
327327
d := path.Dir(p)
328328
if d == "/" {
@@ -344,7 +344,7 @@ func (cc *cacheContext) HandleChange(kind fsutil.ChangeKind, p string, fi os.Fil
344344

345345
v, ok := cc.node.Get(k)
346346
if ok {
347-
deleteDir(v.(*CacheRecord))
347+
deleteDir(v)
348348
}
349349

350350
cr := &CacheRecord{
@@ -371,7 +371,7 @@ func (cc *cacheContext) HandleChange(kind fsutil.ChangeKind, p string, fi os.Fil
371371
ln := path.Join("/", filepath.ToSlash(stat.Linkname))
372372
v, ok := cc.txn.Get(convertPathToKey(ln))
373373
if ok {
374-
cp := *v.(*CacheRecord)
374+
cp := *v
375375
cr = &cp
376376
}
377377
cc.linkMap[ln] = append(cc.linkMap[ln], k)
@@ -496,7 +496,7 @@ func (cc *cacheContext) includedPaths(ctx context.Context, m *mount, p string, o
496496
root = txn.Root()
497497
var (
498498
updated bool
499-
iter *iradix.Iterator
499+
iter *iradix.Iterator[*CacheRecord]
500500
k []byte
501501
keyOk bool
502502
origPrefix string
@@ -716,7 +716,7 @@ func shouldIncludePath(
716716
return true, nil
717717
}
718718

719-
func wildcardPrefix(root *iradix.Node, p string) (string, []byte, bool, error) {
719+
func wildcardPrefix(root *iradix.Node[*CacheRecord], p string) (string, []byte, bool, error) {
720720
// For consistency with what the copy implementation in fsutil
721721
// does: split pattern into non-wildcard prefix and rest of
722722
// pattern, then follow symlinks when resolving the non-wildcard
@@ -849,7 +849,7 @@ func (cc *cacheContext) scanChecksum(ctx context.Context, m *mount, p string, fo
849849
return cr, err
850850
}
851851

852-
func (cc *cacheContext) checksum(ctx context.Context, root *iradix.Node, txn *iradix.Txn, m *mount, k []byte, followTrailing bool) (*CacheRecord, bool, error) {
852+
func (cc *cacheContext) checksum(ctx context.Context, root *iradix.Node[*CacheRecord], txn *iradix.Txn[*CacheRecord], m *mount, k []byte, followTrailing bool) (*CacheRecord, bool, error) {
853853
origk := k
854854
k, cr, err := getFollowLinks(root, k, followTrailing)
855855
if err != nil {
@@ -930,7 +930,7 @@ func (cc *cacheContext) checksum(ctx context.Context, root *iradix.Node, txn *ir
930930

931931
// needsScan returns false if path is in the tree or a parent path is in tree
932932
// and subpath is missing.
933-
func (cc *cacheContext) needsScan(root *iradix.Node, path string, followTrailing bool) (bool, error) {
933+
func (cc *cacheContext) needsScan(root *iradix.Node[*CacheRecord], path string, followTrailing bool) (bool, error) {
934934
var (
935935
lastGoodPath string
936936
hasParentInTree bool
@@ -1047,7 +1047,7 @@ func (cc *cacheContext) scanPath(ctx context.Context, m *mount, p string, follow
10471047
type followLinksCallback func(path string, cr *CacheRecord) error
10481048

10491049
// getFollowLinks is shorthand for getFollowLinksCallback(..., nil).
1050-
func getFollowLinks(root *iradix.Node, k []byte, followTrailing bool) ([]byte, *CacheRecord, error) {
1050+
func getFollowLinks(root *iradix.Node[*CacheRecord], k []byte, followTrailing bool) ([]byte, *CacheRecord, error) {
10511051
return getFollowLinksCallback(root, k, followTrailing, nil)
10521052
}
10531053

@@ -1063,10 +1063,10 @@ func getFollowLinks(root *iradix.Node, k []byte, followTrailing bool) ([]byte, *
10631063
// The callback cb is called after each cache lookup done by
10641064
// getFollowLinksCallback, except for the first lookup where the verbatim key
10651065
// is looked up in the cache.
1066-
func getFollowLinksCallback(root *iradix.Node, k []byte, followTrailing bool, cb followLinksCallback) ([]byte, *CacheRecord, error) {
1066+
func getFollowLinksCallback(root *iradix.Node[*CacheRecord], k []byte, followTrailing bool, cb followLinksCallback) ([]byte, *CacheRecord, error) {
10671067
v, ok := root.Get(k)
1068-
if ok && (!followTrailing || v.(*CacheRecord).Type != CacheRecordTypeSymlink) {
1069-
return k, v.(*CacheRecord), nil
1068+
if ok && (!followTrailing || v.Type != CacheRecordTypeSymlink) {
1069+
return k, v, nil
10701070
}
10711071
if len(k) == 0 {
10721072
return k, nil, nil
@@ -1104,11 +1104,7 @@ func getFollowLinksCallback(root *iradix.Node, k []byte, followTrailing bool, cb
11041104
continue
11051105
}
11061106

1107-
cr = nil
1108-
v, ok := root.Get(convertPathToKey(nextPath))
1109-
if ok {
1110-
cr = v.(*CacheRecord)
1111-
}
1107+
cr, ok = root.Get(convertPathToKey(nextPath))
11121108
if cb != nil {
11131109
if err := cb(nextPath, cr); err != nil {
11141110
return nil, nil, err
@@ -1137,12 +1133,8 @@ func getFollowLinksCallback(root *iradix.Node, k []byte, followTrailing bool, cb
11371133
// trailing slash in the original path, we need to do the lookup again with
11381134
// the slash applied.
11391135
if hadTrailingSlash {
1140-
cr = nil
11411136
currentPath += "/"
1142-
v, ok := root.Get(convertPathToKey(currentPath))
1143-
if ok {
1144-
cr = v.(*CacheRecord)
1145-
}
1137+
cr, _ = root.Get(convertPathToKey(currentPath))
11461138
if cb != nil {
11471139
if err := cb(currentPath, cr); err != nil {
11481140
return nil, nil, err

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ require (
4040
github.com/google/go-cmp v0.6.0
4141
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
4242
github.com/hashicorp/go-cleanhttp v0.5.2
43-
github.com/hashicorp/go-immutable-radix v1.3.1
43+
github.com/hashicorp/go-immutable-radix/v2 v2.1.0
4444
github.com/hashicorp/go-multierror v1.1.1
45-
github.com/hashicorp/golang-lru v0.5.4
45+
github.com/hashicorp/golang-lru/v2 v2.0.0
4646
github.com/in-toto/in-toto-golang v0.5.0
4747
github.com/klauspost/compress v1.17.4
4848
github.com/mitchellh/hashstructure/v2 v2.0.2

go.sum

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,16 @@ github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/S
241241
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
242242
github.com/hashicorp/go-hclog v1.2.0 h1:La19f8d7WIlm4ogzNHB0JGqs5AUDAZ2UfCY4sJXcJdM=
243243
github.com/hashicorp/go-hclog v1.2.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
244-
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc=
245-
github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
244+
github.com/hashicorp/go-immutable-radix/v2 v2.1.0 h1:CUW5RYIcysz+D3B+l1mDeXrQ7fUvGGCwJfdASSzbrfo=
245+
github.com/hashicorp/go-immutable-radix/v2 v2.1.0/go.mod h1:hgdqLXA4f6NIjRVisM1TJ9aOJVNRqKZj+xDGF6m7PBw=
246246
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
247247
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
248248
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
249249
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
250-
github.com/hashicorp/go-uuid v1.0.0 h1:RS8zrF7PhGwyNPOtxSClXXj9HA8feRnJzgnI1RJCSnM=
251-
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
252-
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
253-
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
254-
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
250+
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
251+
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
252+
github.com/hashicorp/golang-lru/v2 v2.0.0 h1:Lf+9eD8m5pncvHAOCQj49GSN6aQI8XGfI5OpXNkoWaA=
253+
github.com/hashicorp/golang-lru/v2 v2.0.0/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
255254
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
256255
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
257256
github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
@@ -466,6 +465,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
466465
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
467466
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
468467
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
468+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
469+
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
469470
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
470471
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
471472
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=

vendor/github.com/hashicorp/go-immutable-radix/edges.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

vendor/github.com/hashicorp/go-immutable-radix/CHANGELOG.md renamed to vendor/github.com/hashicorp/go-immutable-radix/v2/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hashicorp/golang-lru/LICENSE renamed to vendor/github.com/hashicorp/go-immutable-radix/v2/LICENSE

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hashicorp/go-immutable-radix/README.md renamed to vendor/github.com/hashicorp/go-immutable-radix/v2/README.md

Lines changed: 12 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hashicorp/go-immutable-radix/v2/edges.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)