diff --git a/properties.go b/properties.go index 62ae2d6..f001b03 100644 --- a/properties.go +++ b/properties.go @@ -700,22 +700,17 @@ func (p *Properties) Delete(key string) { // Merge merges properties, comments and keys from other *Properties into p func (p *Properties) Merge(other *Properties) { + for _, k := range other.k { + if _, ok := p.m[k]; !ok { + p.k = append(p.k, k) + } + } for k, v := range other.m { p.m[k] = v } for k, v := range other.c { p.c[k] = v } - -outer: - for _, otherKey := range other.k { - for _, key := range p.k { - if otherKey == key { - continue outer - } - } - p.k = append(p.k, otherKey) - } } // ---------------------------------------------------------------------------- diff --git a/properties_test.go b/properties_test.go index 33f516c..55d4162 100644 --- a/properties_test.go +++ b/properties_test.go @@ -913,6 +913,37 @@ func TestLoad(t *testing.T) { // ---------------------------------------------------------------------------- +var inputs = []struct { + input int +}{ + {input: 1e2}, + {input: 1e3}, + {input: 1e4}, + {input: 1e5}, +} + +func BenchmarkMerge(b *testing.B) { + for _, v := range inputs { + p := generateProperties(v.input) + b.Run(fmt.Sprintf("num_properties_%d", v.input), func(b *testing.B) { + for i := 0; i < b.N; i++ { + p.Merge(p) + } + }) + } +} + +func generateProperties(n int) *Properties { + p := NewProperties() + for i := 0; i < n; i++ { + s := fmt.Sprintf("%v", i) + p.Set(s, s) + } + return p +} + +// ---------------------------------------------------------------------------- + // tests all combinations of delimiters, leading and/or trailing whitespace and newlines. func testWhitespaceAndDelimiterCombinations(t *testing.T, key, value string) { whitespace := []string{"", " ", "\f", "\t"}