Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ Speedup merge #65

Merged
merged 1 commit into from Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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