react-native/React/Base/RCTPerformanceLogger.m

130 lines
2.8 KiB
Mathematica
Raw Normal View History

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <QuartzCore/QuartzCore.h>
#import "RCTPerformanceLogger.h"
#import "RCTRootView.h"
#import "RCTLog.h"
#import "RCTProfile.h"
@interface RCTPerformanceLogger ()
{
int64_t _data[RCTPLSize][2];
NSUInteger _cookies[RCTPLSize];
}
@property (nonatomic, copy) NSArray<NSString *> *labelsForTags;
@end
@implementation RCTPerformanceLogger
- (instancetype)init
{
if (self = [super init]) {
_labelsForTags = @[
@"ScriptDownload",
@"ScriptExecution",
@"RAMBundleLoad",
@"RAMStartupCodeSize",
@"RAMStartupNativeRequires",
@"RAMStartupNativeRequiresCount",
@"RAMNativeRequires",
@"RAMNativeRequiresCount",
@"NativeModuleInit",
@"NativeModuleMainThread",
@"NativeModulePrepareConfig",
@"NativeModuleInjectConfig",
@"NativeModuleMainThreadUsesCount",
@"JSCWrapperOpenLibrary",
@"JSCExecutorSetup",
@"BridgeStartup",
@"RootViewTTI",
@"BundleSize",
];
}
return self;
}
- (void)markStartForTag:(RCTPLTag)tag
{
#if RCT_PROFILE
if (RCTProfileIsProfiling()) {
NSString *label = _labelsForTags[tag];
_cookies[tag] = RCTProfileBeginAsyncEvent(RCTProfileTagAlways, label, nil);
}
#endif
_data[tag][0] = CACurrentMediaTime() * 1000;
_data[tag][1] = 0;
}
- (void)markStopForTag:(RCTPLTag)tag
{
#if RCT_PROFILE
if (RCTProfileIsProfiling()) {
NSString *label =_labelsForTags[tag];
RCTProfileEndAsyncEvent(RCTProfileTagAlways, @"native", _cookies[tag], label, @"RCTPerformanceLogger");
}
#endif
if (_data[tag][0] != 0 && _data[tag][1] == 0) {
_data[tag][1] = CACurrentMediaTime() * 1000;
} else {
RCTLogInfo(@"Unbalanced calls start/end for tag %li", (unsigned long)tag);
}
}
- (void)setValue:(int64_t)value forTag:(RCTPLTag)tag
{
_data[tag][0] = 0;
_data[tag][1] = value;
}
- (void)addValue:(int64_t)value forTag:(RCTPLTag)tag
{
_data[tag][0] = 0;
_data[tag][1] += value;
}
- (void)appendStartForTag:(RCTPLTag)tag
{
_data[tag][0] = CACurrentMediaTime() * 1000;
}
Refactored module access to allow for lazy loading Summary: public The `bridge.modules` dictionary provides access to all native modules, but this API requires that every module is initialized in advance so that any module can be accessed. This diff introduces a better API that will allow modules to be initialized lazily as they are needed, and deprecates `bridge.modules` (modules that use it will still work, but should be rewritten to use `bridge.moduleClasses` or `-[bridge moduleForName/Class:` instead. The rules are now as follows: * Any module that overrides `init` or `setBridge:` will be initialized on the main thread when the bridge is created * Any module that implements `constantsToExport:` will be initialized later when the config is exported (the module itself will be initialized on a background queue, but `constantsToExport:` will still be called on the main thread. * All other modules will be initialized lazily when a method is first called on them. These rules may seem slightly arcane, but they have the advantage of not violating any assumptions that may have been made by existing code - any module written under the original assumption that it would be initialized synchronously on the main thread when the bridge is created should still function exactly the same, but modules that avoid overriding `init` or `setBridge:` will now be loaded lazily. I've rewritten most of the standard modules to take advantage of this new lazy loading, with the following results: Out of the 65 modules included in UIExplorer: * 16 are initialized on the main thread when the bridge is created * A further 8 are initialized when the config is exported to JS * The remaining 41 will be initialized lazily on-demand Reviewed By: jspahrsummers Differential Revision: D2677695 fb-gh-sync-id: 507ae7e9fd6b563e89292c7371767c978e928f33
2015-11-25 11:09:00 +00:00
- (void)appendStopForTag:(RCTPLTag)tag
{
if (_data[tag][0] != 0) {
_data[tag][1] += CACurrentMediaTime() * 1000 - _data[tag][0];
_data[tag][0] = 0;
} else {
RCTLogInfo(@"Unbalanced calls start/end for tag %li", (unsigned long)tag);
}
}
- (NSArray<NSNumber *> *)valuesForTags
{
NSMutableArray *result = [NSMutableArray array];
for (NSUInteger index = 0; index < RCTPLSize; index++) {
[result addObject:@(_data[index][0])];
[result addObject:@(_data[index][1])];
}
return result;
}
- (int64_t)durationForTag:(RCTPLTag)tag
{
return _data[tag][1] - _data[tag][0];
}
- (int64_t)valueForTag:(RCTPLTag)tag
{
return _data[tag][1];
}
@end