-
-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize memory usage by avoiding intermediate buffer in message seri…
…alization (#928) * Optimize memory usage by avoiding intermediate buffer in message serialization This commit replaces the use of an intermediate buffer in the message serialization process with a direct write-to-buffer approach. The original implementation used MustMarshalBinary() which involved an extra memory copy to an intermediate buffer before writing to the final writeBuffer, leading to high memory consumption for large messages. The new WriteTo function writes message data directly to the writeBuffer, significantly reducing memory overhead and CPU time spent on garbage collection. * add benchmark for write * benchmark for 1M/4M/8M * Tidy up new benchmarks * Maintain older payload write implementation --------- Co-authored-by: luozhengjie.lzj <[email protected]> Co-authored-by: Matt Joiner <[email protected]>
- Loading branch information
1 parent
78e00f9
commit 3f5ef0b
Showing
4 changed files
with
136 additions
and
17 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,68 @@ | ||
package torrent | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/dustin/go-humanize" | ||
|
||
pp "github.com/anacrolix/torrent/peer_protocol" | ||
) | ||
|
||
func PieceMsg(length int64) pp.Message { | ||
return pp.Message{ | ||
Type: pp.Piece, | ||
Index: pp.Integer(0), | ||
Begin: pp.Integer(0), | ||
Piece: make([]byte, length), | ||
} | ||
} | ||
|
||
var benchmarkPieceLengths = []int{defaultChunkSize, 1 << 20, 4 << 20, 8 << 20} | ||
|
||
func runBenchmarkWriteToBuffer(b *testing.B, length int64) { | ||
writer := &peerConnMsgWriter{ | ||
writeBuffer: &bytes.Buffer{}, | ||
} | ||
msg := PieceMsg(length) | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
//b.StopTimer() | ||
writer.writeBuffer.Reset() | ||
//b.StartTimer() | ||
writer.writeToBuffer(msg) | ||
} | ||
} | ||
|
||
func BenchmarkWritePieceMsg(b *testing.B) { | ||
for _, length := range benchmarkPieceLengths { | ||
b.Run(humanize.IBytes(uint64(length)), func(b *testing.B) { | ||
b.Run("ToBuffer", func(b *testing.B) { | ||
b.SetBytes(int64(length)) | ||
runBenchmarkWriteToBuffer(b, int64(length)) | ||
}) | ||
b.Run("MarshalBinary", func(b *testing.B) { | ||
b.SetBytes(int64(length)) | ||
runBenchmarkMarshalBinaryWrite(b, int64(length)) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func runBenchmarkMarshalBinaryWrite(b *testing.B, length int64) { | ||
writer := &peerConnMsgWriter{ | ||
writeBuffer: &bytes.Buffer{}, | ||
} | ||
msg := PieceMsg(length) | ||
|
||
b.ReportAllocs() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
//b.StopTimer() | ||
writer.writeBuffer.Reset() | ||
//b.StartTimer() | ||
writer.writeBuffer.Write(msg.MustMarshalBinary()) | ||
} | ||
} |
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