Skip to content

Commit 4753406

Browse files
author
Antoine Pelisse
committed
Rent ValueInterfaces to avoid allocations
1 parent bef085d commit 4753406

16 files changed

+87
-31
lines changed

fieldpath/element.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func KeyByFields(nameValues ...interface{}) *value.FieldList {
127127
}
128128
out := value.FieldList{}
129129
for i := 0; i < len(nameValues)-1; i += 2 {
130-
out = append(out, value.Field{Name: nameValues[i].(string), Value: value.ValueInterface{Value: nameValues[i+1]}})
130+
out = append(out, value.Field{Name: nameValues[i].(string), Value: value.NewValueInterface(nameValues[i+1])})
131131
}
132132
out.Sort()
133133
return &out

fieldpath/element_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestPathElementSet(t *testing.T) {
6565
func strptr(s string) *string { return &s }
6666
func intptr(i int) *int { return &i }
6767
func valptr(i interface{}) *value.Value {
68-
v := value.Value(value.ValueInterface{Value: i})
68+
v := value.NewValueInterface(i)
6969
return &v
7070
}
7171

fieldpath/fromvalue_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestFromValue(t *testing.T) {
7171
if err != nil {
7272
t.Fatalf("couldn't parse: %v", err)
7373
}
74-
got := SetFromValue(value.ValueInterface{Value: v})
74+
got := SetFromValue(value.NewValueInterface(v))
7575
if !got.Equals(tt.set) {
7676
t.Errorf("wanted\n%s\nbut got\n%s\n", tt.set, got)
7777
}

fieldpath/path_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
var (
2626
_V = func(i interface{}) *value.Value {
27-
v := value.Value(value.ValueInterface{Value: i})
27+
v := value.NewValueInterface(i)
2828
return &v
2929
}
3030
)

fieldpath/pathelementmap_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@ func TestPathElementValueMap(t *testing.T) {
2929
t.Fatal("Unexpected path-element found in empty map")
3030
}
3131

32-
m.Insert(PathElement{FieldName: strptr("carrot")}, value.ValueInterface{Value: "knife"})
33-
m.Insert(PathElement{FieldName: strptr("chive")}, value.ValueInterface{Value: 2})
32+
m.Insert(PathElement{FieldName: strptr("carrot")}, value.NewValueInterface("knife"))
33+
m.Insert(PathElement{FieldName: strptr("chive")}, value.NewValueInterface(2))
3434

3535
if _, ok := m.Get(PathElement{FieldName: strptr("onion")}); ok {
3636
t.Fatal("Unexpected path-element in map")
3737
}
3838

3939
if val, ok := m.Get(PathElement{FieldName: strptr("carrot")}); !ok {
4040
t.Fatal("Missing path-element in map")
41-
} else if !value.Equals(val, value.ValueInterface{Value: "knife"}) {
41+
} else if !value.Equals(val, value.NewValueInterface("knife")) {
4242
t.Fatalf("Unexpected value found: %#v", val)
4343
}
4444

4545
if val, ok := m.Get(PathElement{FieldName: strptr("chive")}); !ok {
4646
t.Fatal("Missing path-element in map")
47-
} else if !value.Equals(val, value.ValueInterface{Value: 2}) {
47+
} else if !value.Equals(val, value.NewValueInterface(2)) {
4848
t.Fatalf("Unexpected value found: %#v", val)
4949
}
5050
}

merge/conflict_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var (
3030
_P = fieldpath.MakePathOrDie
3131
_KBF = fieldpath.KeyByFields
3232
_V = func(i interface{}) *value.Value {
33-
v := value.Value(value.ValueInterface{Value: i})
33+
v := value.NewValueInterface(i)
3434
return &v
3535
}
3636
)

typed/merge.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ func (w *mergingWalker) visitMapItems(t *schema.Map, lhs, rhs value.Map) (errs V
321321
if rhs != nil {
322322
if item, ok := rhs.Get(key); ok {
323323
rval = &item
324+
defer (*rval).Recycle()
324325
}
325326
}
326327
errs = append(errs, w.visitMapItem(t, out, key, &val, rval)...)
@@ -331,7 +332,8 @@ func (w *mergingWalker) visitMapItems(t *schema.Map, lhs, rhs value.Map) (errs V
331332
if rhs != nil {
332333
rhs.Iterate(func(key string, val value.Value) bool {
333334
if lhs != nil {
334-
if _, ok := lhs.Get(key); ok {
335+
if v, ok := lhs.Get(key); ok {
336+
v.Recycle()
335337
return true
336338
}
337339
}

typed/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ func (p ParseableType) FromYAML(object YAMLObject) (*TypedValue, error) {
9999
if err != nil {
100100
return nil, err
101101
}
102-
return AsTyped(value.ValueInterface{Value: v}, p.Schema, p.TypeRef)
102+
return AsTyped(value.NewValueInterface(v), p.Schema, p.TypeRef)
103103
}
104104

105105
// FromUnstructured converts a go interface to a TypedValue. It will return an
106106
// error if the resulting object fails schema validation.
107107
func (p ParseableType) FromUnstructured(in interface{}) (*TypedValue, error) {
108-
return AsTyped(value.ValueInterface{Value: in}, p.Schema, p.TypeRef)
108+
return AsTyped(value.NewValueInterface(in), p.Schema, p.TypeRef)
109109
}
110110

111111
// DeducedParseableType is a ParseableType that deduces the type from

typed/remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func removeItemsWithSchema(val value.Value, toRemove *fieldpath.Set, schema *sch
3333
toRemove: toRemove,
3434
}
3535
resolveSchema(schema, typeRef, &val, w)
36-
return value.ValueInterface{Value: w.out}
36+
return value.NewValueInterface(w.out)
3737
}
3838

3939
func (w *removingWalker) doScalar(t *schema.Scalar) ValidationErrors {

typed/toset_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var (
4343
_P = fieldpath.MakePathOrDie
4444
_KBF = fieldpath.KeyByFields
4545
_V = func(i interface{}) *value.Value {
46-
v := value.Value(value.ValueInterface{Value: i})
46+
v := value.NewValueInterface(i)
4747
return &v
4848
}
4949
)

0 commit comments

Comments
 (0)