]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/binary: give LittleEndian, BigEndian specific types
authorRuss Cox <rsc@golang.org>
Thu, 21 Oct 2010 15:25:14 +0000 (11:25 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 21 Oct 2010 15:25:14 +0000 (11:25 -0400)
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

src/pkg/encoding/binary/binary.go

index 2343e0398b155c6fa34b497cf0ea7f9489305d89..ebc2ae8b7cff3bc1efa6ca65bf62127ae184fcee 100644 (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