Skip to content

Commit

Permalink
Merge pull request #87 from logicalhan/optimize
Browse files Browse the repository at this point in the history
check lengths of maps and recurse over only one if it is necessary
  • Loading branch information
evanphx authored Aug 15, 2019
2 parents cb8f3b5 + 93727de commit e83c0a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
6 changes: 2 additions & 4 deletions merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,8 @@ func matchesValue(av, bv interface{}) bool {
return true
case map[string]interface{}:
bt := bv.(map[string]interface{})
for key := range at {
if !matchesValue(at[key], bt[key]) {
return false
}
if len(bt) != len(at) {
return false
}
for key := range bt {
if !matchesValue(at[key], bt[key]) {
Expand Down
40 changes: 40 additions & 0 deletions merge_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsonpatch

import (
"fmt"
"strings"
"testing"
)
Expand Down Expand Up @@ -447,6 +448,45 @@ func TestCreateMergePatchComplexAddAll(t *testing.T) {
}
}

// createNestedMap created a series of nested map objects such that the number of
// objects is roughly 2^n (precisely, 2^(n+1)-1).
func createNestedMap(m map[string]interface{}, depth int, objectCount *int) {
if depth == 0 {
return
}
for i := 0; i< 2;i++ {
nested := map[string]interface{}{}
*objectCount += 1
createNestedMap(nested, depth-1, objectCount)
m[fmt.Sprintf("key-%v", i)] = nested
}
}

func benchmarkMatchesValueWithDeeplyNestedFields(depth int, b *testing.B) {
a := map[string]interface{}{}
objCount := 1
createNestedMap(a, depth, &objCount)
b.ResetTimer()
b.Run(fmt.Sprintf("objectCount=%v", objCount), func(b *testing.B) {
for i := 0; i < b.N; i++ {
if !matchesValue(a, a) {
b.Errorf("Should be equal")
}
}
})
}

func BenchmarkMatchesValue1(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(1, b) }
func BenchmarkMatchesValue2(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(2, b) }
func BenchmarkMatchesValue3(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(3, b) }
func BenchmarkMatchesValue4(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(4, b) }
func BenchmarkMatchesValue5(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(5, b) }
func BenchmarkMatchesValue6(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(6, b) }
func BenchmarkMatchesValue7(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(7, b) }
func BenchmarkMatchesValue8(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(8, b) }
func BenchmarkMatchesValue9(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(9, b) }
func BenchmarkMatchesValue10(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(10, b) }

func TestCreateMergePatchComplexRemoveAll(t *testing.T) {
doc := `{"hello": "world","t": true ,"f": false, "n": null,"i": 123,"pi": 3.1416,"a": [1, 2, 3, 4], "nested": {"hello": "world","t": true ,"f": false, "n": null,"i": 123,"pi": 3.1416,"a": [1, 2, 3, 4]} }`
exp := `{"a":null,"f":null,"hello":null,"i":null,"n":null,"nested":null,"pi":null,"t":null}`
Expand Down

0 comments on commit e83c0a1

Please sign in to comment.