Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protoutil: prefer MarshalToSizedBuffer over MarshalTo where possible #105683

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/kv/kvserver/logstore/sideload.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func MaybeSideloadEntries(
{
data := make([]byte, raftlog.RaftCommandPrefixLen+e.Cmd.Size())
raftlog.EncodeRaftCommandPrefix(data[:raftlog.RaftCommandPrefixLen], typ, e.ID)
_, err := protoutil.MarshalTo(&e.Cmd, data[raftlog.RaftCommandPrefixLen:])
_, err := protoutil.MarshalToSizedBuffer(&e.Cmd, data[raftlog.RaftCommandPrefixLen:])
if err != nil {
return nil, 0, 0, 0, errors.Wrap(err, "while marshaling stripped sideloaded command")
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func MaybeInlineSideloadedRaftCommand(
{
data := make([]byte, raftlog.RaftCommandPrefixLen+e.Cmd.Size())
raftlog.EncodeRaftCommandPrefix(data[:raftlog.RaftCommandPrefixLen], typ, e.ID)
_, err := protoutil.MarshalTo(&e.Cmd, data[raftlog.RaftCommandPrefixLen:])
_, err := protoutil.MarshalToSizedBuffer(&e.Cmd, data[raftlog.RaftCommandPrefixLen:])
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/kv/kvserver/raftlog/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func EncodeCommand(
return nil, errors.AssertionFailedf("missing origin node for flow token returns")
}
}
if _, err := protoutil.MarshalTo(
if _, err := protoutil.MarshalToSizedBuffer(
raftAdmissionMeta,
data[preLen:preLen+admissionMetaLen],
); err != nil {
Expand All @@ -112,7 +112,7 @@ func EncodeCommand(
}

// Encode the rest of the command.
if _, err := protoutil.MarshalTo(command, data[preLen+admissionMetaLen:]); err != nil {
if _, err := protoutil.MarshalToSizedBuffer(command, data[preLen+admissionMetaLen:]); err != nil {
return nil, err
}
return data, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/kv/kvserver/replica_consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ func CalcReplicaDigest(
} else {
timestampBuf = timestampBuf[:size]
}
if _, err := protoutil.MarshalTo(&legacyTimestamp, timestampBuf); err != nil {
if _, err := protoutil.MarshalToSizedBuffer(&legacyTimestamp, timestampBuf); err != nil {
return err
}
if _, err := hasher.Write(timestampBuf); err != nil {
Expand Down Expand Up @@ -585,7 +585,7 @@ func CalcReplicaDigest(
} else {
timestampBuf = timestampBuf[:size]
}
if _, err := protoutil.MarshalTo(&legacyTimestamp, timestampBuf); err != nil {
if _, err := protoutil.MarshalToSizedBuffer(&legacyTimestamp, timestampBuf); err != nil {
return err
}
if _, err := hasher.Write(timestampBuf); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kv/kvserver/replica_proposal_buf.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ func (b *propBuf) marshallLAIAndClosedTimestampToProposalLocked(
// capacity for this footer.
preLen := len(p.encodedCommand)
p.encodedCommand = p.encodedCommand[:preLen+buf.Size()]
_, err := protoutil.MarshalTo(buf, p.encodedCommand[preLen:])
_, err := protoutil.MarshalToSizedBuffer(buf, p.encodedCommand[preLen:])
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/kv/kvserver/replica_proposal_buf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (pc proposalCreator) encodeProposal(p *ProposalData) []byte {
data := make([]byte, raftlog.RaftCommandPrefixLen, needed)
raftlog.EncodeRaftCommandPrefix(data, raftlog.EntryEncodingStandardWithoutAC, p.idKey)
data = data[:raftlog.RaftCommandPrefixLen+p.command.Size()]
if _, err := protoutil.MarshalTo(p.command, data[raftlog.RaftCommandPrefixLen:]); err != nil {
if _, err := protoutil.MarshalToSizedBuffer(p.command, data[raftlog.RaftCommandPrefixLen:]); err != nil {
panic(err)
}
return data
Expand Down
2 changes: 1 addition & 1 deletion pkg/roachpb/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func (v *Value) SetProto(msg protoutil.Message) error {
// directly into the Value.RawBytes field instead of allocating a separate
// []byte and copying.
v.ensureRawBytes(headerSize + msg.Size())
if _, err := protoutil.MarshalTo(msg, v.RawBytes[headerSize:]); err != nil {
if _, err := protoutil.MarshalToSizedBuffer(msg, v.RawBytes[headerSize:]); err != nil {
return err
}
// Special handling for timeseries data.
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/mvcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ func (b *putBuffer) marshalMeta(meta *enginepb.MVCCMetadata) (_ []byte, err erro
} else {
data = data[:size]
}
n, err := protoutil.MarshalTo(meta, data)
n, err := protoutil.MarshalToSizedBuffer(meta, data)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/util/protoutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
importpath = "github.com/cockroachdb/cockroach/pkg/util/protoutil",
visibility = ["//visibility:public"],
deps = [
"//pkg/util/buildutil",
"//pkg/util/syncutil",
"@com_github_cockroachdb_errors//:errors",
"@com_github_gogo_protobuf//jsonpb",
Expand Down
31 changes: 27 additions & 4 deletions pkg/util/protoutil/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

package protoutil

import "github.com/gogo/protobuf/proto"
import (
"fmt"

"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/gogo/protobuf/proto"
)

// Message extends the proto.Message interface with the MarshalTo and Size
// methods we tell gogoproto to generate for us.
Expand All @@ -22,7 +27,7 @@ type Message interface {
Size() int
}

// Marshal encodes pb into the wire format.
// Marshal encodes pb into the wire format and returns the resulting byte slice.
func Marshal(pb Message) ([]byte, error) {
dest := make([]byte, pb.Size())
if _, err := MarshalToSizedBuffer(pb, dest); err != nil {
Expand All @@ -31,13 +36,31 @@ func Marshal(pb Message) ([]byte, error) {
return dest, nil
}

// MarshalTo encodes pb into the wire format.
// MarshalTo encodes pb into the wire format and writes the result into the
// provided byte slice, returning the number of bytes written.
//
// dest is required to have a capacity of at least pb.Size() bytes.
func MarshalTo(pb Message, dest []byte) (int, error) {
if buildutil.CrdbTestBuild {
if pb.Size() > cap(dest) {
panic(fmt.Sprintf("MarshalTo called for %T with slice with insufficient "+
"capacity: pb.Size()=%d, cap(dest)=%d", pb, pb.Size(), cap(dest)))
}
}
return pb.MarshalTo(dest)
}

// MarshalToSizedBuffer encodes pb into the wire format.
// MarshalToSizedBuffer encodes pb into the wire format and writes the result
// into the provided byte slice, returning the number of bytes written.
//
// dest is required to have a length of exactly pb.Size() bytes.
func MarshalToSizedBuffer(pb Message, dest []byte) (int, error) {
if buildutil.CrdbTestBuild {
if pb.Size() != len(dest) {
panic(fmt.Sprintf("MarshalToSizedBuffer called for %T with slice with "+
"incorrect length: pb.Size()=%d, len(dest)=%d", pb, pb.Size(), len(dest)))
}
}
return pb.MarshalToSizedBuffer(dest)
}

Expand Down