From 497aac6d4accaabd0df9a2706b7399a5afe819ea Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 8 Jan 2014 17:21:51 -0600 Subject: [PATCH] Add new funcs to allow size hints on getdata/inv. This commit adds two new functions named NewMsgGetDataSizeHint and NewMsgInvSizeHint. These are intended to allow callers which know in advance how large the inventory lists will grow the ability to provides that information when creating the message. This in turn provides a mechanism to avoid the need to perform several grow operations of the backing array when adding large number of inventory vectors. --- msggetdata.go | 21 +++++++++++++++++++++ msginv.go | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/msggetdata.go b/msggetdata.go index c58d89cb..c64deab6 100644 --- a/msggetdata.go +++ b/msggetdata.go @@ -107,3 +107,24 @@ func NewMsgGetData() *MsgGetData { InvList: make([]*InvVect, 0, defaultInvListAlloc), } } + +// NewMsgGetDataSizeHint returns a new bitcoin getdata message that conforms to +// the Message interface. See MsgGetData for details. This function differs +// from NewMsgGetData in that it allows a default allocation size for the +// backing array which houses the inventory vector list. This allows callers +// who know in advance how large the inventory list will grow to avoid the +// overhead of growing the internal backing array several times when appending +// large amounts of inventory vectors with AddInvVect. Note that the specified +// hint is just that - a hint that is used for the default allocation size. +// Adding more (or less) inventory vectors will still work properly. The size +// hint is limited to MaxInvPerMsg. +func NewMsgGetDataSizeHint(sizeHint uint) *MsgGetData { + // Limit the specified hint to the maximum allow per message. + if sizeHint > MaxInvPerMsg { + sizeHint = MaxInvPerMsg + } + + return &MsgGetData{ + InvList: make([]*InvVect, 0, sizeHint), + } +} diff --git a/msginv.go b/msginv.go index 96538af8..7f7f08c4 100644 --- a/msginv.go +++ b/msginv.go @@ -115,3 +115,24 @@ func NewMsgInv() *MsgInv { InvList: make([]*InvVect, 0, defaultInvListAlloc), } } + +// NewMsgInvSizeHint returns a new bitcoin inv message that conforms to the +// Message interface. See MsgInv for details. This function differs from +// NewMsgInv in that it allows a default allocation size for the backing array +// which houses the inventory vector list. This allows callers who know in +// advance how large the inventory list will grow to avoid the overhead of +// growing the internal backing array several times when appending large amounts +// of inventory vectors with AddInvVect. Note that the specified hint is just +// that - a hint that is used for the default allocation size. Adding more +// (or less) inventory vectors will still work properly. The size hint is +// limited to MaxInvPerMsg. +func NewMsgInvSizeHint(sizeHint uint) *MsgInv { + // Limit the specified hint to the maximum allow per message. + if sizeHint > MaxInvPerMsg { + sizeHint = MaxInvPerMsg + } + + return &MsgInv{ + InvList: make([]*InvVect, 0, sizeHint), + } +}