Skip to content

Commit

Permalink
Refactor: Assign string label to each case in RCTPLTag enum for start…
Browse files Browse the repository at this point in the history
…up performance logging

Summary:
`_labelsForTags` is an array with string labels used only for local profiling, that we had to manually keep it in sync with `RCTPLTag`. Refactor so labels are assigned with switch instead.

Changelog: [iOS] Refactor: Assign string label to each case in RCTPLTag enum for startup performance logging

Reviewed By: fkgozali

Differential Revision: D32889043

fbshipit-source-id: 81da592a160a31b91e78289be0990cc2ff960f29
  • Loading branch information
p-sun authored and facebook-github-bot committed Dec 8, 2021
1 parent d393e94 commit 60e60a9
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 75 deletions.
2 changes: 2 additions & 0 deletions BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,11 @@ REACT_PUBLIC_HEADERS = {
"React/RCTModuleMethod.h": RCTBASE_PATH + "RCTModuleMethod.h",
"React/RCTMultipartStreamReader.h": RCTBASE_PATH + "RCTMultipartStreamReader.h",
"React/RCTNullability.h": RCTBASE_PATH + "RCTNullability.h",
"React/RCTPLTag.h": RCTBASE_PATH + "RCTPLTag.h",
"React/RCTPackagerClient.h": RCTDEVSUPPORT_PATH + "RCTPackagerClient.h",
"React/RCTPackagerConnection.h": RCTDEVSUPPORT_PATH + "RCTPackagerConnection.h",
"React/RCTPerformanceLogger.h": RCTBASE_PATH + "RCTPerformanceLogger.h",
"React/RCTPerformanceLoggerLabels.h": RCTBASE_PATH + "RCTPerformanceLoggerLabels.h",
"React/RCTPointerEvents.h": RCTVIEWS_PATH + "RCTPointerEvents.h",
"React/RCTProfile.h": "React/Profiler/RCTProfile.h",
"React/RCTPushNotificationManager.h": RCTLIB_PATH + "PushNotificationIOS/RCTPushNotificationManager.h",
Expand Down
29 changes: 29 additions & 0 deletions React/Base/RCTPLTag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.
*/

typedef NS_ENUM(NSInteger, RCTPLTag) {
RCTPLScriptDownload = 0,
RCTPLScriptExecution,
RCTPLRAMBundleLoad,
RCTPLRAMStartupCodeSize,
RCTPLRAMStartupNativeRequires,
RCTPLRAMStartupNativeRequiresCount,
RCTPLRAMNativeRequires,
RCTPLRAMNativeRequiresCount,
RCTPLNativeModuleInit,
RCTPLNativeModuleMainThread,
RCTPLNativeModulePrepareConfig,
RCTPLNativeModuleMainThreadUsesCount,
RCTPLNativeModuleSetup,
RCTPLTurboModuleSetup,
RCTPLJSCWrapperOpenLibrary,
RCTPLBridgeStartup,
RCTPLTTI,
RCTPLBundleSize,
RCTPLReactInstanceInit,
RCTPLSize // This is used to count the size
};
31 changes: 1 addition & 30 deletions React/Base/RCTPerformanceLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,7 @@
*/

#import <Foundation/Foundation.h>

// Keep this in sync with _labelsForTags
typedef NS_ENUM(NSInteger, RCTPLTag) {
RCTPLScriptDownload = 0,
RCTPLScriptExecution,
RCTPLRAMBundleLoad,
RCTPLRAMStartupCodeSize,
RCTPLRAMStartupNativeRequires,
RCTPLRAMStartupNativeRequiresCount,
RCTPLRAMNativeRequires,
RCTPLRAMNativeRequiresCount,
RCTPLNativeModuleInit,
RCTPLNativeModuleMainThread,
RCTPLNativeModulePrepareConfig,
RCTPLNativeModuleMainThreadUsesCount,
RCTPLNativeModuleSetup,
RCTPLTurboModuleSetup,
RCTPLJSCWrapperOpenLibrary,
RCTPLBridgeStartup,
RCTPLTTI,
RCTPLBundleSize,
RCTPLReactInstanceInit,
RCTPLSize // This is used to count the size
};
#import "RCTPLTag.h"

@interface RCTPerformanceLogger : NSObject

Expand Down Expand Up @@ -95,10 +72,4 @@ typedef NS_ENUM(NSInteger, RCTPLTag) {
*/
- (int64_t)valueForTag:(RCTPLTag)tag;

/**
* Returns an array with values for all tags.
* Use RCTPLTag to go over the array.
*/
- (NSArray<NSString *> *)labelsForTags;

@end
38 changes: 4 additions & 34 deletions React/Base/RCTPerformanceLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,24 @@

#import "RCTLog.h"
#import "RCTPerformanceLogger.h"
#import "RCTPerformanceLoggerLabels.h"
#import "RCTProfile.h"
#import "RCTRootView.h"

@interface RCTPerformanceLogger () {
int64_t _data[RCTPLSize][2];
NSUInteger _cookies[RCTPLSize];
NSInteger _cookies[RCTPLSize];
}

@property (nonatomic, copy) NSArray<NSString *> *labelsForTags;

@end

@implementation RCTPerformanceLogger

- (instancetype)init
{
if (self = [super init]) {
// Keep this in sync with RCTPLTag
_labelsForTags = @[
@"ScriptDownload",
@"ScriptExecution",
@"RAMBundleLoad",
@"RAMStartupCodeSize",
@"RAMStartupNativeRequires",
@"RAMStartupNativeRequiresCount",
@"RAMNativeRequires",
@"RAMNativeRequiresCount",
@"NativeModuleInit",
@"NativeModuleMainThread",
@"NativeModulePrepareConfig",
@"NativeModuleMainThreadUsesCount",
@"NativeModuleSetup",
@"TurboModuleSetup",
@"JSCWrapperOpenLibrary",
@"BridgeStartup",
@"RootViewTTI",
@"BundleSize",
@"ReactInstanceInit",
];
}
return self;
}

- (void)markStartForTag:(RCTPLTag)tag
{
#if RCT_PROFILE
if (RCTProfileIsProfiling()) {
NSString *label = _labelsForTags[tag];
NSString *label = RCTPLLabelForTag(tag);
_cookies[tag] = RCTProfileBeginAsyncEvent(RCTProfileTagAlways, label, nil);
}
#endif
Expand All @@ -68,7 +38,7 @@ - (void)markStopForTag:(RCTPLTag)tag
{
#if RCT_PROFILE
if (RCTProfileIsProfiling()) {
NSString *label = _labelsForTags[tag];
NSString *label = RCTPLLabelForTag(tag);
RCTProfileEndAsyncEvent(RCTProfileTagAlways, @"native", _cookies[tag], label, @"RCTPerformanceLogger");
}
#endif
Expand Down
12 changes: 12 additions & 0 deletions React/Base/RCTPerformanceLoggerLabels.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 <React/RCTDefines.h>
#import "RCTPLTag.h"

// Return the string label for the enum RCTPLTag for performance logging
__attribute__((used)) RCT_EXTERN NSString *RCTPLLabelForTag(RCTPLTag tag);
56 changes: 56 additions & 0 deletions React/Base/RCTPerformanceLoggerLabels.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 "RCTPerformanceLoggerLabels.h"
#import <React/RCTAssert.h>

NSString *RCTPLLabelForTag(RCTPLTag tag)
{
switch (tag) {
case RCTPLScriptDownload:
return @"ScriptDownload";
case RCTPLScriptExecution:
return @"ScriptExecution";
case RCTPLRAMBundleLoad:
return @"RAMBundleLoad";
case RCTPLRAMStartupCodeSize:
return @"RAMStartupCodeSize";
case RCTPLRAMStartupNativeRequires:
return @"RAMStartupNativeRequires";
case RCTPLRAMStartupNativeRequiresCount:
return @"RAMStartupNativeRequiresCount";
case RCTPLRAMNativeRequires:
return @"RAMNativeRequires";
case RCTPLRAMNativeRequiresCount:
return @"RAMNativeRequiresCount";
case RCTPLNativeModuleInit:
return @"NativeModuleInit";
case RCTPLNativeModuleMainThread:
return @"NativeModuleMainThread";
case RCTPLNativeModulePrepareConfig:
return @"NativeModulePrepareConfig";
case RCTPLNativeModuleMainThreadUsesCount:
return @"NativeModuleMainThreadUsesCount";
case RCTPLNativeModuleSetup:
return @"NativeModuleSetup";
case RCTPLTurboModuleSetup:
return @"TurboModuleSetup";
case RCTPLJSCWrapperOpenLibrary:
return @"JSCWrapperOpenLibrary";
case RCTPLBridgeStartup:
return @"BridgeStartup";
case RCTPLTTI:
return @"RootViewTTI";
case RCTPLBundleSize:
return @"BundleSize";
case RCTPLReactInstanceInit:
return @"ReactInstanceInit";
case RCTPLSize: // Only used to count enum size
RCTAssert(NO, @"RCTPLSize should not be used to track performance timestamps.");
return nil;
}
}
14 changes: 13 additions & 1 deletion React/CoreModules/RCTPerfMonitor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import <React/RCTInvalidating.h>
#import <React/RCTJavaScriptExecutor.h>
#import <React/RCTPerformanceLogger.h>
#import <React/RCTPerformanceLoggerLabels.h>
#import <React/RCTRootView.h>
#import <React/RCTUIManager.h>
#import <React/RCTUtils.h>
Expand All @@ -40,6 +41,8 @@

typedef BOOL (*RCTJSCSetOptionType)(const char *);

NSArray<NSString *> *LabelsForRCTPerformanceLoggerTags();

static BOOL RCTJSCSetOption(const char *option)
{
static RCTJSCSetOptionType setOption;
Expand Down Expand Up @@ -515,7 +518,7 @@ - (void)loadPerformanceLoggerData
NSMutableArray<NSString *> *data = [NSMutableArray new];
RCTPerformanceLogger *performanceLogger = [_bridge performanceLogger];
NSArray<NSNumber *> *values = [performanceLogger valuesForTags];
for (NSString *label in [performanceLogger labelsForTags]) {
for (NSString *label in LabelsForRCTPerformanceLoggerTags()) {
long long value = values[i + 1].longLongValue - values[i].longLongValue;
NSString *unit = @"ms";
if ([label hasSuffix:@"Size"]) {
Expand Down Expand Up @@ -572,6 +575,15 @@ - (CGFloat)tableView:(__unused UITableView *)tableView heightForRowAtIndexPath:(

@end

NSArray<NSString *> *LabelsForRCTPerformanceLoggerTags()
{
NSMutableArray<NSString *> *labels = [NSMutableArray new];
for (int i = 0; i < RCTPLSize; i++) {
[labels addObject:RCTPLLabelForTag((RCTPLTag)i)];
}
return labels;
}

#endif

Class RCTPerfMonitorCls(void)
Expand Down
8 changes: 4 additions & 4 deletions packages/rn-tester/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ SPEC CHECKSUMS:
CocoaAsyncSocket: 065fd1e645c7abab64f7a6a2007a48038fdc6a99
DoubleConversion: 831926d9b8bf8166fd87886c4abab286c2422662
FBLazyVector: b81a2b70c72d8b0aefb652cea22c11e9ffd02949
FBReactNativeSpec: 755b7fee1b08aefd74fb2fa9f7312b253719d536
FBReactNativeSpec: 5bd61f19eb8d9ab489a9f89f6e862767fbb6cced
Flipper: 30e8eeeed6abdc98edaf32af0cda2f198be4b733
Flipper-Boost-iOSX: fd1e2b8cbef7e662a122412d7ac5f5bea715403c
Flipper-DoubleConversion: 57ffbe81ef95306cc9e69c4aa3aeeeeb58a6a28c
Expand All @@ -904,7 +904,7 @@ SPEC CHECKSUMS:
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: c10b67b343303f51715e5c5eedb18a41402f350a
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
MyNativeView: 4eaeecdc16568e81e702ef0a901127823d1ddc80
MyNativeView: 68ba0ad4bcfc2bc9b776a346c70ca9d19a2f2542
OpenSSL-Universal: 1aa4f6a6ee7256b83db99ec1ccdaa80d10f9af9b
RCT-Folly: 803a9cfd78114b2ec0f140cfa6fa2a6bafb2d685
RCTRequired: af2d6080a4b9ba0885b28ca78879a92066c71cab
Expand Down Expand Up @@ -934,10 +934,10 @@ SPEC CHECKSUMS:
React-RCTTest: 12bbd7fc2e72bd9920dc7286c5b8ef96639582b6
React-RCTText: e9146b2c0550a83d1335bfe2553760070a2d75c7
React-RCTVibration: 50be9c390f2da76045ef0dfdefa18b9cf9f35cfa
React-rncore: d09af3a25cbff0b484776785676c28f3729e07f5
React-rncore: 03e9b66d28812371afe3e7f180968f8e2a4ea71e
React-runtimeexecutor: 4b0c6eb341c7d3ceb5e2385cb0fdb9bf701024f3
ReactCommon: 7a2714d1128f965392b6f99a8b390e3aa38c9569
ScreenshotManager: e8a3fc9b2e24b81127b36cb4ebe0eed65090c949
ScreenshotManager: 6923f27c7ce9505117b8c743c9f099fa3054df6a
Yoga: c0d06f5380d34e939f55420669a60fe08b79bd75
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,4 @@ @interface RCTPerformanceLoggerTests : XCTestCase

@implementation RCTPerformanceLoggerTests

- (void)testLabelCountInSyncWithRCTPLTag
{
RCTPerformanceLogger *logger = [RCTPerformanceLogger new];
XCTAssertEqual([logger labelsForTags].count, RCTPLSize);
}

@end

0 comments on commit 60e60a9

Please sign in to comment.