-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modifying lock to improve code performance #667
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ | |
@interface MJProperty() | ||
@property (strong, nonatomic) NSMutableDictionary *propertyKeysDict; | ||
@property (strong, nonatomic) NSMutableDictionary *objectClassInArrayDict; | ||
@property (strong, nonatomic) dispatch_semaphore_t propertyKeysLock; | ||
@property (strong, nonatomic) dispatch_semaphore_t objectClassInArrayLock; | ||
@end | ||
|
||
@implementation MJProperty | ||
|
@@ -24,22 +26,21 @@ - (instancetype)init | |
if (self = [super init]) { | ||
_propertyKeysDict = [NSMutableDictionary dictionary]; | ||
_objectClassInArrayDict = [NSMutableDictionary dictionary]; | ||
_propertyKeysLock = dispatch_semaphore_create(1); | ||
_objectClassInArrayLock = dispatch_semaphore_create(1); | ||
} | ||
return self; | ||
} | ||
|
||
#pragma mark - 缓存 | ||
+ (instancetype)cachedPropertyWithProperty:(objc_property_t)property | ||
{ | ||
MJExtensionSemaphoreCreate | ||
MJExtensionSemaphoreWait | ||
MJProperty *propertyObj = objc_getAssociatedObject(self, property); | ||
if (propertyObj == nil) { | ||
propertyObj = [[self alloc] init]; | ||
propertyObj.property = property; | ||
objc_setAssociatedObject(self, property, propertyObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | ||
} | ||
MJExtensionSemaphoreSignal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MJPropertySemaphoreCreate
MJ_LOCK(_propertySemaphore);
NSArray *cachedProperties = [self properties];
MJ_UNLOCK(_propertySemaphore); 在 |
||
return propertyObj; | ||
} | ||
|
||
|
@@ -156,17 +157,20 @@ - (void)setPorpertyKeys:(NSArray *)propertyKeys forClass:(Class)c | |
NSString *key = NSStringFromClass(c); | ||
if (!key) return; | ||
|
||
MJExtensionSemaphoreCreate | ||
MJExtensionSemaphoreWait | ||
MJ_LOCK(self.propertyKeysLock); | ||
self.propertyKeysDict[key] = propertyKeys; | ||
MJExtensionSemaphoreSignal | ||
MJ_UNLOCK(self.propertyKeysLock); | ||
} | ||
|
||
- (NSArray *)propertyKeysForClass:(Class)c | ||
{ | ||
NSString *key = NSStringFromClass(c); | ||
if (!key) return nil; | ||
return self.propertyKeysDict[key]; | ||
|
||
MJ_LOCK(self.propertyKeysLock); | ||
NSArray *propertyKeys = self.propertyKeysDict[key]; | ||
MJ_UNLOCK(self.propertyKeysLock); | ||
return propertyKeys; | ||
} | ||
|
||
/** 模型数组中的模型类型 */ | ||
|
@@ -176,16 +180,19 @@ - (void)setObjectClassInArray:(Class)objectClass forClass:(Class)c | |
NSString *key = NSStringFromClass(c); | ||
if (!key) return; | ||
|
||
MJExtensionSemaphoreCreate | ||
MJExtensionSemaphoreWait | ||
MJ_LOCK(self.objectClassInArrayLock); | ||
self.objectClassInArrayDict[key] = objectClass; | ||
MJExtensionSemaphoreSignal | ||
MJ_UNLOCK(self.objectClassInArrayLock); | ||
} | ||
|
||
- (Class)objectClassInArrayForClass:(Class)c | ||
{ | ||
NSString *key = NSStringFromClass(c); | ||
if (!key) return nil; | ||
return self.objectClassInArrayDict[key]; | ||
|
||
MJ_LOCK(self.objectClassInArrayLock); | ||
Class objectClass = self.objectClassInArrayDict[key]; | ||
MJ_UNLOCK(self.objectClassInArrayLock); | ||
return objectClass; | ||
} | ||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,15 +23,12 @@ + (instancetype)cachedTypeWithCode:(NSString *)code | |
types = [NSMutableDictionary dictionary]; | ||
}); | ||
|
||
MJExtensionSemaphoreCreate | ||
MJExtensionSemaphoreWait | ||
MJPropertyType *type = types[code]; | ||
if (type == nil) { | ||
type = [[self alloc] init]; | ||
type.code = code; | ||
types[code] = type; | ||
} | ||
MJExtensionSemaphoreSignal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上 |
||
return type; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,13 @@ | |
#import "MJFoundation.h" | ||
#import <objc/runtime.h> | ||
|
||
#define MJClassSemaphoreCreate \ | ||
static dispatch_semaphore_t _classSemaphore; \ | ||
static dispatch_once_t onceTokenSemaphore; \ | ||
dispatch_once(&onceTokenSemaphore, ^{ \ | ||
_classSemaphore = dispatch_semaphore_create(1); \ | ||
}); | ||
|
||
static const char MJAllowedPropertyNamesKey = '\0'; | ||
static const char MJIgnoredPropertyNamesKey = '\0'; | ||
static const char MJAllowedCodingPropertyNamesKey = '\0'; | ||
|
@@ -139,17 +146,16 @@ + (void)mj_setupBlockReturnValue:(id (^)(void))block key:(const char *)key | |
} | ||
|
||
// 清空数据 | ||
MJExtensionSemaphoreCreate | ||
MJExtensionSemaphoreWait | ||
MJClassSemaphoreCreate | ||
MJ_LOCK(_classSemaphore); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
[[self classDictForKey:key] removeAllObjects]; | ||
MJExtensionSemaphoreSignal | ||
MJ_UNLOCK(_classSemaphore); | ||
} | ||
|
||
+ (NSMutableArray *)mj_totalObjectsWithSelector:(SEL)selector key:(const char *)key | ||
{ | ||
MJExtensionSemaphoreCreate | ||
MJExtensionSemaphoreWait | ||
|
||
MJClassSemaphoreCreate | ||
MJ_LOCK(_classSemaphore); | ||
NSMutableArray *array = [self classDictForKey:key][NSStringFromClass(self)]; | ||
if (array == nil) { | ||
// 创建、存储 | ||
|
@@ -170,9 +176,7 @@ + (NSMutableArray *)mj_totalObjectsWithSelector:(SEL)selector key:(const char *) | |
[array addObjectsFromArray:subArray]; | ||
}]; | ||
} | ||
|
||
MJExtensionSemaphoreSignal | ||
|
||
MJ_UNLOCK(_classSemaphore); | ||
return array; | ||
} | ||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面删掉的代码在其他类里的改动和放在这里应该是一样的. define 宏定义只是在编译阶段替换而已.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用
MJ_LOCK ()
主要原因是不使用全局的一把锁,在每个对应的功能点加锁,后期如果还有线程安全相关问题,使用自己定义的锁,提高锁的效率:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
主要目的是不使用全局的那一把锁