Skip to content

Commit

Permalink
lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
caio committed Apr 29, 2024
1 parent 6f7453e commit a09a005
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 86 deletions.
2 changes: 1 addition & 1 deletion serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func FromBytes(buf *bytes.Reader, options ...tdigestOption) (*TDigest, error) {
}

if encoding != smallEncoding {
return nil, fmt.Errorf("Unsupported encoding version: %d", encoding)
return nil, fmt.Errorf("unsupported encoding version: %d", encoding)
}

t, err := newWithoutSummary(options...)
Expand Down
2 changes: 1 addition & 1 deletion summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (s *summary) Len() int {

func (s *summary) Add(key float64, value uint64) error {
if math.IsNaN(key) {
return fmt.Errorf("Key must not be NaN")
return fmt.Errorf("key must not be NaN")
}
if value == 0 {
return fmt.Errorf("Count must be >0")
Expand Down
18 changes: 11 additions & 7 deletions tdigest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
// ElasticSearch), performance metrics for distributed systems, etc.
//
// After you create (and configure, if desired) the digest:
// digest, err := tdigest.New(tdigest.Compression(100))
//
// digest, err := tdigest.New(tdigest.Compression(100))
//
// You can then use it for registering measurements:
// digest.Add(number)
//
// digest.Add(number)
//
// Estimating quantiles:
// digest.Quantile(0.99)
//
// digest.Quantile(0.99)
//
// And merging with another digest:
// digest.Merge(otherDigest)
//
// digest.Merge(otherDigest)
package tdigest

import (
Expand Down Expand Up @@ -158,7 +162,7 @@ func boundedWeightedAverage(x1 float64, w1 float64, x2 float64, w2 float64) floa
// This will emit an error if `value` is NaN or if `count` is zero.
func (t *TDigest) AddWeighted(value float64, count uint64) (err error) {
if count == 0 {
return fmt.Errorf("Illegal datapoint <value: %.4f, count: %d>", value, count)
return fmt.Errorf("illegal datapoint <value: %.4f, count: %d>", value, count)
}

if t.summary.Len() == 0 {
Expand All @@ -174,7 +178,7 @@ func (t *TDigest) AddWeighted(value float64, count uint64) (err error) {

begin, end := t.findNeighbors(begin, value)

closest := t.chooseMergeCandidate(begin, end, value, count)
closest := t.chooseMergeCandidate(begin, end, count)

if closest == t.summary.Len() {
err = t.summary.Add(value, count)
Expand Down Expand Up @@ -366,7 +370,7 @@ func (t TDigest) findNeighbors(start int, value float64) (int, int) {
return start, lastNeighbor
}

func (t TDigest) chooseMergeCandidate(begin, end int, value float64, count uint64) int {
func (t TDigest) chooseMergeCandidate(begin, end int, count uint64) int {
closest := t.summary.Len()
sum := t.summary.HeadSum(begin)
var n float32
Expand Down
78 changes: 1 addition & 77 deletions tdigest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import (
"sort"
"testing"

"github.com/leesper/go_rng"
rng "github.com/leesper/go_rng"
"gonum.org/v1/gonum/stat"
)

func init() {
rand.Seed(0xDEADBEE)
}

func uncheckedNew(options ...tdigestOption) *TDigest {
t, _ := New(options...)
return t
Expand Down Expand Up @@ -794,78 +790,6 @@ func randomTDigest(compression float64) *TDigest {
return t
}

var sumSizes = []int{10, 100, 1000, 10000}

func BenchmarkSumLoopSimple(b *testing.B) {
for _, size := range sumSizes {
size := size
b.Run(fmt.Sprint(size), func(b *testing.B) {
benchmarkSumLoopSimple(b, size)
})
}
}

func benchmarkSumLoopSimple(b *testing.B, size int) {
counts := generateCounts(size)
indexes := generateIndexes(size)

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
for _, idx := range indexes {
_ = sumUntilIndexSimple(counts, idx)
}
}
}

func BenchmarkSumLoopUnrolled(b *testing.B) {
for _, size := range sumSizes {
size := size
b.Run(fmt.Sprint(size), func(b *testing.B) {
benchmarkSumLoopUnrolled(b, size)
})
}
}

func benchmarkSumLoopUnrolled(b *testing.B, size int) {
counts := generateCounts(size)
indexes := generateIndexes(size)

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
for _, idx := range indexes {
_ = sumUntilIndex(counts, idx)
}
}
}

func generateCounts(size int) []uint64 {
counts := make([]uint64, size)
for i := 0; i < size; i++ {
counts[i] = rand.Uint64()
}
return counts
}

func generateIndexes(size int) []int {
const num = 100

indexes := make([]int, num)
for i := 0; i < num; i++ {
indexes[i] = rand.Intn(size)
}
return indexes
}

func sumUntilIndexSimple(counts []uint64, idx int) uint64 {
var sum uint64
for _, c := range counts {
sum += uint64(c)
}
return sum
}

// Pathological ordered-input case.
func BenchmarkAddOrdered(b *testing.B) {
t, _ := New(Compression(100))
Expand Down

0 comments on commit a09a005

Please sign in to comment.