protobuf/objectivec
Thomas Van Lenten 79a23c435c 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).
2016-03-17 10:04:21 -04:00
..
DevTools Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
google/protobuf Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
ProtocolBuffers_iOS.xcodeproj Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
ProtocolBuffers_OSX.xcodeproj Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
Tests Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
generate_well_known_types.sh Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
GPBArray_PackagePrivate.h Alpha 1 drop of Google's Objective C plugin and runtime support for protobufs. 2015-05-06 13:19:14 -04:00
GPBArray.h Add nonnil markup to ObjC library. 2015-06-16 17:04:50 -04:00
GPBArray.m Objective C Second Alpha Drop 2015-05-22 14:27:31 -04:00
GPBBootstrap.h Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
GPBCodedInputStream_PackagePrivate.h Drop all use of OSSpinLock 2015-12-17 16:05:50 -05:00
GPBCodedInputStream.h HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBCodedInputStream.m Drop all use of OSSpinLock 2015-12-17 16:05:50 -05:00
GPBCodedOutputStream_PackagePrivate.h HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBCodedOutputStream.h HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBCodedOutputStream.m HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBDescriptor_PackagePrivate.h Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
GPBDescriptor.h Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
GPBDescriptor.m Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
GPBDictionary_PackagePrivate.h Beta quality drop of Objective C Support. 2015-06-08 17:17:22 -04:00
GPBDictionary.h Support ObjC Generic Collections 2016-02-18 13:55:59 -05:00
GPBDictionary.m HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBExtensionInternals.h Beta quality drop of Objective C Support. 2015-06-08 17:17:22 -04:00
GPBExtensionInternals.m HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBExtensionRegistry.h HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBExtensionRegistry.m Beta quality drop of Objective C Support. 2015-06-08 17:17:22 -04:00
GPBMessage_PackagePrivate.h Drop all use of OSSpinLock 2015-12-17 16:05:50 -05:00
GPBMessage.h HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBMessage.m Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
GPBProtocolBuffers_RuntimeSupport.h Beta quality drop of Objective C Support. 2015-06-08 17:17:22 -04:00
GPBProtocolBuffers.h Update GPBProtocolBuffers.h 2015-11-24 00:16:29 +02:00
GPBProtocolBuffers.m Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
GPBRootObject_PackagePrivate.h Beta quality drop of Objective C Support. 2015-06-08 17:17:22 -04:00
GPBRootObject.h HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBRootObject.m Drop all use of OSSpinLock 2015-12-17 16:05:50 -05:00
GPBRuntimeTypes.h Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
GPBUnknownField_PackagePrivate.h Beta quality drop of Objective C Support. 2015-06-08 17:17:22 -04:00
GPBUnknownField.h HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBUnknownField.m HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBUnknownFieldSet_PackagePrivate.h Alpha 1 drop of Google's Objective C plugin and runtime support for protobufs. 2015-05-06 13:19:14 -04:00
GPBUnknownFieldSet.h HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBUnknownFieldSet.m Beta quality drop of Objective C Support. 2015-06-08 17:17:22 -04:00
GPBUtilities_PackagePrivate.h Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
GPBUtilities.h HeaderDoc support in the library and generated sources 2016-03-07 12:07:03 -05:00
GPBUtilities.m Shrink ObjC overhead (generated size and some runtime sizes) 2016-03-17 10:04:21 -04:00
GPBWellKnownTypes.h Revert "Add packFrom, unpackTo and is in google.protobuf.Any." 2015-08-11 16:22:47 -04:00
GPBWellKnownTypes.m Revert "Add packFrom, unpackTo and is in google.protobuf.Any." 2015-08-11 16:22:47 -04:00
GPBWireFormat.h Add nonnil markup to ObjC library. 2015-06-16 17:04:50 -04:00
GPBWireFormat.m Beta quality drop of Objective C Support. 2015-06-08 17:17:22 -04:00
README.md Fix typos in README.md/CHANGES.txt 2016-01-11 14:52:01 -08:00

Protocol Buffers - Google's data interchange format

Build Status

Copyright 2008 Google Inc.

This directory contains the Objective C Protocol Buffers runtime library.

Requirements

The Objective C implementation requires:

  • Objective C 2.0 Runtime (32bit & 64bit iOS, 64bit OS X).
  • Xcode 7.0 (or later).
  • The library code does not use ARC (for performance reasons), but it all can be called from ARC code.

