aboutsummaryrefslogtreecommitdiff
path: root/objectivec/GPBDescriptor_PackagePrivate.h
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_PackagePrivate.h
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_PackagePrivate.h')
-rw-r--r--objectivec/GPBDescriptor_PackagePrivate.h183
1 files changed, 89 insertions, 94 deletions
diff --git a/objectivec/GPBDescriptor_PackagePrivate.h b/objectivec/GPBDescriptor_PackagePrivate.h
index 7987d928..e3d0a80f 100644
--- a/objectivec/GPBDescriptor_PackagePrivate.h
+++ b/objectivec/GPBDescriptor_PackagePrivate.h
@@ -36,7 +36,7 @@
#import "GPBWireFormat.h"
// Describes attributes of the field.
-typedef NS_OPTIONS(uint32_t, GPBFieldFlags) {
+typedef NS_OPTIONS(uint16_t, GPBFieldFlags) {
// These map to standard protobuf concepts.
GPBFieldRequired = 1 << 0,
GPBFieldRepeated = 1 << 1,
@@ -44,6 +44,12 @@ typedef NS_OPTIONS(uint32_t, GPBFieldFlags) {
GPBFieldOptional = 1 << 3,
GPBFieldHasDefaultValue = 1 << 4,
+ // Indicates the field needs custom handling for the TextFormat name, if not
+ // set, the name can be derived from the ObjC name.
+ GPBFieldTextFormatNameCustom = 1 << 6,
+ // Indicates the field has an enum descriptor.
+ GPBFieldHasEnumDescriptor = 1 << 7,
+
// These are not standard protobuf concepts, they are specific to the
// Objective C runtime.
@@ -62,67 +68,49 @@ typedef NS_OPTIONS(uint32_t, GPBFieldFlags) {
GPBFieldMapKeySFixed64 = 10 << 8,
GPBFieldMapKeyBool = 11 << 8,
GPBFieldMapKeyString = 12 << 8,
-
- // Indicates the field needs custom handling for the TextFormat name, if not
- // set, the name can be derived from the ObjC name.
- GPBFieldTextFormatNameCustom = 1 << 16,
- // Indicates the field has an enum descriptor.
- GPBFieldHasEnumDescriptor = 1 << 17,
};
+// NOTE: The structures defined here have their members ordered to minimize
+// their size. This directly impacts the size of apps since these exist per
+// field/extension.
+
// Describes a single field in a protobuf as it is represented as an ivar.
typedef struct GPBMessageFieldDescription {
// Name of ivar.
const char *name;
+ union {
+ const char *className; // Name for message class.
+ // For enums only: If EnumDescriptors are compiled in, it will be that,
+ // otherwise it will be the verifier.
+ GPBEnumDescriptorFunc enumDescFunc;
+ GPBEnumValidationFunc enumVerifier;
+ } dataTypeSpecific;
// The field number for the ivar.
uint32_t number;
// The index (in bits) into _has_storage_.
- // > 0: the bit to use for a value being set.
- // = 0: no storage used.
+ // >= 0: the bit to use for a value being set.
+ // = GPBNoHasBit(INT32_MAX): no storage used.
// < 0: in a oneOf, use a full int32 to record the field active.
int32_t hasIndex;
+ // Offset of the variable into it's structure struct.
+ uint32_t offset;
// Field flags. Use accessor functions below.
GPBFieldFlags flags;
// Data type of the ivar.
GPBDataType dataType;
- // Offset of the variable into it's structure struct.
- size_t offset;
- // FieldOptions protobuf, serialized as string.
- const char *fieldOptions;
-
- GPBGenericValue defaultValue; // Default value for the ivar.
- union {
- const char *className; // Name for message class.
- // For enums only: If EnumDescriptors are compiled in, it will be that,
- // otherwise it will be the verifier.
- GPBEnumDescriptorFunc enumDescFunc;
- GPBEnumValidationFunc enumVerifier;
- } dataTypeSpecific;
} GPBMessageFieldDescription;
-// Describes a oneof.
-typedef struct GPBMessageOneofDescription {
- // Name of this enum oneof.
- const char *name;
- // The index of this oneof in the has_storage.
- int32_t index;
-} GPBMessageOneofDescription;
-
-// Describes an enum type defined in a .proto file.
-typedef struct GPBMessageEnumDescription {
- GPBEnumDescriptorFunc enumDescriptorFunc;
-} GPBMessageEnumDescription;
+// Fields in messages defined in a 'proto2' syntax file can provide a default
+// value. This struct provides the default along with the field info.
+typedef struct GPBMessageFieldDescriptionWithDefault {
+ // Default value for the ivar.
+ GPBGenericValue defaultValue;
-// Describes an individual enum constant of a particular type.
-typedef struct GPBMessageEnumValueDescription {
- // Name of this enum constant.
- const char *name;
- // Numeric value of this enum constant.
- int32_t number;
-} GPBMessageEnumValueDescription;
+ GPBMessageFieldDescription core;
+} GPBMessageFieldDescriptionWithDefault;
// Describes attributes of the extension.
-typedef NS_OPTIONS(uint32_t, GPBExtensionOptions) {
+typedef NS_OPTIONS(uint8_t, GPBExtensionOptions) {
// These map to standard protobuf concepts.
GPBExtensionRepeated = 1 << 0,
GPBExtensionPacked = 1 << 1,
@@ -131,65 +119,53 @@ typedef NS_OPTIONS(uint32_t, GPBExtensionOptions) {
// An extension
typedef struct GPBExtensionDescription {
+ GPBGenericValue defaultValue;
const char *singletonName;
- GPBDataType dataType;
const char *extendedClass;
- int32_t fieldNumber;
- GPBGenericValue defaultValue;
const char *messageOrGroupClassName;
- GPBExtensionOptions options;
GPBEnumDescriptorFunc enumDescriptorFunc;
+ int32_t fieldNumber;
+ GPBDataType dataType;
+ GPBExtensionOptions options;
} GPBExtensionDescription;
+typedef NS_OPTIONS(uint32_t, GPBDescriptorInitializationFlags) {
+ GPBDescriptorInitializationFlag_FieldsWithDefault = 1 << 0,
+ GPBDescriptorInitializationFlag_WireFormat = 1 << 1,
+};
+
@interface GPBDescriptor () {
@package
NSArray *fields_;
NSArray *oneofs_;
- size_t storageSize_;
+ uint32_t storageSize_;
}
-// fieldDescriptions, enumDescriptions, rangeDescriptions, and
-// extraTextFormatInfo have to be long lived, they are held as raw pointers.
+// fieldDescriptions have to be long lived, they are held as raw pointers.
+ (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;
-+ (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;
+ fields:(void *)fieldDescriptions
+ fieldCount:(uint32_t)fieldCount
+ storageSize:(uint32_t)storageSize
+ flags:(GPBDescriptorInitializationFlags)flags;
- (instancetype)initWithClass:(Class)messageClass
file:(GPBFileDescriptor *)file
fields:(NSArray *)fields
- oneofs:(NSArray *)oneofs
- enums:(NSArray *)enums
- extensionRanges:(const GPBExtensionRange *)ranges
- extensionRangesCount:(NSUInteger)rangeCount
- storageSize:(size_t)storage
+ storageSize:(uint32_t)storage
wireFormat:(BOOL)wireFormat;
+// Called right after init to provide extra information to avoid init having
+// an explosion of args. These pointers are recorded, so they are expected
+// to live for the lifetime of the app.
+- (void)setupOneofs:(const char **)oneofNames
+ count:(uint32_t)count
+ firstHasIndex:(int32_t)firstHasIndex;
+- (void)setupExtraTextInfo:(const char *)extraTextFormatInfo;
+- (void)setupExtensionRanges:(const GPBExtensionRange *)ranges count:(int32_t)count;
+
@end
@interface GPBFileDescriptor ()
@@ -199,14 +175,12 @@ typedef struct GPBExtensionDescription {
@interface GPBOneofDescriptor () {
@package
- GPBMessageOneofDescription *oneofDescription_;
+ const char *name_;
NSArray *fields_;
-
SEL caseSel_;
}
-- (instancetype)initWithOneofDescription:
- (GPBMessageOneofDescription *)oneofDescription
- fields:(NSArray *)fields;
+// name must be long lived.
+- (instancetype)initWithName:(const char *)name fields:(NSArray *)fields;
@end
@interface GPBFieldDescriptor () {
@@ -222,30 +196,32 @@ typedef struct GPBExtensionDescription {
// Single initializer
// description has to be long lived, it is held as a raw pointer.
-- (instancetype)initWithFieldDescription:
- (GPBMessageFieldDescription *)description
- rootClass:(Class)rootClass
+- (instancetype)initWithFieldDescription:(void *)description
+ includesDefault:(BOOL)includesDefault
syntax:(GPBFileSyntax)syntax;
@end
@interface GPBEnumDescriptor ()
-// valueDescriptions and extraTextFormatInfo have to be long lived, they are
+// valueNames, values and extraTextFormatInfo have to be long lived, they are
// held as raw pointers.
+ (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;
+ (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;
- (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;
@end
@@ -314,5 +290,24 @@ GPB_INLINE BOOL GPBExtensionIsWireFormat(GPBExtensionDescription *description) {
return (description->options & GPBExtensionSetWireFormat) != 0;
}
+// Helper for compile time assets.
+#ifndef _GPBCompileAssert
+ #if __has_feature(c_static_assert) || __has_extension(c_static_assert)
+ #define _GPBCompileAssert(test, msg) _Static_assert((test), #msg)
+ #else
+ // Pre-Xcode 7 support.
+ #define _GPBCompileAssertSymbolInner(line, msg) _GPBCompileAssert ## line ## __ ## msg
+ #define _GPBCompileAssertSymbol(line, msg) _GPBCompileAssertSymbolInner(line, msg)
+ #define _GPBCompileAssert(test, msg) \
+ typedef char _GPBCompileAssertSymbol(__LINE__, msg) [ ((test) ? 1 : -1) ]
+ #endif // __has_feature(c_static_assert) || __has_extension(c_static_assert)
+#endif // _GPBCompileAssert
+
+// Sanity check that there isn't padding between the field description
+// structures with and without a default.
+_GPBCompileAssert(sizeof(GPBMessageFieldDescriptionWithDefault) ==
+ (sizeof(GPBGenericValue) +
+ sizeof(GPBMessageFieldDescription)),
+ DescriptionsWithDefault_different_size_than_expected);
CF_EXTERN_C_END