From acbb9a7e0cbde173b59a12ebf72b4da4464669bd Mon Sep 17 00:00:00 2001 From: Dan Laine Date: Mon, 15 Jun 2020 13:12:55 -0400 Subject: [PATCH] remove expansionBoost from packer (Go's append does similar already). change initialSliceCap 1024 --> 256. Streamline packer.Expand, as this method is called very often --- utils/wrappers/packing.go | 32 ++++++++------------------------ vms/components/codec/codec.go | 2 +- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/utils/wrappers/packing.go b/utils/wrappers/packing.go index 22c7464..c048f9c 100644 --- a/utils/wrappers/packing.go +++ b/utils/wrappers/packing.go @@ -16,11 +16,6 @@ const ( // MaxStringLen ... MaxStringLen = math.MaxUint16 - // When the byte array is expanded, this many extra bytes - // are added to capacity of the array. - // Higher value --> need to expand byte array less --> less memory allocations - expansionBoost = 256 - // ByteLen is the number of bytes per byte... ByteLen = 1 // ShortLen is the number of bytes per short @@ -71,30 +66,19 @@ func (p *Packer) CheckSpace(bytes int) { // In order to understand this code, its important to understand the difference // between a slice's length and its capacity. func (p *Packer) Expand(bytes int) { - p.CheckSpace(0) - if p.Errored() { + neededSize := bytes + p.Offset // Need byte slice's length to be at least [neededSize] + switch { + case neededSize <= len(p.Bytes): // Byte slice has sufficient length already return - } - - neededSize := bytes + p.Offset // Need byte slice's length to be at least [neededSize] - if neededSize <= len(p.Bytes) { // Byte slice has sufficient length already + case neededSize > p.MaxSize: // Lengthening the byte slice would cause it to grow too large + p.Err = errBadLength return - } else if neededSize > p.MaxSize { // Lengthening the byte slice would cause it to grow too large - p.Add(errBadLength) - return - } else if neededSize <= cap(p.Bytes) { // Byte slice has sufficient capacity to lengthen it without mem alloc + case neededSize <= cap(p.Bytes): // Byte slice has sufficient capacity to lengthen it without mem alloc p.Bytes = p.Bytes[:neededSize] return + default: // Add capacity/length to byte slice + p.Bytes = append(p.Bytes[:cap(p.Bytes)], make([]byte, neededSize-cap(p.Bytes))...) } - - // See if we can expand the byte slice an extra [expansionBoost] bytes in order to - // prevent need for future expansions (and therefore memory allocations) - capToAdd := neededSize - cap(p.Bytes) + expansionBoost - if capToAdd > p.MaxSize { - capToAdd = neededSize - cap(p.Bytes) - } - // increase slice's length and capacity - p.Bytes = append(p.Bytes[:cap(p.Bytes)], make([]byte, neededSize-cap(p.Bytes), capToAdd)...) } // PackByte append a byte to the byte array diff --git a/vms/components/codec/codec.go b/vms/components/codec/codec.go index 53852a9..6521993 100644 --- a/vms/components/codec/codec.go +++ b/vms/components/codec/codec.go @@ -19,7 +19,7 @@ const ( // initial capacity of byte slice that values are marshaled into. // Larger value --> need less memory allocations but possibly have allocated but unused memory // Smaller value --> need more memory allocations but more efficient use of allocated memory - initialSliceCap = 1024 + initialSliceCap = 256 ) var (