Unverified Commit ffa6bfc0 authored by Thomas Van Lenten's avatar Thomas Van Lenten Committed by GitHub

ObjC: small improvement to extension serialization.

For messages that have multiple extension ranges, this will improve things
by avoiding repeated work.  For messages with a single range, it should
be a wash.

- Sort the list of set extensions once during serialization and reuse the list.
- Break out of the serialization loop as soon as the loop has moved pasted at
  accepted range for field ids.
parent 59133296
...@@ -1283,16 +1283,20 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) { ...@@ -1283,16 +1283,20 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) {
NSUInteger fieldCount = fieldsArray.count; NSUInteger fieldCount = fieldsArray.count;
const GPBExtensionRange *extensionRanges = descriptor.extensionRanges; const GPBExtensionRange *extensionRanges = descriptor.extensionRanges;
NSUInteger extensionRangesCount = descriptor.extensionRangesCount; NSUInteger extensionRangesCount = descriptor.extensionRangesCount;
NSArray *sortedExtensions =
[[extensionMap_ allKeys] sortedArrayUsingSelector:@selector(compareByFieldNumber:)];
for (NSUInteger i = 0, j = 0; i < fieldCount || j < extensionRangesCount;) { for (NSUInteger i = 0, j = 0; i < fieldCount || j < extensionRangesCount;) {
if (i == fieldCount) { if (i == fieldCount) {
[self writeExtensionsToCodedOutputStream:output [self writeExtensionsToCodedOutputStream:output
range:extensionRanges[j++]]; range:extensionRanges[j++]
sortedExtensions:sortedExtensions];
} else if (j == extensionRangesCount || } else if (j == extensionRangesCount ||
GPBFieldNumber(fieldsArray[i]) < extensionRanges[j].start) { GPBFieldNumber(fieldsArray[i]) < extensionRanges[j].start) {
[self writeField:fieldsArray[i++] toCodedOutputStream:output]; [self writeField:fieldsArray[i++] toCodedOutputStream:output];
} else { } else {
[self writeExtensionsToCodedOutputStream:output [self writeExtensionsToCodedOutputStream:output
range:extensionRanges[j++]]; range:extensionRanges[j++]
sortedExtensions:sortedExtensions];
} }
} }
if (descriptor.isWireFormat) { if (descriptor.isWireFormat) {
...@@ -1808,17 +1812,20 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) { ...@@ -1808,17 +1812,20 @@ static GPBUnknownFieldSet *GetOrMakeUnknownFields(GPBMessage *self) {
} }
- (void)writeExtensionsToCodedOutputStream:(GPBCodedOutputStream *)output - (void)writeExtensionsToCodedOutputStream:(GPBCodedOutputStream *)output
range:(GPBExtensionRange)range { range:(GPBExtensionRange)range
NSArray *sortedExtensions = [[extensionMap_ allKeys] sortedExtensions:(NSArray *)sortedExtensions {
sortedArrayUsingSelector:@selector(compareByFieldNumber:)];
uint32_t start = range.start; uint32_t start = range.start;
uint32_t end = range.end; uint32_t end = range.end;
for (GPBExtensionDescriptor *extension in sortedExtensions) { for (GPBExtensionDescriptor *extension in sortedExtensions) {
uint32_t fieldNumber = extension.fieldNumber; uint32_t fieldNumber = extension.fieldNumber;
if (fieldNumber >= start && fieldNumber < end) { if (fieldNumber < start) {
id value = [extensionMap_ objectForKey:extension]; continue;
GPBWriteExtensionValueToOutputStream(extension, value, output); }
if (fieldNumber >= end) {
break;
} }
id value = [extensionMap_ objectForKey:extension];
GPBWriteExtensionValueToOutputStream(extension, value, output);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment