-
Notifications
You must be signed in to change notification settings - Fork 132
Implement Report#merge for efficiently combining many reports #110
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -358,49 +358,70 @@ def print_file(filter, f = STDOUT) | |
end | ||
|
||
def +(other) | ||
raise ArgumentError, "cannot combine #{other.class}" unless self.class == other.class | ||
raise ArgumentError, "cannot combine #{modeline} with #{other.modeline}" unless modeline == other.modeline | ||
raise ArgumentError, "cannot combine v#{version} with v#{other.version}" unless version == other.version | ||
|
||
f1, f2 = normalized_frames, other.normalized_frames | ||
frames = (f1.keys + f2.keys).uniq.inject(Hash.new) do |hash, id| | ||
if f1[id].nil? | ||
hash[id] = f2[id] | ||
elsif f2[id] | ||
hash[id] = f1[id] | ||
hash[id][:total_samples] += f2[id][:total_samples] | ||
hash[id][:samples] += f2[id][:samples] | ||
if f2[id][:edges] | ||
edges = hash[id][:edges] ||= {} | ||
f2[id][:edges].each do |edge, weight| | ||
edges[edge] ||= 0 | ||
edges[edge] += weight | ||
merge(other) | ||
end | ||
|
||
def merge(*others) | ||
other_class = others.find {|o| o.class != self.class} | ||
if other_class | ||
raise ArgumentError, "cannot combine #{self.class} with #{other_class}" | ||
end | ||
|
||
other_modeline = others.find {|o| o.modeline != modeline} | ||
if other_modeline | ||
raise ArgumentError, "cannot combine #{modeline} with #{other_modeline}" | ||
end | ||
|
||
other_version = others.find {|o| o.version != version} | ||
if other_version | ||
raise ArgumentError, "cannot combine v#{version} with v#{other_version}" | ||
end | ||
|
||
all = [self] + others | ||
merged = {} | ||
|
||
all.each do |report| | ||
report.normalized_frames.each do |id, frame| | ||
if !merged[id] | ||
merged[id] = frame | ||
next | ||
end | ||
|
||
merged_frame = merged[id] | ||
|
||
merged_frame[:total_samples] += frame[:total_samples] | ||
merged_frame[:samples] += frame[:samples] | ||
|
||
if frame[:edges] | ||
merged_edges = (merged_frame[:edges] ||= {}) | ||
frame[:edges].each do |edge, weight| | ||
merged_edges[edge] ||= 0 | ||
merged_edges[edge] += weight | ||
end | ||
end | ||
if f2[id][:lines] | ||
lines = hash[id][:lines] ||= {} | ||
f2[id][:lines].each do |line, weight| | ||
lines[line] = add_lines(lines[line], weight) | ||
|
||
if frame[:lines] | ||
merged_lines = (merged_frame[:lines] ||= {}) | ||
frame[:lines].each do |line, weight| | ||
merged_lines[line] = add_lines(merged_lines[line], weight) | ||
end | ||
end | ||
else | ||
hash[id] = f1[id] | ||
end | ||
hash | ||
end | ||
|
||
d1, d2 = data, other.data | ||
data = { | ||
all_data = all.map(&:data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like you could save a few iterations of this data by just calculating the # untested code... you have been warned!
new_samples, new_gc_samples, new_missed_samples = all.inject([0,0,0]) do |result, data|
result[0] += data[:samples]
result[1] += data[:gc_samples]
result[2] += data[:missed_samples]
result
end
new_data = {
# ...
samples: new_samples,
gc_samples: new_gc_samples,
missed_samples: new_missed_samples
} However, I realize it looks a bit uglier this way... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure it's worth optimizing this much since it's O(number of reports) whereas the main loop is O(frames) which should usually be many orders of magnitude larger. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, also that. Just something I noticed when looking through the code that stuck out to me, but definitely agree that it probably isn't the big focal point of the optimization. I think it can be adjusted at a second pass if it turns out to be beneficial, but I think what you have is more readable and fine to keep as is. |
||
|
||
new_data = { | ||
version: version, | ||
mode: d1[:mode], | ||
interval: d1[:interval], | ||
samples: d1[:samples] + d2[:samples], | ||
gc_samples: d1[:gc_samples] + d2[:gc_samples], | ||
missed_samples: d1[:missed_samples] + d2[:missed_samples], | ||
frames: frames | ||
mode: data[:mode], | ||
interval: data[:interval], | ||
samples: all_data.map {|d| d[:samples]}.inject(:+), | ||
gc_samples: all_data.map {|d| d[:gc_samples]}.inject(:+), | ||
missed_samples: all_data.map {|d| d[:missed_samples]}.inject(:+), | ||
frames: merged | ||
} | ||
|
||
self.class.new(data) | ||
self.class.new(new_data) | ||
end | ||
|
||
private | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the early eject here. 👍