Skip to content

Commit

Permalink
Performance improvement:
Browse files Browse the repository at this point in the history
1. remove `className` in warpObj()
2. use NSRecursiveLock instead of @synchronized
3. remove the unnecessary lock
  • Loading branch information
bang590 committed Aug 31, 2015
1 parent 43ebe7d commit 72717d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
45 changes: 23 additions & 22 deletions JSPatch/JPEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ + (void)startEngine
return defineClass(classDeclaration, instanceMethods, classMethods);
};

context[@"_OC_callI"] = ^id(NSString *className, JSValue *obj, NSString *selectorName, JSValue *arguments, BOOL isSuper) {
return callSelector(className, selectorName, arguments, obj, isSuper);
context[@"_OC_callI"] = ^id(JSValue *obj, NSString *selectorName, JSValue *arguments, BOOL isSuper) {
return callSelector(nil, selectorName, arguments, obj, isSuper);
};
context[@"_OC_callC"] = ^id(NSString *className, NSString *selectorName, JSValue *arguments) {
return callSelector(className, selectorName, arguments, nil, NO);
Expand Down Expand Up @@ -168,16 +168,18 @@ + (void)startEngine
NSAssert(NO, @"js exception: %@", exception);
};


_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;

_nilObj = [[NSObject alloc] init];
_JSMethodSignatureLock = [[NSLock alloc] init];
_JSMethodForwardCallLock = [[NSRecursiveLock alloc] init];
registeredStruct = [[NSMutableDictionary alloc] init];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];

NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"JSPatch" ofType:@"js"];
NSAssert(path, @"can't find JSPatch.js");
NSString *jsCore = [[NSString alloc] initWithData:[[NSFileManager defaultManager] contentsAtPath:path] encoding:NSUTF8StringEncoding];
Expand Down Expand Up @@ -237,11 +239,9 @@ + (void)addExtensions:(NSArray *)extensions
return;
}
NSAssert(_context, @"please call [JPEngine startEngine]");
@synchronized (_context) {
for (NSString *className in extensions) {
Class extCls = NSClassFromString(className);
[extCls main:_context];
}
for (NSString *className in extensions) {
Class extCls = NSClassFromString(className);
[extCls main:_context];
}
}

Expand All @@ -258,9 +258,9 @@ + (NSMutableDictionary *)registeredStruct
}

+ (void)handleMemoryWarning {
@synchronized(_JSMethodSignatureCache) {
_JSMethodSignatureCache = nil;
}
[_JSMethodSignatureLock lock];
_JSMethodSignatureCache = nil;
[_JSMethodSignatureLock unlock];
}

#pragma mark - Implements
Expand All @@ -271,6 +271,7 @@ + (void)handleMemoryWarning {
static NSMutableDictionary *_propKeys;
static NSMutableDictionary *_JSMethodSignatureCache;
static NSLock *_JSMethodSignatureLock;
static NSRecursiveLock *_JSMethodForwardCallLock;

static const void *propKey(NSString *propName) {
if (!_propKeys) _propKeys = [[NSMutableDictionary alloc] init];
Expand Down Expand Up @@ -527,9 +528,9 @@ static void JPForwardInvocation(id slf, SEL selector, NSInvocation *invocation)
#define JP_FWD_RET_CALL_JS \
JSValue *fun = getJSFunctionInObjectHierachy(slf, JPSelectorName); \
JSValue *jsval; \
@synchronized(_context) { \
jsval = [fun callWithArguments:params];\
}
[_JSMethodForwardCallLock lock]; \
jsval = [fun callWithArguments:params]; \
[_JSMethodForwardCallLock unlock];

#define JP_FWD_RET_CASE_RET(_typeChar, _type, _retCode) \
case _typeChar : { \
Expand Down Expand Up @@ -727,13 +728,13 @@ static id callSelector(NSString *className, NSString *selectorName, JSValue *arg
}
if (instance) {
[_JSMethodSignatureLock lock];
if (!_JSMethodSignatureCache[className]) {
_JSMethodSignatureCache[className] = [[NSMutableDictionary alloc]init];
if (!_JSMethodSignatureCache[cls]) {
_JSMethodSignatureCache[(id<NSCopying>)cls] = [[NSMutableDictionary alloc]init];
}
methodSignature = _JSMethodSignatureCache[className][selectorName];
methodSignature = _JSMethodSignatureCache[cls][selectorName];
if (!methodSignature) {
methodSignature = [cls instanceMethodSignatureForSelector:selector];
_JSMethodSignatureCache[className][selectorName] = methodSignature;
_JSMethodSignatureCache[cls][selectorName] = methodSignature;
}
[_JSMethodSignatureLock unlock];
NSCAssert(methodSignature, @"unrecognized selector %@ for instance %@", selectorName, instance);
Expand Down Expand Up @@ -1251,7 +1252,7 @@ static id _formatOCToJSList(NSArray *list)
if (!obj || obj == _nilObj) {
return @{@"__isNil": @(YES)};
}
return @{@"__clsName": NSStringFromClass([obj class]), @"__obj": obj};
return @{@"__obj": obj};
}

static id _unboxOCObjectToJS(id obj)
Expand Down
2 changes: 1 addition & 1 deletion JSPatch/JSPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var global = this
selectorName += ":"
}
}
var ret = instance ? _OC_callI(clsName, instance, selectorName, args, isSuper):
var ret = instance ? _OC_callI(instance, selectorName, args, isSuper):
_OC_callC(clsName, selectorName, args)
return _formatOCToJS(ret)
}
Expand Down

0 comments on commit 72717d8

Please sign in to comment.