mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-28 09:41:57 +00:00
optimisation of Copy()ing/Merge()ing some report data structures
We consistently - pre-allocate in Copy() - merge the smaller into a copy of the larger in Merge() This doesn't make much of a difference overall, since there are comparatively few instances of these structures. But it costs little in the code, and the consistency alone is worth it.
This commit is contained in:
@@ -111,7 +111,7 @@ func (e MetadataTemplates) Copy() MetadataTemplates {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
result := MetadataTemplates{}
|
||||
result := make(MetadataTemplates, len(e))
|
||||
for k, v := range e {
|
||||
result[k] = v
|
||||
}
|
||||
@@ -124,10 +124,10 @@ func (e MetadataTemplates) Merge(other MetadataTemplates) MetadataTemplates {
|
||||
if e == nil && other == nil {
|
||||
return nil
|
||||
}
|
||||
result := make(MetadataTemplates, len(e))
|
||||
for k, v := range e {
|
||||
result[k] = v
|
||||
if len(other) > len(e) {
|
||||
e, other = other, e
|
||||
}
|
||||
result := e.Copy()
|
||||
for k, v := range other {
|
||||
if existing, ok := result[k]; !ok || existing.Priority < v.Priority {
|
||||
result[k] = v
|
||||
|
||||
@@ -60,7 +60,7 @@ func (e MetricTemplates) Copy() MetricTemplates {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
result := MetricTemplates{}
|
||||
result := make(MetricTemplates, len(e))
|
||||
for k, v := range e {
|
||||
result[k] = v
|
||||
}
|
||||
@@ -73,10 +73,10 @@ func (e MetricTemplates) Merge(other MetricTemplates) MetricTemplates {
|
||||
if e == nil && other == nil {
|
||||
return nil
|
||||
}
|
||||
result := make(MetricTemplates, len(e))
|
||||
for k, v := range e {
|
||||
result[k] = v
|
||||
if len(other) > len(e) {
|
||||
e, other = other, e
|
||||
}
|
||||
result := e.Copy()
|
||||
for k, v := range other {
|
||||
if existing, ok := result[k]; !ok || existing.Priority < v.Priority {
|
||||
result[k] = v
|
||||
|
||||
@@ -19,6 +19,9 @@ func (m Metrics) Lookup(key string) (Metric, bool) {
|
||||
// Merge merges two sets maps into a fresh set, performing set-union merges as
|
||||
// appropriate.
|
||||
func (m Metrics) Merge(other Metrics) Metrics {
|
||||
if len(other) > len(m) {
|
||||
m, other = other, m
|
||||
}
|
||||
result := m.Copy()
|
||||
for k, v := range other {
|
||||
if rv, ok := result[k]; ok {
|
||||
|
||||
@@ -294,10 +294,10 @@ func (t TableTemplates) Merge(other TableTemplates) TableTemplates {
|
||||
if t == nil && other == nil {
|
||||
return nil
|
||||
}
|
||||
result := make(TableTemplates, len(t))
|
||||
for k, v := range t {
|
||||
result[k] = v
|
||||
if len(other) > len(t) {
|
||||
t, other = other, t
|
||||
}
|
||||
result := t.Copy()
|
||||
for k, v := range other {
|
||||
if existing, ok := result[k]; ok {
|
||||
result[k] = v.Merge(existing)
|
||||
|
||||
@@ -174,10 +174,10 @@ func (n Nodes) Copy() Nodes {
|
||||
// Merge merges the other object into this one, and returns the result object.
|
||||
// The original is not modified.
|
||||
func (n Nodes) Merge(other Nodes) Nodes {
|
||||
cp := make(Nodes, len(n))
|
||||
for k, v := range n {
|
||||
cp[k] = v
|
||||
if len(other) > len(n) {
|
||||
n, other = other, n
|
||||
}
|
||||
cp := n.Copy()
|
||||
for k, v := range other {
|
||||
if n, ok := cp[k]; ok { // don't overwrite
|
||||
cp[k] = v.Merge(n)
|
||||
|
||||
Reference in New Issue
Block a user