aboutsummaryrefslogtreecommitdiff
path: root/objectivec/GPBDescriptor.m
diff options
context:
space:
mode:
authorThomas Van Lenten <thomasvl@google.com>2016-03-17 10:04:21 -0400
committerThomas Van Lenten <thomasvl@google.com>2016-03-17 10:04:21 -0400
commit79a23c435c4862d0c7af3c2740662104c77171dc (patch)
treed21b31c76dee2e7d9a06a65a21eaad5e0c6615f8 /objectivec/GPBDescriptor.m
parentca3dc15d4ca3bb1b092928b456ea844637693b61 (diff)
downloadprotobuf-79a23c435c4862d0c7af3c2740662104c77171dc.tar.gz
protobuf-79a23c435c4862d0c7af3c2740662104c77171dc.tar.bz2
protobuf-79a23c435c4862d0c7af3c2740662104c77171dc.zip
Shrink ObjC overhead (generated size and some runtime sizes)
NOTE: This is a binary breaking change as structure sizes have changed size and/or order. - Drop capturing field options, no other options were captured and other mobile targeted languages don't try to capture this sort information (saved 8 bytes for every field defined (in static data and again in field descriptor instance size data). - No longer generate/compile in the messages/enums in descriptor.proto. If developers need it, they should generate it and compile it in. Reduced the overhead of the core library. - Compute the number of has_bits actually needs to avoid over reserving. - Let the boolean single fields store via a has_bit to avoid storage, makes the common cases of the instance size smaller. - Reorder some flags and down size the enums to contain the bits needed. - Reorder the items in the structures to manually ensure they are are packed better (especially when generating 64bit code - 8 bytes for every field, 16 bytes for every extension, instance sizes 8 bytes also). - Split off the structure initialization so when the default is zero, the generated static storage doesn't need to reserve the space. This is batched at the message level, so all the fields for the message have to have zero defaults to get the saves. By definition all proto3 syntax files fall into this case but it also saves space for the proto2 that use the standard defaults. (saves 8 bytes of static data for every field that had a zero default) - Don't track the enums defined by a message. Nothing in the runtime needs it and it was just generation and runtime overhead. (saves 8 bytes per enum) - Ensure EnumDescriptors are started up threadsafe in all cases. - Split some of the Descriptor initialization into multiple methods so the generated code isn't padded with lots of zero/nil args. - Change how oneof info is feed to the runtime enabling us to generate less static data (8 bytes saved per oneof for 64bit). - Change how enum value informat is capture to pack the data and only decode it if it ends up being needed. Avoids padding issues causing bloat of 64bit, and removes the needs for extra pointers in addition to the data (just the data and one pointer now).
Diffstat (limited to 'objectivec/GPBDescriptor.m')
-rw-r--r--objectivec/GPBDescriptor.m360
1 files changed, 163 insertions, 197 deletions
diff --git a/objectivec/GPBDescriptor.m b/objectivec/GPBDescriptor.m
index bae9187e..2709737c 100644
--- a/objectivec/GPBDescriptor.m
+++ b/objectivec/GPBDescriptor.m
@@ -35,7 +35,6 @@
#import "GPBUtilities_PackagePrivate.h"
#import "GPBWireFormat.h"
#import "GPBMessage_PackagePrivate.h"
-#import "google/protobuf/Descriptor.pbobjc.h"
// The address of this variable is used as a key for obj_getAssociatedObject.
static const char kTextFormatExtraValueKey = 0;
@@ -92,7 +91,6 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
@implementation GPBDescriptor {
Class messageClass_;
- NSArray *enums_;
GPBFileDescriptor *file_;
BOOL wireFormat_;
}
@@ -100,7 +98,6 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
@synthesize messageClass = messageClass_;
@synthesize fields = fields_;
@synthesize oneofs = oneofs_;
-@synthesize enums = enums_;
@synthesize extensionRanges = extensionRanges_;
@synthesize extensionRangesCount = extensionRangesCount_;
@synthesize file = file_;
@@ -110,130 +107,58 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
allocDescriptorForClass:(Class)messageClass
rootClass:(Class)rootClass
file:(GPBFileDescriptor *)file
- fields:(GPBMessageFieldDescription *)fieldDescriptions
- fieldCount:(NSUInteger)fieldCount
- oneofs:(GPBMessageOneofDescription *)oneofDescriptions
- oneofCount:(NSUInteger)oneofCount
- enums:(GPBMessageEnumDescription *)enumDescriptions
- enumCount:(NSUInteger)enumCount
- ranges:(const GPBExtensionRange *)ranges
- rangeCount:(NSUInteger)rangeCount
- storageSize:(size_t)storageSize
- wireFormat:(BOOL)wireFormat {
+ fields:(void *)fieldDescriptions
+ fieldCount:(uint32_t)fieldCount
+ storageSize:(uint32_t)storageSize
+ flags:(GPBDescriptorInitializationFlags)flags {
+ // The rootClass is no longer used, but it is passed in to ensure it
+ // was started up during initialization also.
+ (void)rootClass;
NSMutableArray *fields = nil;
- NSMutableArray *oneofs = nil;
- NSMutableArray *enums = nil;
- NSMutableArray *extensionRanges = nil;
GPBFileSyntax syntax = file.syntax;
- for (NSUInteger i = 0; i < fieldCount; ++i) {
+ BOOL fieldsIncludeDefault =
+ (flags & GPBDescriptorInitializationFlag_FieldsWithDefault) != 0;
+
+ void *desc;
+ for (uint32_t i = 0; i < fieldCount; ++i) {
if (fields == nil) {
fields = [[NSMutableArray alloc] initWithCapacity:fieldCount];
}
- GPBFieldDescriptor *fieldDescriptor = [[GPBFieldDescriptor alloc]
- initWithFieldDescription:&fieldDescriptions[i]
- rootClass:rootClass
- syntax:syntax];
+ // Need correctly typed pointer for array indexing below to work.
+ if (fieldsIncludeDefault) {
+ GPBMessageFieldDescriptionWithDefault *fieldDescWithDefault = fieldDescriptions;
+ desc = &(fieldDescWithDefault[i]);
+ } else {
+ GPBMessageFieldDescription *fieldDesc = fieldDescriptions;
+ desc = &(fieldDesc[i]);
+ }
+ GPBFieldDescriptor *fieldDescriptor =
+ [[GPBFieldDescriptor alloc] initWithFieldDescription:desc
+ includesDefault:fieldsIncludeDefault
+ syntax:syntax];
[fields addObject:fieldDescriptor];
[fieldDescriptor release];
}
- for (NSUInteger i = 0; i < oneofCount; ++i) {
- if (oneofs == nil) {
- oneofs = [[NSMutableArray alloc] initWithCapacity:oneofCount];
- }
- GPBMessageOneofDescription *oneofDescription = &oneofDescriptions[i];
- NSArray *fieldsForOneof =
- NewFieldsArrayForHasIndex(oneofDescription->index, fields);
- GPBOneofDescriptor *oneofDescriptor =
- [[GPBOneofDescriptor alloc] initWithOneofDescription:oneofDescription
- fields:fieldsForOneof];
- [oneofs addObject:oneofDescriptor];
- [oneofDescriptor release];
- [fieldsForOneof release];
- }
- for (NSUInteger i = 0; i < enumCount; ++i) {
- if (enums == nil) {
- enums = [[NSMutableArray alloc] initWithCapacity:enumCount];
- }
- GPBEnumDescriptor *enumDescriptor =
- enumDescriptions[i].enumDescriptorFunc();
- [enums addObject:enumDescriptor];
- }
+ BOOL wireFormat = (flags & GPBDescriptorInitializationFlag_WireFormat) != 0;
GPBDescriptor *descriptor = [[self alloc] initWithClass:messageClass
file:file
fields:fields
- oneofs:oneofs
- enums:enums
- extensionRanges:ranges
- extensionRangesCount:rangeCount
storageSize:storageSize
wireFormat:wireFormat];
[fields release];
- [oneofs release];
- [enums release];
- [extensionRanges release];
- return descriptor;
-}
-
-+ (instancetype)
- allocDescriptorForClass:(Class)messageClass
- rootClass:(Class)rootClass
- file:(GPBFileDescriptor *)file
- fields:(GPBMessageFieldDescription *)fieldDescriptions
- fieldCount:(NSUInteger)fieldCount
- oneofs:(GPBMessageOneofDescription *)oneofDescriptions
- oneofCount:(NSUInteger)oneofCount
- enums:(GPBMessageEnumDescription *)enumDescriptions
- enumCount:(NSUInteger)enumCount
- ranges:(const GPBExtensionRange *)ranges
- rangeCount:(NSUInteger)rangeCount
- storageSize:(size_t)storageSize
- wireFormat:(BOOL)wireFormat
- extraTextFormatInfo:(const char *)extraTextFormatInfo {
- GPBDescriptor *descriptor = [self allocDescriptorForClass:messageClass
- rootClass:rootClass
- file:file
- fields:fieldDescriptions
- fieldCount:fieldCount
- oneofs:oneofDescriptions
- oneofCount:oneofCount
- enums:enumDescriptions
- enumCount:enumCount
- ranges:ranges
- rangeCount:rangeCount
- storageSize:storageSize
- wireFormat:wireFormat];
- // Extra info is a compile time option, so skip the work if not needed.
- if (extraTextFormatInfo) {
- NSValue *extraInfoValue = [NSValue valueWithPointer:extraTextFormatInfo];
- for (GPBFieldDescriptor *fieldDescriptor in descriptor->fields_) {
- if (fieldDescriptor->description_->flags & GPBFieldTextFormatNameCustom) {
- objc_setAssociatedObject(fieldDescriptor, &kTextFormatExtraValueKey,
- extraInfoValue,
- OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- }
- }
return descriptor;
}
- (instancetype)initWithClass:(Class)messageClass
file:(GPBFileDescriptor *)file
fields:(NSArray *)fields
- oneofs:(NSArray *)oneofs
- enums:(NSArray *)enums
- extensionRanges:(const GPBExtensionRange *)extensionRanges
- extensionRangesCount:(NSUInteger)extensionRangesCount
- storageSize:(size_t)storageSize
+ storageSize:(uint32_t)storageSize
wireFormat:(BOOL)wireFormat {
if ((self = [super init])) {
messageClass_ = messageClass;
file_ = file;
fields_ = [fields retain];
- oneofs_ = [oneofs retain];
- enums_ = [enums retain];
- extensionRanges_ = extensionRanges;
- extensionRangesCount_ = extensionRangesCount;
storageSize_ = storageSize;
wireFormat_ = wireFormat;
}
@@ -243,10 +168,47 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
- (void)dealloc {
[fields_ release];
[oneofs_ release];
- [enums_ release];
[super dealloc];
}
+- (void)setupOneofs:(const char **)oneofNames
+ count:(uint32_t)count
+ firstHasIndex:(int32_t)firstHasIndex {
+ NSCAssert(firstHasIndex < 0, @"Should always be <0");
+ NSMutableArray *oneofs = [[NSMutableArray alloc] initWithCapacity:count];
+ for (uint32_t i = 0, hasIndex = firstHasIndex; i < count; ++i, --hasIndex) {
+ const char *name = oneofNames[i];
+ NSArray *fieldsForOneof = NewFieldsArrayForHasIndex(hasIndex, fields_);
+ NSCAssert(fieldsForOneof.count > 0,
+ @"No fields for this oneof? (%s:%d)", name, hasIndex);
+ GPBOneofDescriptor *oneofDescriptor =
+ [[GPBOneofDescriptor alloc] initWithName:name fields:fieldsForOneof];
+ [oneofs addObject:oneofDescriptor];
+ [oneofDescriptor release];
+ [fieldsForOneof release];
+ }
+ oneofs_ = oneofs;
+}
+
+- (void)setupExtraTextInfo:(const char *)extraTextFormatInfo {
+ // Extra info is a compile time option, so skip the work if not needed.
+ if (extraTextFormatInfo) {
+ NSValue *extraInfoValue = [NSValue valueWithPointer:extraTextFormatInfo];
+ for (GPBFieldDescriptor *fieldDescriptor in fields_) {
+ if (fieldDescriptor->description_->flags & GPBFieldTextFormatNameCustom) {
+ objc_setAssociatedObject(fieldDescriptor, &kTextFormatExtraValueKey,
+ extraInfoValue,
+ OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ }
+ }
+ }
+}
+
+- (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)count {
+ extensionRanges_ = ranges;
+ extensionRangesCount_ = count;
+}
+
- (NSString *)name {
return NSStringFromClass(messageClass_);
}
@@ -283,15 +245,6 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
return nil;
}
-- (GPBEnumDescriptor *)enumWithName:(NSString *)name {
- for (GPBEnumDescriptor *descriptor in enums_) {
- if ([descriptor.name isEqual:name]) {
- return descriptor;
- }
- }
- return nil;
-}
-
@end
@implementation GPBFileDescriptor {
@@ -318,19 +271,16 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
@synthesize fields = fields_;
-- (instancetype)initWithOneofDescription:
- (GPBMessageOneofDescription *)oneofDescription
- fields:(NSArray *)fields {
+- (instancetype)initWithName:(const char *)name fields:(NSArray *)fields {
self = [super init];
if (self) {
- NSAssert(oneofDescription->index < 0, @"Should always be <0");
- oneofDescription_ = oneofDescription;
+ name_ = name;
fields_ = [fields retain];
for (GPBFieldDescriptor *fieldDesc in fields) {
fieldDesc->containingOneof_ = self;
}
- caseSel_ = SelFromStrings(NULL, oneofDescription->name, "OneOfCase", NO);
+ caseSel_ = SelFromStrings(NULL, name, "OneOfCase", NO);
}
return self;
}
@@ -341,7 +291,7 @@ static NSArray *NewFieldsArrayForHasIndex(int hasIndex,
}
- (NSString *)name {
- return @(oneofDescription_->name);
+ return @(name_);
}
- (GPBFieldDescriptor *)fieldWithNumber:(uint32_t)fieldNumber {
@@ -389,7 +339,6 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
@implementation GPBFieldDescriptor {
GPBGenericValue defaultValue_;
- GPBFieldOptions *fieldOptions_;
// Message ivars
Class msgClass_;
@@ -403,7 +352,6 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
} enumHandling_;
}
-@synthesize fieldOptions = fieldOptions_;
@synthesize msgClass = msgClass_;
@synthesize containingOneof = containingOneof_;
@@ -417,16 +365,21 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
return self;
}
-- (instancetype)initWithFieldDescription:
- (GPBMessageFieldDescription *)description
- rootClass:(Class)rootClass
+- (instancetype)initWithFieldDescription:(void *)description
+ includesDefault:(BOOL)includesDefault
syntax:(GPBFileSyntax)syntax {
if ((self = [super init])) {
- description_ = description;
- getSel_ = sel_getUid(description->name);
- setSel_ = SelFromStrings("set", description->name, NULL, YES);
+ GPBMessageFieldDescription *coreDesc;
+ if (includesDefault) {
+ coreDesc = &(((GPBMessageFieldDescriptionWithDefault *)description)->core);
+ } else {
+ coreDesc = description;
+ }
+ description_ = coreDesc;
+ getSel_ = sel_getUid(coreDesc->name);
+ setSel_ = SelFromStrings("set", coreDesc->name, NULL, YES);
- GPBDataType dataType = description->dataType;
+ GPBDataType dataType = coreDesc->dataType;
BOOL isMessage = GPBDataTypeIsMessage(dataType);
BOOL isMapOrArray = GPBFieldIsMapOrArray(self);
@@ -434,39 +387,39 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
// map<>/repeated fields get a *Count property (inplace of a has*) to
// support checking if there are any entries without triggering
// autocreation.
- hasOrCountSel_ = SelFromStrings(NULL, description->name, "_Count", NO);
+ hasOrCountSel_ = SelFromStrings(NULL, coreDesc->name, "_Count", NO);
} else {
// If there is a positive hasIndex, then:
// - All fields types for proto2 messages get has* selectors.
// - Only message fields for proto3 messages get has* selectors.
// Note: the positive check is to handle oneOfs, we can't check
// containingOneof_ because it isn't set until after initialization.
- if ((description->hasIndex >= 0) &&
- (description->hasIndex != GPBNoHasBit) &&
+ if ((coreDesc->hasIndex >= 0) &&
+ (coreDesc->hasIndex != GPBNoHasBit) &&
((syntax != GPBFileSyntaxProto3) || isMessage)) {
- hasOrCountSel_ = SelFromStrings("has", description->name, NULL, NO);
- setHasSel_ = SelFromStrings("setHas", description->name, NULL, YES);
+ hasOrCountSel_ = SelFromStrings("has", coreDesc->name, NULL, NO);
+ setHasSel_ = SelFromStrings("setHas", coreDesc->name, NULL, YES);
}
}
// Extra type specific data.
if (isMessage) {
- const char *className = description->dataTypeSpecific.className;
+ const char *className = coreDesc->dataTypeSpecific.className;
msgClass_ = objc_getClass(className);
NSAssert(msgClass_, @"Class %s not defined", className);
} else if (dataType == GPBDataTypeEnum) {
- if ((description_->flags & GPBFieldHasEnumDescriptor) != 0) {
+ if ((coreDesc->flags & GPBFieldHasEnumDescriptor) != 0) {
enumHandling_.enumDescriptor_ =
- description->dataTypeSpecific.enumDescFunc();
+ coreDesc->dataTypeSpecific.enumDescFunc();
} else {
enumHandling_.enumVerifier_ =
- description->dataTypeSpecific.enumVerifier;
+ coreDesc->dataTypeSpecific.enumVerifier;
}
}
- // Non map<>/repeated fields can have defaults.
- if (!isMapOrArray) {
- defaultValue_ = description->defaultValue;
+ // Non map<>/repeated fields can have defaults in proto2 syntax.
+ if (!isMapOrArray && includesDefault) {
+ defaultValue_ = ((GPBMessageFieldDescriptionWithDefault *)description)->defaultValue;
if (dataType == GPBDataTypeBytes) {
// Data stored as a length prefixed (network byte order) c-string in
// descriptor structure.
@@ -480,24 +433,6 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
}
}
-
- // FieldOptions stored as a length prefixed (network byte order) c-escaped
- // string in descriptor records.
- if (description->fieldOptions) {
- uint8_t *optionsBytes = (uint8_t *)description->fieldOptions;
- uint32_t optionsLength = *((uint32_t *)optionsBytes);
- optionsLength = ntohl(optionsLength);
- if (optionsLength > 0) {
- optionsBytes += sizeof(optionsLength);
- NSData *optionsData = [NSData dataWithBytesNoCopy:optionsBytes
- length:optionsLength
- freeWhenDone:NO];
- GPBExtensionRegistry *registry = [rootClass extensionRegistry];
- fieldOptions_ = [[GPBFieldOptions parseFromData:optionsData
- extensionRegistry:registry
- error:NULL] retain];
- }
- }
}
return self;
}
@@ -666,7 +601,7 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
} else {
// Undo the CamelCase.
NSMutableString *result = [NSMutableString stringWithCapacity:len];
- for (NSUInteger i = 0; i < len; i++) {
+ for (uint32_t i = 0; i < len; i++) {
unichar c = [name characterAtIndex:i];
if (c >= 'A' && c <= 'Z') {
if (i > 0) {
@@ -686,10 +621,16 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
@implementation GPBEnumDescriptor {
NSString *name_;
- GPBMessageEnumValueDescription *valueDescriptions_;
- NSUInteger valueDescriptionsCount_;
+ // valueNames_ is a single c string with all of the value names appended
+ // together, each null terminated. -calcValueNameOffsets fills in
+ // nameOffsets_ with the offsets to allow quicker access to the individual
+ // names.
+ const char *valueNames_;
+ const int32_t *values_;
GPBEnumValidationFunc enumVerifier_;
const uint8_t *extraTextFormatInfo_;
+ uint32_t *nameOffsets_;
+ uint32_t valueCount_;
}
@synthesize name = name_;
@@ -697,26 +638,30 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
+ (instancetype)
allocDescriptorForName:(NSString *)name
- values:(GPBMessageEnumValueDescription *)valueDescriptions
- valueCount:(NSUInteger)valueCount
+ valueNames:(const char *)valueNames
+ values:(const int32_t *)values
+ count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier {
GPBEnumDescriptor *descriptor = [[self alloc] initWithName:name
- values:valueDescriptions
- valueCount:valueCount
+ valueNames:valueNames
+ values:values
+ count:valueCount
enumVerifier:enumVerifier];
return descriptor;
}
+ (instancetype)
allocDescriptorForName:(NSString *)name
- values:(GPBMessageEnumValueDescription *)valueDescriptions
- valueCount:(NSUInteger)valueCount
+ valueNames:(const char *)valueNames
+ values:(const int32_t *)values
+ count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier
extraTextFormatInfo:(const char *)extraTextFormatInfo {
// Call the common case.
GPBEnumDescriptor *descriptor = [self allocDescriptorForName:name
- values:valueDescriptions
- valueCount:valueCount
+ valueNames:valueNames
+ values:values
+ count:valueCount
enumVerifier:enumVerifier];
// Set the extra info.
descriptor->extraTextFormatInfo_ = (const uint8_t *)extraTextFormatInfo;
@@ -724,24 +669,49 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
}
- (instancetype)initWithName:(NSString *)name
- values:(GPBMessageEnumValueDescription *)valueDescriptions
- valueCount:(NSUInteger)valueCount
+ valueNames:(const char *)valueNames
+ values:(const int32_t *)values
+ count:(uint32_t)valueCount
enumVerifier:(GPBEnumValidationFunc)enumVerifier {
if ((self = [super init])) {
name_ = [name copy];
- valueDescriptions_ = valueDescriptions;
- valueDescriptionsCount_ = valueCount;
+ valueNames_ = valueNames;
+ values_ = values;
+ valueCount_ = valueCount;
enumVerifier_ = enumVerifier;
}
return self;
}
+- (void)dealloc {
+ [name_ release];
+ if (nameOffsets_) free(nameOffsets_);
+ [super dealloc];
+}
+
+- (void)calcValueNameOffsets {
+ @synchronized(self) {
+ if (nameOffsets_ != NULL) {
+ return;
+ }
+ uint32_t *offsets = malloc(valueCount_ * sizeof(uint32_t));
+ const char *scan = valueNames_;
+ for (uint32_t i = 0; i < valueCount_; ++i) {
+ offsets[i] = (uint32_t)(scan - valueNames_);
+ while (*scan != '\0') ++scan;
+ ++scan; // Step over the null.
+ }
+ nameOffsets_ = offsets;
+ }
+}
+
- (NSString *)enumNameForValue:(int32_t)number {
- for (NSUInteger i = 0; i < valueDescriptionsCount_; ++i) {
- GPBMessageEnumValueDescription *scan = &valueDescriptions_[i];
- if ((scan->number == number) && (scan->name != NULL)) {
- NSString *fullName =
- [NSString stringWithFormat:@"%@_%s", name_, scan->name];
+ if (nameOffsets_ == NULL) [self calcValueNameOffsets];
+
+ for (uint32_t i = 0; i < valueCount_; ++i) {
+ if (values_[i] == number) {
+ const char *valueName = valueNames_ + nameOffsets_[i];
+ NSString *fullName = [NSString stringWithFormat:@"%@_%s", name_, valueName];
return fullName;
}
}
@@ -760,12 +730,14 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
const char *nameAsCStr = [name UTF8String];
nameAsCStr += prefixLen;
+ if (nameOffsets_ == NULL) [self calcValueNameOffsets];
+
// Find it.
- for (NSUInteger i = 0; i < valueDescriptionsCount_; ++i) {
- GPBMessageEnumValueDescription *scan = &valueDescriptions_[i];
- if ((scan->name != NULL) && (strcmp(nameAsCStr, scan->name) == 0)) {
+ for (uint32_t i = 0; i < valueCount_; ++i) {
+ const char *valueName = valueNames_ + nameOffsets_[i];
+ if (strcmp(nameAsCStr, valueName) == 0) {
if (outValue) {
- *outValue = scan->number;
+ *outValue = values_[i];
}
return YES;
}
@@ -773,34 +745,28 @@ uint32_t GPBFieldAlternateTag(GPBFieldDescriptor *self) {
return NO;
}
-- (void)dealloc {
- [name_ release];
- [super dealloc];
-}
-
- (NSString *)textFormatNameForValue:(int32_t)number {
+ if (nameOffsets_ == NULL) [self calcValueNameOffsets];
+
// Find the EnumValue descriptor and its index.
- GPBMessageEnumValueDescription *valueDescriptor = NULL;
- NSUInteger valueDescriptorIndex;
- for (valueDescriptorIndex = 0; valueDescriptorIndex < valueDescriptionsCount_;
+ BOOL foundIt = NO;
+ uint32_t valueDescriptorIndex;
+ for (valueDescriptorIndex = 0; valueDescriptorIndex < valueCount_;
++valueDescriptorIndex) {
- GPBMessageEnumValueDescription *scan =
- &valueDescriptions_[valueDescriptorIndex];
- if (scan->number == number) {
- valueDescriptor = scan;
+ if (values_[valueDescriptorIndex] == number) {
+ foundIt = YES;
break;
}
}
- // If we didn't find it, or names were disable at proto compile time, nothing
- // we can do.
- if (!valueDescriptor || !valueDescriptor->name) {
+ if (!foundIt) {
return nil;
}
NSString *result = nil;
// Naming adds an underscore between enum name and value name, skip that also.
- NSString *shortName = @(valueDescriptor->name);
+ const char *valueName = valueNames_ + nameOffsets_[valueDescriptorIndex];
+ NSString *shortName = @(valueName);
// See if it is in the map of special format handling.
if (extraTextFormatInfo_) {