Skip to content

Commit dd98df3

Browse files
committed
Stop FieldDescriptor from holding the whole proto just for two strings.
1 parent c9bf607 commit dd98df3

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

Sources/SwiftProtobufPluginLibrary/Descriptor.swift

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,10 @@ public final class FieldDescriptor {
767767
/// The FieldOptions for this field.
768768
public var options: Google_Protobuf_FieldOptions
769769

770-
let proto: Google_Protobuf_FieldDescriptorProto
771770
let proto3Optional: Bool
771+
// These next two cache values until bind().
772+
var extendee: String?
773+
var typeName: String?
772774

773775
fileprivate init(proto: Google_Protobuf_FieldDescriptorProto,
774776
index: Int,
@@ -779,13 +781,13 @@ public final class FieldDescriptor {
779781
self.defaultValue = proto.hasDefaultValue ? proto.defaultValue : nil
780782
assert(proto.hasJsonName) // protoc should always set the name
781783
self.jsonName = proto.jsonName
784+
assert(isExtension == !proto.extendee.isEmpty)
782785
self.isExtension = isExtension
783786
self.number = proto.number
784787
self.type = proto.type
785788
self.label = proto.label
786789
self.options = proto.options
787-
self.proto = proto
788-
self.proto3Optional = proto.proto3Optional
790+
789791
if proto.hasOneofIndex {
790792
assert(!isExtension)
791793
oneofIndex = proto.oneofIndex
@@ -799,27 +801,37 @@ public final class FieldDescriptor {
799801
// is checked on the oneof side.
800802
assert(!proto.proto3Optional || isExtension)
801803
}
804+
805+
self.proto3Optional = proto.proto3Optional
806+
self.extendee = isExtension ? proto.extendee : nil
807+
switch type {
808+
case .group, .message, .enum:
809+
typeName = proto.typeName
810+
default:
811+
typeName = nil
812+
}
802813
}
803814

804815
fileprivate func bind(file: FileDescriptor, registry: Registry, containingType: Descriptor?) {
805816
self.file = file
806817

807-
assert(isExtension == !proto.extendee.isEmpty)
808-
if isExtension {
818+
if let extendee = extendee {
819+
assert(isExtension)
809820
extensionScope = containingType
810-
self.containingType = registry.descriptor(name: proto.extendee)
821+
self.containingType = registry.descriptor(name: extendee)
811822
} else {
812823
self.containingType = containingType
813824
}
825+
extendee = nil
814826

815-
switch type {
816-
case .group, .message:
817-
messageType = registry.descriptor(name: proto.typeName)
818-
case .enum:
819-
enumType = registry.enumDescriptor(name: proto.typeName)
820-
default:
821-
break
827+
if let typeName = typeName {
828+
if type == .enum {
829+
enumType = registry.enumDescriptor(name: typeName)
830+
} else {
831+
messageType = registry.descriptor(name: typeName)
832+
}
822833
}
834+
typeName = nil
823835
}
824836
}
825837

0 commit comments

Comments
 (0)