Skip to content

Commit

Permalink
remove allocs from MapStr.Clone
Browse files Browse the repository at this point in the history
  • Loading branch information
urso committed Jul 11, 2017
1 parent 8e66d95 commit 521f10a
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions libbeat/common/mapstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ func (m MapStr) Clone() MapStr {
result := MapStr{}

for k, v := range m {
innerMap, err := toMapStr(v)
if err == nil {
result[k] = innerMap.Clone()
} else {
result[k] = v
if innerMap, ok := tryToMapStr(v); ok {
v = innerMap.Clone()
}
result[k] = v
}

return result
Expand Down Expand Up @@ -237,14 +235,21 @@ func AddTags(ms MapStr, tags []string) error {
// a MapStr or a map[string]interface{}. If it's any other type or nil then
// an error is returned.
func toMapStr(v interface{}) (MapStr, error) {
switch v.(type) {
m, ok := tryToMapStr(v)
if !ok {
return nil, errors.Errorf("expected map but type is %T", v)
}
return m, nil
}

func tryToMapStr(v interface{}) (MapStr, bool) {
switch m := v.(type) {
case MapStr:
return v.(MapStr), nil
return m, true
case map[string]interface{}:
m := v.(map[string]interface{})
return MapStr(m), nil
return MapStr(m), true
default:
return nil, errors.Errorf("expected map but type is %T", v)
return nil, false
}
}

Expand Down

0 comments on commit 521f10a

Please sign in to comment.