aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/bytes
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/bytes')
-rw-r--r--libgo/go/bytes/buffer.go17
-rw-r--r--libgo/go/bytes/bytes_test.go31
2 files changed, 41 insertions, 7 deletions
diff --git a/libgo/go/bytes/buffer.go b/libgo/go/bytes/buffer.go
index 4db9386..ddaba3b 100644
--- a/libgo/go/bytes/buffer.go
+++ b/libgo/go/bytes/buffer.go
@@ -36,10 +36,11 @@ const (
// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
var ErrTooLarge = errors.New("bytes.Buffer: too large")
-// Bytes returns a slice of the contents of the unread portion of the buffer;
-// len(b.Bytes()) == b.Len(). If the caller changes the contents of the
-// returned slice, the contents of the buffer will change provided there
-// are no intervening method calls on the Buffer.
+// Bytes returns a slice of length b.Len() holding the unread portion of the buffer.
+// The slice is valid for use only until the next buffer modification (that is,
+// only until the next call to a method like Read, Write, Reset, or Truncate).
+// The slice aliases the buffer content at least until the next buffer modification,
+// so immediate changes to the slice will affect the result of future reads.
func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }
// String returns the contents of the unread portion of the buffer
@@ -60,7 +61,8 @@ func (b *Buffer) Len() int { return len(b.buf) - b.off }
// total space allocated for the buffer's data.
func (b *Buffer) Cap() int { return cap(b.buf) }
-// Truncate discards all but the first n unread bytes from the buffer.
+// Truncate discards all but the first n unread bytes from the buffer
+// but continues to use the same allocated storage.
// It panics if n is negative or greater than the length of the buffer.
func (b *Buffer) Truncate(n int) {
b.lastRead = opInvalid
@@ -74,8 +76,9 @@ func (b *Buffer) Truncate(n int) {
b.buf = b.buf[0 : b.off+n]
}
-// Reset resets the buffer so it has no content.
-// b.Reset() is the same as b.Truncate(0).
+// Reset resets the buffer to be empty,
+// but it retains the underlying storage for use by future writes.
+// Reset is the same as Truncate(0).
func (b *Buffer) Reset() { b.Truncate(0) }
// grow grows the buffer to guarantee space for n more bytes.
diff --git a/libgo/go/bytes/bytes_test.go b/libgo/go/bytes/bytes_test.go
index 6245e48..8df62fc 100644
--- a/libgo/go/bytes/bytes_test.go
+++ b/libgo/go/bytes/bytes_test.go
@@ -1255,3 +1255,34 @@ func BenchmarkRepeat(b *testing.B) {
Repeat([]byte("-"), 80)
}
}
+
+func benchmarkBytesCompare(b *testing.B, n int) {
+ var x = make([]byte, n)
+ var y = make([]byte, n)
+
+ for i := 0; i < n; i++ {
+ x[i] = 'a'
+ }
+
+ for i := 0; i < n; i++ {
+ y[i] = 'a'
+ }
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ Compare(x, y)
+ }
+}
+
+func BenchmarkBytesCompare1(b *testing.B) { benchmarkBytesCompare(b, 1) }
+func BenchmarkBytesCompare2(b *testing.B) { benchmarkBytesCompare(b, 2) }
+func BenchmarkBytesCompare4(b *testing.B) { benchmarkBytesCompare(b, 4) }
+func BenchmarkBytesCompare8(b *testing.B) { benchmarkBytesCompare(b, 8) }
+func BenchmarkBytesCompare16(b *testing.B) { benchmarkBytesCompare(b, 16) }
+func BenchmarkBytesCompare32(b *testing.B) { benchmarkBytesCompare(b, 32) }
+func BenchmarkBytesCompare64(b *testing.B) { benchmarkBytesCompare(b, 64) }
+func BenchmarkBytesCompare128(b *testing.B) { benchmarkBytesCompare(b, 128) }
+func BenchmarkBytesCompare256(b *testing.B) { benchmarkBytesCompare(b, 256) }
+func BenchmarkBytesCompare512(b *testing.B) { benchmarkBytesCompare(b, 512) }
+func BenchmarkBytesCompare1024(b *testing.B) { benchmarkBytesCompare(b, 1024) }
+func BenchmarkBytesCompare2048(b *testing.B) { benchmarkBytesCompare(b, 2048) }