-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Use standard merge algorithm when merging Values #8131
Conversation
@jwilder two-way merges works best when the two lists are around the same size. Would it be unusual to merge very differently sized lists? If so, could we add a benchmark for that? |
@e-dard I'll add those. Sizes of both lists will usually be different. |
Merge had the side effect of modifying the original values so the results are wrong because they always hit the fast path after the first run.
6040e9b
to
0e07a12
Compare
@e-dard Added some other benchmarks:
|
if a[0].UnixNano() < b[0].UnixNano() { | ||
out, a = append(out, a[0]), a[1:] | ||
} else { | ||
if len(b) > 0 && a[0].UnixNano() == b[0].UnixNano() { |
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.
You can move line 145
up to make 144
an else if
.
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.
Fixed in 7561dea
out, a = append(out, a[0]), a[1:] | ||
} else { | ||
if len(b) > 0 && a[0].UnixNano() == b[0].UnixNano() { | ||
a = a[1:] | ||
} else { |
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.
This else
can then be an else
on the outer if
block, removing some indentation.
0e07a12
to
7561dea
Compare
out, b = append(out, b[0]), b[1:] | ||
} | ||
} | ||
if len(a) > 0 { |
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.
This can be simplified to:
if len(a) > 0 {
return append(out, a...)
}
return append(out, b...)
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.
Fixed in a4cfeac
The previous version was very innefficient due to the benchmarks used to optimize it having a bug. This version always allocates a new slice, but is O(n).
7561dea
to
a4cfeac
Compare
While investigating a runtime/GC stall, @aclements noticed that
Values.Merge
was using a sub-optimal algorithm and that a traditional merge algorithm would likely perform better in general. He also discovered that the benchmark we were using had a bug which caused the current implementation to appear better when it was actually quite worse in all but one case.This switches the
Values.Merge
to use a standard merge algorithm.