File tree 1 file changed +18
-1
lines changed
1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -276,12 +276,29 @@ func (m *Map) Get(key string) (*Field, bool) {
276
276
277
277
// Set inserts or updates the given item.
278
278
func (m * Map ) Set (key string , value Value ) {
279
+ if len (m .Items ) < 1 {
280
+ m .Items = append (m .Items , Field {Name : key , Value : value })
281
+ return
282
+ }
279
283
if f , ok := m .Get (key ); ok {
280
284
f .Value = value
281
285
return
282
286
}
287
+ f0 := & m .Items [0 ]
283
288
m .Items = append (m .Items , Field {Name : key , Value : value })
284
- m .index = nil // Since the append might have reallocated
289
+ if f0 == & m .Items [0 ] {
290
+ // No reallocation, it's safe to just update the map
291
+ i := len (m .Items ) - 1
292
+ f := & m .Items [i ]
293
+ m .index [f .Name ] = f
294
+ } else {
295
+ // The slice was reallocated, so we need to update all the
296
+ // pointers in the map.
297
+ for i := range m .Items {
298
+ f := & m .Items [i ]
299
+ m .index [f .Name ] = f
300
+ }
301
+ }
285
302
m .order = nil
286
303
}
287
304
You can’t perform that action at this time.
0 commit comments