ソースを参照

goprotobuf: Make Encode{Varint,Fixed{32,64}} faster by using append.

R=r
CC=golang-dev
http://codereview.appspot.com/4956052
David Symonds 14 年 前
コミット
d9da6bac2f
2 ファイル変更20 行追加30 行削除
  1. 4 1
      proto/all_test.go
  2. 16 29
      proto/encode.go

+ 4 - 1
proto/all_test.go

@@ -839,7 +839,7 @@ func TestEncodeDecodeBytes1(t *testing.T) {
 
 	// Create our bytes
 	pb.F_BytesRequired = []byte{}
-	pb.F_BytesRepeated = [][]byte{[]byte{}}
+	pb.F_BytesRepeated = [][]byte{{}}
 	pb.F_BytesOptional = []byte{}
 
 	d, err := Marshal(pb)
@@ -1292,11 +1292,14 @@ func BenchmarkMarshal(b *testing.B) {
 	// Create an array
 	const N = 1000 // Internally the library starts much smaller.
 	pb.F_Int32Repeated = make([]int32, N)
+	pb.F_DoubleRepeated = make([]float64, N)
 
 	// Fill in the array with some values.
 	for i := 0; i < N; i++ {
 		pb.F_Int32Repeated[i] = int32(i)
+		pb.F_DoubleRepeated[i] = float64(i)
 	}
+
 	p := NewBuffer(nil)
 
 	b.StartTimer()

+ 16 - 29
proto/encode.go

@@ -91,23 +91,16 @@ func EncodeVarint(x uint64) []byte {
 	return buf[0:n]
 }
 
-var emptyBytes [maxVarintBytes]byte
-
 // EncodeVarint writes a varint-encoded integer to the Buffer.
 // This is the format for the
 // int32, int64, uint32, uint64, bool, and enum
 // protocol buffer types.
 func (p *Buffer) EncodeVarint(x uint64) os.Error {
-	l := len(p.buf)
-	p.buf = append(p.buf, emptyBytes[:]...)
-
 	for x >= 1<<7 {
-		p.buf[l] = uint8(x&0x7f | 0x80)
-		l++
+		p.buf = append(p.buf, uint8(x&0x7f|0x80))
 		x >>= 7
 	}
-	p.buf[l] = uint8(x)
-	p.buf = p.buf[:l+1]
+	p.buf = append(p.buf, uint8(x))
 	return nil
 }
 
@@ -115,18 +108,15 @@ func (p *Buffer) EncodeVarint(x uint64) os.Error {
 // This is the format for the
 // fixed64, sfixed64, and double protocol buffer types.
 func (p *Buffer) EncodeFixed64(x uint64) os.Error {
-	const fixed64Bytes = 8
-	l := len(p.buf)
-	p.buf = append(p.buf, emptyBytes[:fixed64Bytes]...)
-
-	p.buf[l] = uint8(x)
-	p.buf[l+1] = uint8(x >> 8)
-	p.buf[l+2] = uint8(x >> 16)
-	p.buf[l+3] = uint8(x >> 24)
-	p.buf[l+4] = uint8(x >> 32)
-	p.buf[l+5] = uint8(x >> 40)
-	p.buf[l+6] = uint8(x >> 48)
-	p.buf[l+7] = uint8(x >> 56)
+	p.buf = append(p.buf,
+		uint8(x),
+		uint8(x>>8),
+		uint8(x>>16),
+		uint8(x>>24),
+		uint8(x>>32),
+		uint8(x>>40),
+		uint8(x>>48),
+		uint8(x>>56))
 	return nil
 }
 
@@ -134,14 +124,11 @@ func (p *Buffer) EncodeFixed64(x uint64) os.Error {
 // This is the format for the
 // fixed32, sfixed32, and float protocol buffer types.
 func (p *Buffer) EncodeFixed32(x uint64) os.Error {
-	const fixed32Bytes = 4
-	l := len(p.buf)
-	p.buf = append(p.buf, emptyBytes[:fixed32Bytes]...)
-
-	p.buf[l] = uint8(x)
-	p.buf[l+1] = uint8(x >> 8)
-	p.buf[l+2] = uint8(x >> 16)
-	p.buf[l+3] = uint8(x >> 24)
+	p.buf = append(p.buf,
+		uint8(x),
+		uint8(x>>8),
+		uint8(x>>16),
+		uint8(x>>24))
 	return nil
 }