Installation

The full distribution pulled from github includes the sources for both the compiler (protoc) and the runtime (this directory). To build the compiler and run the runtime tests, you can use:

 $ objectivec/DevTools/full_mac_build.sh

This will generate the src/protoc binary.

Building

There are two ways to include the Runtime sources in your project:

Add objectivec/\*.h & objectivec/GPBProtocolBuffers.m to your project.

or

Add objectivec/\*.h & objectivec/\*.m except for objectivec/GPBProtocolBuffers.m to your project.

If the target is using ARC, remember to turn off ARC (-fno-objc-arc) for the .m files.

The files generated by protoc for the *.proto files (\*.pbobjc.h' and *.pbobjc.m`) are then also added to the target.

Usage

The objects generated for messages should work like any other Objective C object. They are mutable objects, but if you don't change them, they are safe to share between threads (similar to passing an NSMutableDictionary between threads/queues; as long as no one mutates it, things are fine).

There are a few behaviors worth calling out:

A property that is type NSString* will never return nil. If the value is unset, it will return an empty string (@""). This is inpart to align things with the Protocol Buffers spec which says the default for strings is an empty string, but also so you can always safely pass them to isEqual:/compare:, etc. and have deterministic results.

A property that is type NSData* also won't return nil, it will return an empty data ([NSData data]). The reasoning is the same as for NSString not returning nil.

A property that is another GPBMessage class also will not return nil. If the field wasn't already set, you will get a instance of the correct class. This instance will be a temporary instance unless you mutate it, at which point it will be attached to its parent object. We call this pattern autocreators. Similar to NSString and NSData properties it makes things a little safer when using them with isEqual:/etc.; but more importantly, this allows you to write code that uses Objective C's property dot notation to walk into nested objects and access and/or assign things without having to check that they are not nil and create them each step along the way. You can write this:

- (void)updateRecord:(MyMessage *)msg {
  ...
  // Note: You don't have to check subMessage and otherMessage for nil and
  // alloc/init/assign them back along the way.
  msg.subMessage.otherMessage.lastName = @"Smith";
  ...
}

If you want to check if a GPBMessage property is present, there is always as has\[NAME\] property to go with the main property to check if it is set.

A property that is of an Array or Dictionary type also provides autocreator behavior and will never return nil. This provides all the same benefits you see for the message properties. Again, you can write:

- (void)updateRecord:(MyMessage *)msg {
  ...
  // Note: Just like above, you don't have to check subMessage and otherMessage
  // for nil and alloc/init/assign them back along the way. You also don't have
  // to create the siblingsArray, you can safely just append to it.
  [msg.subMessage.otherMessage.siblingsArray addObject:@"Pat"];
  ...
}

If you are inspecting a message you got from some other place (server, disk, etc), you may want to check if the Array or Dictionary has entries without causing it to be created for you. For this, there is always a \[NAME\]_Count property also provided that can return zero or the real count, but won't trigger the creation.

For primitive type fields (ints, floats, bools, enum) in messages defined in a .proto file that use proto2 syntax there are conceptual differences between having an explicit and default value. You can always get the value of the property. In the case that it hasn't been set you will get the default. In cases where you need to know whether it was set explicitly or you are just getting the default, you can use the has\[NAME\] property. If the value has been set, and you want to clear it, you can set the has\[NAME\] to NO. proto3 syntax messages do away with this concept, thus the default values are never included when the message is encoded.

The Objective C classes/enums can be used from Swift code.

Objective C Generator Options

objc_class_prefix=<prefix> (no default)

Since Objective C uses a global namespace for all of its classes, there can be collisions. This option provides a prefix that will be added to the Enums and Objects (for messages) generated from the proto. Convention is to base the prefix on the package the proto is in.

Contributing

Please make updates to the tests along with changes. If just changing the runtime, the Xcode projects can be used to build and run tests. If your change also requires changes to the generated code, objectivec/DevTools/full_mac_build.sh can be used to easily rebuild and test changes. Passing -h to the script will show the addition options that could be useful.

Documentation

The complete documentation for Protocol Buffers is available via the web at:

https://developers.google.com/protocol-buffers/