Gate more code with if (__DEV__) { }

Reviewed By: amnn

Differential Revision: D5621165

fbshipit-source-id: ee8b3df523858323a3ce4484ab56fcae0da3d633
This commit is contained in:
Alexey Lang 2017-08-14 07:10:27 -07:00 committed by Facebook Github Bot
parent 419652d4e9
commit f11f00197d

View File

@ -39,60 +39,56 @@ let _useFiber = false;
// Implements a subset of User Timing API necessary for React measurements. // Implements a subset of User Timing API necessary for React measurements.
// https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API // https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API
const REACT_MARKER = '\u269B'; const REACT_MARKER = '\u269B';
const userTimingPolyfill = { const userTimingPolyfill = __DEV__ ? {
mark(markName: string) { mark(markName: string) {
if (__DEV__) { if (_enabled) {
if (_enabled) { _markStackIndex++;
_markStackIndex++; _markStack[_markStackIndex] = markName;
_markStack[_markStackIndex] = markName; let systraceLabel = markName;
let systraceLabel = markName; // Since perf measurements are a shared namespace in User Timing API,
// Since perf measurements are a shared namespace in User Timing API, // we prefix all React results with a React emoji.
// we prefix all React results with a React emoji. if (markName[0] === REACT_MARKER) {
if (markName[0] === REACT_MARKER) { // This is coming from React.
// This is coming from React. // Removing component IDs keeps trace colors stable.
// Removing component IDs keeps trace colors stable. const indexOfId = markName.lastIndexOf(' (#');
const indexOfId = markName.lastIndexOf(' (#'); const cutoffIndex = indexOfId !== -1 ? indexOfId : markName.length;
const cutoffIndex = indexOfId !== -1 ? indexOfId : markName.length; // Also cut off the emoji because it breaks Systrace
// Also cut off the emoji because it breaks Systrace systraceLabel = markName.slice(2, cutoffIndex);
systraceLabel = markName.slice(2, cutoffIndex);
}
Systrace.beginEvent(systraceLabel);
} }
Systrace.beginEvent(systraceLabel);
} }
}, },
measure(measureName: string, startMark: ?string, endMark: ?string) { measure(measureName: string, startMark: ?string, endMark: ?string) {
if (__DEV__) { if (_enabled) {
if (_enabled) { invariant(
invariant( typeof measureName === 'string' &&
typeof measureName === 'string' && typeof startMark === 'string' &&
typeof startMark === 'string' && typeof endMark === 'undefined',
typeof endMark === 'undefined', 'Only performance.measure(string, string) overload is supported.'
'Only performance.measure(string, string) overload is supported.' );
); const topMark = _markStack[_markStackIndex];
const topMark = _markStack[_markStackIndex]; invariant(
invariant( startMark === topMark,
startMark === topMark, 'There was a mismatching performance.measure() call. ' +
'There was a mismatching performance.measure() call. ' + 'Expected "%s" but got "%s."',
'Expected "%s" but got "%s."', topMark,
topMark, startMark,
startMark, );
); _markStackIndex--;
_markStackIndex--; // We can't use more descriptive measureName because Systrace doesn't
// We can't use more descriptive measureName because Systrace doesn't // let us edit labels post factum.
// let us edit labels post factum. Systrace.endEvent();
Systrace.endEvent();
}
} }
}, },
clearMarks(markName: string) { clearMarks(markName: string) {
if (__DEV__) { if (_enabled) {
if (_enabled) { if (_markStackIndex === -1) {
if (_markStackIndex === -1) { return;
return; }
} if (markName === _markStack[_markStackIndex]) {
if (markName === _markStack[_markStackIndex]) { // React uses this for "cancelling" started measurements.
// React uses this for "cancelling" started measurements. // Systrace doesn't support deleting measurements, so we just stop them.
// Systrace doesn't support deleting measurements, so we just stop them. if (userTimingPolyfill != null) {
userTimingPolyfill.measure(markName, markName); userTimingPolyfill.measure(markName, markName);
} }
} }
@ -102,10 +98,10 @@ const userTimingPolyfill = {
// React calls this to avoid memory leaks in browsers, but we don't keep // React calls this to avoid memory leaks in browsers, but we don't keep
// measurements anyway. // measurements anyway.
}, },
}; } : null;
// A hook to get React Stack markers in Systrace. // A hook to get React Stack markers in Systrace.
const reactDebugToolHook = { const reactDebugToolHook = __DEV__ ? {
onBeforeMountComponent(debugID) { onBeforeMountComponent(debugID) {
const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook; const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook;
const displayName = ReactComponentTreeHook.getDisplayName(debugID); const displayName = ReactComponentTreeHook.getDisplayName(debugID);
@ -138,15 +134,17 @@ const reactDebugToolHook = {
onEndLifeCycleTimer(debugID, timerType) { onEndLifeCycleTimer(debugID, timerType) {
Systrace.endEvent(); Systrace.endEvent();
}, },
}; } : null;
const Systrace = { const Systrace = {
installReactHook(useFiber: boolean) { installReactHook(useFiber: boolean) {
if (_enabled) { if (_enabled) {
if (useFiber) { if (__DEV__) {
global.performance = userTimingPolyfill; if (useFiber) {
} else { global.performance = userTimingPolyfill;
require('ReactDebugTool').addHook(reactDebugToolHook); } else {
require('ReactDebugTool').addHook(reactDebugToolHook);
}
} }
} }
_useFiber = useFiber; _useFiber = useFiber;