Skip to content

Commit 73f53b7

Browse files
add benchmark for Settings.toInternal + optimizations (#1)
1 parent 23ceb17 commit 73f53b7

File tree

4 files changed

+37
-10
lines changed

4 files changed

+37
-10
lines changed

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/segmentio/go-hll
2+
3+
go 1.12
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.0
7+
github.com/pkg/errors v0.8.0
8+
github.com/pmezard/go-difflib v1.0.0
9+
github.com/stretchr/testify v1.2.2
10+
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
4+
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
8+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

settings.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,21 @@ type settings struct {
130130
// This function will also compute and populate auto-generated thresholds and
131131
// constant values used by the Hll calculations and cache the result.
132132
func (s Settings) toInternal() (*settings, error) {
133-
134133
if err := s.validate(); err != nil {
135134
return nil, err
136135
}
137136

138-
log2m := s.Log2m
139-
regwidth := s.Regwidth
137+
settingsCacheLock.RLock()
138+
cachedSettings := settingsCache[s]
139+
settingsCacheLock.RUnlock()
140140

141-
// try to get the settings
142-
if settings := func() *settings {
143-
settingsCacheLock.RLock()
144-
defer settingsCacheLock.RUnlock()
145-
return settingsCache[s]
146-
}(); settings != nil {
147-
return settings, nil
141+
if cachedSettings != nil {
142+
return cachedSettings, nil
148143
}
149144

145+
log2m := s.Log2m
146+
regwidth := s.Regwidth
147+
150148
var explicitThreshold int
151149
explicitAuto := s.ExplicitThreshold == AutoExplicitThreshold
152150
if explicitAuto {

settings_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,14 @@ func resetDefaults() {
136136
defaultSettings = nil
137137
defaultSettingsLock.Unlock()
138138
}
139+
140+
func BenchmarkSettingsToInternal(b *testing.B) {
141+
s := Settings{
142+
Log2m: 11,
143+
Regwidth: 5,
144+
}
145+
146+
for i := 0; i < b.N; i++ {
147+
s.toInternal()
148+
}
149+
}

0 commit comments

Comments
 (0)