Skip to content

Commit

Permalink
don't Copy() self on Merge()
Browse files Browse the repository at this point in the history
Merge() is always returning a copy, so there is no need to Copy()
struct fields first before calling Merge() on them.

This reduces GC pressure (#1010) and helps overall app performance
(#1457).
  • Loading branch information
rade committed Jul 28, 2016
1 parent 1ffdafd commit c4fa721
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
38 changes: 21 additions & 17 deletions report/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,27 @@ func (n Node) Copy() Node {
// Merge mergses the individual components of a node and returns a
// fresh node.
func (n Node) Merge(other Node) Node {
cp := n.Copy()
if cp.ID == "" {
cp.ID = other.ID
id := n.ID
if id == "" {
id = other.ID
}
if cp.Topology == "" {
cp.Topology = other.Topology
} else if other.Topology != "" && cp.Topology != other.Topology {
panic("Cannot merge nodes with different topology types: " + cp.Topology + " != " + other.Topology)
topology := n.Topology
if topology == "" {
topology = other.Topology
} else if other.Topology != "" && topology != other.Topology {
panic("Cannot merge nodes with different topology types: " + topology + " != " + other.Topology)
}
return Node{
ID: id,
Topology: topology,
Counters: n.Counters.Merge(other.Counters),
Sets: n.Sets.Merge(other.Sets),
Adjacency: n.Adjacency.Merge(other.Adjacency),
Edges: n.Edges.Merge(other.Edges),
Controls: n.Controls.Merge(other.Controls),
Latest: n.Latest.Merge(other.Latest),
Metrics: n.Metrics.Merge(other.Metrics),
Parents: n.Parents.Merge(other.Parents),
Children: n.Children.Merge(other.Children),
}
cp.Counters = cp.Counters.Merge(other.Counters)
cp.Sets = cp.Sets.Merge(other.Sets)
cp.Adjacency = cp.Adjacency.Merge(other.Adjacency)
cp.Edges = cp.Edges.Merge(other.Edges)
cp.Controls = cp.Controls.Merge(other.Controls)
cp.Latest = cp.Latest.Merge(other.Latest)
cp.Metrics = cp.Metrics.Merge(other.Metrics)
cp.Parents = cp.Parents.Merge(other.Parents)
cp.Children = cp.Children.Merge(other.Children)
return cp
}
31 changes: 16 additions & 15 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,22 @@ func (r Report) Copy() Report {
// Merge merges another Report into the receiver and returns the result. The
// original is not modified.
func (r Report) Merge(other Report) Report {
cp := r.Copy()
cp.Endpoint = r.Endpoint.Merge(other.Endpoint)
cp.Process = r.Process.Merge(other.Process)
cp.Container = r.Container.Merge(other.Container)
cp.ContainerImage = r.ContainerImage.Merge(other.ContainerImage)
cp.Host = r.Host.Merge(other.Host)
cp.Pod = r.Pod.Merge(other.Pod)
cp.Service = r.Service.Merge(other.Service)
cp.Deployment = r.Deployment.Merge(other.Deployment)
cp.ReplicaSet = r.ReplicaSet.Merge(other.ReplicaSet)
cp.Overlay = r.Overlay.Merge(other.Overlay)
cp.Sampling = r.Sampling.Merge(other.Sampling)
cp.Window += other.Window
cp.Plugins = r.Plugins.Merge(other.Plugins)
return cp
return Report{
Endpoint: r.Endpoint.Merge(other.Endpoint),
Process: r.Process.Merge(other.Process),
Container: r.Container.Merge(other.Container),
ContainerImage: r.ContainerImage.Merge(other.ContainerImage),
Host: r.Host.Merge(other.Host),
Pod: r.Pod.Merge(other.Pod),
Service: r.Service.Merge(other.Service),
Deployment: r.Deployment.Merge(other.Deployment),
ReplicaSet: r.ReplicaSet.Merge(other.ReplicaSet),
Overlay: r.Overlay.Merge(other.Overlay),
Sampling: r.Sampling.Merge(other.Sampling),
Window: r.Window + other.Window,
Plugins: r.Plugins.Merge(other.Plugins),
ID: fmt.Sprintf("%d", rand.Int63()),
}
}

// Topologies returns a slice of Topologies in this report
Expand Down

0 comments on commit c4fa721

Please sign in to comment.