@@ -167,7 +167,7 @@ type Map struct {
167167
168168 // may be nil; lazily constructed.
169169 // TODO: Direct modifications to Items above will cause serious problems.
170- index map [string ]* Field
170+ index map [string ]int
171171 // may be empty; lazily constructed.
172172 // TODO: Direct modifications to Items above will cause serious problems.
173173 order []int
@@ -264,41 +264,27 @@ func (m *Map) Less(rhs *Map) bool {
264264// Get returns the (Field, true) or (nil, false) if it is not present
265265func (m * Map ) Get (key string ) (* Field , bool ) {
266266 if m .index == nil {
267- m .index = map [string ]* Field {}
267+ m .index = map [string ]int {}
268268 for i := range m .Items {
269- f := & m .Items [i ]
270- m .index [f .Name ] = f
269+ m .index [m .Items [i ].Name ] = i
271270 }
272271 }
273272 f , ok := m .index [key ]
274- return f , ok
273+ if ! ok {
274+ return nil , false
275+ }
276+ return & m .Items [f ], true
275277}
276278
277279// Set inserts or updates the given item.
278280func (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- }
283281 if f , ok := m .Get (key ); ok {
284282 f .Value = value
285283 return
286284 }
287- f0 := & m .Items [0 ]
288285 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
302288 m .order = nil
303289}
304290
0 commit comments