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

optimisation: pre-allocate, and fewer slices during summarisation #3002

Merged
merged 1 commit into from
Dec 25, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion render/detailed/parents.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ var (

// Parents renders the parents of this report.Node, which have been aggregated
// from the probe reports.
func Parents(r report.Report, n report.Node) (result []Parent) {
func Parents(r report.Report, n report.Node) []Parent {
if n.Parents.Size() == 0 {
return nil
}
result := make([]Parent, 0, n.Parents.Size())
topologyIDs := []string{}
for topologyID := range getLabelForTopology {
topologyIDs = append(topologyIDs, topologyID)
Expand Down Expand Up @@ -80,6 +84,9 @@ func Parents(r report.Report, n report.Node) (result []Parent) {
})
}
}
if len(result) == 0 {
return nil
}
return result
}

Expand Down
22 changes: 15 additions & 7 deletions report/metadata_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type MetadataTemplate struct {
From string `json:"from,omitempty"` // Defines how to get the value from a report node
}

// MetadataRows returns the rows for a node
func (t MetadataTemplate) MetadataRows(n Node) []MetadataRow {
// MetadataRow returns the row for a node
func (t MetadataTemplate) MetadataRow(n Node) (MetadataRow, bool) {
from := fromDefault
switch t.From {
case FromLatest:
Expand All @@ -40,16 +40,16 @@ func (t MetadataTemplate) MetadataRows(n Node) []MetadataRow {
from = fromCounters
}
if val, ok := from(n, t.ID); ok {
return []MetadataRow{{
return MetadataRow{
ID: t.ID,
Label: t.Label,
Value: val,
Truncate: t.Truncate,
Datatype: t.Datatype,
Priority: t.Priority,
}}
}, true
}
return nil
return MetadataRow{}, false
}

func fromDefault(n Node, key string) (string, bool) {
Expand Down Expand Up @@ -90,9 +90,17 @@ type MetadataTemplates map[string]MetadataTemplate

// MetadataRows returns the rows for a node
func (e MetadataTemplates) MetadataRows(n Node) []MetadataRow {
var rows []MetadataRow
if len(e) == 0 {
return nil
}
rows := make([]MetadataRow, 0, len(e))
for _, template := range e {
rows = append(rows, template.MetadataRows(n)...)
if row, ok := template.MetadataRow(n); ok {
rows = append(rows, row)
}
}
if len(rows) == 0 {
return nil
}
sort.Sort(MetadataRowsByPriority(rows))
return rows
Expand Down
20 changes: 14 additions & 6 deletions report/metric_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ type MetricTemplate struct {
Priority float64 `json:"priority,omitempty"`
}

// MetricRows returns the rows for a node
func (t MetricTemplate) MetricRows(n Node) []MetricRow {
// MetricRow returns the row for a node
func (t MetricTemplate) MetricRow(n Node) (MetricRow, bool) {
metric, ok := n.Metrics.Lookup(t.ID)
if !ok {
return nil
return MetricRow{}, false
}
row := MetricRow{
ID: t.ID,
Expand All @@ -31,17 +31,25 @@ func (t MetricTemplate) MetricRows(n Node) []MetricRow {
if s, ok := metric.LastSample(); ok {
row.Value = toFixed(s.Value, 2)
}
return []MetricRow{row}
return row, true
}

// MetricTemplates is a mergeable set of metric templates
type MetricTemplates map[string]MetricTemplate

// MetricRows returns the rows for a node
func (e MetricTemplates) MetricRows(n Node) []MetricRow {
var rows []MetricRow
if len(e) == 0 {
return nil
}
rows := make([]MetricRow, 0, len(e))
for _, template := range e {
rows = append(rows, template.MetricRows(n)...)
if row, ok := template.MetricRow(n); ok {
rows = append(rows, row)
}
}
if len(rows) == 0 {
return nil
}
sort.Sort(MetricRowsByPriority(rows))
return rows
Expand Down
5 changes: 4 additions & 1 deletion report/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ type TableTemplates map[string]TableTemplate

// Tables renders the TableTemplates for a given node.
func (t TableTemplates) Tables(node Node) []Table {
var result []Table
if len(t) == 0 {
return nil
}
result := make([]Table, 0, len(t))
for _, template := range t {
rows, truncationCount := node.ExtractTable(template)
// Extract the type from the template; default to
Expand Down