@@ -24,7 +24,7 @@ type DefinitionOp struct {
24
24
platforms map [digest.Digest ]* ocispecs.Platform
25
25
dgst digest.Digest
26
26
index pb.OutputIndex
27
- inputCache map [digest. Digest ][] * DefinitionOp
27
+ inputCache * sync. Map // shared and written among DefinitionOps so avoid race on this map using sync.Map
28
28
}
29
29
30
30
// NewDefinitionOp returns a new operation from a marshalled definition.
@@ -101,7 +101,7 @@ func NewDefinitionOp(def *pb.Definition) (*DefinitionOp, error) {
101
101
platforms : platforms ,
102
102
dgst : dgst ,
103
103
index : index ,
104
- inputCache : make ( map [digest. Digest ][] * DefinitionOp ),
104
+ inputCache : new (sync. Map ),
105
105
}, nil
106
106
}
107
107
@@ -180,6 +180,18 @@ func (d *DefinitionOp) Output() Output {
180
180
}}
181
181
}
182
182
183
+ func (d * DefinitionOp ) loadInputCache (dgst digest.Digest ) ([]* DefinitionOp , bool ) {
184
+ a , ok := d .inputCache .Load (dgst .String ())
185
+ if ok {
186
+ return a .([]* DefinitionOp ), true
187
+ }
188
+ return nil , false
189
+ }
190
+
191
+ func (d * DefinitionOp ) storeInputCache (dgst digest.Digest , c []* DefinitionOp ) {
192
+ d .inputCache .Store (dgst .String (), c )
193
+ }
194
+
183
195
func (d * DefinitionOp ) Inputs () []Output {
184
196
if d .dgst == "" {
185
197
return nil
@@ -195,7 +207,7 @@ func (d *DefinitionOp) Inputs() []Output {
195
207
for _ , input := range op .Inputs {
196
208
var vtx * DefinitionOp
197
209
d .mu .Lock ()
198
- if existingIndexes , ok := d .inputCache [ input .Digest ] ; ok {
210
+ if existingIndexes , ok := d .loadInputCache ( input .Digest ) ; ok {
199
211
if int (input .Index ) < len (existingIndexes ) && existingIndexes [input .Index ] != nil {
200
212
vtx = existingIndexes [input .Index ]
201
213
}
@@ -211,14 +223,14 @@ func (d *DefinitionOp) Inputs() []Output {
211
223
inputCache : d .inputCache ,
212
224
sources : d .sources ,
213
225
}
214
- existingIndexes := d .inputCache [ input .Digest ]
226
+ existingIndexes , _ := d .loadInputCache ( input .Digest )
215
227
indexDiff := int (input .Index ) - len (existingIndexes )
216
228
if indexDiff >= 0 {
217
229
// make room in the slice for the new index being set
218
230
existingIndexes = append (existingIndexes , make ([]* DefinitionOp , indexDiff + 1 )... )
219
231
}
220
232
existingIndexes [input .Index ] = vtx
221
- d .inputCache [ input .Digest ] = existingIndexes
233
+ d .storeInputCache ( input .Digest , existingIndexes )
222
234
}
223
235
d .mu .Unlock ()
224
236
0 commit comments