Skip to content

Commit

Permalink
Merge pull request bang590#86 from albert438/master
Browse files Browse the repository at this point in the history
Fix: Add Cache for MethodSignature
  • Loading branch information
bang590 committed Aug 21, 2015
2 parents fc70d3c + 3842b2d commit 1d688c6
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions JSPatch/JPEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ + (void)startEngine
_nullObj = [[NSObject alloc] init];
_nilObj = [[NSObject alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
_JSMethodSignatureLock = [[NSLock alloc]init];
context[@"_OC_null"] = formatOCToJS(_nullObj);

_context = context;
Expand Down Expand Up @@ -247,6 +248,7 @@ + (void)handleMemoryWarning {
static NSRegularExpression *countArgRegex;
static NSMutableDictionary *_propKeys;
static NSMutableDictionary *_JSMethodSignatureCache;
static NSLock *_JSMethodSignatureLock;

static const void *propKey(NSString *propName) {
if (!_propKeys) _propKeys = [[NSMutableDictionary alloc] init];
Expand Down Expand Up @@ -706,16 +708,16 @@ static id callSelector(NSString *className, NSString *selectorName, JSValue *arg
_JSMethodSignatureCache = [[NSMutableDictionary alloc]init];
}
if (instance) {
@synchronized(_JSMethodSignatureCache) {
if (!_JSMethodSignatureCache[className]) {
_JSMethodSignatureCache[className] = [[NSMutableDictionary alloc]init];
}
methodSignature = _JSMethodSignatureCache[className][selectorName];
if (!methodSignature) {
methodSignature = [cls instanceMethodSignatureForSelector:selector];
_JSMethodSignatureCache[className][selectorName] = methodSignature;
}
[_JSMethodSignatureLock lock];
if (!_JSMethodSignatureCache[className]) {
_JSMethodSignatureCache[className] = [[NSMutableDictionary alloc]init];
}
methodSignature = _JSMethodSignatureCache[className][selectorName];
if (!methodSignature) {
methodSignature = [cls instanceMethodSignatureForSelector:selector];
_JSMethodSignatureCache[className][selectorName] = methodSignature;
}
[_JSMethodSignatureLock unlock];
NSCAssert(methodSignature, @"unrecognized selector %@ for instance %@", selectorName, instance);
invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:instance];
Expand Down

0 comments on commit 1d688c6

Please sign in to comment.