From 324dc7bbae1d5746f8c5d27e0b4399c560ba2718 Mon Sep 17 00:00:00 2001 From: Dhiogo Brustolin Date: Mon, 25 Sep 2023 10:03:34 +0200 Subject: [PATCH] ref: Reduce complexity for SentryDependencyContainer (#3294) Making SentryDependencyContainer less complex and more predictable by removing some unnecessary lazy loads. The goal would be to have all necessary objects initialized according to SentryOptions and remove all the @syncronized locks. --- Sources/Sentry/SentryDependencyContainer.m | 94 ++++------------------ 1 file changed, 15 insertions(+), 79 deletions(-) diff --git a/Sources/Sentry/SentryDependencyContainer.m b/Sources/Sentry/SentryDependencyContainer.m index 778755882f6..c5e3e02be4b 100644 --- a/Sources/Sentry/SentryDependencyContainer.m +++ b/Sources/Sentry/SentryDependencyContainer.m @@ -48,24 +48,31 @@ + (void)initialize { if (self == [SentryDependencyContainer class]) { sentryDependencyContainerLock = [[NSObject alloc] init]; + instance = [[SentryDependencyContainer alloc] init]; } } + (instancetype)sharedInstance { - @synchronized(sentryDependencyContainerLock) { - if (instance == nil) { - instance = [[self alloc] init]; - } - return instance; - } + return instance; } + (void)reset { - @synchronized(sentryDependencyContainerLock) { - instance = nil; + instance = [[SentryDependencyContainer alloc] init]; +} + +- (instancetype)init +{ + if (self = [super init]) { + _dispatchQueueWrapper = [[SentryDispatchQueueWrapper alloc] init]; + _random = [[SentryRandom alloc] init]; + _threadWrapper = [[SentryThreadWrapper alloc] init]; + _binaryImageCache = [[SentryBinaryImageCache alloc] init]; + _debugImageProvider = [[SentryDebugImageProvider alloc] init]; + _dateProvider = [[SentryCurrentDateProvider alloc] init]; } + return self; } - (SentryFileManager *)fileManager @@ -130,28 +137,6 @@ - (SentryExtraContextProvider *)extraContextProvider return _extraContextProvider; } -- (SentryThreadWrapper *)threadWrapper -{ - if (_threadWrapper == nil) { - @synchronized(sentryDependencyContainerLock) { - if (_threadWrapper == nil) { - _threadWrapper = [[SentryThreadWrapper alloc] init]; - } - } - } - return _threadWrapper; -} - -- (SentryDispatchQueueWrapper *)dispatchQueueWrapper -{ - @synchronized(sentryDependencyContainerLock) { - if (_dispatchQueueWrapper == nil) { - _dispatchQueueWrapper = [[SentryDispatchQueueWrapper alloc] init]; - } - return _dispatchQueueWrapper; - } -} - - (SentryNSNotificationCenterWrapper *)notificationCenterWrapper { @synchronized(sentryDependencyContainerLock) { @@ -162,30 +147,6 @@ - (SentryNSNotificationCenterWrapper *)notificationCenterWrapper } } -- (id)random -{ - if (_random == nil) { - @synchronized(sentryDependencyContainerLock) { - if (_random == nil) { - _random = [[SentryRandom alloc] init]; - } - } - } - return _random; -} - -- (SentryBinaryImageCache *)binaryImageCache -{ - if (_binaryImageCache == nil) { - @synchronized(sentryDependencyContainerLock) { - if (_binaryImageCache == nil) { - _binaryImageCache = [[SentryBinaryImageCache alloc] init]; - } - } - } - return _binaryImageCache; -} - #if TARGET_OS_IOS - (SentryUIDeviceWrapper *)uiDeviceWrapper { @@ -263,19 +224,6 @@ - (SentrySwizzleWrapper *)swizzleWrapper return _swizzleWrapper; } -- (SentryDebugImageProvider *)debugImageProvider -{ - if (_debugImageProvider == nil) { - @synchronized(sentryDependencyContainerLock) { - if (_debugImageProvider == nil) { - _debugImageProvider = [[SentryDebugImageProvider alloc] init]; - } - } - } - - return _debugImageProvider; -} - - (SentryANRTracker *)getANRTracker:(NSTimeInterval)timeout { if (_anrTracker == nil) { @@ -341,18 +289,6 @@ - (SentryNSTimerFactory *)timerFactory return _timerFactory; } -- (SentryCurrentDateProvider *)dateProvider -{ - if (_dateProvider == nil) { - @synchronized(sentryDependencyContainerLock) { - if (_dateProvider == nil) { - _dateProvider = [[SentryCurrentDateProvider alloc] init]; - } - } - } - return _dateProvider; -} - #if SENTRY_HAS_METRIC_KIT - (SentryMXManager *)metricKitManager {