From 1cf5be4d7507d73458ba031078da3e2c1947b038 Mon Sep 17 00:00:00 2001 From: Aditya Vallabh Ganti Date: Tue, 8 Nov 2022 17:23:26 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Speedup=20merge?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- properties.go | 15 +++++---------- properties_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) 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"}