Slightly optimize writeVarInt for the normal case.
Most variable length integers are smaller numbers, so this commit reverses the order of the if checks in the writeVarInt to assume smaller numbers are more common. This is part of the ongoing effort to optimize serialization as noted in conformal/btcd#27
This commit is contained in:
parent
bd297491b9
commit
cbf648a02f
2 changed files with 26 additions and 35 deletions
29
common.go
29
common.go
|
@ -96,28 +96,19 @@ func readVarInt(r io.Reader, pver uint32) (uint64, error) {
|
||||||
// writeVarInt serializes val to w using a variable number of bytes depending
|
// writeVarInt serializes val to w using a variable number of bytes depending
|
||||||
// on its value.
|
// on its value.
|
||||||
func writeVarInt(w io.Writer, pver uint32, val uint64) error {
|
func writeVarInt(w io.Writer, pver uint32, val uint64) error {
|
||||||
if val > math.MaxUint32 {
|
if val < 0xfd {
|
||||||
err := writeElements(w, []byte{0xff}, uint64(val))
|
return writeElement(w, uint8(val))
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
if val > math.MaxUint16 {
|
|
||||||
err := writeElements(w, []byte{0xfe}, uint32(val))
|
if val <= math.MaxUint16 {
|
||||||
if err != nil {
|
return writeElements(w, []byte{0xfd}, uint16(val))
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
if val >= 0xfd {
|
|
||||||
err := writeElements(w, []byte{0xfd}, uint16(val))
|
if val <= math.MaxUint32 {
|
||||||
if err != nil {
|
return writeElements(w, []byte{0xfe}, uint32(val))
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
return writeElement(w, uint8(val))
|
|
||||||
|
return writeElements(w, []byte{0xff}, uint64(val))
|
||||||
}
|
}
|
||||||
|
|
||||||
// readVarString reads a variable length string from r and returns it as a Go
|
// readVarString reads a variable length string from r and returns it as a Go
|
||||||
|
|
|
@ -16,15 +16,14 @@ github.com/conformal/btcwire/msgblock.go MsgBlock.DeserializeTxLoc 100.00% (1
|
||||||
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcEncode 100.00% (18/18)
|
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.BtcEncode 100.00% (18/18)
|
||||||
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.BtcEncode 100.00% (18/18)
|
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.BtcEncode 100.00% (18/18)
|
||||||
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcDecode 100.00% (17/17)
|
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcDecode 100.00% (17/17)
|
||||||
github.com/conformal/btcwire/common.go writeVarInt 100.00% (16/16)
|
github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecode 100.00% (15/15)
|
||||||
|
github.com/conformal/btcwire/msgtx.go readTxOut 100.00% (15/15)
|
||||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcEncode 100.00% (15/15)
|
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcEncode 100.00% (15/15)
|
||||||
github.com/conformal/btcwire/shahash.go NewShaHashFromStr 100.00% (15/15)
|
github.com/conformal/btcwire/shahash.go NewShaHashFromStr 100.00% (15/15)
|
||||||
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcEncode 100.00% (15/15)
|
github.com/conformal/btcwire/msgheaders.go MsgHeaders.BtcEncode 100.00% (15/15)
|
||||||
github.com/conformal/btcwire/msgblock.go MsgBlock.BtcDecode 100.00% (15/15)
|
|
||||||
github.com/conformal/btcwire/msgtx.go writeTxIn 100.00% (15/15)
|
github.com/conformal/btcwire/msgtx.go writeTxIn 100.00% (15/15)
|
||||||
github.com/conformal/btcwire/msgtx.go readTxOut 100.00% (15/15)
|
|
||||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcDecode 100.00% (14/14)
|
|
||||||
github.com/conformal/btcwire/msginv.go MsgInv.BtcDecode 100.00% (14/14)
|
github.com/conformal/btcwire/msginv.go MsgInv.BtcDecode 100.00% (14/14)
|
||||||
|
github.com/conformal/btcwire/msgaddr.go MsgAddr.BtcDecode 100.00% (14/14)
|
||||||
github.com/conformal/btcwire/netaddress.go writeNetAddress 100.00% (14/14)
|
github.com/conformal/btcwire/netaddress.go writeNetAddress 100.00% (14/14)
|
||||||
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcDecode 100.00% (14/14)
|
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcDecode 100.00% (14/14)
|
||||||
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcDecode 100.00% (14/14)
|
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcDecode 100.00% (14/14)
|
||||||
|
@ -32,39 +31,40 @@ github.com/conformal/btcwire/msginv.go MsgInv.BtcEncode 100.00% (12/12)
|
||||||
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcEncode 100.00% (12/12)
|
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.BtcEncode 100.00% (12/12)
|
||||||
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcEncode 100.00% (12/12)
|
github.com/conformal/btcwire/msggetdata.go MsgGetData.BtcEncode 100.00% (12/12)
|
||||||
github.com/conformal/btcwire/protocol.go ServiceFlag.String 100.00% (12/12)
|
github.com/conformal/btcwire/protocol.go ServiceFlag.String 100.00% (12/12)
|
||||||
github.com/conformal/btcwire/msgtx.go writeTxOut 100.00% (11/11)
|
|
||||||
github.com/conformal/btcwire/common.go readVarString 100.00% (11/11)
|
github.com/conformal/btcwire/common.go readVarString 100.00% (11/11)
|
||||||
github.com/conformal/btcwire/message.go discardInput 100.00% (10/10)
|
github.com/conformal/btcwire/msgtx.go writeTxOut 100.00% (11/11)
|
||||||
github.com/conformal/btcwire/blockheader.go readBlockHeader 100.00% (10/10)
|
github.com/conformal/btcwire/blockheader.go readBlockHeader 100.00% (10/10)
|
||||||
|
github.com/conformal/btcwire/message.go discardInput 100.00% (10/10)
|
||||||
github.com/conformal/btcwire/msgblock.go MsgBlock.BtcEncode 100.00% (9/9)
|
github.com/conformal/btcwire/msgblock.go MsgBlock.BtcEncode 100.00% (9/9)
|
||||||
github.com/conformal/btcwire/msgalert.go MsgAlert.BtcEncode 100.00% (8/8)
|
github.com/conformal/btcwire/msgalert.go MsgAlert.BtcEncode 100.00% (8/8)
|
||||||
github.com/conformal/btcwire/msgalert.go MsgAlert.BtcDecode 100.00% (8/8)
|
github.com/conformal/btcwire/msgalert.go MsgAlert.BtcDecode 100.00% (8/8)
|
||||||
github.com/conformal/btcwire/blockheader.go writeBlockHeader 100.00% (8/8)
|
github.com/conformal/btcwire/blockheader.go writeBlockHeader 100.00% (8/8)
|
||||||
github.com/conformal/btcwire/common.go randomUint64 100.00% (7/7)
|
|
||||||
github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 100.00% (7/7)
|
|
||||||
github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 100.00% (7/7)
|
|
||||||
github.com/conformal/btcwire/message.go readMessageHeader 100.00% (7/7)
|
github.com/conformal/btcwire/message.go readMessageHeader 100.00% (7/7)
|
||||||
github.com/conformal/btcwire/msgversion.go NewMsgVersionFromConn 100.00% (7/7)
|
github.com/conformal/btcwire/common.go randomUint64 100.00% (7/7)
|
||||||
|
github.com/conformal/btcwire/common.go writeVarInt 100.00% (7/7)
|
||||||
|
github.com/conformal/btcwire/msgpong.go MsgPong.BtcEncode 100.00% (7/7)
|
||||||
github.com/conformal/btcwire/common.go writeVarString 100.00% (7/7)
|
github.com/conformal/btcwire/common.go writeVarString 100.00% (7/7)
|
||||||
|
github.com/conformal/btcwire/msgpong.go MsgPong.BtcDecode 100.00% (7/7)
|
||||||
|
github.com/conformal/btcwire/msgversion.go NewMsgVersionFromConn 100.00% (7/7)
|
||||||
github.com/conformal/btcwire/common.go DoubleSha256 100.00% (6/6)
|
github.com/conformal/btcwire/common.go DoubleSha256 100.00% (6/6)
|
||||||
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5)
|
github.com/conformal/btcwire/msgnotfound.go MsgNotFound.AddInvVect 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddresses 100.00% (5/5)
|
|
||||||
github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 100.00% (5/5)
|
|
||||||
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 100.00% (5/5)
|
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddress 100.00% (5/5)
|
||||||
|
github.com/conformal/btcwire/msginv.go MsgInv.AddInvVect 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.AddBlockLocatorHash 100.00% (5/5)
|
github.com/conformal/btcwire/msggetblocks.go MsgGetBlocks.AddBlockLocatorHash 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5)
|
github.com/conformal/btcwire/msggetheaders.go MsgGetHeaders.AddBlockLocatorHash 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/shahash.go NewShaHash 100.00% (5/5)
|
github.com/conformal/btcwire/shahash.go NewShaHash 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5)
|
github.com/conformal/btcwire/msgheaders.go MsgHeaders.AddBlockHeader 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/msggetdata.go MsgGetData.AddInvVect 100.00% (5/5)
|
github.com/conformal/btcwire/msggetdata.go MsgGetData.AddInvVect 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/blockheader.go BlockHeader.BlockSha 100.00% (5/5)
|
github.com/conformal/btcwire/blockheader.go BlockHeader.BlockSha 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 100.00% (5/5)
|
|
||||||
github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5)
|
github.com/conformal/btcwire/shahash.go ShaHash.SetBytes 100.00% (5/5)
|
||||||
|
github.com/conformal/btcwire/msgtx.go MsgTx.TxSha 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/common.go readElements 100.00% (5/5)
|
github.com/conformal/btcwire/common.go readElements 100.00% (5/5)
|
||||||
|
github.com/conformal/btcwire/common.go writeElements 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 100.00% (5/5)
|
github.com/conformal/btcwire/msgping.go MsgPing.BtcEncode 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/msgping.go MsgPing.BtcDecode 100.00% (5/5)
|
github.com/conformal/btcwire/msgping.go MsgPing.BtcDecode 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/common.go writeElements 100.00% (5/5)
|
|
||||||
github.com/conformal/btcwire/msgblock.go MsgBlock.TxShas 100.00% (5/5)
|
|
||||||
github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5)
|
github.com/conformal/btcwire/netaddress.go NewNetAddress 100.00% (5/5)
|
||||||
|
github.com/conformal/btcwire/msgblock.go MsgBlock.TxShas 100.00% (5/5)
|
||||||
|
github.com/conformal/btcwire/msgaddr.go MsgAddr.AddAddresses 100.00% (5/5)
|
||||||
github.com/conformal/btcwire/invvect.go readInvVect 100.00% (4/4)
|
github.com/conformal/btcwire/invvect.go readInvVect 100.00% (4/4)
|
||||||
github.com/conformal/btcwire/invvect.go writeInvVect 100.00% (4/4)
|
github.com/conformal/btcwire/invvect.go writeInvVect 100.00% (4/4)
|
||||||
github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcDecode 100.00% (4/4)
|
github.com/conformal/btcwire/msgmempool.go MsgMemPool.BtcDecode 100.00% (4/4)
|
||||||
|
@ -154,5 +154,5 @@ github.com/conformal/btcwire/msggetaddr.go MsgGetAddr.BtcEncode 100.00% (1/1
|
||||||
github.com/conformal/btcwire/msginv.go MsgInv.Command 100.00% (1/1)
|
github.com/conformal/btcwire/msginv.go MsgInv.Command 100.00% (1/1)
|
||||||
github.com/conformal/btcwire/msginv.go MsgInv.MaxPayloadLength 100.00% (1/1)
|
github.com/conformal/btcwire/msginv.go MsgInv.MaxPayloadLength 100.00% (1/1)
|
||||||
github.com/conformal/btcwire/msginv.go NewMsgInv 100.00% (1/1)
|
github.com/conformal/btcwire/msginv.go NewMsgInv 100.00% (1/1)
|
||||||
github.com/conformal/btcwire --------------------------------- 100.00% (964/964)
|
github.com/conformal/btcwire --------------------------------- 100.00% (955/955)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue