Skip to content

Commit cb9c039

Browse files
committed
don't throw away a freshly made map
1 parent 0c1d754 commit cb9c039

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

value/value.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,29 @@ func (m *Map) Get(key string) (*Field, bool) {
276276

277277
// Set inserts or updates the given item.
278278
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+
}
279283
if f, ok := m.Get(key); ok {
280284
f.Value = value
281285
return
282286
}
287+
f0 := &m.Items[0]
283288
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+
}
285302
m.order = nil
286303
}
287304

0 commit comments

Comments
 (0)