Skip to content

Commit

Permalink
fix: 减少dyld时机的runtime操作
Browse files Browse the repository at this point in the history
  • Loading branch information
CaicaiNo committed Nov 10, 2021
1 parent 1f90d5e commit 7ecfa73
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 64 deletions.
2 changes: 1 addition & 1 deletion GrowingAnalytics.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'GrowingAnalytics'
s.version = '3.3.1-hotfix.1'
s.version = '3.3.1-hotfix.2'
s.summary = 'iOS SDK of GrowingIO.'
s.description = <<-DESC
GrowingAnalytics具备自动采集基本的用户行为事件,比如访问和行为数据等。目前支持代码埋点、无埋点、可视化圈选、热图等功能。
Expand Down
23 changes: 11 additions & 12 deletions GrowingTrackerCore/Core/GrowingAnnotationCore.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#endif

NSArray<NSString *> *GrowingReadConfiguration(char *sectionName, const struct mach_header *mhp) {
NSMutableArray *configs = [NSMutableArray array];
unsigned long size = 0;
#ifndef __LP64__
uintptr_t *memory = (uintptr_t *)getsectiondata(mhp, SEG_DATA, sectionName, &size);
Expand All @@ -44,27 +43,28 @@
#endif

unsigned long counter = size / sizeof(void *);
if (counter == 0) {
return nil;
}
NSMutableArray *configs = [NSMutableArray array];
for (int idx = 0; idx < counter; ++idx) {
char *string = (char *)memory[idx];
NSString *str = [NSString stringWithUTF8String:string];
if (!str) continue;
GrowingLog(@"config = %@", str);
GrowingLog(@"[Growing] %@", str);
if (str) [configs addObject:str];
}

return configs;
}

static void dyld_callback(const struct mach_header *mhp, intptr_t vmaddr_slide) {

NSArray *mods = GrowingReadConfiguration(GrowingModSectName, mhp);
for (NSString *modName in mods) {
Class cls;
if (modName) {
cls = NSClassFromString(modName);

if (cls) {
[[GrowingModuleManager sharedInstance] registerDynamicModule:cls];
}
// 这里不进行 name -> class 转换,且只存储NSString
[[GrowingModuleManager sharedInstance] addLocalModule:modName];
}
}

Expand All @@ -78,16 +78,15 @@ static void dyld_callback(const struct mach_header *mhp, intptr_t vmaddr_slide)
if ([json isKindOfClass:[NSDictionary class]] && [json allKeys].count) {
NSString *protocol = [json allKeys][0];
NSString *clsName = [json allValues][0];

if (protocol && clsName) {
[[GrowingServiceManager sharedInstance] registerService:NSProtocolFromString(protocol)
implClass:NSClassFromString(clsName)];
// 这里不进行 name -> class 转换,且只存储NSString
[[GrowingServiceManager sharedInstance] registerServiceName:protocol implClassName:clsName];
}
}
}
}
}

// add callback before main()
__attribute__((constructor)) void GrowingInitProphet(void) {
_dyld_register_func_for_add_image(dyld_callback);
}
Expand Down
11 changes: 9 additions & 2 deletions GrowingTrackerCore/Core/GrowingModuleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ NS_ASSUME_NONNULL_BEGIN

+ (instancetype)sharedInstance;

#pragma mark - dyld期间添加module信息

- (void)addLocalModule:(NSString *)modulename;

#pragma mark - 调用方法
// If you do not comply with set Level protocol, the default Normal
// 将 class 存入Module Info,并不会初始化
- (void)registerDynamicModule:(Class)moduleClass;

- (void)unRegisterDynamicModule:(Class)moduleClass;

//- (void)loadLocalModules;

// 初始化各个Module Info
- (void)registedAllModules;

#pragma mark - 事件和module关联

- (void)registerCustomEvent:(NSInteger)eventType
withModuleInstance:(id)moduleInstance
andSelectorStr:(NSString *)selectorStr;
Expand Down
117 changes: 69 additions & 48 deletions GrowingTrackerCore/Core/GrowingModuleManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@


@interface GrowingModuleManager()
/// 存module名,load之后清空
@property(nonatomic, strong) NSMutableArray *growingModuleNames;
/// 存module class
@property(nonatomic, strong) NSMutableArray *growingModuleDynamicClasses;
/// 存封装有module class的info类
@property(nonatomic, strong) NSMutableArray<NSDictionary *> *growingModuleInfos;
@property(nonatomic, strong) NSMutableArray *growingModules;

@property(nonatomic, strong) NSMutableArray *GrowingModuleDynamicClasses;

