forked from algorand/go-algorand
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize LimitedReaderSlurper memory utilization (algorand#1876)
The memory profiler shown that the `LimitedReaderSlurper` is one of the bigger memory consumer on a relay. This behavior aligns well with the original intent - preallocate memory so that we won't need to reallocate it on every message. The unintended consequence of that was that a relay that has many incoming active connections might be using more memory than it really needs to. To address this solution, I have used series of reading buffers. These buffers would be dynamically allocated on per-need basis. In order to retain original performance characteristics, I have set the size of the base buffer to be larger than the average message. Doing so would allow us to avoid allocation on *most* of the messages, and allocate buffers only when truly needed. Another advantage is that the allocated memory is of a fixed, and small size. Allocating smaller buffers improves the performance when compared with larger buffers.
- Loading branch information
1 parent
9a0c452
commit 5e4ca61
Showing
4 changed files
with
238 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Copyright (C) 2019-2021 Algorand, Inc. | ||
// This file is part of go-algorand | ||
// | ||
// go-algorand is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// go-algorand is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package network | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/algorand/go-algorand/crypto" | ||
) | ||
|
||
func TestLimitedReaderSlurper(t *testing.T) { | ||
for _, arraySize := range []uint64{30000, 90000, 200000} { | ||
// create a random bytes array. | ||
bytesBlob := make([]byte, arraySize) | ||
crypto.RandBytes(bytesBlob[:]) | ||
for baseBufferSize := uint64(0); baseBufferSize < uint64(len(bytesBlob)); baseBufferSize += 731 { | ||
for _, maxSize := range []uint64{arraySize - 10000, arraySize, arraySize + 10000} { | ||
buffer := bytes.NewBuffer(bytesBlob) | ||
reader := MakeLimitedReaderSlurper(baseBufferSize, maxSize) | ||
err := reader.Read(buffer) | ||
if maxSize < uint64(len(bytesBlob)) { | ||
require.Equal(t, ErrIncomingMsgTooLarge, err) | ||
continue | ||
} | ||
|
||
require.NoError(t, err) | ||
bytes := reader.Bytes() | ||
require.Equal(t, bytesBlob, bytes) | ||
} | ||
} | ||
} | ||
} | ||
|
||
type fuzzReader struct { | ||
pos int | ||
buf []byte | ||
} | ||
|
||
func (f *fuzzReader) Read(b []byte) (n int, err error) { | ||
s := int(crypto.RandUint64() % 19) | ||
if s > len(b) { | ||
s = len(b) | ||
} | ||
if f.pos >= len(f.buf) { | ||
return 0, io.EOF | ||
} | ||
if f.pos+s >= len(f.buf) { | ||
// we want a chunk that ends at ( or after ) the end of the data. | ||
n = len(f.buf) - f.pos | ||
err = io.EOF | ||
} else { | ||
n = s | ||
} | ||
copy(b, f.buf[f.pos:f.pos+n]) | ||
f.pos += n | ||
return | ||
} | ||
|
||
func TestLimitedReaderSlurper_FuzzedBlippedSource(t *testing.T) { | ||
arraySize := uint64(300000) | ||
bytesBlob := make([]byte, arraySize) | ||
crypto.RandBytes(bytesBlob[:]) | ||
for i := 0; i < 500; i++ { | ||
for _, maxSize := range []uint64{arraySize - 10000, arraySize, arraySize + 10000} { | ||
reader := MakeLimitedReaderSlurper(512, maxSize) | ||
err := reader.Read(&fuzzReader{buf: bytesBlob}) | ||
if maxSize < uint64(len(bytesBlob)) { | ||
require.Equal(t, ErrIncomingMsgTooLarge, err, "i: %d\nmaxSize: %d", i, maxSize) | ||
continue | ||
} | ||
require.NoError(t, err) | ||
bytes := reader.Bytes() | ||
require.Equal(t, bytesBlob, bytes) | ||
} | ||
} | ||
} | ||
|
||
func benchmarkLimitedReaderSlurper(b *testing.B, arraySize uint64) { | ||
bytesBlob := make([]byte, arraySize) | ||
crypto.RandBytes(bytesBlob[:]) | ||
readers := make([]*LimitedReaderSlurper, b.N) | ||
buffers := make([]*bytes.Buffer, b.N) | ||
for i := 0; i < b.N; i++ { | ||
buffers[i] = bytes.NewBuffer(bytesBlob) | ||
readers[i] = MakeLimitedReaderSlurper(1024, 1024*1024) | ||
} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
reader := readers[i] | ||
err := reader.Read(buffers[i]) | ||
require.NoError(b, err) | ||
reader.Bytes() | ||
reader.Reset() | ||
} | ||
} | ||
func BenchmarkLimitedReaderSlurper(b *testing.B) { | ||
for _, arraySize := range []uint64{200, 2048, 300000} { | ||
b.Run(fmt.Sprintf("%dbytes_message", arraySize), func(b *testing.B) { | ||
benchmarkLimitedReaderSlurper(b, arraySize) | ||
}) | ||
} | ||
} | ||
|
||
func TestLimitedReaderSlurperMemoryConsumption(t *testing.T) { | ||
for _, arraySize := range []uint64{1024, 2048, 65536, 1024 * 1024} { | ||
result := testing.Benchmark(func(b *testing.B) { | ||
benchmarkLimitedReaderSlurper(b, arraySize) | ||
}) | ||
require.True(t, uint64(result.AllocedBytesPerOp()) < 2*arraySize+allocationStep, "AllocedBytesPerOp:%d\nmessage size:%d", result.AllocedBytesPerOp(), arraySize) | ||
} | ||
} | ||
|
||
func TestLimitedReaderSlurperBufferAllocations(t *testing.T) { | ||
for baseAllocation := uint64(512); baseAllocation < 100000; baseAllocation += 2048 { | ||
for maxAllocation := uint64(512); maxAllocation < 100000; maxAllocation += 512 { | ||
lrs := MakeLimitedReaderSlurper(baseAllocation, maxAllocation) | ||
// check to see if the allocated buffers count is exactly what needed to match the allocation needs. | ||
allocationNeeds := 1 | ||
remainingBytes := int64(maxAllocation - baseAllocation) | ||
for remainingBytes > 0 { | ||
allocationNeeds++ | ||
remainingBytes -= int64(allocationStep) | ||
} | ||
require.Equal(t, allocationNeeds, len(lrs.buffers)) | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters