Skip to content

Commit e83c0a1

Browse files
authored
Merge pull request #87 from logicalhan/optimize
check lengths of maps and recurse over only one if it is necessary
2 parents cb8f3b5 + 93727de commit e83c0a1

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

merge.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,8 @@ func matchesValue(av, bv interface{}) bool {
307307
return true
308308
case map[string]interface{}:
309309
bt := bv.(map[string]interface{})
310-
for key := range at {
311-
if !matchesValue(at[key], bt[key]) {
312-
return false
313-
}
310+
if len(bt) != len(at) {
311+
return false
314312
}
315313
for key := range bt {
316314
if !matchesValue(at[key], bt[key]) {

merge_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package jsonpatch
22

33
import (
4+
"fmt"
45
"strings"
56
"testing"
67
)
@@ -447,6 +448,45 @@ func TestCreateMergePatchComplexAddAll(t *testing.T) {
447448
}
448449
}
449450

451+
// createNestedMap created a series of nested map objects such that the number of
452+
// objects is roughly 2^n (precisely, 2^(n+1)-1).
453+
func createNestedMap(m map[string]interface{}, depth int, objectCount *int) {
454+
if depth == 0 {
455+
return
456+
}
457+
for i := 0; i< 2;i++ {
458+
nested := map[string]interface{}{}
459+
*objectCount += 1
460+
createNestedMap(nested, depth-1, objectCount)
461+
m[fmt.Sprintf("key-%v", i)] = nested
462+
}
463+
}
464+
465+
func benchmarkMatchesValueWithDeeplyNestedFields(depth int, b *testing.B) {
466+
a := map[string]interface{}{}
467+
objCount := 1
468+
createNestedMap(a, depth, &objCount)
469+
b.ResetTimer()
470+
b.Run(fmt.Sprintf("objectCount=%v", objCount), func(b *testing.B) {
471+
for i := 0; i < b.N; i++ {
472+
if !matchesValue(a, a) {
473+
b.Errorf("Should be equal")
474+
}
475+
}
476+
})
477+
}
478+
479+
func BenchmarkMatchesValue1(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(1, b) }
480+
func BenchmarkMatchesValue2(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(2, b) }
481+
func BenchmarkMatchesValue3(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(3, b) }
482+
func BenchmarkMatchesValue4(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(4, b) }
483+
func BenchmarkMatchesValue5(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(5, b) }
484+
func BenchmarkMatchesValue6(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(6, b) }
485+
func BenchmarkMatchesValue7(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(7, b) }
486+
func BenchmarkMatchesValue8(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(8, b) }
487+
func BenchmarkMatchesValue9(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(9, b) }
488+
func BenchmarkMatchesValue10(b *testing.B) { benchmarkMatchesValueWithDeeplyNestedFields(10, b) }
489+
450490
func TestCreateMergePatchComplexRemoveAll(t *testing.T) {
451491
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]} }`
452492
exp := `{"a":null,"f":null,"hello":null,"i":null,"n":null,"nested":null,"pi":null,"t":null}`

0 commit comments

Comments
 (0)