@property(nonatomic, strong) NSMutableArray<NSDictionary *> *GrowingModuleInfos;
@property(nonatomic, strong) NSMutableArray *GrowingModules;

@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSMutableArray<id<GrowingModuleProtocol>> *> *GrowingModulesByEvent;
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *GrowingSelectorByEvent;
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSMutableArray<id<GrowingModuleProtocol>> *> *growingModulesByEvent;
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *growingSelectorByEvent;

@end

Expand All @@ -81,7 +83,18 @@ + (instancetype)sharedInstance
});
return sharedManager;
}
// 仅添加string,规避运行时转换class
- (void)addLocalModule:(NSString *)modulename {
[self.growingModuleNames addObject:modulename];
}

// 从存储的name数组中读取所有的module
- (void)loadLocalModules {
for (NSString *name in self.growingModuleNames) {
[self registerDynamicModule:NSClassFromString(name)];
}
[self.growingModuleNames removeAllObjects];
}

- (void)registerDynamicModule:(Class)moduleClass
{
Expand All @@ -93,18 +106,18 @@ - (void)unRegisterDynamicModule:(Class)moduleClass {
if (!moduleClass) {
return;
}
[self.GrowingModuleInfos filterUsingPredicate:[NSPredicate predicateWithFormat:@"%@!=%@", kModuleInfoNameKey, NSStringFromClass(moduleClass)]];
[self.growingModuleInfos filterUsingPredicate:[NSPredicate predicateWithFormat:@"%@!=%@", kModuleInfoNameKey, NSStringFromClass(moduleClass)]];
__block NSInteger index = -1;
[self.GrowingModules enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.growingModules enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:moduleClass]) {
index = idx;
*stop = YES;
}
}];
if (index >= 0) {
[self.GrowingModules removeObjectAtIndex:index];
[self.growingModules removeObjectAtIndex:index];
}
[self.GrowingModulesByEvent enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, NSMutableArray<id<GrowingModuleProtocol>> * _Nonnull obj, BOOL * _Nonnull stop) {
[self.growingModulesByEvent enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, NSMutableArray<id<GrowingModuleProtocol>> * _Nonnull obj, BOOL * _Nonnull stop) {
__block NSInteger index = -1;
[obj enumerateObjectsUsingBlock:^(id<GrowingModuleProtocol> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:moduleClass]) {
Expand All @@ -120,8 +133,8 @@ - (void)unRegisterDynamicModule:(Class)moduleClass {

- (void)registedAllModules
{

[self.GrowingModuleInfos sortUsingComparator:^NSComparisonResult(NSDictionary *module1, NSDictionary *module2) {
[self loadLocalModules];
[self.growingModuleInfos sortUsingComparator:^NSComparisonResult(NSDictionary *module1, NSDictionary *module2) {
NSNumber *module1Level = (NSNumber *)[module1 objectForKey:kModuleInfoLevelKey];
NSNumber *module2Level = (NSNumber *)[module2 objectForKey:kModuleInfoLevelKey];
if (module1Level.integerValue != module2Level.integerValue) {
Expand All @@ -136,7 +149,7 @@ - (void)registedAllModules
NSMutableArray *tmpArray = [NSMutableArray array];

//module init
[self.GrowingModuleInfos enumerateObjectsUsingBlock:^(NSDictionary *module, NSUInteger idx, BOOL * _Nonnull stop) {
[self.growingModuleInfos enumerateObjectsUsingBlock:^(NSDictionary *module, NSUInteger idx, BOOL * _Nonnull stop) {

NSString *classStr = [module objectForKey:kModuleInfoNameKey];

Expand All @@ -150,9 +163,9 @@ - (void)registedAllModules

}];

[self.GrowingModules removeAllObjects];
[self.growingModules removeAllObjects];

[self.GrowingModules addObjectsFromArray:tmpArray];
[self.growingModules addObjectsFromArray:tmpArray];

[self registerAllSystemEvents];
}
Expand Down Expand Up @@ -200,7 +213,7 @@ - (instancetype)init
{
self = [super init];
if (self) {
self.GrowingModuleDynamicClasses = [NSMutableArray array];
self.growingModuleDynamicClasses = [NSMutableArray array];
}
return self;
}
Expand Down Expand Up @@ -253,24 +266,24 @@ - (void)addModuleFromObject:(id)object
[moduleInfo setObject:moduleName forKey:kModuleInfoNameKey];
}

[self.GrowingModuleInfos addObject:moduleInfo];
[self.growingModuleInfos addObject:moduleInfo];

[moduleInfo setObject:@(NO) forKey:kModuleInfoHasInstantiatedKey];
}
}

- (void)registerAllSystemEvents
{
[self.GrowingModules enumerateObjectsUsingBlock:^(id<GrowingModuleProtocol> moduleInstance, NSUInteger idx, BOOL * _Nonnull stop) {
[self.growingModules enumerateObjectsUsingBlock:^(id<GrowingModuleProtocol> moduleInstance, NSUInteger idx, BOOL * _Nonnull stop) {
[self registerEventsByModuleInstance:moduleInstance];
}];
}

- (void)registerEventsByModuleInstance:(id<GrowingModuleProtocol>)moduleInstance
{
NSArray<NSNumber *> *events = self.GrowingSelectorByEvent.allKeys;
NSArray<NSNumber *> *events = self.growingSelectorByEvent.allKeys;
[events enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self registerEvent:obj.integerValue withModuleInstance:moduleInstance andSelectorStr:self.GrowingSelectorByEvent[obj]];
[self registerEvent:obj.integerValue withModuleInstance:moduleInstance andSelectorStr:self.growingSelectorByEvent[obj]];
}];
}

Expand All @@ -282,13 +295,13 @@ - (void)registerEvent:(NSInteger)eventType
return;
}
NSNumber *eventTypeNumber = @(eventType);
if (!self.GrowingSelectorByEvent[eventTypeNumber]) {
[self.GrowingSelectorByEvent setObject:selectorStr forKey:eventTypeNumber];
if (!self.growingSelectorByEvent[eventTypeNumber]) {
[self.growingSelectorByEvent setObject:selectorStr forKey:eventTypeNumber];
}
if (!self.GrowingModulesByEvent[eventTypeNumber]) {
[self.GrowingModulesByEvent setObject:@[].mutableCopy forKey:eventTypeNumber];
if (!self.growingModulesByEvent[eventTypeNumber]) {
[self.growingModulesByEvent setObject:@[].mutableCopy forKey:eventTypeNumber];
}
NSMutableArray *eventModules = [self.GrowingModulesByEvent objectForKey:eventTypeNumber];
NSMutableArray *eventModules = [self.growingModulesByEvent objectForKey:eventTypeNumber];
if (![eventModules containsObject:moduleInstance]) {
[eventModules addObject:moduleInstance];
[eventModules sortUsingComparator:^NSComparisonResult(id<GrowingModuleProtocol> moduleInstance1, id<GrowingModuleProtocol> moduleInstance2) {
Expand Down Expand Up @@ -318,33 +331,41 @@ - (void)registerEvent:(NSInteger)eventType
}

#pragma mark - property setter or getter
- (NSMutableArray<NSDictionary *> *)GrowingModuleInfos {
if (!_GrowingModuleInfos) {
_GrowingModuleInfos = @[].mutableCopy;

- (NSMutableArray *)growingModuleNames {
if (!_growingModuleNames) {
_growingModuleNames = @[].mutableCopy;
}
return _growingModuleNames;
}

- (NSMutableArray<NSDictionary *> *)growingModuleInfos {
if (!_growingModuleInfos) {
_growingModuleInfos = @[].mutableCopy;
}
return _GrowingModuleInfos;
return _growingModuleInfos;
}

- (NSMutableArray *)GrowingModules
- (NSMutableArray *)growingModules
{
if (!_GrowingModules) {
_GrowingModules = [NSMutableArray array];
if (!_growingModules) {
_growingModules = [NSMutableArray array];
}
return _GrowingModules;
return _growingModules;
}

- (NSMutableDictionary<NSNumber *, NSMutableArray<id<GrowingModuleProtocol>> *> *)GrowingModulesByEvent
- (NSMutableDictionary<NSNumber *, NSMutableArray<id<GrowingModuleProtocol>> *> *)growingModulesByEvent
{
if (!_GrowingModulesByEvent) {
_GrowingModulesByEvent = @{}.mutableCopy;
if (!_growingModulesByEvent) {
_growingModulesByEvent = @{}.mutableCopy;
}
return _GrowingModulesByEvent;
return _growingModulesByEvent;
}

- (NSMutableDictionary<NSNumber *, NSString *> *)GrowingSelectorByEvent
- (NSMutableDictionary<NSNumber *, NSString *> *)growingSelectorByEvent
{
if (!_GrowingSelectorByEvent) {
_GrowingSelectorByEvent = @{
if (!_growingSelectorByEvent) {
_growingSelectorByEvent = @{
@(GrowingMSetupEvent):kSetupSelector,
@(GrowingMInitEvent):kInitSelector,
@(GrowingMTearDownEvent):kTearDownSelector,
Expand Down Expand Up @@ -380,7 +401,7 @@ - (NSMutableArray *)GrowingModules
@(GrowingMDidCustomEvent):kAppCustomSelector,
}.mutableCopy;
}
return _GrowingSelectorByEvent;
return _growingSelectorByEvent;
}

#pragma mark - module protocol
Expand All @@ -398,7 +419,7 @@ - (void)handleModuleEvent:(NSInteger)eventType
[self handleModulesTearDownEventForTarget:nil withCustomParam:customParam];
break;
default: {
NSString *selectorStr = [self.GrowingSelectorByEvent objectForKey:@(eventType)];
NSString *selectorStr = [self.growingSelectorByEvent objectForKey:@(eventType)];
[self handleModuleEvent:eventType forTarget:nil withSeletorStr:selectorStr andCustomParam:customParam];
}
break;
Expand All @@ -417,7 +438,7 @@ - (void)handleModulesInitEventForTarget:(id<GrowingModuleProtocol>)target
if (target) {
moduleInstances = @[target];
} else {
moduleInstances = [self.GrowingModulesByEvent objectForKey:@(GrowingMInitEvent)];
moduleInstances = [self.growingModulesByEvent objectForKey:@(GrowingMInitEvent)];
}

[moduleInstances enumerateObjectsUsingBlock:^(id<GrowingModuleProtocol> moduleInstance, NSUInteger idx, BOOL * _Nonnull stop) {
Expand Down Expand Up @@ -460,7 +481,7 @@ - (void)handleModulesTearDownEventForTarget:(id<GrowingModuleProtocol>)target
if (target) {
moduleInstances = @[target];
} else {
moduleInstances = [self.GrowingModulesByEvent objectForKey:@(GrowingMTearDownEvent)];
moduleInstances = [self.growingModulesByEvent objectForKey:@(GrowingMTearDownEvent)];
}

//Reverse Order to unload
Expand All @@ -478,18 +499,18 @@ - (void)handleModuleEvent:(NSInteger)eventType
andCustomParam:(NSDictionary *)customParam
{
if (!selectorStr.length) {
selectorStr = [self.GrowingSelectorByEvent objectForKey:@(eventType)];
selectorStr = [self.growingSelectorByEvent objectForKey:@(eventType)];
}
SEL seletor = NSSelectorFromString(selectorStr);
if (!seletor) {
selectorStr = [self.GrowingSelectorByEvent objectForKey:@(eventType)];
selectorStr = [self.growingSelectorByEvent objectForKey:@(eventType)];
seletor = NSSelectorFromString(selectorStr);
}
NSArray<id<GrowingModuleProtocol>> *moduleInstances;
if (target) {
moduleInstances = @[target];
} else {
moduleInstances = [self.GrowingModulesByEvent objectForKey:@(eventType)];
moduleInstances = [self.growingModulesByEvent objectForKey:@(eventType)];
}
[moduleInstances enumerateObjectsUsingBlock:^(id<GrowingModuleProtocol> moduleInstance, NSUInteger idx, BOOL * _Nonnull stop) {
if ([moduleInstance respondsToSelector:seletor]) {
Expand Down
2 changes: 2 additions & 0 deletions GrowingTrackerCore/Core/GrowingServiceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ NS_ASSUME_NONNULL_BEGIN

+ (instancetype)sharedInstance;

- (void)registerServiceName:(NSString *)serviceName implClassName:(NSString *)serviceClassName;

- (void)registerService:(Protocol*)service implClass:(Class)serviceClass;

- (id)createService:(Protocol*)service;
Expand Down
6 changes: 6 additions & 0 deletions GrowingTrackerCore/Core/GrowingServiceManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ - (instancetype)init {

#pragma mark - private

- (void)registerServiceName:(NSString *)serviceName implClassName:(NSString *)serviceClassName {
dispatch_semaphore_wait(_signallock, DISPATCH_TIME_FOREVER);
[_allServiceDict setValue:serviceClassName forKey:serviceName];
dispatch_semaphore_signal(_signallock);
}

- (void)registerService:(Protocol*)service implClass:(Class)serviceClass {
dispatch_semaphore_wait(_signallock, DISPATCH_TIME_FOREVER);
[_allServiceDict setValue:NSStringFromClass(serviceClass) forKey:NSStringFromProtocol(service)];
Expand Down
2 changes: 1 addition & 1 deletion GrowingTrackerCore/GrowingRealTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#import "GrowingModuleManager.h"
#import "GrowingEventManager.h"

NSString *const GrowingTrackerVersionName = @"3.3.1-hotfix.1";
NSString *const GrowingTrackerVersionName = @"3.3.1-hotfix.2";
const int GrowingTrackerVersionCode = 30301;

@interface GrowingRealTracker ()
Expand Down

0 comments on commit 7ecfa73

Please sign in to comment.