Skip to content

Commit

Permalink
Replaced use of NSInvocation with direct IMP calls for performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkaplan committed May 18, 2015
1 parent 79ae468 commit 097177c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 29 deletions.
21 changes: 7 additions & 14 deletions Mantle/MTLJSONAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,9 @@ + (NSDictionary *)valueTransformersForModelClass:(Class)modelClass {
for (NSString *key in [modelClass propertyKeys]) {
SEL selector = MTLSelectorWithKeyPattern(key, "JSONTransformer");
if ([modelClass respondsToSelector:selector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[modelClass methodSignatureForSelector:selector]];
invocation.target = modelClass;
invocation.selector = selector;
[invocation invoke];

__unsafe_unretained id transformer = nil;
[invocation getReturnValue:&transformer];
IMP imp = [modelClass methodForSelector:selector];
NSValueTransformer*(*function)(id, SEL) = (void *)imp;
__unsafe_unretained NSValueTransformer *transformer = function(modelClass, selector);

if (transformer != nil) result[key] = transformer;

Expand Down Expand Up @@ -449,13 +445,10 @@ + (NSValueTransformer *)transformerForModelPropertiesOfClass:(Class)modelClass {
SEL selector = MTLSelectorWithKeyPattern(NSStringFromClass(modelClass), "JSONTransformer");
if (![self respondsToSelector:selector]) return nil;

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:selector]];
invocation.target = self;
invocation.selector = selector;
[invocation invoke];

__unsafe_unretained id result = nil;
[invocation getReturnValue:&result];
IMP imp = [self methodForSelector:selector];
NSValueTransformer*(*function)(id, SEL) = (void *)imp;
__unsafe_unretained NSValueTransformer *result = function(self, selector);

return result;
}

Expand Down
13 changes: 4 additions & 9 deletions Mantle/MTLModel+NSCoding.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,10 @@ - (id)decodeValueForKey:(NSString *)key withCoder:(NSCoder *)coder modelVersion:

SEL selector = MTLSelectorWithCapitalizedKeyPattern("decode", key, "WithCoder:modelVersion:");
if ([self respondsToSelector:selector]) {
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:selector]];
invocation.target = self;
invocation.selector = selector;
[invocation setArgument:&coder atIndex:2];
[invocation setArgument:&modelVersion atIndex:3];
[invocation invoke];

__unsafe_unretained id result = nil;
[invocation getReturnValue:&result];
IMP imp = [self methodForSelector:selector];
id(*function)(id, SEL, id, NSUInteger) = (void *)imp;
__unsafe_unretained id result = function(self, selector, coder, modelVersion);

return result;
}

Expand Down
9 changes: 3 additions & 6 deletions Mantle/MTLModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,9 @@ - (void)mergeValueForKey:(NSString *)key fromModel:(NSObject<MTLModel> *)model {
return;
}

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:selector]];
invocation.target = self;
invocation.selector = selector;

[invocation setArgument:&model atIndex:2];
[invocation invoke];
IMP imp = [self methodForSelector:selector];
void(*function)(id, SEL, id) = (void *)imp;
function(self, selector, model);
}

- (void)mergeValuesForKeysFromModel:(id<MTLModel>)model {
Expand Down

0 comments on commit 097177c

Please sign in to comment.