diff options
author | Alexandru Moșoi <mosoi@google.com> | 2016-03-13 22:12:03 +0100 |
---|---|---|
committer | Alexandru Moșoi <alexandru@mosoi.ro> | 2016-03-17 20:48:39 +0000 |
commit | ebd9f1bd4c39bc2fe3bcf6f0d3c81f70dae495d8 (patch) | |
tree | 8ba57c81353e31e45134bc2b9c47954f53481bd9 /src/encoding/binary | |
parent | dbed1c6361f333f7a7aaec1a33a69d0b53956f6b (diff) | |
download | go-ebd9f1bd4c39bc2fe3bcf6f0d3c81f70dae495d8.tar.gz go-ebd9f1bd4c39bc2fe3bcf6f0d3c81f70dae495d8.zip |
encoding/binary: remove bound checks from conversions.
* This the simplest solution I could came up with
that doesn't required changing the compiler.
* The bound checks become constants now
so they are removed during opt phase.
Updates #14808
Change-Id: If32c33d7ec08bb400321b465015d152f0a5d3001
Reviewed-on: https://go-review.googlesource.com/20654
Reviewed-by: Russ Cox <rsc@golang.org>
Run-TryBot: Alexandru Moșoi <alexandru@mosoi.ro>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/binary')
-rw-r--r-- | src/encoding/binary/binary.go | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/src/encoding/binary/binary.go b/src/encoding/binary/binary.go index d2e267eca5..69b7b03091 100644 --- a/src/encoding/binary/binary.go +++ b/src/encoding/binary/binary.go @@ -48,18 +48,24 @@ var BigEndian bigEndian type littleEndian struct{} -func (littleEndian) Uint16(b []byte) uint16 { return uint16(b[0]) | uint16(b[1])<<8 } +func (littleEndian) Uint16(b []byte) uint16 { + b = b[:2] // bounds check hint to compiler; see golang.org/issue/14808 + return uint16(b[0]) | uint16(b[1])<<8 +} func (littleEndian) PutUint16(b []byte, v uint16) { + b = b[:2] // early bounds check to guarantee safety of writes below b[0] = byte(v) b[1] = byte(v >> 8) } func (littleEndian) Uint32(b []byte) uint32 { + b = b[:4] // bounds check hint to compiler; see golang.org/issue/14808 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 } func (littleEndian) PutUint32(b []byte, v uint32) { + b = b[:4] // early bounds check to guarantee safety of writes below b[0] = byte(v) b[1] = byte(v >> 8) b[2] = byte(v >> 16) @@ -67,11 +73,13 @@ func (littleEndian) PutUint32(b []byte, v uint32) { } func (littleEndian) Uint64(b []byte) uint64 { + b = b[:8] // bounds check hint to compiler; see golang.org/issue/14808 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 } func (littleEndian) PutUint64(b []byte, v uint64) { + b = b[:8] // early bounds check to guarantee safety of writes below b[0] = byte(v) b[1] = byte(v >> 8) b[2] = byte(v >> 16) @@ -88,18 +96,24 @@ func (littleEndian) GoString() string { return "binary.LittleEndian" } type bigEndian struct{} -func (bigEndian) Uint16(b []byte) uint16 { return uint16(b[1]) | uint16(b[0])<<8 } +func (bigEndian) Uint16(b []byte) uint16 { + b = b[:2] // bounds check hint to compiler; see golang.org/issue/14808 + return uint16(b[1]) | uint16(b[0])<<8 +} func (bigEndian) PutUint16(b []byte, v uint16) { + b = b[:2] // early bounds check to guarantee safety of writes below b[0] = byte(v >> 8) b[1] = byte(v) } func (bigEndian) Uint32(b []byte) uint32 { + b = b[:4] // bounds check hint to compiler; see golang.org/issue/14808 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 } func (bigEndian) PutUint32(b []byte, v uint32) { + b = b[:4] // early bounds check to guarantee safety of writes below b[0] = byte(v >> 24) b[1] = byte(v >> 16) b[2] = byte(v >> 8) @@ -107,11 +121,13 @@ func (bigEndian) PutUint32(b []byte, v uint32) { } func (bigEndian) Uint64(b []byte) uint64 { + b = b[:8] // bounds check hint to compiler; see golang.org/issue/14808 return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 | uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56 } func (bigEndian) PutUint64(b []byte, v uint64) { + b = b[:8] // early bounds check to guarantee safety of writes below b[0] = byte(v >> 56) b[1] = byte(v >> 48) b[2] = byte(v >> 40) |