1
0
mirror of https://github.com/golang/go synced 2024-11-22 04:44:39 -07:00

encoding/binary: give LittleEndian, BigEndian specific types

Giving them specific types has the benefit that
binary.BigEndian.Uint32(b) is now a direct call, not an
indirect via a mutable interface value, so it can potentially
be inlined.

Recent changes to the spec relaxed the rules for comparison,
so this code is still valid:

	func isLittle(o binary.ByteOrder) { return o == binary.LittleEndian }

The change does break this potential idiom:

	o := binary.BigEndian
	if foo {
		o = binary.LittleEndian
	}

That must rewrite to give o an explicit binary.ByteOrder type.
On balance I think the benefit from the direct call and inlining
outweigh the cost of breaking that idiom.

R=r, r2
CC=golang-dev
https://golang.org/cl/2427042
This commit is contained in:
Russ Cox 2010-10-21 11:25:14 -04:00
parent 4f6fb1b775
commit 1451695f86

View File

@ -29,8 +29,11 @@ type ByteOrder interface {
// allowing, e.g., order == binary.LittleEndian.
type unused byte
var LittleEndian ByteOrder = littleEndian(0)
var BigEndian ByteOrder = bigEndian(0)
// LittleEndian is the little-endian implementation of ByteOrder.
var LittleEndian littleEndian
// BigEndian is the big-endian implementation of ByteOrder.
var BigEndian bigEndian
type littleEndian unused