Skip to content

Commit

Permalink
Switch separate min/max compute to single function
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Dec 17, 2024
1 parent 6600e6d commit 6a24ced
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
32 changes: 13 additions & 19 deletions pkg/aggregation/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,32 +120,26 @@ func (s *TableAggregator) OrderedRows(sorter sorting.NameValueSorter) []*TableRo
return rows
}

func (s *TableAggregator) ComputeMin() (ret int64) {
ret = math.MaxInt64
func (s *TableAggregator) ComputeMinMax() (min, max int64) {
min, max = math.MaxInt64, math.MinInt64

for _, r := range s.rows {
for colKey := range s.cols {
if val := r.cols[colKey]; val < ret {
ret = val
val := r.cols[colKey]
if val < min {
min = val
}
if val > max {
max = val
}
}
}
if ret == math.MaxInt64 {
return 0
}
return
}

func (s *TableAggregator) ComputeMax() (ret int64) {
ret = math.MinInt64
for _, r := range s.rows {
for colKey := range s.cols {
if val := r.cols[colKey]; val > ret {
ret = val
}
}
if min == math.MinInt64 {
min = 0
}
if ret == math.MinInt64 {
return 0
if max == math.MaxInt64 {
max = 0
}
return
}
Expand Down
23 changes: 19 additions & 4 deletions pkg/aggregation/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func TestSimpleTable(t *testing.T) {
assert.Equal(t, int64(5), table.Sum())

// Minmax
assert.Equal(t, int64(0), table.ComputeMin())
assert.Equal(t, int64(3), table.ComputeMax())
min, max := table.ComputeMinMax()
assert.Equal(t, int64(0), min)
assert.Equal(t, int64(3), max)
}

func TestTableMultiIncrement(t *testing.T) {
Expand All @@ -74,8 +75,9 @@ func TestTableMultiIncrement(t *testing.T) {
assert.Equal(t, int64(6), table.Sum())

// Minmax
assert.Equal(t, int64(0), table.ComputeMin())
assert.Equal(t, int64(5), table.ComputeMax())
min, max := table.ComputeMinMax()
assert.Equal(t, int64(0), min)
assert.Equal(t, int64(5), max)
}

func TestSingleRowTable(t *testing.T) {
Expand Down Expand Up @@ -116,3 +118,16 @@ func TestTrimData(t *testing.T) {
assert.Len(t, table.Rows(), 1)
assert.Len(t, table.Rows()[0].cols, 5)
}

// BenchmarkMinMax-4 1020728 1234 ns/op 0 B/op 0 allocs/op
func BenchmarkMinMax(b *testing.B) {
table := NewTable(" ")
for i := 0; i < 10; i++ {
table.Sample(fmt.Sprintf("%d a", i))
table.Sample(fmt.Sprintf("%d b", i))
}

for i := 0; i < b.N; i++ {
table.ComputeMinMax()
}
}
14 changes: 9 additions & 5 deletions pkg/multiterm/termrenderers/heatmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ func (s *Heatmap) WriteFooter(idx int, line string) {

func (s *Heatmap) UpdateMinMaxFromData(agg *aggregation.TableAggregator) {
min := s.minVal
if !s.FixedMin {
min = agg.ComputeMin()
}
max := s.maxVal
if !s.FixedMax {
max = agg.ComputeMax()

if !s.FixedMin || !s.FixedMax {
tableMin, tableMax := agg.ComputeMinMax()
if !s.FixedMin {
min = tableMin
}
if !s.FixedMax {
max = tableMax
}
}

s.UpdateMinMax(min, max)
Expand Down
3 changes: 1 addition & 2 deletions pkg/multiterm/termrenderers/spark.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func NewSpark(term multiterm.MultilineTerm, rows, cols int) *Spark {
}

func (s *Spark) WriteTable(agg *aggregation.TableAggregator, rowSorter, colSorter sorting.NameValueSorter) {
minVal := agg.ComputeMin() // Optimization: ComputeMinMax()
maxVal := agg.ComputeMax()
minVal, maxVal := agg.ComputeMinMax()

colNames := agg.OrderedColumns(colSorter)
if len(colNames) > s.colCount {
Expand Down

0 comments on commit 6a24ced

Please sign in to comment.