protobuf-go/proto/size_gen.go
Damien Neil cedb595154 proto, internal/impl: avoid string->[]byte conversions
We trust the compiler to optimize away the string->[]byte conversion in
code like:

	b = wire.AppendBytes(b, []byte(s))

In testing (go 1.12.5 linux/amd64), this optimization is not happening.
Perhaps newer versions of the compiler will optimize this, but we
shouldn't rely on it; avoid unnecessary conversions.

Benchmark differences vs https://golang.org/cl/171462:

  name                                   old time/op    new time/op    delta
  Wire/Marshal/google_message1_proto2-6     310ns ± 2%     189ns ± 3%  -39.20%  (p=0.000 n=8+8)
  Wire/Marshal/google_message1_proto3-6     389ns ± 8%     261ns ± 2%  -33.03%  (p=0.000 n=8+8)
  Wire/Marshal/google_message2-6            103µs ±11%      59µs ± 4%  -42.17%  (p=0.000 n=8+8)

  name                                   old alloc/op   new alloc/op   delta
  Wire/Marshal/google_message1_proto2-6      592B ± 0%      240B ± 0%  -59.46%  (p=0.000 n=8+8)
  Wire/Marshal/google_message1_proto3-6      576B ± 0%      224B ± 0%  -61.11%  (p=0.000 n=8+8)
  Wire/Marshal/google_message2-6            196kB ± 0%      90kB ± 0%  -54.05%  (p=0.000 n=8+8)

  name                                   old allocs/op  new allocs/op  delta
  Wire/Marshal/google_message1_proto2-6      5.00 ± 0%      1.00 ± 0%  -80.00%  (p=0.000 n=8+8)
  Wire/Marshal/google_message1_proto3-6      5.00 ± 0%      1.00 ± 0%  -80.00%  (p=0.000 n=8+8)
  Wire/Marshal/google_message2-6            1.66k ± 0%     0.00k ± 0%  -99.94%  (p=0.000 n=8+8)

Change-Id: Idab7634b8c86604dffa46895ba2e61be38c9bd9c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/183380
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-06-24 17:41:45 +00:00

56 lines
1.7 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style.
// license that can be found in the LICENSE file.
// Code generated by generate-types. DO NOT EDIT.
package proto
import (
"google.golang.org/protobuf/internal/encoding/wire"
"google.golang.org/protobuf/reflect/protoreflect"
)
func sizeSingular(num wire.Number, kind protoreflect.Kind, v protoreflect.Value) int {
switch kind {
case protoreflect.BoolKind:
return wire.SizeVarint(wire.EncodeBool(v.Bool()))
case protoreflect.EnumKind:
return wire.SizeVarint(uint64(v.Enum()))
case protoreflect.Int32Kind:
return wire.SizeVarint(uint64(int32(v.Int())))
case protoreflect.Sint32Kind:
return wire.SizeVarint(wire.EncodeZigZag(int64(int32(v.Int()))))
case protoreflect.Uint32Kind:
return wire.SizeVarint(uint64(uint32(v.Uint())))
case protoreflect.Int64Kind:
return wire.SizeVarint(uint64(v.Int()))
case protoreflect.Sint64Kind:
return wire.SizeVarint(wire.EncodeZigZag(v.Int()))
case protoreflect.Uint64Kind:
return wire.SizeVarint(v.Uint())
case protoreflect.Sfixed32Kind:
return wire.SizeFixed32()
case protoreflect.Fixed32Kind:
return wire.SizeFixed32()
case protoreflect.FloatKind:
return wire.SizeFixed32()
case protoreflect.Sfixed64Kind:
return wire.SizeFixed64()
case protoreflect.Fixed64Kind:
return wire.SizeFixed64()
case protoreflect.DoubleKind:
return wire.SizeFixed64()
case protoreflect.StringKind:
return wire.SizeBytes(len(v.String()))
case protoreflect.BytesKind:
return wire.SizeBytes(len(v.Bytes()))
case protoreflect.MessageKind:
return wire.SizeBytes(sizeMessage(v.Message()))
case protoreflect.GroupKind:
return wire.SizeGroup(num, sizeMessage(v.Message()))
default:
return 0
}
}