@@ -167,7 +167,7 @@ type Map struct {
167
167
168
168
// may be nil; lazily constructed.
169
169
// TODO: Direct modifications to Items above will cause serious problems.
170
- index map [string ]* Field
170
+ index map [string ]int
171
171
// may be empty; lazily constructed.
172
172
// TODO: Direct modifications to Items above will cause serious problems.
173
173
order []int
@@ -264,41 +264,27 @@ func (m *Map) Less(rhs *Map) bool {
264
264
// Get returns the (Field, true) or (nil, false) if it is not present
265
265
func (m * Map ) Get (key string ) (* Field , bool ) {
266
266
if m .index == nil {
267
- m .index = map [string ]* Field {}
267
+ m .index = map [string ]int {}
268
268
for i := range m .Items {
269
- f := & m .Items [i ]
270
- m .index [f .Name ] = f
269
+ m .index [m .Items [i ].Name ] = i
271
270
}
272
271
}
273
272
f , ok := m .index [key ]
274
- return f , ok
273
+ if ! ok {
274
+ return nil , false
275
+ }
276
+ return & m .Items [f ], true
275
277
}
276
278
277
279
// Set inserts or updates the given item.
278
280
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
- }
283
281
if f , ok := m .Get (key ); ok {
284
282
f .Value = value
285
283
return
286
284
}
287
- f0 := & m .Items [0 ]
288
285
m .Items = append (m .Items , Field {Name : key , Value : value })
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
- }
286
+ i := len (m .Items ) - 1
287
+ m .index [key ] = i
302
288
m .order = nil
303
289
}
304
290
0 commit comments