Skip to content

Commit

Permalink
Merge pull request #65 from AdityaVallabh/main
Browse files Browse the repository at this point in the history
⚡ Speedup merge
  • Loading branch information
magiconair committed Dec 8, 2022
2 parents c3d49a3 + 1cf5be4 commit 44ba29f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
15 changes: 5 additions & 10 deletions properties.go
Expand Up @@ -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)
}
}

// ----------------------------------------------------------------------------
Expand Down
31 changes: 31 additions & 0 deletions properties_test.go
Expand Up @@ -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"}
Expand Down

0 comments on commit 44ba29f

Please sign in to comment.