Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ini

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -114,3 +115,36 @@ func Benchmark_Key_SetValue_VisSection(b *testing.B) {
sec.Key("NAME").SetValue("10")
}
}

func Benchmark_NewKey_With_N_Keys(b *testing.B) {
for _, n := range []int{10, 100, 500, 1000} {
b.Run(fmt.Sprintf("keys=%d", n), func(b *testing.B) {
f := Empty()
sec, _ := f.NewSection("bench")
for i := 0; i < n; i++ {
sec.NewKey(fmt.Sprintf("key%d", i), "val")
}
target := fmt.Sprintf("key%d", n-1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
sec.NewKey(target, "newval")
}
})
}
}

func Benchmark_NewSection_With_N_Sections(b *testing.B) {
for _, n := range []int{10, 100, 500, 1000} {
b.Run(fmt.Sprintf("sections=%d", n), func(b *testing.B) {
f := Empty()
for i := 0; i < n; i++ {
f.NewSection(fmt.Sprintf("sec%d", i))
}
target := fmt.Sprintf("sec%d", n-1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
f.NewSection(target)
}
})
}
}
2 changes: 1 addition & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (f *File) NewSection(name string) (*Section, error) {
defer f.lock.Unlock()
}

if !f.options.AllowNonUniqueSections && inSlice(name, f.sectionList) {
if !f.options.AllowNonUniqueSections && f.sections[name] != nil {
return f.sections[name][0], nil
}

Expand Down
24 changes: 0 additions & 24 deletions helper.go

This file was deleted.

27 changes: 0 additions & 27 deletions helper_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion section.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (s *Section) NewKey(name, val string) (*Key, error) {
defer s.f.lock.Unlock()
}

if inSlice(name, s.keyList) {
if _, ok := s.keys[name]; ok {
if s.f.options.AllowShadows {
if err := s.keys[name].addShadow(val); err != nil {
return nil, err
Expand Down