Files
react-native/Libraries/Performance/Systrace.js
T
Rubén Norte 5efb4f09fd Remove (bad) implementation of the User Timing API from Systrace
Summary:
The `Systrace` module implements a polyfill for the User Timing API that's:
1. Incompatible with the Web standard. This polyfill expects `performance.mark` and `performance.measure` to be used exactly like `Systrace.beginEvent` and `Systrace.endEvent` respectively. If not used like those functions, they throw an error at runtime, which is really bad.
2. Never actually used. See below.

This polyfill is only installed if we're actually profiling on startup:
https://www.internalfb.com/code/fbsource/[4d888a933920]/xplat/js/react-native-github/Libraries/Core/setUpSystrace.js?lines=17-21

While the only code that we have that uses this API is actually not installed if we're profiling:

https://www.internalfb.com/code/fbsource/[4d888a933920]/xplat/js/react-native-github/Libraries/Core/setUpDeveloperTools.js?lines=20-21

This should be safe to remove then.

We have plans to add proper support for this API in the short term, so this also gets out of the way of doing that.

Additionally, installing this polyfill was the only reason why we had `setupSystrace`, so we can get rid of that too :)

Changelog: [Internal]

Reviewed By: javache

Differential Revision: D39210813

fbshipit-source-id: a90b6237c311d2157069b41975d10b33f1f464ef
2022-09-08 11:12:06 -07:00

111 lines
2.8 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
'use strict';
const TRACE_TAG_REACT_APPS = 1 << 17; // eslint-disable-line no-bitwise
let _enabled = false;
let _asyncCookie = 0;
const Systrace = {
setEnabled(enabled: boolean) {
_enabled = enabled;
},
isEnabled(): boolean {
return _enabled;
},
/**
* beginEvent/endEvent for starting and then ending a profile within the same call stack frame
**/
beginEvent(
profileName?: string | (() => string),
args?: {[string]: string, ...},
) {
if (_enabled) {
const profileNameString =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceBeginSection(
TRACE_TAG_REACT_APPS,
profileNameString,
args,
);
}
},
endEvent() {
if (_enabled) {
global.nativeTraceEndSection(TRACE_TAG_REACT_APPS);
}
},
/**
* beginAsyncEvent/endAsyncEvent for starting and then ending a profile where the end can either
* occur on another thread or out of the current stack frame, eg await
* the returned cookie variable should be used as input into the endAsyncEvent call to end the profile
**/
beginAsyncEvent(profileName?: string | (() => string)): number {
const cookie = _asyncCookie;
if (_enabled) {
_asyncCookie++;
const profileNameString =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceBeginAsyncSection(
TRACE_TAG_REACT_APPS,
profileNameString,
cookie,
);
}
return cookie;
},
endAsyncEvent(profileName?: string | (() => string), cookie?: number) {
if (_enabled) {
const profileNameString =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceEndAsyncSection(
TRACE_TAG_REACT_APPS,
profileNameString,
cookie,
);
}
},
/**
* counterEvent registers the value to the profileName on the systrace timeline
**/
counterEvent(profileName?: string | (() => string), value?: number) {
if (_enabled) {
const profileNameString =
typeof profileName === 'function' ? profileName() : profileName;
global.nativeTraceCounter &&
global.nativeTraceCounter(
TRACE_TAG_REACT_APPS,
profileNameString,
value,
);
}
},
};
if (__DEV__) {
// The metro require polyfill can not have dependencies (true for all polyfills).
// Ensure that `Systrace` is available in polyfill by exposing it globally.
global[(global.__METRO_GLOBAL_PREFIX__ || '') + '__SYSTRACE'] = Systrace;
}
if (global.__RCTProfileIsProfiling) {
Systrace.setEnabled(true);
}
module.exports = Systrace;