Skip to content

Commit

Permalink
Make experiment checks faster (#1393)
Browse files Browse the repository at this point in the history
* Make experiment checks use dispatch_once when not debugging, clean up singleton

* One more

* Pull out the variable
  • Loading branch information
Adlai-Holler authored Mar 12, 2019
1 parent 6f44c39 commit 5490d4d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
16 changes: 15 additions & 1 deletion Source/ASConfigurationInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,21 @@ NS_ASSUME_NONNULL_BEGIN
*
* The delegate will be notified asynchronously.
*/
AS_EXTERN BOOL ASActivateExperimentalFeature(ASExperimentalFeatures option);
#if DEBUG
#define ASActivateExperimentalFeature(opt) _ASActivateExperimentalFeature(opt)
#else
#define ASActivateExperimentalFeature(opt) ({\
static BOOL result;\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{ result = _ASActivateExperimentalFeature(opt); });\
result;\
})
#endif

/**
* Internal function. Use the macro without the underbar.
*/
AS_EXTERN BOOL _ASActivateExperimentalFeature(ASExperimentalFeatures option);

/**
* Notify the configuration delegate that the framework initialized, if needed.
Expand Down
29 changes: 13 additions & 16 deletions Source/ASConfigurationInternal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
#import <AsyncDisplayKit/ASConfigurationDelegate.h>
#import <stdatomic.h>

#define ASGetSharedConfigMgr() (__bridge ASConfigurationManager *)ASConfigurationManager.sharedInstance
static ASConfigurationManager *ASSharedConfigurationManager;
static dispatch_once_t ASSharedConfigurationManagerOnceToken;

NS_INLINE ASConfigurationManager *ASConfigurationManagerGet() {
dispatch_once(&ASSharedConfigurationManagerOnceToken, ^{
ASSharedConfigurationManager = [[ASConfigurationManager alloc] init];
});
return ASSharedConfigurationManager;
}

@implementation ASConfigurationManager {
ASConfiguration *_config;
Expand All @@ -21,17 +29,6 @@ @implementation ASConfigurationManager {
_Atomic(ASExperimentalFeatures) _activatedExperiments;
}

/// Return CFTypeRef to avoid retain/release on this singleton.
+ (CFTypeRef)sharedInstance
{
static CFTypeRef inst;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
inst = (__bridge_retained CFTypeRef)[[ASConfigurationManager alloc] init];
});
return inst;
}

+ (ASConfiguration *)defaultConfiguration NS_RETURNS_RETAINED
{
ASConfiguration *config = [[ASConfiguration alloc] init];
Expand Down Expand Up @@ -96,19 +93,19 @@ - (BOOL)activateExperimentalFeature:(ASExperimentalFeatures)requested
// Define this even when !DEBUG, since we may run our tests in release mode.
+ (void)test_resetWithConfiguration:(ASConfiguration *)configuration
{
ASConfigurationManager *inst = ASGetSharedConfigMgr();
ASConfigurationManager *inst = ASConfigurationManagerGet();
inst->_config = configuration ?: [self defaultConfiguration];
atomic_store(&inst->_activatedExperiments, 0);
}

@end

BOOL ASActivateExperimentalFeature(ASExperimentalFeatures feature)
BOOL _ASActivateExperimentalFeature(ASExperimentalFeatures feature)
{
return [ASGetSharedConfigMgr() activateExperimentalFeature:feature];
return [ASConfigurationManagerGet() activateExperimentalFeature:feature];
}

void ASNotifyInitialized()
{
[ASGetSharedConfigMgr() frameworkDidInitialize];
[ASConfigurationManagerGet() frameworkDidInitialize];
}

0 comments on commit 5490d4d

Please sign in to comment.