Skip to content
Permalink
Browse files

Implement equality for Values

Equality doesn't require the same amount of sophistication than ordering
comparison. Implement an Equal function that does just that but does it
much faster.

Benchmark results show up to 96% improvement:
```
benchmark                                                         old ns/op     new ns/op     delta
BenchmarkDeducedSimple-12                                         110083        103256        -6.20%
BenchmarkDeducedNested-12                                         374366        374160        -0.06%
BenchmarkDeducedNestedAcrossVersion-12                            392864        405525        +3.22%
BenchmarkLeafConflictAcrossVersion-12                             89112         89070         -0.05%
BenchmarkMultipleApplierRecursiveRealConversion-12                1564330       1574620       +0.66%
BenchmarkOperations/Pod/Create-12                                 103693        103970        +0.27%
BenchmarkOperations/Pod/Apply-12                                  291760        291317        -0.15%
BenchmarkOperations/Pod/Update-12                                 193419        190470        -1.52%
BenchmarkOperations/Pod/UpdateVersion-12                          261692        251966        -3.72%
BenchmarkOperations/Node/Create-12                                152047        155710        +2.41%
BenchmarkOperations/Node/Apply-12                                 499187        473901        -5.07%
BenchmarkOperations/Node/Update-12                                299271        279142        -6.73%
BenchmarkOperations/Node/UpdateVersion-12                         438723        403125        -8.11%
BenchmarkOperations/Endpoints/Create-12                           12246         11940         -2.50%
BenchmarkOperations/Endpoints/Apply-12                            915806        924080        +0.90%
BenchmarkOperations/Endpoints/Update-12                           7155675       285092        -96.02%
BenchmarkOperations/Endpoints/UpdateVersion-12                    14278150      544040        -96.19%
BenchmarkOperations/CustomResourceDefinition/Create-12            1312734       1288472       -1.85%
BenchmarkOperations/CustomResourceDefinition/Apply-12             3346591       3376864       +0.90%
BenchmarkOperations/CustomResourceDefinition/Update-12            10681243      1758764       -83.53%
BenchmarkOperations/CustomResourceDefinition/UpdateVersion-12     19069925      2202330       -88.45%
```
  • Loading branch information
apelisse committed Nov 6, 2019
1 parent 1ad9a28 commit 9cd3089c0b85b7b49c1d3096dda422f3cd7c984c
Showing with 72 additions and 34 deletions.
  1. +3 −33 value/less_test.go
  2. +69 −1 value/value.go
@@ -29,39 +29,6 @@ func TestValueLess(t *testing.T) {
eq bool
}{
{
name: "Invalid-1",
a: Value{},
b: Value{},
eq: true,
}, {
name: "Invalid-2",
a: FloatValue(1),
b: Value{},
}, {
name: "Invalid-3",
a: IntValue(1),
b: Value{},
}, {
name: "Invalid-4",
a: StringValue("aoeu"),
b: Value{},
}, {
name: "Invalid-5",
a: BooleanValue(true),
b: Value{},
}, {
name: "Invalid-6",
a: Value{ListValue: &List{}},
b: Value{},
}, {
name: "Invalid-7",
a: Value{MapValue: &Map{}},
b: Value{},
}, {
name: "Invalid-8",
a: Value{Null: true},
b: Value{},
}, {
name: "Float-1",
a: FloatValue(1.14),
b: FloatValue(3.14),
@@ -296,6 +263,9 @@ func TestValueLess(t *testing.T) {
t.Run(table[i].name, func(t *testing.T) {
tt := table[i]
if tt.eq {
if !tt.a.Equals(tt.b) {
t.Errorf("oops, a != b: %#v, %#v", tt.a, tt.b)
}
if tt.a.Less(tt.b) {
t.Errorf("oops, a < b: %#v, %#v", tt.a, tt.b)
}
@@ -36,7 +36,44 @@ type Value struct {

// Equals returns true iff the two values are equal.
func (v Value) Equals(rhs Value) bool {
return !v.Less(rhs) && !rhs.Less(v)
if v.IntValue != nil && rhs.IntValue != nil {
return *v.IntValue == *rhs.IntValue
}
if v.FloatValue != nil || rhs.FloatValue != nil {
lf := float64(0.0)
if v.FloatValue != nil {
lf = float64(*v.FloatValue)
} else if v.IntValue != nil {
lf = float64(*v.IntValue)
} else {
return false
}
rf := 0.0
if rhs.FloatValue != nil {
rf = float64(*rhs.FloatValue)
} else if rhs.IntValue != nil {
rf = float64(*rhs.IntValue)
} else {
return false
}
return lf == rf
}
if v.StringValue != nil && rhs.StringValue != nil {
return *v.StringValue == *rhs.StringValue
}
if v.BooleanValue != nil && rhs.BooleanValue != nil {
return *v.BooleanValue == *rhs.BooleanValue
}
if v.ListValue != nil && rhs.ListValue != nil {
return v.ListValue.Equals(rhs.ListValue)
}
if v.MapValue != nil && rhs.MapValue != nil {
return v.MapValue.Equals(rhs.MapValue)
}
if v.Null && rhs.Null {
return true
}
return false
}

// Less provides a total ordering for Value (so that they can be sorted, even
@@ -187,6 +224,20 @@ type List struct {
Items []Value
}

// Equals compares two lists lexically.
func (l *List) Equals(rhs *List) bool {
if len(l.Items) != len(rhs.Items) {
return false
}

for i, lv := range l.Items {
if !lv.Equals(rhs.Items[i]) {
return false
}
}
return true
}

// Less compares two lists lexically.
func (l *List) Less(rhs *List) bool {
i := 0
@@ -244,6 +295,23 @@ func (m *Map) computeOrder() []int {
return m.order
}

// Equals compares two maps lexically.
func (m *Map) Equals(rhs *Map) bool {
if len(m.Items) != len(rhs.Items) {
return false
}
for _, lfield := range m.Items {
rfield, ok := rhs.Get(lfield.Name)
if !ok {
return false
}
if !lfield.Value.Equals(rfield.Value) {
return false
}
}
return true
}

// Less compares two maps lexically.
func (m *Map) Less(rhs *Map) bool {
var noAllocL, noAllocR [2]int

0 comments on commit 9cd3089

Please sign in to comment.
You can’t perform that action at this time.