diff options
| author | Josh Bleecher Snyder <josharian@gmail.com> | 2019-04-22 14:39:55 -0700 |
|---|---|---|
| committer | Josh Bleecher Snyder <josharian@gmail.com> | 2019-04-24 00:56:36 +0000 |
| commit | ca0c449a6b1c6ecc75169f93cffa8a5630740030 (patch) | |
| tree | decb56cb6c8eb3e7e910ee090f3694465af30e60 /src/bytes | |
| parent | de050717f1b668dfd196f1dc4d18c77d03f3afb4 (diff) | |
| download | go-ca0c449a6b1c6ecc75169f93cffa8a5630740030.tar.xz | |
bytes, internal/bytealg: simplify Equal
The compiler has advanced enough that it is cheaper
to convert to strings than to go through the assembly
trampolines to call runtime.memequal.
Simplify Equal accordingly, and cull dead code from bytealg.
While we're here, simplify Equal's documentation.
Fixes #31587
Change-Id: Ie721d33f9a6cbd86b1d873398b20e7882c2c63e9
Reviewed-on: https://go-review.googlesource.com/c/go/+/173323
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dave Cheney <dave@cheney.net>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/bytes')
| -rw-r--r-- | src/bytes/bytes.go | 17 | ||||
| -rw-r--r-- | src/bytes/bytes_test.go | 23 | ||||
| -rw-r--r-- | src/bytes/export_test.go | 1 |
3 files changed, 13 insertions, 28 deletions
diff --git a/src/bytes/bytes.go b/src/bytes/bytes.go index 22aeded5e1..9d586581f5 100644 --- a/src/bytes/bytes.go +++ b/src/bytes/bytes.go @@ -12,23 +12,12 @@ import ( "unicode/utf8" ) -// Equal returns a boolean reporting whether a and b +// Equal reports whether a and b // are the same length and contain the same bytes. // A nil argument is equivalent to an empty slice. func Equal(a, b []byte) bool { - return bytealg.Equal(a, b) -} - -func equalPortable(a, b []byte) bool { - if len(a) != len(b) { - return false - } - for i, c := range a { - if c != b[i] { - return false - } - } - return true + // Neither cmd/compile nor gccgo allocates for these string conversions. + return string(a) == string(b) } // Compare returns an integer comparing two byte slices lexicographically. diff --git a/src/bytes/bytes_test.go b/src/bytes/bytes_test.go index 340810facf..4c50755e7c 100644 --- a/src/bytes/bytes_test.go +++ b/src/bytes/bytes_test.go @@ -51,15 +51,17 @@ type BinOpTest struct { } func TestEqual(t *testing.T) { - for _, tt := range compareTests { - eql := Equal(tt.a, tt.b) - if eql != (tt.i == 0) { - t.Errorf(`Equal(%q, %q) = %v`, tt.a, tt.b, eql) - } - eql = EqualPortable(tt.a, tt.b) - if eql != (tt.i == 0) { - t.Errorf(`EqualPortable(%q, %q) = %v`, tt.a, tt.b, eql) + // Run the tests and check for allocation at the same time. + allocs := testing.AllocsPerRun(10, func() { + for _, tt := range compareTests { + eql := Equal(tt.a, tt.b) + if eql != (tt.i == 0) { + t.Errorf(`Equal(%q, %q) = %v`, tt.a, tt.b, eql) + } } + }) + if allocs > 0 { + t.Errorf("Equal allocated %v times", allocs) } } @@ -572,11 +574,6 @@ func BenchmarkEqual(b *testing.B) { benchBytes(b, sizes, bmEqual(Equal)) } -func BenchmarkEqualPort(b *testing.B) { - sizes := []int{1, 6, 32, 4 << 10, 4 << 20, 64 << 20} - benchBytes(b, sizes, bmEqual(EqualPortable)) -} - func bmEqual(equal func([]byte, []byte) bool) func(b *testing.B, n int) { return func(b *testing.B, n int) { if len(bmbuf) < 2*n { diff --git a/src/bytes/export_test.go b/src/bytes/export_test.go index f61523e60b..b65428d9ce 100644 --- a/src/bytes/export_test.go +++ b/src/bytes/export_test.go @@ -6,4 +6,3 @@ package bytes // Export func for testing var IndexBytePortable = indexBytePortable -var EqualPortable = equalPortable |
