diff --git a/pkg/kv/kvserver/BUILD.bazel b/pkg/kv/kvserver/BUILD.bazel index 9c75361ea619..6ff094cb8012 100644 --- a/pkg/kv/kvserver/BUILD.bazel +++ b/pkg/kv/kvserver/BUILD.bazel @@ -189,6 +189,7 @@ go_library( "//pkg/util/buildutil", "//pkg/util/circuit", "//pkg/util/ctxgroup", + "//pkg/util/encoding", "//pkg/util/envutil", "//pkg/util/errorutil", "//pkg/util/future", diff --git a/pkg/kv/kvserver/replica_evaluate.go b/pkg/kv/kvserver/replica_evaluate.go index 255203546a55..7e6becf70301 100644 --- a/pkg/kv/kvserver/replica_evaluate.go +++ b/pkg/kv/kvserver/replica_evaluate.go @@ -27,6 +27,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/roachpb" "github.com/cockroachdb/cockroach/pkg/storage" "github.com/cockroachdb/cockroach/pkg/storage/enginepb" + "github.com/cockroachdb/cockroach/pkg/util/encoding" "github.com/cockroachdb/cockroach/pkg/util/hlc" "github.com/cockroachdb/cockroach/pkg/util/log" "github.com/cockroachdb/cockroach/pkg/util/tracing" @@ -54,12 +55,15 @@ func optimizePuts( } // Returns false on occurrence of a duplicate key. maybeAddPut := func(key roachpb.Key) bool { - // Note that casting the byte slice key to a string does not allocate. + // The lookup will not copy key but the map insertion will, but since the + // map doesn't escape and we don't mutate the keys its safe to + // not allocate for both. + mapKey := encoding.UnsafeConvertBytesToString(key) if unique != nil { - if _, ok := unique[string(key)]; ok { + if _, ok := unique[mapKey]; ok { return false } - unique[string(key)] = struct{}{} + unique[mapKey] = struct{}{} } if minKey == nil || bytes.Compare(key, minKey) < 0 { minKey = key