From 50c10faf4e679d96b25a633a7c33e5f80e894628 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Tue, 5 Nov 2013 20:32:36 -0600 Subject: [PATCH] Optimize writeVarInt. Before: BenchmarkWriteVarInt1 10000000 173 ns/op BenchmarkWriteVarInt3 2000000 965 ns/op BenchmarkWriteVarInt5 2000000 966 ns/op BenchmarkWriteVarInt9 2000000 968 ns/op After: BenchmarkWriteVarInt1 20000000 101 ns/op BenchmarkWriteVarInt3 20000000 136 ns/op BenchmarkWriteVarInt5 10000000 142 ns/op BenchmarkWriteVarInt9 10000000 156 ns/op This is part of the ongoing effort to optimize serialization as noted in conformal/btcd#27. --- common.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/common.go b/common.go index 8eabcbc8..3c1bfeff 100644 --- a/common.go +++ b/common.go @@ -97,18 +97,31 @@ func readVarInt(r io.Reader, pver uint32) (uint64, error) { // on its value. func writeVarInt(w io.Writer, pver uint32, val uint64) error { if val < 0xfd { - return writeElement(w, uint8(val)) + _, err := w.Write([]byte{uint8(val)}) + return err } if val <= math.MaxUint16 { - return writeElements(w, []byte{0xfd}, uint16(val)) + buf := make([]byte, 3) + buf[0] = 0xfd + binary.LittleEndian.PutUint16(buf[1:], uint16(val)) + _, err := w.Write(buf) + return err } if val <= math.MaxUint32 { - return writeElements(w, []byte{0xfe}, uint32(val)) + buf := make([]byte, 5) + buf[0] = 0xfe + binary.LittleEndian.PutUint32(buf[1:], uint32(val)) + _, err := w.Write(buf) + return err } - return writeElements(w, []byte{0xff}, uint64(val)) + buf := make([]byte, 9) + buf[0] = 0xff + binary.LittleEndian.PutUint64(buf[1:], val) + _, err := w.Write(buf) + return err } // varIntSerializeSize returns the number of bytes it would take to serialize