From 9b816b49e10a7d0fd728d81ca70f74a3dcafd5e0 Mon Sep 17 00:00:00 2001 From: Chris O'Hara Date: Tue, 21 Nov 2023 14:22:23 +1000 Subject: [PATCH 1/6] Intern strings --- gen/proto/go/coroutine/v1/coroutine.pb.go | 53 +++++--- .../go/coroutine/v1/coroutine_vtproto.pb.go | 47 +++++++ gen/proto/go/coroutine/v1/type.pb.go | 43 +++--- gen/proto/go/coroutine/v1/type_vtproto.pb.go | 128 +++++------------- proto/coroutine/v1/coroutine.proto | 3 + proto/coroutine/v1/type.proto | 19 +-- types/inspect.go | 33 ++++- types/serde.go | 21 +-- types/string.go | 41 ++++++ types/types.go | 22 +-- 10 files changed, 241 insertions(+), 169 deletions(-) create mode 100644 types/string.go diff --git a/gen/proto/go/coroutine/v1/coroutine.pb.go b/gen/proto/go/coroutine/v1/coroutine.pb.go index d5d100e..997a47a 100644 --- a/gen/proto/go/coroutine/v1/coroutine.pb.go +++ b/gen/proto/go/coroutine/v1/coroutine.pb.go @@ -41,6 +41,8 @@ type State struct { Regions []*Region `protobuf:"bytes,5,rep,name=regions,proto3" json:"regions,omitempty"` // Root is the root object. Root *Region `protobuf:"bytes,6,opt,name=root,proto3" json:"root,omitempty"` + // Strings is the string table. + Strings []string `protobuf:"bytes,7,rep,name=strings,proto3" json:"strings,omitempty"` } func (x *State) Reset() { @@ -117,6 +119,13 @@ func (x *State) GetRoot() *Region { return nil } +func (x *State) GetStrings() []string { + if x != nil { + return x.Strings + } + return nil +} + // Build is info about the build in which a durable coroutine // is/was running. type Build struct { @@ -249,7 +258,7 @@ var file_coroutine_v1_coroutine_proto_rawDesc = []byte{ 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x2f, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x17, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x22, 0x82, 0x02, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x29, 0x0a, 0x05, + 0x74, 0x6f, 0x22, 0x9c, 0x02, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x29, 0x0a, 0x05, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x42, 0x75, 0x69, 0x6c, 0x64, 0x52, 0x05, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x74, 0x65, @@ -265,26 +274,28 @@ var file_coroutine_v1_coroutine_proto_rawDesc = []byte{ 0x67, 0x69, 0x6f, 0x6e, 0x52, 0x07, 0x72, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x28, 0x0a, 0x04, 0x72, 0x6f, 0x6f, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x14, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x52, 0x65, 0x67, 0x69, 0x6f, - 0x6e, 0x52, 0x04, 0x72, 0x6f, 0x6f, 0x74, 0x22, 0x3b, 0x0a, 0x05, 0x42, 0x75, 0x69, 0x6c, 0x64, - 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, - 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x6f, 0x73, - 0x12, 0x12, 0x0a, 0x04, 0x61, 0x72, 0x63, 0x68, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, - 0x61, 0x72, 0x63, 0x68, 0x22, 0x30, 0x0a, 0x06, 0x52, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x12, 0x12, - 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x79, - 0x70, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, - 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x42, 0xbd, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, - 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x42, 0x0e, 0x43, 0x6f, 0x72, - 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x48, 0x67, - 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x74, 0x65, 0x61, 0x6c, 0x74, - 0x68, 0x72, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2f, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, - 0x65, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x6f, 0x2f, 0x63, - 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x6f, 0x72, 0x6f, - 0x75, 0x74, 0x69, 0x6e, 0x65, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x43, 0x58, 0x58, 0xaa, 0x02, 0x0c, - 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x0c, 0x43, - 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5c, 0x56, 0x31, 0xe2, 0x02, 0x18, 0x43, 0x6f, - 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, - 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x0d, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, - 0x6e, 0x65, 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6e, 0x52, 0x04, 0x72, 0x6f, 0x6f, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x74, 0x72, 0x69, 0x6e, + 0x67, 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x09, 0x52, 0x07, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, + 0x73, 0x22, 0x3b, 0x0a, 0x05, 0x42, 0x75, 0x69, 0x6c, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x0e, 0x0a, 0x02, 0x6f, 0x73, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x6f, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x72, + 0x63, 0x68, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x72, 0x63, 0x68, 0x22, 0x30, + 0x0a, 0x06, 0x52, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x12, 0x0a, 0x04, + 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, + 0x42, 0xbd, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, + 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x42, 0x0e, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, + 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x48, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x74, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x72, 0x6f, 0x63, 0x6b, 0x65, + 0x74, 0x2f, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x67, 0x65, 0x6e, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x6f, 0x2f, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, + 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x76, + 0x31, 0xa2, 0x02, 0x03, 0x43, 0x58, 0x58, 0xaa, 0x02, 0x0c, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, + 0x69, 0x6e, 0x65, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x0c, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, + 0x6e, 0x65, 0x5c, 0x56, 0x31, 0xe2, 0x02, 0x18, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, + 0x65, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, + 0xea, 0x02, 0x0d, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x3a, 0x3a, 0x56, 0x31, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/gen/proto/go/coroutine/v1/coroutine_vtproto.pb.go b/gen/proto/go/coroutine/v1/coroutine_vtproto.pb.go index 47556b1..a5a96e0 100644 --- a/gen/proto/go/coroutine/v1/coroutine_vtproto.pb.go +++ b/gen/proto/go/coroutine/v1/coroutine_vtproto.pb.go @@ -47,6 +47,15 @@ func (m *State) MarshalToSizedBufferVT(dAtA []byte) (int, error) { i -= len(m.unknownFields) copy(dAtA[i:], m.unknownFields) } + if len(m.Strings) > 0 { + for iNdEx := len(m.Strings) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.Strings[iNdEx]) + copy(dAtA[i:], m.Strings[iNdEx]) + i = encodeVarint(dAtA, i, uint64(len(m.Strings[iNdEx]))) + i-- + dAtA[i] = 0x3a + } + } if m.Root != nil { size, err := m.Root.MarshalToSizedBufferVT(dAtA[:i]) if err != nil { @@ -248,6 +257,12 @@ func (m *State) SizeVT() (n int) { l = m.Root.SizeVT() n += 1 + l + sov(uint64(l)) } + if len(m.Strings) > 0 { + for _, s := range m.Strings { + l = len(s) + n += 1 + l + sov(uint64(l)) + } + } n += len(m.unknownFields) return n } @@ -528,6 +543,38 @@ func (m *State) UnmarshalVT(dAtA []byte) error { return err } iNdEx = postIndex + case 7: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Strings", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflow + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLength + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLength + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Strings = append(m.Strings, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skip(dAtA[iNdEx:]) diff --git a/gen/proto/go/coroutine/v1/type.pb.go b/gen/proto/go/coroutine/v1/type.pb.go index 01f5a27..a174bc6 100644 --- a/gen/proto/go/coroutine/v1/type.pb.go +++ b/gen/proto/go/coroutine/v1/type.pb.go @@ -208,10 +208,11 @@ type Type struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - // Name is the name of the type within the package it was defined, - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` - // Package is the name of the package that defines the type. - Package string `protobuf:"bytes,2,opt,name=package,proto3" json:"package,omitempty"` + // Name is the string ID of the name of the type within the package it was + // defined. + Name int32 `protobuf:"varint,1,opt,name=name,proto3" json:"name,omitempty"` + // Package is the string ID the name of the package that defines the type. + Package int32 `protobuf:"varint,2,opt,name=package,proto3" json:"package,omitempty"` // Kind is the underlying type. Kind Kind `protobuf:"varint,3,opt,name=kind,proto3,enum=coroutine.v1.Kind" json:"kind,omitempty"` // Elem is the type of an array, slice, pointer, chan, or map's element. @@ -269,18 +270,18 @@ func (*Type) Descriptor() ([]byte, []int) { return file_coroutine_v1_type_proto_rawDescGZIP(), []int{0} } -func (x *Type) GetName() string { +func (x *Type) GetName() int32 { if x != nil { return x.Name } - return "" + return 0 } -func (x *Type) GetPackage() string { +func (x *Type) GetPackage() int32 { if x != nil { return x.Package } - return "" + return 0 } func (x *Type) GetKind() Kind { @@ -365,11 +366,11 @@ type Field struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - // Name is the name of the field. - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` - // Package is the package path that qualifies a lower case (unexported) - // field name. It is empty for upper case (exported) field names. - Package string `protobuf:"bytes,2,opt,name=package,proto3" json:"package,omitempty"` + // Name is the string ID of the name of the field. + Name int32 `protobuf:"varint,1,opt,name=name,proto3" json:"name,omitempty"` + // Package is the string ID of package path that qualifies a lower case + // (unexported) field name. It is empty for exported field names. + Package int32 `protobuf:"varint,2,opt,name=package,proto3" json:"package,omitempty"` // Type is the type of the field. Type int32 `protobuf:"varint,3,opt,name=type,proto3" json:"type,omitempty"` // Offset is the offset of the field within its struct, in bytes. @@ -415,18 +416,18 @@ func (*Field) Descriptor() ([]byte, []int) { return file_coroutine_v1_type_proto_rawDescGZIP(), []int{1} } -func (x *Field) GetName() string { +func (x *Field) GetName() int32 { if x != nil { return x.Name } - return "" + return 0 } -func (x *Field) GetPackage() string { +func (x *Field) GetPackage() int32 { if x != nil { return x.Package } - return "" + return 0 } func (x *Field) GetType() int32 { @@ -470,9 +471,9 @@ var file_coroutine_v1_type_proto_rawDesc = []byte{ 0x0a, 0x17, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0c, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x22, 0x99, 0x03, 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, - 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, + 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x26, + 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x26, 0x0a, 0x04, 0x6b, 0x69, 0x6e, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x12, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x4b, 0x69, 0x6e, 0x64, 0x52, 0x04, 0x6b, 0x69, 0x6e, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x65, 0x6c, 0x65, 0x6d, 0x18, 0x04, @@ -496,9 +497,9 @@ var file_coroutine_v1_type_proto_rawDesc = []byte{ 0x5f, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x72, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x08, 0x52, 0x10, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x72, 0x22, 0xa7, 0x01, 0x0a, 0x05, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x12, 0x0a, - 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, + 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, + 0x28, 0x05, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6e, 0x64, 0x65, 0x78, diff --git a/gen/proto/go/coroutine/v1/type_vtproto.pb.go b/gen/proto/go/coroutine/v1/type_vtproto.pb.go index bdbb2d1..c1241dc 100644 --- a/gen/proto/go/coroutine/v1/type_vtproto.pb.go +++ b/gen/proto/go/coroutine/v1/type_vtproto.pb.go @@ -151,19 +151,15 @@ func (m *Type) MarshalToSizedBufferVT(dAtA []byte) (int, error) { i-- dAtA[i] = 0x18 } - if len(m.Package) > 0 { - i -= len(m.Package) - copy(dAtA[i:], m.Package) - i = encodeVarint(dAtA, i, uint64(len(m.Package))) + if m.Package != 0 { + i = encodeVarint(dAtA, i, uint64(m.Package)) i-- - dAtA[i] = 0x12 + dAtA[i] = 0x10 } - if len(m.Name) > 0 { - i -= len(m.Name) - copy(dAtA[i:], m.Name) - i = encodeVarint(dAtA, i, uint64(len(m.Name))) + if m.Name != 0 { + i = encodeVarint(dAtA, i, uint64(m.Name)) i-- - dAtA[i] = 0xa + dAtA[i] = 0x8 } return len(dAtA) - i, nil } @@ -246,19 +242,15 @@ func (m *Field) MarshalToSizedBufferVT(dAtA []byte) (int, error) { i-- dAtA[i] = 0x18 } - if len(m.Package) > 0 { - i -= len(m.Package) - copy(dAtA[i:], m.Package) - i = encodeVarint(dAtA, i, uint64(len(m.Package))) + if m.Package != 0 { + i = encodeVarint(dAtA, i, uint64(m.Package)) i-- - dAtA[i] = 0x12 + dAtA[i] = 0x10 } - if len(m.Name) > 0 { - i -= len(m.Name) - copy(dAtA[i:], m.Name) - i = encodeVarint(dAtA, i, uint64(len(m.Name))) + if m.Name != 0 { + i = encodeVarint(dAtA, i, uint64(m.Name)) i-- - dAtA[i] = 0xa + dAtA[i] = 0x8 } return len(dAtA) - i, nil } @@ -269,13 +261,11 @@ func (m *Type) SizeVT() (n int) { } var l int _ = l - l = len(m.Name) - if l > 0 { - n += 1 + l + sov(uint64(l)) + if m.Name != 0 { + n += 1 + sov(uint64(m.Name)) } - l = len(m.Package) - if l > 0 { - n += 1 + l + sov(uint64(l)) + if m.Package != 0 { + n += 1 + sov(uint64(m.Package)) } if m.Kind != 0 { n += 1 + sov(uint64(m.Kind)) @@ -331,13 +321,11 @@ func (m *Field) SizeVT() (n int) { } var l int _ = l - l = len(m.Name) - if l > 0 { - n += 1 + l + sov(uint64(l)) + if m.Name != 0 { + n += 1 + sov(uint64(m.Name)) } - l = len(m.Package) - if l > 0 { - n += 1 + l + sov(uint64(l)) + if m.Package != 0 { + n += 1 + sov(uint64(m.Package)) } if m.Type != 0 { n += 1 + sov(uint64(m.Type)) @@ -393,10 +381,10 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } switch fieldNum { case 1: - if wireType != 2 { + if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType) } - var stringLen uint64 + m.Name = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflow @@ -406,29 +394,16 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + m.Name |= int32(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLength - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLength - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Name = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex case 2: - if wireType != 2 { + if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Package", wireType) } - var stringLen uint64 + m.Package = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflow @@ -438,24 +413,11 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + m.Package |= int32(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLength - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLength - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Package = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex case 3: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Kind", wireType) @@ -848,10 +810,10 @@ func (m *Field) UnmarshalVT(dAtA []byte) error { } switch fieldNum { case 1: - if wireType != 2 { + if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType) } - var stringLen uint64 + m.Name = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflow @@ -861,29 +823,16 @@ func (m *Field) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + m.Name |= int32(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLength - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLength - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Name = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex case 2: - if wireType != 2 { + if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Package", wireType) } - var stringLen uint64 + m.Package = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflow @@ -893,24 +842,11 @@ func (m *Field) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + m.Package |= int32(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLength - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLength - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Package = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex case 3: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Type", wireType) diff --git a/proto/coroutine/v1/coroutine.proto b/proto/coroutine/v1/coroutine.proto index 486fc18..a24903a 100644 --- a/proto/coroutine/v1/coroutine.proto +++ b/proto/coroutine/v1/coroutine.proto @@ -27,6 +27,9 @@ message State { // Root is the root object. Region root = 6; + + // Strings is the string table. + repeated string strings = 7; } // Build is info about the build in which a durable coroutine diff --git a/proto/coroutine/v1/type.proto b/proto/coroutine/v1/type.proto index 206419b..180739d 100644 --- a/proto/coroutine/v1/type.proto +++ b/proto/coroutine/v1/type.proto @@ -10,11 +10,12 @@ package coroutine.v1; // in the array becomes its unique identifier, and internal references // to other types are simply an int32 index. message Type { - // Name is the name of the type within the package it was defined, - string name = 1; + // Name is the string ID of the name of the type within the package it was + // defined. + int32 name = 1; - // Package is the name of the package that defines the type. - string package = 2; + // Package is the string ID the name of the package that defines the type. + int32 package = 2; // Kind is the underlying type. Kind kind = 3; @@ -52,12 +53,12 @@ message Type { } message Field { - // Name is the name of the field. - string name = 1; + // Name is the string ID of the name of the field. + int32 name = 1; - // Package is the package path that qualifies a lower case (unexported) - // field name. It is empty for upper case (exported) field names. - string package = 2; + // Package is the string ID of package path that qualifies a lower case + // (unexported) field name. It is empty for exported field names. + int32 package = 2; // Type is the type of the field. int32 type = 3; diff --git a/types/inspect.go b/types/inspect.go index d65c400..d4f988d 100644 --- a/types/inspect.go +++ b/types/inspect.go @@ -89,6 +89,19 @@ func (s *State) Region(i int) *Region { } } +// NumString returns the number of strings referenced by types. +func (s *State) NumString() int { + return len(s.state.Strings) +} + +// String retrieves a string by index. +func (s *State) String(i int) string { + if i < 0 || i >= len(s.state.Strings) { + panic(fmt.Sprintf("string %d not found", i)) + } + return s.state.Strings[i] +} + // Root is the root object that was serialized. func (s *State) Root() *Region { return &Region{ @@ -105,12 +118,18 @@ type Type struct { // Name is the name of the type within the package it was defined. func (t *Type) Name() string { - return t.typ.Name + if t.typ.Name == 0 { + return "" + } + return t.state.String(int(t.typ.Name - 1)) } // Package is the name of the package that defines the type. func (t *Type) Package() string { - return t.typ.Package + if t.typ.Package == 0 { + return "" + } + return t.state.String(int(t.typ.Package - 1)) } // Kind is the underlying kind for this type. @@ -462,13 +481,19 @@ type Field struct { // Name is the name of the field. func (f *Field) Name() string { - return f.field.Name + if f.field.Name == 0 { + return "" + } + return f.state.String(int(f.field.Name - 1)) } // Package is the package path that qualifies a lwer case (unexported) // field name. It is empty for upper case (exported) field names. func (f *Field) Package() string { - return f.field.Package + if f.field.Package == 0 { + return "" + } + return f.state.String(int(f.field.Package - 1)) } // Type is the type of the field. diff --git a/types/serde.go b/types/serde.go index cdf92c9..c7ee8d3 100644 --- a/types/serde.go +++ b/types/serde.go @@ -58,6 +58,7 @@ func Serialize(x any) ([]byte, error) { Build: buildInfo, Types: s.types.types, Functions: s.funcs.funcs, + Strings: s.strings.strings, Regions: s.regions, Root: &coroutinev1.Region{ Type: int32(rootTypeID), @@ -77,7 +78,7 @@ func Deserialize(b []byte) (interface{}, error) { return nil, fmt.Errorf("%w: got %v, expect %v", ErrBuildIDMismatch, state.Build.Id, buildInfo.Id) } - d := newDeserializer(state.Root.Data, state.Types, state.Functions, state.Regions) + d := newDeserializer(state.Root.Data, state.Types, state.Functions, state.Regions, state.Strings) var x interface{} px := &x @@ -106,8 +107,9 @@ type deserializerContext struct { ptrs map[sID]unsafe.Pointer } -func newDeserializer(b []byte, ctypes []*coroutinev1.Type, cfuncs []*coroutinev1.Function, regions []*coroutinev1.Region) *Deserializer { - types := newTypeMap(serdes, ctypes) +func newDeserializer(b []byte, ctypes []*coroutinev1.Type, cfuncs []*coroutinev1.Function, regions []*coroutinev1.Region, cstrings []string) *Deserializer { + strings := newStringMap(cstrings) + types := newTypeMap(serdes, strings, ctypes) return &Deserializer{ &deserializerContext{ serdes: serdes, @@ -171,20 +173,23 @@ type serializerContext struct { serdes *serdemap types *typemap funcs *funcmap + strings *stringmap ptrs map[unsafe.Pointer]sID regions []*coroutinev1.Region containers containers } func newSerializer() *Serializer { - types := newTypeMap(serdes, nil) + strings := newStringMap(nil) + types := newTypeMap(serdes, strings, nil) return &Serializer{ &serializerContext{ - serdes: serdes, - types: types, - funcs: newFuncMap(types, nil), - ptrs: make(map[unsafe.Pointer]sID), + serdes: serdes, + types: types, + strings: strings, + funcs: newFuncMap(types, nil), + ptrs: make(map[unsafe.Pointer]sID), }, make([]byte, 0, 128), } diff --git a/types/string.go b/types/string.go new file mode 100644 index 0000000..cee6f52 --- /dev/null +++ b/types/string.go @@ -0,0 +1,41 @@ +package types + +import "fmt" + +type stringid = uint32 + +type stringmap struct { + strings []string + + seen map[string]stringid +} + +func newStringMap(strings []string) *stringmap { + return &stringmap{ + strings: strings, + seen: map[string]stringid{}, + } +} + +func (m *stringmap) Intern(s string) stringid { + if s == "" { + return 0 + } + if id, ok := m.seen[s]; ok { + return id + } + m.strings = append(m.strings, s) + id := stringid(len(m.strings)) // IDs start at 1 + m.seen[s] = id + return id +} + +func (m *stringmap) Lookup(id stringid) string { + if id < 0 || int(id) > len(m.strings) { + panic(fmt.Sprintf("string %d not found", id)) + } + if id == 0 { + return "" + } + return m.strings[id-1] +} diff --git a/types/types.go b/types/types.go index 3f29b31..ef87dac 100644 --- a/types/types.go +++ b/types/types.go @@ -11,16 +11,18 @@ import ( type typeid = uint32 type typemap struct { - serdes *serdemap + serdes *serdemap + strings *stringmap types []*coroutinev1.Type cache doublemap[typeid, reflect.Type] } -func newTypeMap(serdes *serdemap, types []*coroutinev1.Type) *typemap { +func newTypeMap(serdes *serdemap, strings *stringmap, types []*coroutinev1.Type) *typemap { return &typemap{ - serdes: serdes, - types: types, + serdes: serdes, + strings: strings, + types: types, } } @@ -128,8 +130,8 @@ func (m *typemap) ToReflect(id typeid) reflect.Type { case coroutinev1.Kind_KIND_STRUCT: fields := make([]reflect.StructField, len(t.Fields)) for i, f := range t.Fields { - fields[i].Name = f.Name - fields[i].PkgPath = f.Package + fields[i].Name = m.strings.Lookup(stringid(f.Name)) + fields[i].PkgPath = m.strings.Lookup(stringid(f.Package)) fields[i].Tag = reflect.StructTag(f.Tag) index := make([]int, len(f.Index)) @@ -188,8 +190,8 @@ func (m *typemap) ToType(t reflect.Type) typeid { } ti := &coroutinev1.Type{ - Name: t.Name(), - Package: t.PkgPath(), + Name: int32(m.strings.Intern(t.Name())), + Package: int32(m.strings.Intern(t.PkgPath())), } if t.Name() != "" { @@ -296,8 +298,8 @@ func (m *typemap) ToType(t reflect.Type) typeid { index[j] = int32(f.Index[j]) } ti.Fields[i] = &coroutinev1.Field{ - Name: f.Name, - Package: f.PkgPath, + Name: int32(m.strings.Intern(f.Name)), + Package: int32(m.strings.Intern(f.PkgPath)), Offset: uint64(f.Offset), Anonymous: f.Anonymous, Tag: string(f.Tag), From 24b7af82229aa884145a8ce0085ec03af654d4f0 Mon Sep 17 00:00:00 2001 From: Chris O'Hara Date: Tue, 21 Nov 2023 14:35:35 +1000 Subject: [PATCH 2/6] Don't store type information for types with custom serializers registered --- gen/proto/go/coroutine/v1/type.pb.go | 134 +++++++++---------- gen/proto/go/coroutine/v1/type_vtproto.pb.go | 10 +- proto/coroutine/v1/type.proto | 4 +- types/custom.go | 25 +++- types/inspect.go | 17 ++- types/reflect.go | 4 +- types/scan.go | 2 +- types/string.go | 2 +- types/types.go | 15 ++- 9 files changed, 121 insertions(+), 92 deletions(-) diff --git a/gen/proto/go/coroutine/v1/type.pb.go b/gen/proto/go/coroutine/v1/type.pb.go index a174bc6..736574d 100644 --- a/gen/proto/go/coroutine/v1/type.pb.go +++ b/gen/proto/go/coroutine/v1/type.pb.go @@ -234,8 +234,8 @@ type Type struct { ChanDir ChanDir `protobuf:"varint,11,opt,name=chan_dir,json=chanDir,proto3,enum=coroutine.v1.ChanDir" json:"chan_dir,omitempty"` // Variadic is true for function types with a variadic argument. Variadic bool `protobuf:"varint,12,opt,name=variadic,proto3" json:"variadic,omitempty"` - // CustomSerializer is true if the type has a custom serializer registered. - CustomSerializer bool `protobuf:"varint,13,opt,name=custom_serializer,json=customSerializer,proto3" json:"custom_serializer,omitempty"` + // Custom is true if the type is a custom opaque type. + Custom bool `protobuf:"varint,13,opt,name=custom,proto3" json:"custom,omitempty"` } func (x *Type) Reset() { @@ -354,9 +354,9 @@ func (x *Type) GetVariadic() bool { return false } -func (x *Type) GetCustomSerializer() bool { +func (x *Type) GetCustom() bool { if x != nil { - return x.CustomSerializer + return x.Custom } return false } @@ -470,7 +470,7 @@ var File_coroutine_v1_type_proto protoreflect.FileDescriptor var file_coroutine_v1_type_proto_rawDesc = []byte{ 0x0a, 0x17, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0c, 0x63, 0x6f, 0x72, 0x6f, 0x75, - 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x22, 0x99, 0x03, 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, + 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x22, 0x84, 0x03, 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x26, @@ -493,69 +493,67 @@ var file_coroutine_v1_type_proto_rawDesc = []byte{ 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x43, 0x68, 0x61, 0x6e, 0x44, 0x69, 0x72, 0x52, 0x07, 0x63, 0x68, 0x61, 0x6e, 0x44, 0x69, 0x72, 0x12, 0x1a, 0x0a, 0x08, 0x76, 0x61, 0x72, 0x69, 0x61, 0x64, 0x69, 0x63, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x76, 0x61, - 0x72, 0x69, 0x61, 0x64, 0x69, 0x63, 0x12, 0x2b, 0x0a, 0x11, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, - 0x5f, 0x73, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x72, 0x18, 0x0d, 0x20, 0x01, 0x28, - 0x08, 0x52, 0x10, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6c, 0x69, - 0x7a, 0x65, 0x72, 0x22, 0xa7, 0x01, 0x0a, 0x05, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x12, 0x0a, - 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x6e, 0x61, 0x6d, - 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, - 0x28, 0x05, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, - 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, - 0x16, 0x0a, 0x06, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, - 0x06, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6e, 0x64, 0x65, 0x78, - 0x18, 0x05, 0x20, 0x03, 0x28, 0x05, 0x52, 0x05, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x12, 0x1c, 0x0a, - 0x09, 0x61, 0x6e, 0x6f, 0x6e, 0x79, 0x6d, 0x6f, 0x75, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x09, 0x61, 0x6e, 0x6f, 0x6e, 0x79, 0x6d, 0x6f, 0x75, 0x73, 0x12, 0x10, 0x0a, 0x03, 0x74, - 0x61, 0x67, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x2a, 0xe4, 0x03, - 0x0a, 0x04, 0x4b, 0x69, 0x6e, 0x64, 0x12, 0x14, 0x0a, 0x10, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, - 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, - 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x4e, 0x49, 0x4c, 0x10, 0x01, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, - 0x4e, 0x44, 0x5f, 0x42, 0x4f, 0x4f, 0x4c, 0x10, 0x02, 0x12, 0x0c, 0x0a, 0x08, 0x4b, 0x49, 0x4e, - 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x10, 0x03, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, - 0x49, 0x4e, 0x54, 0x38, 0x10, 0x04, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, - 0x4e, 0x54, 0x31, 0x36, 0x10, 0x05, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, - 0x4e, 0x54, 0x33, 0x32, 0x10, 0x06, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, - 0x4e, 0x54, 0x36, 0x34, 0x10, 0x07, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, - 0x49, 0x4e, 0x54, 0x10, 0x08, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, - 0x4e, 0x54, 0x38, 0x10, 0x09, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, - 0x4e, 0x54, 0x31, 0x36, 0x10, 0x0a, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, - 0x49, 0x4e, 0x54, 0x33, 0x32, 0x10, 0x0b, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, - 0x55, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x10, 0x0c, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, - 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x50, 0x54, 0x52, 0x10, 0x0d, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, - 0x4e, 0x44, 0x5f, 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x33, 0x32, 0x10, 0x0e, 0x12, 0x10, 0x0a, 0x0c, - 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x36, 0x34, 0x10, 0x0f, 0x12, 0x12, - 0x0a, 0x0e, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x43, 0x4f, 0x4d, 0x50, 0x4c, 0x45, 0x58, 0x36, 0x34, - 0x10, 0x10, 0x12, 0x13, 0x0a, 0x0f, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x43, 0x4f, 0x4d, 0x50, 0x4c, - 0x45, 0x58, 0x31, 0x32, 0x38, 0x10, 0x11, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, - 0x41, 0x52, 0x52, 0x41, 0x59, 0x10, 0x12, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, - 0x43, 0x48, 0x41, 0x4e, 0x10, 0x13, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x46, - 0x55, 0x4e, 0x43, 0x10, 0x14, 0x12, 0x12, 0x0a, 0x0e, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, - 0x54, 0x45, 0x52, 0x46, 0x41, 0x43, 0x45, 0x10, 0x15, 0x12, 0x0c, 0x0a, 0x08, 0x4b, 0x49, 0x4e, - 0x44, 0x5f, 0x4d, 0x41, 0x50, 0x10, 0x16, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, - 0x50, 0x4f, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x10, 0x17, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, - 0x44, 0x5f, 0x53, 0x4c, 0x49, 0x43, 0x45, 0x10, 0x18, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, - 0x44, 0x5f, 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x10, 0x19, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, - 0x4e, 0x44, 0x5f, 0x53, 0x54, 0x52, 0x55, 0x43, 0x54, 0x10, 0x1a, 0x12, 0x17, 0x0a, 0x13, 0x4b, - 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x4e, 0x53, 0x41, 0x46, 0x45, 0x5f, 0x50, 0x4f, 0x49, 0x4e, 0x54, - 0x45, 0x52, 0x10, 0x1b, 0x2a, 0x5c, 0x0a, 0x07, 0x43, 0x68, 0x61, 0x6e, 0x44, 0x69, 0x72, 0x12, - 0x18, 0x0a, 0x14, 0x43, 0x48, 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x55, 0x4e, 0x53, 0x50, - 0x45, 0x43, 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x11, 0x0a, 0x0d, 0x43, 0x48, 0x41, - 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x52, 0x45, 0x43, 0x56, 0x10, 0x01, 0x12, 0x11, 0x0a, 0x0d, - 0x43, 0x48, 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x53, 0x45, 0x4e, 0x44, 0x10, 0x02, 0x12, - 0x11, 0x0a, 0x0d, 0x43, 0x48, 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x42, 0x4f, 0x54, 0x48, - 0x10, 0x03, 0x42, 0xb8, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, - 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x42, 0x09, 0x54, 0x79, 0x70, 0x65, 0x50, 0x72, 0x6f, - 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x48, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, - 0x2f, 0x73, 0x74, 0x65, 0x61, 0x6c, 0x74, 0x68, 0x72, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2f, 0x63, - 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x2f, 0x67, 0x6f, 0x2f, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, - 0x76, 0x31, 0x3b, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x76, 0x31, 0xa2, 0x02, - 0x03, 0x43, 0x58, 0x58, 0xaa, 0x02, 0x0c, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, - 0x2e, 0x56, 0x31, 0xca, 0x02, 0x0c, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5c, - 0x56, 0x31, 0xe2, 0x02, 0x18, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5c, 0x56, - 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x0d, - 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x06, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x72, 0x69, 0x61, 0x64, 0x69, 0x63, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, + 0x18, 0x0d, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x22, 0xa7, + 0x01, 0x0a, 0x05, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, + 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x70, + 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x66, + 0x66, 0x73, 0x65, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x6f, 0x66, 0x66, 0x73, + 0x65, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x18, 0x05, 0x20, 0x03, 0x28, + 0x05, 0x52, 0x05, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x12, 0x1c, 0x0a, 0x09, 0x61, 0x6e, 0x6f, 0x6e, + 0x79, 0x6d, 0x6f, 0x75, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x61, 0x6e, 0x6f, + 0x6e, 0x79, 0x6d, 0x6f, 0x75, 0x73, 0x12, 0x10, 0x0a, 0x03, 0x74, 0x61, 0x67, 0x18, 0x07, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x2a, 0xe4, 0x03, 0x0a, 0x04, 0x4b, 0x69, 0x6e, + 0x64, 0x12, 0x14, 0x0a, 0x10, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, + 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x4b, 0x49, 0x4e, 0x44, 0x5f, + 0x4e, 0x49, 0x4c, 0x10, 0x01, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x42, 0x4f, + 0x4f, 0x4c, 0x10, 0x02, 0x12, 0x0c, 0x0a, 0x08, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, + 0x10, 0x03, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x38, 0x10, + 0x04, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x31, 0x36, 0x10, + 0x05, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x10, + 0x06, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x10, + 0x07, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x10, 0x08, + 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x38, 0x10, 0x09, + 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x31, 0x36, 0x10, + 0x0a, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x33, 0x32, + 0x10, 0x0b, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x36, + 0x34, 0x10, 0x0c, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, + 0x50, 0x54, 0x52, 0x10, 0x0d, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x46, 0x4c, + 0x4f, 0x41, 0x54, 0x33, 0x32, 0x10, 0x0e, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, + 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x36, 0x34, 0x10, 0x0f, 0x12, 0x12, 0x0a, 0x0e, 0x4b, 0x49, 0x4e, + 0x44, 0x5f, 0x43, 0x4f, 0x4d, 0x50, 0x4c, 0x45, 0x58, 0x36, 0x34, 0x10, 0x10, 0x12, 0x13, 0x0a, + 0x0f, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x43, 0x4f, 0x4d, 0x50, 0x4c, 0x45, 0x58, 0x31, 0x32, 0x38, + 0x10, 0x11, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x41, 0x52, 0x52, 0x41, 0x59, + 0x10, 0x12, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x43, 0x48, 0x41, 0x4e, 0x10, + 0x13, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x46, 0x55, 0x4e, 0x43, 0x10, 0x14, + 0x12, 0x12, 0x0a, 0x0e, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x46, 0x41, + 0x43, 0x45, 0x10, 0x15, 0x12, 0x0c, 0x0a, 0x08, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x4d, 0x41, 0x50, + 0x10, 0x16, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x50, 0x4f, 0x49, 0x4e, 0x54, + 0x45, 0x52, 0x10, 0x17, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x53, 0x4c, 0x49, + 0x43, 0x45, 0x10, 0x18, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x53, 0x54, 0x52, + 0x49, 0x4e, 0x47, 0x10, 0x19, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x53, 0x54, + 0x52, 0x55, 0x43, 0x54, 0x10, 0x1a, 0x12, 0x17, 0x0a, 0x13, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, + 0x4e, 0x53, 0x41, 0x46, 0x45, 0x5f, 0x50, 0x4f, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x10, 0x1b, 0x2a, + 0x5c, 0x0a, 0x07, 0x43, 0x68, 0x61, 0x6e, 0x44, 0x69, 0x72, 0x12, 0x18, 0x0a, 0x14, 0x43, 0x48, + 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, + 0x45, 0x44, 0x10, 0x00, 0x12, 0x11, 0x0a, 0x0d, 0x43, 0x48, 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, + 0x5f, 0x52, 0x45, 0x43, 0x56, 0x10, 0x01, 0x12, 0x11, 0x0a, 0x0d, 0x43, 0x48, 0x41, 0x4e, 0x5f, + 0x44, 0x49, 0x52, 0x5f, 0x53, 0x45, 0x4e, 0x44, 0x10, 0x02, 0x12, 0x11, 0x0a, 0x0d, 0x43, 0x48, + 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x42, 0x4f, 0x54, 0x48, 0x10, 0x03, 0x42, 0xb8, 0x01, + 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, + 0x76, 0x31, 0x42, 0x09, 0x54, 0x79, 0x70, 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, + 0x48, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x74, 0x65, 0x61, + 0x6c, 0x74, 0x68, 0x72, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2f, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, + 0x69, 0x6e, 0x65, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x6f, + 0x2f, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x6f, + 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x43, 0x58, 0x58, 0xaa, + 0x02, 0x0c, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x56, 0x31, 0xca, 0x02, + 0x0c, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5c, 0x56, 0x31, 0xe2, 0x02, 0x18, + 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, + 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x0d, 0x43, 0x6f, 0x72, 0x6f, 0x75, + 0x74, 0x69, 0x6e, 0x65, 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/gen/proto/go/coroutine/v1/type_vtproto.pb.go b/gen/proto/go/coroutine/v1/type_vtproto.pb.go index c1241dc..4cac6b1 100644 --- a/gen/proto/go/coroutine/v1/type_vtproto.pb.go +++ b/gen/proto/go/coroutine/v1/type_vtproto.pb.go @@ -47,9 +47,9 @@ func (m *Type) MarshalToSizedBufferVT(dAtA []byte) (int, error) { i -= len(m.unknownFields) copy(dAtA[i:], m.unknownFields) } - if m.CustomSerializer { + if m.Custom { i-- - if m.CustomSerializer { + if m.Custom { dAtA[i] = 1 } else { dAtA[i] = 0 @@ -308,7 +308,7 @@ func (m *Type) SizeVT() (n int) { if m.Variadic { n += 2 } - if m.CustomSerializer { + if m.Custom { n += 2 } n += len(m.unknownFields) @@ -740,7 +740,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { m.Variadic = bool(v != 0) case 13: if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field CustomSerializer", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field Custom", wireType) } var v int for shift := uint(0); ; shift += 7 { @@ -757,7 +757,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { break } } - m.CustomSerializer = bool(v != 0) + m.Custom = bool(v != 0) default: iNdEx = preIndex skippy, err := skip(dAtA[iNdEx:]) diff --git a/proto/coroutine/v1/type.proto b/proto/coroutine/v1/type.proto index 180739d..c7042d1 100644 --- a/proto/coroutine/v1/type.proto +++ b/proto/coroutine/v1/type.proto @@ -48,8 +48,8 @@ message Type { // Variadic is true for function types with a variadic argument. bool variadic = 12; - // CustomSerializer is true if the type has a custom serializer registered. - bool custom_serializer = 13; + // Custom is true if the type is a custom opaque type. + bool custom = 13; } message Field { diff --git a/types/custom.go b/types/custom.go index c93876f..206de05 100644 --- a/types/custom.go +++ b/types/custom.go @@ -87,20 +87,24 @@ func registerSerde[T any](serdes *serdemap, type serializerFunc func(*Serializer, reflect.Type, unsafe.Pointer) type deserializerFunc func(*Deserializer, reflect.Type, unsafe.Pointer) +type serdeid = uint32 + type serde struct { + id serdeid typ reflect.Type ser serializerFunc des deserializerFunc } type serdemap struct { - serdes map[reflect.Type]serde + serdes []serde + serdesByT map[reflect.Type]serde interfaces []serde } func newSerdeMap() *serdemap { return &serdemap{ - serdes: make(map[reflect.Type]serde), + serdesByT: make(map[reflect.Type]serde), } } @@ -109,19 +113,21 @@ func (m *serdemap) attach(t reflect.Type, ser serializerFunc, des deserializerFu panic("both serializer and deserializer need to be provided") } - s := m.serdes[t] + s := m.serdesByT[t] + s.id = serdeid(len(m.serdes)) s.typ = t s.ser = ser s.des = des - m.serdes[t] = s + m.serdes = append(m.serdes, s) + m.serdesByT[t] = s if t.Kind() == reflect.Interface { m.interfaces = append(m.interfaces, s) } } -func (m *serdemap) serdeOf(x reflect.Type) (serde, bool) { - s, ok := m.serdes[x] +func (m *serdemap) serdeByType(x reflect.Type) (serde, bool) { + s, ok := m.serdesByT[x] if ok { return s, true } @@ -133,3 +139,10 @@ func (m *serdemap) serdeOf(x reflect.Type) (serde, bool) { } return serde{}, false } + +func (m *serdemap) serdeByID(id serdeid) serde { + if int(id) >= len(m.serdes) { + panic(fmt.Sprintf("serde %d not found", id)) + } + return m.serdes[id] +} diff --git a/types/inspect.go b/types/inspect.go index d4f988d..a99478a 100644 --- a/types/inspect.go +++ b/types/inspect.go @@ -283,10 +283,11 @@ func (t *Type) Variadic() bool { return t.typ.Variadic } -// CustomSerializer is true for types that had a custom serializer registered -// in the program that generated the coroutine state. -func (t *Type) CustomSerializer() bool { - return t.typ.CustomSerializer +// Custom is true for types that had a custom serializer registered +// in the program that generated the coroutine state. Custom types +// are opaque and cannot be inspected. +func (t *Type) Custom() bool { + return t.typ.Custom } // Format implements fmt.Formatter. @@ -299,6 +300,14 @@ func (t *Type) Format(s fmt.State, v rune) { name = pkg + "." + name } + if t.Custom() { + if name == "" { + name = fmt.Sprintf("", t.Kind()) + } + s.Write([]byte(name)) + return + } + verbose := s.Flag('+') || s.Flag('#') if name != "" && !verbose { s.Write([]byte(name)) diff --git a/types/reflect.go b/types/reflect.go index 83f9c89..a60be53 100644 --- a/types/reflect.go +++ b/types/reflect.go @@ -21,7 +21,7 @@ func deserializeType(d *Deserializer) reflect.Type { } func serializeAny(s *Serializer, t reflect.Type, p unsafe.Pointer) { - if serde, ok := s.serdes.serdeOf(t); ok { + if serde, ok := s.serdes.serdeByType(t); ok { offset := len(s.b) s.b = append(s.b, 0, 0, 0, 0, 0, 0, 0, 0) // store a 64-bit size placeholder serde.ser(s, t, p) @@ -97,7 +97,7 @@ func serializeAny(s *Serializer, t reflect.Type, p unsafe.Pointer) { } func deserializeAny(d *Deserializer, t reflect.Type, p unsafe.Pointer) { - if serde, ok := d.serdes.serdeOf(t); ok { + if serde, ok := d.serdes.serdeByType(t); ok { d.b = d.b[8:] // skip size prefix serde.des(d, t, p) return diff --git a/types/scan.go b/types/scan.go index 67e957a..5baab9a 100644 --- a/types/scan.go +++ b/types/scan.go @@ -249,7 +249,7 @@ func (s *Serializer) scan1(t reflect.Type, p unsafe.Pointer, seen map[reflect.Va // Don't scan types where custom serialization routines // have been registered. - if _, ok := s.serdes.serdeOf(t); ok { + if _, ok := s.serdes.serdeByType(t); ok { return } diff --git a/types/string.go b/types/string.go index cee6f52..dffb31b 100644 --- a/types/string.go +++ b/types/string.go @@ -31,7 +31,7 @@ func (m *stringmap) Intern(s string) stringid { } func (m *stringmap) Lookup(id stringid) string { - if id < 0 || int(id) > len(m.strings) { + if int(id) > len(m.strings) { panic(fmt.Sprintf("string %d not found", id)) } if id == 0 { diff --git a/types/types.go b/types/types.go index ef87dac..81640df 100644 --- a/types/types.go +++ b/types/types.go @@ -49,6 +49,11 @@ func (m *typemap) ToReflect(id typeid) reflect.Type { panic(fmt.Sprintf("type %d not found", id)) } + if t.Custom { + id := serdeid(t.MemoryOffset) + return m.serdes.serdeByID(id).typ + } + if t.MemoryOffset != 0 { return typeForOffset(namedTypeOffset(t.MemoryOffset)) } @@ -197,15 +202,19 @@ func (m *typemap) ToType(t reflect.Type) typeid { if t.Name() != "" { ti.MemoryOffset = uint64(offsetForType(t)) } - if _, ok := m.serdes.serdeOf(t); ok { - ti.CustomSerializer = true - } // Register the incomplete type now before recursing, // in case the type references itself. id := m.register(ti) m.cache.add(id, t) + // Types with custom serializers registered are opaque. + if s, ok := m.serdes.serdeByType(t); ok { + ti.Custom = true + ti.MemoryOffset = uint64(s.id) + return id + } + switch t.Kind() { case reflect.Invalid: panic("can't handle reflect.Invalid") From 18196834504bc8ac97f12d80880fa83e90f826e3 Mon Sep 17 00:00:00 2001 From: Chris O'Hara Date: Tue, 21 Nov 2023 15:27:44 +1000 Subject: [PATCH 3/6] Remove nil type; handle nil interfaces another way --- gen/proto/go/coroutine/v1/type.pb.go | 255 +++++++++++++-------------- proto/coroutine/v1/type.proto | 53 +++--- types/inspect.go | 4 - types/reflect.go | 12 +- types/types.go | 13 +- 5 files changed, 164 insertions(+), 173 deletions(-) diff --git a/gen/proto/go/coroutine/v1/type.pb.go b/gen/proto/go/coroutine/v1/type.pb.go index 736574d..394c06a 100644 --- a/gen/proto/go/coroutine/v1/type.pb.go +++ b/gen/proto/go/coroutine/v1/type.pb.go @@ -24,96 +24,93 @@ type Kind int32 const ( Kind_KIND_UNSPECIFIED Kind = 0 - Kind_KIND_NIL Kind = 1 - Kind_KIND_BOOL Kind = 2 - Kind_KIND_INT Kind = 3 - Kind_KIND_INT8 Kind = 4 - Kind_KIND_INT16 Kind = 5 - Kind_KIND_INT32 Kind = 6 - Kind_KIND_INT64 Kind = 7 - Kind_KIND_UINT Kind = 8 - Kind_KIND_UINT8 Kind = 9 - Kind_KIND_UINT16 Kind = 10 - Kind_KIND_UINT32 Kind = 11 - Kind_KIND_UINT64 Kind = 12 - Kind_KIND_UINTPTR Kind = 13 - Kind_KIND_FLOAT32 Kind = 14 - Kind_KIND_FLOAT64 Kind = 15 - Kind_KIND_COMPLEX64 Kind = 16 - Kind_KIND_COMPLEX128 Kind = 17 - Kind_KIND_ARRAY Kind = 18 - Kind_KIND_CHAN Kind = 19 - Kind_KIND_FUNC Kind = 20 - Kind_KIND_INTERFACE Kind = 21 - Kind_KIND_MAP Kind = 22 - Kind_KIND_POINTER Kind = 23 - Kind_KIND_SLICE Kind = 24 - Kind_KIND_STRING Kind = 25 - Kind_KIND_STRUCT Kind = 26 - Kind_KIND_UNSAFE_POINTER Kind = 27 + Kind_KIND_BOOL Kind = 1 + Kind_KIND_INT Kind = 2 + Kind_KIND_INT8 Kind = 3 + Kind_KIND_INT16 Kind = 4 + Kind_KIND_INT32 Kind = 5 + Kind_KIND_INT64 Kind = 6 + Kind_KIND_UINT Kind = 7 + Kind_KIND_UINT8 Kind = 8 + Kind_KIND_UINT16 Kind = 9 + Kind_KIND_UINT32 Kind = 10 + Kind_KIND_UINT64 Kind = 11 + Kind_KIND_UINTPTR Kind = 12 + Kind_KIND_FLOAT32 Kind = 13 + Kind_KIND_FLOAT64 Kind = 14 + Kind_KIND_COMPLEX64 Kind = 15 + Kind_KIND_COMPLEX128 Kind = 16 + Kind_KIND_ARRAY Kind = 17 + Kind_KIND_CHAN Kind = 18 + Kind_KIND_FUNC Kind = 19 + Kind_KIND_INTERFACE Kind = 20 + Kind_KIND_MAP Kind = 21 + Kind_KIND_POINTER Kind = 22 + Kind_KIND_SLICE Kind = 23 + Kind_KIND_STRING Kind = 24 + Kind_KIND_STRUCT Kind = 25 + Kind_KIND_UNSAFE_POINTER Kind = 26 ) // Enum value maps for Kind. var ( Kind_name = map[int32]string{ 0: "KIND_UNSPECIFIED", - 1: "KIND_NIL", - 2: "KIND_BOOL", - 3: "KIND_INT", - 4: "KIND_INT8", - 5: "KIND_INT16", - 6: "KIND_INT32", - 7: "KIND_INT64", - 8: "KIND_UINT", - 9: "KIND_UINT8", - 10: "KIND_UINT16", - 11: "KIND_UINT32", - 12: "KIND_UINT64", - 13: "KIND_UINTPTR", - 14: "KIND_FLOAT32", - 15: "KIND_FLOAT64", - 16: "KIND_COMPLEX64", - 17: "KIND_COMPLEX128", - 18: "KIND_ARRAY", - 19: "KIND_CHAN", - 20: "KIND_FUNC", - 21: "KIND_INTERFACE", - 22: "KIND_MAP", - 23: "KIND_POINTER", - 24: "KIND_SLICE", - 25: "KIND_STRING", - 26: "KIND_STRUCT", - 27: "KIND_UNSAFE_POINTER", + 1: "KIND_BOOL", + 2: "KIND_INT", + 3: "KIND_INT8", + 4: "KIND_INT16", + 5: "KIND_INT32", + 6: "KIND_INT64", + 7: "KIND_UINT", + 8: "KIND_UINT8", + 9: "KIND_UINT16", + 10: "KIND_UINT32", + 11: "KIND_UINT64", + 12: "KIND_UINTPTR", + 13: "KIND_FLOAT32", + 14: "KIND_FLOAT64", + 15: "KIND_COMPLEX64", + 16: "KIND_COMPLEX128", + 17: "KIND_ARRAY", + 18: "KIND_CHAN", + 19: "KIND_FUNC", + 20: "KIND_INTERFACE", + 21: "KIND_MAP", + 22: "KIND_POINTER", + 23: "KIND_SLICE", + 24: "KIND_STRING", + 25: "KIND_STRUCT", + 26: "KIND_UNSAFE_POINTER", } Kind_value = map[string]int32{ "KIND_UNSPECIFIED": 0, - "KIND_NIL": 1, - "KIND_BOOL": 2, - "KIND_INT": 3, - "KIND_INT8": 4, - "KIND_INT16": 5, - "KIND_INT32": 6, - "KIND_INT64": 7, - "KIND_UINT": 8, - "KIND_UINT8": 9, - "KIND_UINT16": 10, - "KIND_UINT32": 11, - "KIND_UINT64": 12, - "KIND_UINTPTR": 13, - "KIND_FLOAT32": 14, - "KIND_FLOAT64": 15, - "KIND_COMPLEX64": 16, - "KIND_COMPLEX128": 17, - "KIND_ARRAY": 18, - "KIND_CHAN": 19, - "KIND_FUNC": 20, - "KIND_INTERFACE": 21, - "KIND_MAP": 22, - "KIND_POINTER": 23, - "KIND_SLICE": 24, - "KIND_STRING": 25, - "KIND_STRUCT": 26, - "KIND_UNSAFE_POINTER": 27, + "KIND_BOOL": 1, + "KIND_INT": 2, + "KIND_INT8": 3, + "KIND_INT16": 4, + "KIND_INT32": 5, + "KIND_INT64": 6, + "KIND_UINT": 7, + "KIND_UINT8": 8, + "KIND_UINT16": 9, + "KIND_UINT32": 10, + "KIND_UINT64": 11, + "KIND_UINTPTR": 12, + "KIND_FLOAT32": 13, + "KIND_FLOAT64": 14, + "KIND_COMPLEX64": 15, + "KIND_COMPLEX128": 16, + "KIND_ARRAY": 17, + "KIND_CHAN": 18, + "KIND_FUNC": 19, + "KIND_INTERFACE": 20, + "KIND_MAP": 21, + "KIND_POINTER": 22, + "KIND_SLICE": 23, + "KIND_STRING": 24, + "KIND_STRUCT": 25, + "KIND_UNSAFE_POINTER": 26, } ) @@ -505,55 +502,55 @@ var file_coroutine_v1_type_proto_rawDesc = []byte{ 0x05, 0x52, 0x05, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x12, 0x1c, 0x0a, 0x09, 0x61, 0x6e, 0x6f, 0x6e, 0x79, 0x6d, 0x6f, 0x75, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x61, 0x6e, 0x6f, 0x6e, 0x79, 0x6d, 0x6f, 0x75, 0x73, 0x12, 0x10, 0x0a, 0x03, 0x74, 0x61, 0x67, 0x18, 0x07, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x2a, 0xe4, 0x03, 0x0a, 0x04, 0x4b, 0x69, 0x6e, + 0x01, 0x28, 0x09, 0x52, 0x03, 0x74, 0x61, 0x67, 0x2a, 0xd6, 0x03, 0x0a, 0x04, 0x4b, 0x69, 0x6e, 0x64, 0x12, 0x14, 0x0a, 0x10, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, - 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x0c, 0x0a, 0x08, 0x4b, 0x49, 0x4e, 0x44, 0x5f, - 0x4e, 0x49, 0x4c, 0x10, 0x01, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x42, 0x4f, - 0x4f, 0x4c, 0x10, 0x02, 0x12, 0x0c, 0x0a, 0x08, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, - 0x10, 0x03, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x38, 0x10, - 0x04, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x31, 0x36, 0x10, - 0x05, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x10, - 0x06, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x10, - 0x07, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x10, 0x08, - 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x38, 0x10, 0x09, - 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x31, 0x36, 0x10, - 0x0a, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x33, 0x32, - 0x10, 0x0b, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x36, - 0x34, 0x10, 0x0c, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, - 0x50, 0x54, 0x52, 0x10, 0x0d, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x46, 0x4c, - 0x4f, 0x41, 0x54, 0x33, 0x32, 0x10, 0x0e, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, - 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x36, 0x34, 0x10, 0x0f, 0x12, 0x12, 0x0a, 0x0e, 0x4b, 0x49, 0x4e, - 0x44, 0x5f, 0x43, 0x4f, 0x4d, 0x50, 0x4c, 0x45, 0x58, 0x36, 0x34, 0x10, 0x10, 0x12, 0x13, 0x0a, - 0x0f, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x43, 0x4f, 0x4d, 0x50, 0x4c, 0x45, 0x58, 0x31, 0x32, 0x38, - 0x10, 0x11, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x41, 0x52, 0x52, 0x41, 0x59, - 0x10, 0x12, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x43, 0x48, 0x41, 0x4e, 0x10, - 0x13, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x46, 0x55, 0x4e, 0x43, 0x10, 0x14, - 0x12, 0x12, 0x0a, 0x0e, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x46, 0x41, - 0x43, 0x45, 0x10, 0x15, 0x12, 0x0c, 0x0a, 0x08, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x4d, 0x41, 0x50, - 0x10, 0x16, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x50, 0x4f, 0x49, 0x4e, 0x54, - 0x45, 0x52, 0x10, 0x17, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x53, 0x4c, 0x49, - 0x43, 0x45, 0x10, 0x18, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x53, 0x54, 0x52, - 0x49, 0x4e, 0x47, 0x10, 0x19, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x53, 0x54, - 0x52, 0x55, 0x43, 0x54, 0x10, 0x1a, 0x12, 0x17, 0x0a, 0x13, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, - 0x4e, 0x53, 0x41, 0x46, 0x45, 0x5f, 0x50, 0x4f, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x10, 0x1b, 0x2a, - 0x5c, 0x0a, 0x07, 0x43, 0x68, 0x61, 0x6e, 0x44, 0x69, 0x72, 0x12, 0x18, 0x0a, 0x14, 0x43, 0x48, - 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, 0x46, 0x49, - 0x45, 0x44, 0x10, 0x00, 0x12, 0x11, 0x0a, 0x0d, 0x43, 0x48, 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, - 0x5f, 0x52, 0x45, 0x43, 0x56, 0x10, 0x01, 0x12, 0x11, 0x0a, 0x0d, 0x43, 0x48, 0x41, 0x4e, 0x5f, - 0x44, 0x49, 0x52, 0x5f, 0x53, 0x45, 0x4e, 0x44, 0x10, 0x02, 0x12, 0x11, 0x0a, 0x0d, 0x43, 0x48, - 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x42, 0x4f, 0x54, 0x48, 0x10, 0x03, 0x42, 0xb8, 0x01, - 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, - 0x76, 0x31, 0x42, 0x09, 0x54, 0x79, 0x70, 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, - 0x48, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x74, 0x65, 0x61, - 0x6c, 0x74, 0x68, 0x72, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2f, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, - 0x69, 0x6e, 0x65, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x6f, - 0x2f, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x3b, 0x63, 0x6f, - 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x43, 0x58, 0x58, 0xaa, - 0x02, 0x0c, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x56, 0x31, 0xca, 0x02, - 0x0c, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5c, 0x56, 0x31, 0xe2, 0x02, 0x18, - 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, - 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x0d, 0x43, 0x6f, 0x72, 0x6f, 0x75, - 0x74, 0x69, 0x6e, 0x65, 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x49, 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, + 0x42, 0x4f, 0x4f, 0x4c, 0x10, 0x01, 0x12, 0x0c, 0x0a, 0x08, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, + 0x4e, 0x54, 0x10, 0x02, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, + 0x38, 0x10, 0x03, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x31, + 0x36, 0x10, 0x04, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x33, + 0x32, 0x10, 0x05, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x36, + 0x34, 0x10, 0x06, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, + 0x10, 0x07, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x38, + 0x10, 0x08, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, 0x31, + 0x36, 0x10, 0x09, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, 0x54, + 0x33, 0x32, 0x10, 0x0a, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, 0x4e, + 0x54, 0x36, 0x34, 0x10, 0x0b, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x55, 0x49, + 0x4e, 0x54, 0x50, 0x54, 0x52, 0x10, 0x0c, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, + 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x33, 0x32, 0x10, 0x0d, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, + 0x44, 0x5f, 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x36, 0x34, 0x10, 0x0e, 0x12, 0x12, 0x0a, 0x0e, 0x4b, + 0x49, 0x4e, 0x44, 0x5f, 0x43, 0x4f, 0x4d, 0x50, 0x4c, 0x45, 0x58, 0x36, 0x34, 0x10, 0x0f, 0x12, + 0x13, 0x0a, 0x0f, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x43, 0x4f, 0x4d, 0x50, 0x4c, 0x45, 0x58, 0x31, + 0x32, 0x38, 0x10, 0x10, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x41, 0x52, 0x52, + 0x41, 0x59, 0x10, 0x11, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x43, 0x48, 0x41, + 0x4e, 0x10, 0x12, 0x12, 0x0d, 0x0a, 0x09, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x46, 0x55, 0x4e, 0x43, + 0x10, 0x13, 0x12, 0x12, 0x0a, 0x0e, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x49, 0x4e, 0x54, 0x45, 0x52, + 0x46, 0x41, 0x43, 0x45, 0x10, 0x14, 0x12, 0x0c, 0x0a, 0x08, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x4d, + 0x41, 0x50, 0x10, 0x15, 0x12, 0x10, 0x0a, 0x0c, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x50, 0x4f, 0x49, + 0x4e, 0x54, 0x45, 0x52, 0x10, 0x16, 0x12, 0x0e, 0x0a, 0x0a, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x53, + 0x4c, 0x49, 0x43, 0x45, 0x10, 0x17, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, 0x53, + 0x54, 0x52, 0x49, 0x4e, 0x47, 0x10, 0x18, 0x12, 0x0f, 0x0a, 0x0b, 0x4b, 0x49, 0x4e, 0x44, 0x5f, + 0x53, 0x54, 0x52, 0x55, 0x43, 0x54, 0x10, 0x19, 0x12, 0x17, 0x0a, 0x13, 0x4b, 0x49, 0x4e, 0x44, + 0x5f, 0x55, 0x4e, 0x53, 0x41, 0x46, 0x45, 0x5f, 0x50, 0x4f, 0x49, 0x4e, 0x54, 0x45, 0x52, 0x10, + 0x1a, 0x2a, 0x5c, 0x0a, 0x07, 0x43, 0x68, 0x61, 0x6e, 0x44, 0x69, 0x72, 0x12, 0x18, 0x0a, 0x14, + 0x43, 0x48, 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x55, 0x4e, 0x53, 0x50, 0x45, 0x43, 0x49, + 0x46, 0x49, 0x45, 0x44, 0x10, 0x00, 0x12, 0x11, 0x0a, 0x0d, 0x43, 0x48, 0x41, 0x4e, 0x5f, 0x44, + 0x49, 0x52, 0x5f, 0x52, 0x45, 0x43, 0x56, 0x10, 0x01, 0x12, 0x11, 0x0a, 0x0d, 0x43, 0x48, 0x41, + 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x53, 0x45, 0x4e, 0x44, 0x10, 0x02, 0x12, 0x11, 0x0a, 0x0d, + 0x43, 0x48, 0x41, 0x4e, 0x5f, 0x44, 0x49, 0x52, 0x5f, 0x42, 0x4f, 0x54, 0x48, 0x10, 0x03, 0x42, + 0xb8, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, + 0x65, 0x2e, 0x76, 0x31, 0x42, 0x09, 0x54, 0x79, 0x70, 0x65, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, + 0x01, 0x5a, 0x48, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73, 0x74, + 0x65, 0x61, 0x6c, 0x74, 0x68, 0x72, 0x6f, 0x63, 0x6b, 0x65, 0x74, 0x2f, 0x63, 0x6f, 0x72, 0x6f, + 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, + 0x67, 0x6f, 0x2f, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x3b, + 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x43, 0x58, + 0x58, 0xaa, 0x02, 0x0c, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x56, 0x31, + 0xca, 0x02, 0x0c, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5c, 0x56, 0x31, 0xe2, + 0x02, 0x18, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x5c, 0x56, 0x31, 0x5c, 0x47, + 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x0d, 0x43, 0x6f, 0x72, + 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x3a, 0x3a, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, } var ( diff --git a/proto/coroutine/v1/type.proto b/proto/coroutine/v1/type.proto index c7042d1..09d279f 100644 --- a/proto/coroutine/v1/type.proto +++ b/proto/coroutine/v1/type.proto @@ -79,33 +79,32 @@ message Field { enum Kind { KIND_UNSPECIFIED = 0; - KIND_NIL = 1; - KIND_BOOL = 2; - KIND_INT = 3; - KIND_INT8 = 4; - KIND_INT16 = 5; - KIND_INT32 = 6; - KIND_INT64 = 7; - KIND_UINT = 8; - KIND_UINT8 = 9; - KIND_UINT16 = 10; - KIND_UINT32 = 11; - KIND_UINT64 = 12; - KIND_UINTPTR = 13; - KIND_FLOAT32 = 14; - KIND_FLOAT64 = 15; - KIND_COMPLEX64 = 16; - KIND_COMPLEX128 = 17; - KIND_ARRAY = 18; - KIND_CHAN = 19; - KIND_FUNC = 20; - KIND_INTERFACE = 21; - KIND_MAP = 22; - KIND_POINTER = 23; - KIND_SLICE = 24; - KIND_STRING = 25; - KIND_STRUCT = 26; - KIND_UNSAFE_POINTER = 27; + KIND_BOOL = 1; + KIND_INT = 2; + KIND_INT8 = 3; + KIND_INT16 = 4; + KIND_INT32 = 5; + KIND_INT64 = 6; + KIND_UINT = 7; + KIND_UINT8 = 8; + KIND_UINT16 = 9; + KIND_UINT32 = 10; + KIND_UINT64 = 11; + KIND_UINTPTR = 12; + KIND_FLOAT32 = 13; + KIND_FLOAT64 = 14; + KIND_COMPLEX64 = 15; + KIND_COMPLEX128 = 16; + KIND_ARRAY = 17; + KIND_CHAN = 18; + KIND_FUNC = 19; + KIND_INTERFACE = 20; + KIND_MAP = 21; + KIND_POINTER = 22; + KIND_SLICE = 23; + KIND_STRING = 24; + KIND_STRUCT = 25; + KIND_UNSAFE_POINTER = 26; } enum ChanDir { diff --git a/types/inspect.go b/types/inspect.go index a99478a..66b5323 100644 --- a/types/inspect.go +++ b/types/inspect.go @@ -135,8 +135,6 @@ func (t *Type) Package() string { // Kind is the underlying kind for this type. func (t *Type) Kind() reflect.Kind { switch t.typ.Kind { - case coroutinev1.Kind_KIND_NIL: - return reflect.Kind(0) case coroutinev1.Kind_KIND_BOOL: return reflect.Bool case coroutinev1.Kind_KIND_INT: @@ -316,8 +314,6 @@ func (t *Type) Format(s fmt.State, v rune) { var primitiveKind string switch t.typ.Kind { - case coroutinev1.Kind_KIND_NIL: - primitiveKind = "nil" case coroutinev1.Kind_KIND_BOOL: primitiveKind = "bool" case coroutinev1.Kind_KIND_INT: diff --git a/types/reflect.go b/types/reflect.go index a60be53..74db7e5 100644 --- a/types/reflect.go +++ b/types/reflect.go @@ -690,9 +690,10 @@ func serializeInterface(s *Serializer, t reflect.Type, p unsafe.Pointer) { i := (*iface)(p) if i.typ == nil { - serializeType(s, nil) + serializeBool(s, false) return } + serializeBool(s, true) et := reflect.TypeOf(reflect.NewAt(t, p).Elem().Interface()) serializeType(s, et) @@ -708,12 +709,15 @@ func serializeInterface(s *Serializer, t reflect.Type, p unsafe.Pointer) { } func deserializeInterface(d *Deserializer, t reflect.Type, p unsafe.Pointer) { - // Deserialize the type - et := deserializeType(d) - if et == nil { + var ok bool + deserializeBool(d, &ok) + if !ok { return } + // Deserialize the type + et := deserializeType(d) + // Deserialize the pointer ep := deserializePointedAt(d, et) diff --git a/types/types.go b/types/types.go index 81640df..8cb76ce 100644 --- a/types/types.go +++ b/types/types.go @@ -60,9 +60,6 @@ func (m *typemap) ToReflect(id typeid) reflect.Type { var x reflect.Type switch t.Kind { - case coroutinev1.Kind_KIND_NIL: - x = nil - case coroutinev1.Kind_KIND_BOOL: x = reflect.TypeOf(false) @@ -184,14 +181,12 @@ func (m *typemap) ToReflect(id typeid) reflect.Type { } func (m *typemap) ToType(t reflect.Type) typeid { - if x, ok := m.cache.getV(t); ok { - return x + if t == nil { + panic("nil reflect.Type") } - if t == nil { - id := m.register(&coroutinev1.Type{Kind: coroutinev1.Kind_KIND_NIL}) - m.cache.add(id, t) - return id + if x, ok := m.cache.getV(t); ok { + return x } ti := &coroutinev1.Type{ From f297046ca3e47596f92993814e34c609f4150c18 Mon Sep 17 00:00:00 2001 From: Chris O'Hara Date: Tue, 21 Nov 2023 15:38:45 +1000 Subject: [PATCH 4/6] Use unsigned types to represent IDs In all cases, IDs start at one; zero is used as the nil sentinel, so there's no need for signed ints. --- gen/proto/go/coroutine/v1/coroutine.pb.go | 6 +-- .../go/coroutine/v1/coroutine_vtproto.pb.go | 2 +- gen/proto/go/coroutine/v1/function.pb.go | 12 ++--- .../go/coroutine/v1/function_vtproto.pb.go | 4 +- gen/proto/go/coroutine/v1/type.pb.go | 54 +++++++++---------- gen/proto/go/coroutine/v1/type_vtproto.pb.go | 40 +++++++------- proto/coroutine/v1/coroutine.proto | 2 +- proto/coroutine/v1/function.proto | 4 +- proto/coroutine/v1/type.proto | 18 +++---- types/reflect.go | 4 +- types/serde.go | 2 +- types/types.go | 34 ++++++------ 12 files changed, 90 insertions(+), 92 deletions(-) diff --git a/gen/proto/go/coroutine/v1/coroutine.pb.go b/gen/proto/go/coroutine/v1/coroutine.pb.go index 997a47a..223f467 100644 --- a/gen/proto/go/coroutine/v1/coroutine.pb.go +++ b/gen/proto/go/coroutine/v1/coroutine.pb.go @@ -198,7 +198,7 @@ type Region struct { unknownFields protoimpl.UnknownFields // Type is the type of the region. - Type int32 `protobuf:"varint,1,opt,name=type,proto3" json:"type,omitempty"` + Type uint32 `protobuf:"varint,1,opt,name=type,proto3" json:"type,omitempty"` // Data is the encoded contents of the memory region. Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` } @@ -235,7 +235,7 @@ func (*Region) Descriptor() ([]byte, []int) { return file_coroutine_v1_coroutine_proto_rawDescGZIP(), []int{2} } -func (x *Region) GetType() int32 { +func (x *Region) GetType() uint32 { if x != nil { return x.Type } @@ -281,7 +281,7 @@ var file_coroutine_v1_coroutine_proto_rawDesc = []byte{ 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x6f, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x61, 0x72, 0x63, 0x68, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x61, 0x72, 0x63, 0x68, 0x22, 0x30, 0x0a, 0x06, 0x52, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x12, 0x0a, 0x04, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x42, 0xbd, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x42, 0x0e, 0x43, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, diff --git a/gen/proto/go/coroutine/v1/coroutine_vtproto.pb.go b/gen/proto/go/coroutine/v1/coroutine_vtproto.pb.go index a5a96e0..cac79dd 100644 --- a/gen/proto/go/coroutine/v1/coroutine_vtproto.pb.go +++ b/gen/proto/go/coroutine/v1/coroutine_vtproto.pb.go @@ -787,7 +787,7 @@ func (m *Region) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.Type |= int32(b&0x7F) << shift + m.Type |= uint32(b&0x7F) << shift if b < 0x80 { break } diff --git a/gen/proto/go/coroutine/v1/function.pb.go b/gen/proto/go/coroutine/v1/function.pb.go index a070939..26c29bb 100644 --- a/gen/proto/go/coroutine/v1/function.pb.go +++ b/gen/proto/go/coroutine/v1/function.pb.go @@ -29,10 +29,10 @@ type Function struct { // Name is the name of the function. Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` // Type is an identifier for the function's type. - Type int32 `protobuf:"varint,2,opt,name=type,proto3" json:"type,omitempty"` + Type uint32 `protobuf:"varint,2,opt,name=type,proto3" json:"type,omitempty"` // Closure is an identifier for a struct type that represents the // memory layout of the closure. - Closure int32 `protobuf:"varint,3,opt,name=closure,proto3" json:"closure,omitempty"` + Closure uint32 `protobuf:"varint,3,opt,name=closure,proto3" json:"closure,omitempty"` } func (x *Function) Reset() { @@ -74,14 +74,14 @@ func (x *Function) GetName() string { return "" } -func (x *Function) GetType() int32 { +func (x *Function) GetType() uint32 { if x != nil { return x.Type } return 0 } -func (x *Function) GetClosure() int32 { +func (x *Function) GetClosure() uint32 { if x != nil { return x.Closure } @@ -96,8 +96,8 @@ var file_coroutine_v1_function_proto_rawDesc = []byte{ 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x22, 0x4c, 0x0a, 0x08, 0x46, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, - 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, - 0x18, 0x0a, 0x07, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, + 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, + 0x18, 0x0a, 0x07, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65, 0x42, 0xbc, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x42, 0x0d, 0x46, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, diff --git a/gen/proto/go/coroutine/v1/function_vtproto.pb.go b/gen/proto/go/coroutine/v1/function_vtproto.pb.go index 74346ae..ee0133e 100644 --- a/gen/proto/go/coroutine/v1/function_vtproto.pb.go +++ b/gen/proto/go/coroutine/v1/function_vtproto.pb.go @@ -180,7 +180,7 @@ func (m *Function) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.Type |= int32(b&0x7F) << shift + m.Type |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -199,7 +199,7 @@ func (m *Function) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.Closure |= int32(b&0x7F) << shift + m.Closure |= uint32(b&0x7F) << shift if b < 0x80 { break } diff --git a/gen/proto/go/coroutine/v1/type.pb.go b/gen/proto/go/coroutine/v1/type.pb.go index 394c06a..c6f1b72 100644 --- a/gen/proto/go/coroutine/v1/type.pb.go +++ b/gen/proto/go/coroutine/v1/type.pb.go @@ -207,21 +207,21 @@ type Type struct { // Name is the string ID of the name of the type within the package it was // defined. - Name int32 `protobuf:"varint,1,opt,name=name,proto3" json:"name,omitempty"` + Name uint32 `protobuf:"varint,1,opt,name=name,proto3" json:"name,omitempty"` // Package is the string ID the name of the package that defines the type. - Package int32 `protobuf:"varint,2,opt,name=package,proto3" json:"package,omitempty"` + Package uint32 `protobuf:"varint,2,opt,name=package,proto3" json:"package,omitempty"` // Kind is the underlying type. Kind Kind `protobuf:"varint,3,opt,name=kind,proto3,enum=coroutine.v1.Kind" json:"kind,omitempty"` // Elem is the type of an array, slice, pointer, chan, or map's element. - Elem int32 `protobuf:"varint,4,opt,name=elem,proto3" json:"elem,omitempty"` + Elem uint32 `protobuf:"varint,4,opt,name=elem,proto3" json:"elem,omitempty"` // Key is the key type for map types. - Key int32 `protobuf:"varint,5,opt,name=key,proto3" json:"key,omitempty"` + Key uint32 `protobuf:"varint,5,opt,name=key,proto3" json:"key,omitempty"` // Fields is the set of fields defined in a struct type. Fields []*Field `protobuf:"bytes,6,rep,name=fields,proto3" json:"fields,omitempty"` // Params are the types of params for a function type. - Params []int32 `protobuf:"varint,7,rep,packed,name=params,proto3" json:"params,omitempty"` + Params []uint32 `protobuf:"varint,7,rep,packed,name=params,proto3" json:"params,omitempty"` // Results are the types of results for a function type. - Results []int32 `protobuf:"varint,8,rep,packed,name=results,proto3" json:"results,omitempty"` + Results []uint32 `protobuf:"varint,8,rep,packed,name=results,proto3" json:"results,omitempty"` // Length is the length of an array type. Length int64 `protobuf:"varint,9,opt,name=length,proto3" json:"length,omitempty"` // MemoryOffset is an optional field that encodes the type's location @@ -267,14 +267,14 @@ func (*Type) Descriptor() ([]byte, []int) { return file_coroutine_v1_type_proto_rawDescGZIP(), []int{0} } -func (x *Type) GetName() int32 { +func (x *Type) GetName() uint32 { if x != nil { return x.Name } return 0 } -func (x *Type) GetPackage() int32 { +func (x *Type) GetPackage() uint32 { if x != nil { return x.Package } @@ -288,14 +288,14 @@ func (x *Type) GetKind() Kind { return Kind_KIND_UNSPECIFIED } -func (x *Type) GetElem() int32 { +func (x *Type) GetElem() uint32 { if x != nil { return x.Elem } return 0 } -func (x *Type) GetKey() int32 { +func (x *Type) GetKey() uint32 { if x != nil { return x.Key } @@ -309,14 +309,14 @@ func (x *Type) GetFields() []*Field { return nil } -func (x *Type) GetParams() []int32 { +func (x *Type) GetParams() []uint32 { if x != nil { return x.Params } return nil } -func (x *Type) GetResults() []int32 { +func (x *Type) GetResults() []uint32 { if x != nil { return x.Results } @@ -364,12 +364,12 @@ type Field struct { unknownFields protoimpl.UnknownFields // Name is the string ID of the name of the field. - Name int32 `protobuf:"varint,1,opt,name=name,proto3" json:"name,omitempty"` + Name uint32 `protobuf:"varint,1,opt,name=name,proto3" json:"name,omitempty"` // Package is the string ID of package path that qualifies a lower case // (unexported) field name. It is empty for exported field names. - Package int32 `protobuf:"varint,2,opt,name=package,proto3" json:"package,omitempty"` + Package uint32 `protobuf:"varint,2,opt,name=package,proto3" json:"package,omitempty"` // Type is the type of the field. - Type int32 `protobuf:"varint,3,opt,name=type,proto3" json:"type,omitempty"` + Type uint32 `protobuf:"varint,3,opt,name=type,proto3" json:"type,omitempty"` // Offset is the offset of the field within its struct, in bytes. Offset uint64 `protobuf:"varint,4,opt,name=offset,proto3" json:"offset,omitempty"` // Index is a sequence used by (reflect.Type).FieldByIndex. @@ -413,21 +413,21 @@ func (*Field) Descriptor() ([]byte, []int) { return file_coroutine_v1_type_proto_rawDescGZIP(), []int{1} } -func (x *Field) GetName() int32 { +func (x *Field) GetName() uint32 { if x != nil { return x.Name } return 0 } -func (x *Field) GetPackage() int32 { +func (x *Field) GetPackage() uint32 { if x != nil { return x.Package } return 0 } -func (x *Field) GetType() int32 { +func (x *Field) GetType() uint32 { if x != nil { return x.Type } @@ -468,20 +468,20 @@ var file_coroutine_v1_type_proto_rawDesc = []byte{ 0x0a, 0x17, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2f, 0x76, 0x31, 0x2f, 0x74, 0x79, 0x70, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0c, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x22, 0x84, 0x03, 0x0a, 0x04, 0x54, 0x79, 0x70, 0x65, - 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, + 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x26, + 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x26, 0x0a, 0x04, 0x6b, 0x69, 0x6e, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x12, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x4b, 0x69, 0x6e, 0x64, 0x52, 0x04, 0x6b, 0x69, 0x6e, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x65, 0x6c, 0x65, 0x6d, 0x18, 0x04, - 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x65, 0x6c, 0x65, 0x6d, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, - 0x79, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x2b, 0x0a, 0x06, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x65, 0x6c, 0x65, 0x6d, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, + 0x79, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x2b, 0x0a, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x18, 0x06, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x70, 0x61, 0x72, - 0x61, 0x6d, 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x05, 0x52, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, + 0x61, 0x6d, 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x0d, 0x52, 0x06, 0x70, 0x61, 0x72, 0x61, 0x6d, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x18, 0x08, 0x20, 0x03, - 0x28, 0x05, 0x52, 0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x6c, + 0x28, 0x0d, 0x52, 0x07, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x18, 0x09, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x12, 0x23, 0x0a, 0x0d, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x5f, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0c, 0x6d, 0x65, 0x6d, 0x6f, @@ -493,10 +493,10 @@ var file_coroutine_v1_type_proto_rawDesc = []byte{ 0x72, 0x69, 0x61, 0x64, 0x69, 0x63, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x22, 0xa7, 0x01, 0x0a, 0x05, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, - 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x70, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, + 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x70, 0x61, 0x63, 0x6b, 0x61, 0x67, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, - 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x66, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x04, 0x52, 0x06, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x18, 0x05, 0x20, 0x03, 0x28, 0x05, 0x52, 0x05, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x12, 0x1c, 0x0a, 0x09, 0x61, 0x6e, 0x6f, 0x6e, diff --git a/gen/proto/go/coroutine/v1/type_vtproto.pb.go b/gen/proto/go/coroutine/v1/type_vtproto.pb.go index 4cac6b1..af3d6a9 100644 --- a/gen/proto/go/coroutine/v1/type_vtproto.pb.go +++ b/gen/proto/go/coroutine/v1/type_vtproto.pb.go @@ -89,8 +89,7 @@ func (m *Type) MarshalToSizedBufferVT(dAtA []byte) (int, error) { } i -= pksize2 j1 := i - for _, num1 := range m.Results { - num := uint64(num1) + for _, num := range m.Results { for num >= 1<<7 { dAtA[j1] = uint8(uint64(num)&0x7f | 0x80) num >>= 7 @@ -110,8 +109,7 @@ func (m *Type) MarshalToSizedBufferVT(dAtA []byte) (int, error) { } i -= pksize4 j3 := i - for _, num1 := range m.Params { - num := uint64(num1) + for _, num := range m.Params { for num >= 1<<7 { dAtA[j3] = uint8(uint64(num)&0x7f | 0x80) num >>= 7 @@ -394,7 +392,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.Name |= int32(b&0x7F) << shift + m.Name |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -413,7 +411,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.Package |= int32(b&0x7F) << shift + m.Package |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -451,7 +449,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.Elem |= int32(b&0x7F) << shift + m.Elem |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -470,7 +468,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.Key |= int32(b&0x7F) << shift + m.Key |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -511,7 +509,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { iNdEx = postIndex case 7: if wireType == 0 { - var v int32 + var v uint32 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflow @@ -521,7 +519,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - v |= int32(b&0x7F) << shift + v |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -562,10 +560,10 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } elementCount = count if elementCount != 0 && len(m.Params) == 0 { - m.Params = make([]int32, 0, elementCount) + m.Params = make([]uint32, 0, elementCount) } for iNdEx < postIndex { - var v int32 + var v uint32 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflow @@ -575,7 +573,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - v |= int32(b&0x7F) << shift + v |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -587,7 +585,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } case 8: if wireType == 0 { - var v int32 + var v uint32 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflow @@ -597,7 +595,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - v |= int32(b&0x7F) << shift + v |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -638,10 +636,10 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } elementCount = count if elementCount != 0 && len(m.Results) == 0 { - m.Results = make([]int32, 0, elementCount) + m.Results = make([]uint32, 0, elementCount) } for iNdEx < postIndex { - var v int32 + var v uint32 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflow @@ -651,7 +649,7 @@ func (m *Type) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - v |= int32(b&0x7F) << shift + v |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -823,7 +821,7 @@ func (m *Field) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.Name |= int32(b&0x7F) << shift + m.Name |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -842,7 +840,7 @@ func (m *Field) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.Package |= int32(b&0x7F) << shift + m.Package |= uint32(b&0x7F) << shift if b < 0x80 { break } @@ -861,7 +859,7 @@ func (m *Field) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.Type |= int32(b&0x7F) << shift + m.Type |= uint32(b&0x7F) << shift if b < 0x80 { break } diff --git a/proto/coroutine/v1/coroutine.proto b/proto/coroutine/v1/coroutine.proto index a24903a..3205334 100644 --- a/proto/coroutine/v1/coroutine.proto +++ b/proto/coroutine/v1/coroutine.proto @@ -43,7 +43,7 @@ message Build { // Region is an encoded region of memory. message Region { // Type is the type of the region. - int32 type = 1; + uint32 type = 1; // Data is the encoded contents of the memory region. bytes data = 2; diff --git a/proto/coroutine/v1/function.proto b/proto/coroutine/v1/function.proto index 4b6aba7..ea935b1 100644 --- a/proto/coroutine/v1/function.proto +++ b/proto/coroutine/v1/function.proto @@ -8,9 +8,9 @@ message Function { string name = 1; // Type is an identifier for the function's type. - int32 type = 2; + uint32 type = 2; // Closure is an identifier for a struct type that represents the // memory layout of the closure. - int32 closure = 3; + uint32 closure = 3; } diff --git a/proto/coroutine/v1/type.proto b/proto/coroutine/v1/type.proto index 09d279f..4da2076 100644 --- a/proto/coroutine/v1/type.proto +++ b/proto/coroutine/v1/type.proto @@ -12,28 +12,28 @@ package coroutine.v1; message Type { // Name is the string ID of the name of the type within the package it was // defined. - int32 name = 1; + uint32 name = 1; // Package is the string ID the name of the package that defines the type. - int32 package = 2; + uint32 package = 2; // Kind is the underlying type. Kind kind = 3; // Elem is the type of an array, slice, pointer, chan, or map's element. - int32 elem = 4; + uint32 elem = 4; // Key is the key type for map types. - int32 key = 5; + uint32 key = 5; // Fields is the set of fields defined in a struct type. repeated Field fields = 6; // Params are the types of params for a function type. - repeated int32 params = 7; + repeated uint32 params = 7; // Results are the types of results for a function type. - repeated int32 results = 8; + repeated uint32 results = 8; // Length is the length of an array type. int64 length = 9; @@ -54,14 +54,14 @@ message Type { message Field { // Name is the string ID of the name of the field. - int32 name = 1; + uint32 name = 1; // Package is the string ID of package path that qualifies a lower case // (unexported) field name. It is empty for exported field names. - int32 package = 2; + uint32 package = 2; // Type is the type of the field. - int32 type = 3; + uint32 type = 3; // Offset is the offset of the field within its struct, in bytes. uint64 offset = 4; diff --git a/types/reflect.go b/types/reflect.go index 74db7e5..0956d10 100644 --- a/types/reflect.go +++ b/types/reflect.go @@ -391,7 +391,7 @@ func serializePointedAt(s *Serializer, t reflect.Type, p unsafe.Pointer) { } region := &coroutinev1.Region{ - Type: int32(s.types.ToType(r.typ)), + Type: s.types.ToType(r.typ), } s.regions = append(s.regions, region) @@ -464,7 +464,7 @@ func serializeMapReflect(s *Serializer, t reflect.Type, r reflect.Value) { size := r.Len() region := &coroutinev1.Region{ - Type: int32(s.types.ToType(t)), + Type: s.types.ToType(t), } s.regions = append(s.regions, region) diff --git a/types/serde.go b/types/serde.go index c7ee8d3..440b122 100644 --- a/types/serde.go +++ b/types/serde.go @@ -61,7 +61,7 @@ func Serialize(x any) ([]byte, error) { Strings: s.strings.strings, Regions: s.regions, Root: &coroutinev1.Region{ - Type: int32(rootTypeID), + Type: rootTypeID, Data: s.b, }, } diff --git a/types/types.go b/types/types.go index 8cb76ce..49d83bf 100644 --- a/types/types.go +++ b/types/types.go @@ -190,8 +190,8 @@ func (m *typemap) ToType(t reflect.Type) typeid { } ti := &coroutinev1.Type{ - Name: int32(m.strings.Intern(t.Name())), - Package: int32(m.strings.Intern(t.PkgPath())), + Name: m.strings.Intern(t.Name()), + Package: m.strings.Intern(t.PkgPath()), } if t.Name() != "" { @@ -271,23 +271,23 @@ func (m *typemap) ToType(t reflect.Type) typeid { case reflect.Array: ti.Kind = coroutinev1.Kind_KIND_ARRAY ti.Length = int64(t.Len()) - ti.Elem = int32(m.ToType(t.Elem())) + ti.Elem = m.ToType(t.Elem()) case reflect.Map: ti.Kind = coroutinev1.Kind_KIND_MAP - ti.Key = int32(m.ToType(t.Key())) - ti.Elem = int32(m.ToType(t.Elem())) + ti.Key = m.ToType(t.Key()) + ti.Elem = m.ToType(t.Elem()) case reflect.Pointer: ti.Kind = coroutinev1.Kind_KIND_POINTER - ti.Elem = int32(m.ToType(t.Elem())) + ti.Elem = m.ToType(t.Elem()) case reflect.UnsafePointer: ti.Kind = coroutinev1.Kind_KIND_UNSAFE_POINTER case reflect.Slice: ti.Kind = coroutinev1.Kind_KIND_SLICE - ti.Elem = int32(m.ToType(t.Elem())) + ti.Elem = m.ToType(t.Elem()) case reflect.Struct: ti.Kind = coroutinev1.Kind_KIND_STRUCT @@ -302,31 +302,31 @@ func (m *typemap) ToType(t reflect.Type) typeid { index[j] = int32(f.Index[j]) } ti.Fields[i] = &coroutinev1.Field{ - Name: int32(m.strings.Intern(f.Name)), - Package: int32(m.strings.Intern(f.PkgPath)), + Name: m.strings.Intern(f.Name), + Package: m.strings.Intern(f.PkgPath), Offset: uint64(f.Offset), Anonymous: f.Anonymous, Tag: string(f.Tag), - Type: int32(m.ToType(f.Type)), + Type: m.ToType(f.Type), Index: index, } } case reflect.Func: ti.Kind = coroutinev1.Kind_KIND_FUNC - ti.Params = make([]int32, t.NumIn()) + ti.Params = make([]uint32, t.NumIn()) for i := range ti.Params { - ti.Params[i] = int32(m.ToType(t.In(i))) + ti.Params[i] = m.ToType(t.In(i)) } - ti.Results = make([]int32, t.NumOut()) + ti.Results = make([]uint32, t.NumOut()) for i := range ti.Results { - ti.Results[i] = int32(m.ToType(t.Out(i))) + ti.Results[i] = m.ToType(t.Out(i)) } ti.Variadic = t.IsVariadic() case reflect.Chan: ti.Kind = coroutinev1.Kind_KIND_CHAN - ti.Elem = int32(m.ToType(t.Elem())) + ti.Elem = m.ToType(t.Elem()) switch t.ChanDir() { case reflect.RecvDir: ti.ChanDir = coroutinev1.ChanDir_CHAN_DIR_RECV @@ -399,8 +399,8 @@ func (m *funcmap) RegisterAddr(addr unsafe.Pointer) (id funcid, closureType refl id = m.register(&coroutinev1.Function{ Name: f.Name, - Type: int32(m.types.ToType(f.Type)), - Closure: int32(closureTypeID), + Type: m.types.ToType(f.Type), + Closure: closureTypeID, }) return id, f.Closure From 64a9f3826b009e0f0dedcd9b71a0ed145eb95fab Mon Sep 17 00:00:00 2001 From: Chris O'Hara Date: Tue, 21 Nov 2023 15:41:57 +1000 Subject: [PATCH 5/6] Intern function names Although these are likely unique, interning them with all other strings means that get more accurate stats when looking at the size of different parts of the durable coroutine state. --- gen/proto/go/coroutine/v1/function.pb.go | 10 +++--- .../go/coroutine/v1/function_vtproto.pb.go | 32 +++++-------------- gen/proto/go/coroutine/v1/type.pb.go | 2 +- proto/coroutine/v1/function.proto | 4 +-- proto/coroutine/v1/type.proto | 2 +- types/inspect.go | 5 ++- types/serde.go | 4 +-- types/types.go | 17 ++++++---- 8 files changed, 33 insertions(+), 43 deletions(-) diff --git a/gen/proto/go/coroutine/v1/function.pb.go b/gen/proto/go/coroutine/v1/function.pb.go index 26c29bb..8a2deb4 100644 --- a/gen/proto/go/coroutine/v1/function.pb.go +++ b/gen/proto/go/coroutine/v1/function.pb.go @@ -26,8 +26,8 @@ type Function struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - // Name is the name of the function. - Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + // Name is an identifier for the name of the function. + Name uint32 `protobuf:"varint,1,opt,name=name,proto3" json:"name,omitempty"` // Type is an identifier for the function's type. Type uint32 `protobuf:"varint,2,opt,name=type,proto3" json:"type,omitempty"` // Closure is an identifier for a struct type that represents the @@ -67,11 +67,11 @@ func (*Function) Descriptor() ([]byte, []int) { return file_coroutine_v1_function_proto_rawDescGZIP(), []int{0} } -func (x *Function) GetName() string { +func (x *Function) GetName() uint32 { if x != nil { return x.Name } - return "" + return 0 } func (x *Function) GetType() uint32 { @@ -95,7 +95,7 @@ var file_coroutine_v1_function_proto_rawDesc = []byte{ 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0c, 0x63, 0x6f, 0x72, 0x6f, 0x75, 0x74, 0x69, 0x6e, 0x65, 0x2e, 0x76, 0x31, 0x22, 0x4c, 0x0a, 0x08, 0x46, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, - 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, + 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x07, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65, 0x42, 0xbc, 0x01, 0x0a, 0x10, 0x63, 0x6f, diff --git a/gen/proto/go/coroutine/v1/function_vtproto.pb.go b/gen/proto/go/coroutine/v1/function_vtproto.pb.go index ee0133e..7c00364 100644 --- a/gen/proto/go/coroutine/v1/function_vtproto.pb.go +++ b/gen/proto/go/coroutine/v1/function_vtproto.pb.go @@ -58,12 +58,10 @@ func (m *Function) MarshalToSizedBufferVT(dAtA []byte) (int, error) { i-- dAtA[i] = 0x10 } - if len(m.Name) > 0 { - i -= len(m.Name) - copy(dAtA[i:], m.Name) - i = encodeVarint(dAtA, i, uint64(len(m.Name))) + if m.Name != 0 { + i = encodeVarint(dAtA, i, uint64(m.Name)) i-- - dAtA[i] = 0xa + dAtA[i] = 0x8 } return len(dAtA) - i, nil } @@ -85,9 +83,8 @@ func (m *Function) SizeVT() (n int) { } var l int _ = l - l = len(m.Name) - if l > 0 { - n += 1 + l + sov(uint64(l)) + if m.Name != 0 { + n += 1 + sov(uint64(m.Name)) } if m.Type != 0 { n += 1 + sov(uint64(m.Type)) @@ -135,10 +132,10 @@ func (m *Function) UnmarshalVT(dAtA []byte) error { } switch fieldNum { case 1: - if wireType != 2 { + if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Name", wireType) } - var stringLen uint64 + m.Name = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflow @@ -148,24 +145,11 @@ func (m *Function) UnmarshalVT(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + m.Name |= uint32(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLength - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLength - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - m.Name = string(dAtA[iNdEx:postIndex]) - iNdEx = postIndex case 2: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field Type", wireType) diff --git a/gen/proto/go/coroutine/v1/type.pb.go b/gen/proto/go/coroutine/v1/type.pb.go index c6f1b72..5768fe4 100644 --- a/gen/proto/go/coroutine/v1/type.pb.go +++ b/gen/proto/go/coroutine/v1/type.pb.go @@ -208,7 +208,7 @@ type Type struct { // Name is the string ID of the name of the type within the package it was // defined. Name uint32 `protobuf:"varint,1,opt,name=name,proto3" json:"name,omitempty"` - // Package is the string ID the name of the package that defines the type. + // Package is the string ID of the name of the package that defines the type. Package uint32 `protobuf:"varint,2,opt,name=package,proto3" json:"package,omitempty"` // Kind is the underlying type. Kind Kind `protobuf:"varint,3,opt,name=kind,proto3,enum=coroutine.v1.Kind" json:"kind,omitempty"` diff --git a/proto/coroutine/v1/function.proto b/proto/coroutine/v1/function.proto index ea935b1..24ca419 100644 --- a/proto/coroutine/v1/function.proto +++ b/proto/coroutine/v1/function.proto @@ -4,8 +4,8 @@ package coroutine.v1; // Function represents a function, method or closure in the program. message Function { - // Name is the name of the function. - string name = 1; + // Name is an identifier for the name of the function. + uint32 name = 1; // Type is an identifier for the function's type. uint32 type = 2; diff --git a/proto/coroutine/v1/type.proto b/proto/coroutine/v1/type.proto index 4da2076..23e2ae0 100644 --- a/proto/coroutine/v1/type.proto +++ b/proto/coroutine/v1/type.proto @@ -14,7 +14,7 @@ message Type { // defined. uint32 name = 1; - // Package is the string ID the name of the package that defines the type. + // Package is the string ID of the name of the package that defines the type. uint32 package = 2; // Kind is the underlying type. diff --git a/types/inspect.go b/types/inspect.go index 66b5323..f31ad1f 100644 --- a/types/inspect.go +++ b/types/inspect.go @@ -530,7 +530,10 @@ type Function struct { // Name is the name of the function. func (f *Function) Name() string { - return f.function.Name + if f.function.Name == 0 { + return "" + } + return f.state.String(int(f.function.Name - 1)) } // Type is the type of the function. diff --git a/types/serde.go b/types/serde.go index 440b122..f187f00 100644 --- a/types/serde.go +++ b/types/serde.go @@ -114,7 +114,7 @@ func newDeserializer(b []byte, ctypes []*coroutinev1.Type, cfuncs []*coroutinev1 &deserializerContext{ serdes: serdes, types: types, - funcs: newFuncMap(types, cfuncs), + funcs: newFuncMap(types, strings, cfuncs), regions: regions, ptrs: make(map[sID]unsafe.Pointer), }, @@ -188,7 +188,7 @@ func newSerializer() *Serializer { serdes: serdes, types: types, strings: strings, - funcs: newFuncMap(types, nil), + funcs: newFuncMap(types, strings, nil), ptrs: make(map[unsafe.Pointer]sID), }, make([]byte, 0, 128), diff --git a/types/types.go b/types/types.go index 49d83bf..081b4ae 100644 --- a/types/types.go +++ b/types/types.go @@ -345,16 +345,18 @@ func (m *typemap) ToType(t reflect.Type) typeid { type funcid = uint32 type funcmap struct { - types *typemap + types *typemap + strings *stringmap funcs []*coroutinev1.Function cache doublemap[typeid, *Func] } -func newFuncMap(types *typemap, funcs []*coroutinev1.Function) *funcmap { +func newFuncMap(types *typemap, strings *stringmap, funcs []*coroutinev1.Function) *funcmap { return &funcmap{ - types: types, - funcs: funcs, + types: types, + strings: strings, + funcs: funcs, } } @@ -379,9 +381,10 @@ func (m *funcmap) ToFunc(id funcid) *Func { if cf == nil { panic(fmt.Sprintf("function ID %d not found", id)) } - f := FuncByName(cf.Name) + name := m.strings.Lookup(cf.Name) + f := FuncByName(name) if f == nil { - panic(fmt.Sprintf("function %s not found", cf.Name)) + panic(fmt.Sprintf("function %s not found", name)) } return f } @@ -398,7 +401,7 @@ func (m *funcmap) RegisterAddr(addr unsafe.Pointer) (id funcid, closureType refl } id = m.register(&coroutinev1.Function{ - Name: f.Name, + Name: m.strings.Intern(f.Name), Type: m.types.ToType(f.Type), Closure: closureTypeID, }) From 8a851f5d912aab64fc0af1de32a571492a4c58d8 Mon Sep 17 00:00:00 2001 From: Chris O'Hara Date: Tue, 21 Nov 2023 15:56:31 +1000 Subject: [PATCH 6/6] Store custom serializer types once --- types/types.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/types/types.go b/types/types.go index 081b4ae..cdf59f5 100644 --- a/types/types.go +++ b/types/types.go @@ -185,6 +185,14 @@ func (m *typemap) ToType(t reflect.Type) typeid { panic("nil reflect.Type") } + // When a custom serializer has been registered for type T, + // store T only once (don't create opaque types for each + // implementation of T when T is an interface type). + custom, isCustom := m.serdes.serdeByType(t) + if isCustom { + t = custom.typ + } + if x, ok := m.cache.getV(t); ok { return x } @@ -204,9 +212,9 @@ func (m *typemap) ToType(t reflect.Type) typeid { m.cache.add(id, t) // Types with custom serializers registered are opaque. - if s, ok := m.serdes.serdeByType(t); ok { + if isCustom { ti.Custom = true - ti.MemoryOffset = uint64(s.id) + ti.MemoryOffset = uint64(custom.id) return id }