internal/impl: add MessageState to every generated message

We define MessageState, which is essentially an atomically set *MessageInfo.
By nesting this as the first field in every generated message, we can
implement the reflective methods on a *MessageState when obtained by
unsafe casting a concrete message pointer as a *MessageState.
The MessageInfo held by MessageState provides additional Go type information
to interpret the memory that comes after the contents of the MessageState.

Since we are nesting a MessageState in every message,
the memory use of every message instance grows by 8B.

On average, the body of ProtoReflect grows from 133B to 202B (+50%).
However, this is offset by XXX_Methods, which is 108B and
will be removed in a future CL. Taking into account the eventual removal
of XXX_Methods, this is a net reduction of 25%.

name          old time/op    new time/op    delta
Name/Value-4    70.3ns ± 2%    17.5ns ± 6%   -75.08%  (p=0.000 n=10+10)
Name/Nil-4      70.6ns ± 3%    33.4ns ± 2%   -52.66%  (p=0.000 n=10+10)

name          old alloc/op   new alloc/op   delta
Name/Value-4     16.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)
Name/Nil-4       16.0B ± 0%      0.0B       -100.00%  (p=0.000 n=10+10)

name          old allocs/op  new allocs/op  delta
Name/Value-4      1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)
Name/Nil-4        1.00 ± 0%      0.00       -100.00%  (p=0.000 n=10+10)

Change-Id: I92bd58dc681c57c92612fd5ba7fc066aea34e95a
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/185460
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/testprotos/test3/test.pb.go b/internal/testprotos/test3/test.pb.go
index 480baea..11b752b 100644
--- a/internal/testprotos/test3/test.pb.go
+++ b/internal/testprotos/test3/test.pb.go
@@ -111,6 +111,7 @@
 }
 
 type TestAllTypes struct {
+	state                  protoimpl.MessageState
 	OptionalInt32          int32                                  `protobuf:"varint,1,opt,name=optional_int32,json=optionalInt32,proto3" json:"optional_int32,omitempty"`
 	OptionalInt64          int64                                  `protobuf:"varint,2,opt,name=optional_int64,json=optionalInt64,proto3" json:"optional_int64,omitempty"`
 	OptionalUint32         uint32                                 `protobuf:"varint,3,opt,name=optional_uint32,json=optionalUint32,proto3" json:"optional_uint32,omitempty"`
@@ -196,10 +197,18 @@
 func (*TestAllTypes) ProtoMessage() {}
 
 func (x *TestAllTypes) ProtoReflect() protoreflect.Message {
-	return file_test3_test_proto_msgTypes[0].MessageOf(x)
+	mi := &file_test3_test_proto_msgTypes[0]
+	if protoimpl.UnsafeEnabled && x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
 }
 
-func (m *TestAllTypes) XXX_Methods() *protoiface.Methods {
+func (x *TestAllTypes) XXX_Methods() *protoiface.Methods {
 	return file_test3_test_proto_msgTypes[0].Methods()
 }
 
@@ -750,6 +759,7 @@
 func (*TestAllTypes_OneofEnum) isTestAllTypes_OneofField() {}
 
 type ForeignMessage struct {
+	state         protoimpl.MessageState
 	C             int32 `protobuf:"varint,1,opt,name=c,proto3" json:"c,omitempty"`
 	D             int32 `protobuf:"varint,2,opt,name=d,proto3" json:"d,omitempty"`
 	sizeCache     protoimpl.SizeCache
@@ -767,10 +777,18 @@
 func (*ForeignMessage) ProtoMessage() {}
 
 func (x *ForeignMessage) ProtoReflect() protoreflect.Message {
-	return file_test3_test_proto_msgTypes[1].MessageOf(x)
+	mi := &file_test3_test_proto_msgTypes[1]
+	if protoimpl.UnsafeEnabled && x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
 }
 
-func (m *ForeignMessage) XXX_Methods() *protoiface.Methods {
+func (x *ForeignMessage) XXX_Methods() *protoiface.Methods {
 	return file_test3_test_proto_msgTypes[1].Methods()
 }
 
@@ -794,6 +812,7 @@
 }
 
 type TestAllTypes_NestedMessage struct {
+	state         protoimpl.MessageState
 	A             int32         `protobuf:"varint,1,opt,name=a,proto3" json:"a,omitempty"`
 	Corecursive   *TestAllTypes `protobuf:"bytes,2,opt,name=corecursive,proto3" json:"corecursive,omitempty"`
 	sizeCache     protoimpl.SizeCache
@@ -811,10 +830,18 @@
 func (*TestAllTypes_NestedMessage) ProtoMessage() {}
 
 func (x *TestAllTypes_NestedMessage) ProtoReflect() protoreflect.Message {
-	return file_test3_test_proto_msgTypes[2].MessageOf(x)
+	mi := &file_test3_test_proto_msgTypes[2]
+	if protoimpl.UnsafeEnabled && x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
 }
 
-func (m *TestAllTypes_NestedMessage) XXX_Methods() *protoiface.Methods {
+func (x *TestAllTypes_NestedMessage) XXX_Methods() *protoiface.Methods {
 	return file_test3_test_proto_msgTypes[2].Methods()
 }
 
@@ -1322,9 +1349,11 @@
 	if !protoimpl.UnsafeEnabled {
 		file_test3_test_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
 			switch v := v.(*TestAllTypes); i {
-			case 60:
-				return &v.sizeCache
+			case 0:
+				return &v.state
 			case 61:
+				return &v.sizeCache
+			case 62:
 				return &v.unknownFields
 			default:
 				return nil
@@ -1332,9 +1361,11 @@
 		}
 		file_test3_test_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
 			switch v := v.(*ForeignMessage); i {
-			case 2:
-				return &v.sizeCache
+			case 0:
+				return &v.state
 			case 3:
+				return &v.sizeCache
+			case 4:
 				return &v.unknownFields
 			default:
 				return nil
@@ -1342,9 +1373,11 @@
 		}
 		file_test3_test_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
 			switch v := v.(*TestAllTypes_NestedMessage); i {
-			case 2:
-				return &v.sizeCache
+			case 0:
+				return &v.state
 			case 3:
+				return &v.sizeCache
+			case 4:
 				return &v.unknownFields
 			default:
 				return nil