]> Sergey Matveev's repositories - btrtrc.git/commit
Optimize memory usage by avoiding intermediate buffer in message serialization (...
authorLuo Zhengjie <48181069+Zerolzj@users.noreply.github.com>
Thu, 25 Apr 2024 05:19:54 +0000 (13:19 +0800)
committerGitHub <noreply@github.com>
Thu, 25 Apr 2024 05:19:54 +0000 (15:19 +1000)
commit3f5ef0be3e4540b9072af82340bb460233f92a09
tree7cb8ea4a42e695ea300cdb6e0b52926a73c3d8de
parent78e00f96aeefabd7b7dae07ff73f7cf1ec133705
Optimize memory usage by avoiding intermediate buffer in message serialization (#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 <luozhengjie.lzj@alibaba-inc.com>
Co-authored-by: Matt Joiner <anacrolix@gmail.com>
peer-conn-msg-writer.go
peer-conn-msg-writer_test.go [new file with mode: 0644]
peer_protocol/msg.go
requesting.go