Skip to content

Commit 88849e8

Browse files
authored
Allow batched diffing of slices with a custom comparer (#210)
For correctness, cmp checks applicability of the options for every element in a slice. For large []byte, this is a significant performance detriment. The workaround is to specify Comparer(bytes.Equal). However, we would still like to have the batched diffing if the slices are different. Specialize for this situation.
1 parent 9b30031 commit 88849e8

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

cmp/compare_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,15 @@ func reporterTests() []test {
833833
y: MyComposite{IntsA: []int8{12, 29, 13, 27, 22, 23, 17, 18, 19, 20, 21, 10, 26, 16, 25, 28, 11, 15, 24, 14}},
834834
wantEqual: false,
835835
reason: "batched diffing desired since many elements differ",
836+
}, {
837+
label: label + "/BatchedWithComparer",
838+
x: MyComposite{BytesA: []byte{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}},
839+
y: MyComposite{BytesA: []byte{12, 29, 13, 27, 22, 23, 17, 18, 19, 20, 21, 10, 26, 16, 25, 28, 11, 15, 24, 14}},
840+
wantEqual: false,
841+
opts: []cmp.Option{
842+
cmp.Comparer(bytes.Equal),
843+
},
844+
reason: "batched diffing desired since many elements differ",
836845
}, {
837846
label: label + "/BatchedLong",
838847
x: MyComposite{IntsA: []int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127}},

cmp/report_slices.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,25 @@ func (opts formatOptions) CanFormatDiffSlice(v *valueNode) bool {
2323
return false // Must be formatting in diff mode
2424
case v.NumDiff == 0:
2525
return false // No differences detected
26-
case v.NumIgnored+v.NumCompared+v.NumTransformed > 0:
27-
// TODO: Handle the case where someone uses bytes.Equal on a large slice.
28-
return false // Some custom option was used to determined equality
2926
case !v.ValueX.IsValid() || !v.ValueY.IsValid():
3027
return false // Both values must be valid
3128
case v.Type.Kind() == reflect.Slice && (v.ValueX.IsNil() || v.ValueY.IsNil()):
3229
return false // Both of values have to be non-nil
30+
case v.NumIgnored > 0:
31+
return false // Some ignore option was used
32+
case v.NumTransformed > 0:
33+
return false // Some transform option was used
34+
case v.NumCompared > 1:
35+
return false // More than one comparison was used
36+
case v.NumCompared == 1 && v.Type.Name() != "":
37+
// The need for cmp to check applicability of options on every element
38+
// in a slice is a significant performance detriment for large []byte.
39+
// The workaround is to specify Comparer(bytes.Equal),
40+
// which enables cmp to compare []byte more efficiently.
41+
// If they differ, we still want to provide batched diffing.
42+
// The logic disallows named types since they tend to have their own
43+
// String method, with nicer formatting than what this provides.
44+
return false
3345
}
3446

3547
switch t := v.Type; t.Kind() {

cmp/testdata/diffs

+16
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,22 @@
294294
... // 6 identical fields
295295
}
296296
>>> TestDiff/Reporter#01
297+
<<< TestDiff/Reporter/BatchedWithComparer
298+
cmp_test.MyComposite{
299+
StringA: "",
300+
StringB: "",
301+
BytesA: []uint8{
302+
- 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, // -|.......|
303+
+ 0x0c, 0x1d, 0x0d, 0x1b, 0x16, 0x17, // +|......|
304+
0x11, 0x12, 0x13, 0x14, 0x15, // |.....|
305+
- 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, // -|........|
306+
+ 0x0a, 0x1a, 0x10, 0x19, 0x1c, 0x0b, 0x0f, 0x18, 0x0e, // +|.........|
307+
},
308+
BytesB: nil,
309+
BytesC: nil,
310+
... // 9 identical fields
311+
}
312+
>>> TestDiff/Reporter/BatchedWithComparer
297313
<<< TestDiff/Reporter/BatchedLong
298314
interface{}(
299315
- cmp_test.MyComposite{

0 commit comments

Comments
 (0)