diff --git a/.changelog/unreleased/improvements/2949-reduce-protoio-writer-creation-time.md b/.changelog/unreleased/improvements/2949-reduce-protoio-writer-creation-time.md new file mode 100644 index 00000000000..75838e24882 --- /dev/null +++ b/.changelog/unreleased/improvements/2949-reduce-protoio-writer-creation-time.md @@ -0,0 +1,2 @@ +- `[p2p/channel]` Speedup `ProtoIO` writer creation time, and thereby speedup channel writing by 5%. + ([\#2949](https://github.com/cometbft/cometbft/pull/2949)) \ No newline at end of file diff --git a/libs/protoio/writer.go b/libs/protoio/writer.go index 0eb65850cfd..d1f6f03d1bc 100644 --- a/libs/protoio/writer.go +++ b/libs/protoio/writer.go @@ -42,7 +42,7 @@ import ( // equivalent to the gogoproto NewDelimitedWriter, except WriteMsg() also returns the // number of bytes written, which is necessary in the p2p package. func NewDelimitedWriter(w io.Writer) WriteCloser { - return &varintWriter{w, make([]byte, binary.MaxVarintLen64), nil} + return &varintWriter{w, nil, nil} } type varintWriter struct { @@ -69,6 +69,9 @@ func (w *varintWriter) WriteMsg(msg proto.Message) (int, error) { } // fallback + if w.lenBuf == nil { + w.lenBuf = make([]byte, binary.MaxVarintLen64) + } data, err := proto.Marshal(msg) if err != nil { return 0, err