Skip to content

Commit

Permalink
Gate more code with if (__DEV__) { }
Browse files Browse the repository at this point in the history
Reviewed By: amnn

Differential Revision: D5621165

fbshipit-source-id: ee8b3df523858323a3ce4484ab56fcae0da3d633
  • Loading branch information
alexeylang authored and facebook-github-bot committed Aug 14, 2017
1 parent 419652d commit f11f001
Showing 1 changed file with 51 additions and 53 deletions.
104 changes: 51 additions & 53 deletions Libraries/Performance/Systrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,60 +39,56 @@ let _useFiber = false;
// Implements a subset of User Timing API necessary for React measurements.
// https://developer.mozilla.org/en-US/docs/Web/API/User_Timing_API
const REACT_MARKER = '\u269B';
const userTimingPolyfill = {
const userTimingPolyfill = __DEV__ ? {
mark(markName: string) {
if (__DEV__) {
if (_enabled) {
_markStackIndex++;
_markStack[_markStackIndex] = markName;
let systraceLabel = markName;
// Since perf measurements are a shared namespace in User Timing API,
// we prefix all React results with a React emoji.
if (markName[0] === REACT_MARKER) {
// This is coming from React.
// Removing component IDs keeps trace colors stable.
const indexOfId = markName.lastIndexOf(' (#');
const cutoffIndex = indexOfId !== -1 ? indexOfId : markName.length;
// Also cut off the emoji because it breaks Systrace
systraceLabel = markName.slice(2, cutoffIndex);
}
Systrace.beginEvent(systraceLabel);
if (_enabled) {
_markStackIndex++;
_markStack[_markStackIndex] = markName;
let systraceLabel = markName;
// Since perf measurements are a shared namespace in User Timing API,
// we prefix all React results with a React emoji.
if (markName[0] === REACT_MARKER) {
// This is coming from React.
// Removing component IDs keeps trace colors stable.
const indexOfId = markName.lastIndexOf(' (#');
const cutoffIndex = indexOfId !== -1 ? indexOfId : markName.length;
// Also cut off the emoji because it breaks Systrace
systraceLabel = markName.slice(2, cutoffIndex);
}
Systrace.beginEvent(systraceLabel);
}
},
measure(measureName: string, startMark: ?string, endMark: ?string) {
if (__DEV__) {
if (_enabled) {
invariant(
typeof measureName === 'string' &&
typeof startMark === 'string' &&
typeof endMark === 'undefined',
'Only performance.measure(string, string) overload is supported.'
);
const topMark = _markStack[_markStackIndex];
invariant(
startMark === topMark,
'There was a mismatching performance.measure() call. ' +
'Expected "%s" but got "%s."',
topMark,
startMark,
);
_markStackIndex--;
// We can't use more descriptive measureName because Systrace doesn't
// let us edit labels post factum.
Systrace.endEvent();
}
if (_enabled) {
invariant(
typeof measureName === 'string' &&
typeof startMark === 'string' &&
typeof endMark === 'undefined',
'Only performance.measure(string, string) overload is supported.'
);
const topMark = _markStack[_markStackIndex];
invariant(
startMark === topMark,
'There was a mismatching performance.measure() call. ' +
'Expected "%s" but got "%s."',
topMark,
startMark,
);
_markStackIndex--;
// We can't use more descriptive measureName because Systrace doesn't
// let us edit labels post factum.
Systrace.endEvent();
}
},
clearMarks(markName: string) {
if (__DEV__) {
if (_enabled) {
if (_markStackIndex === -1) {
return;
}
if (markName === _markStack[_markStackIndex]) {
// React uses this for "cancelling" started measurements.
// Systrace doesn't support deleting measurements, so we just stop them.
if (_enabled) {
if (_markStackIndex === -1) {
return;
}
if (markName === _markStack[_markStackIndex]) {
// React uses this for "cancelling" started measurements.
// Systrace doesn't support deleting measurements, so we just stop them.
if (userTimingPolyfill != null) {
userTimingPolyfill.measure(markName, markName);
}
}
Expand All @@ -102,10 +98,10 @@ const userTimingPolyfill = {
// React calls this to avoid memory leaks in browsers, but we don't keep
// measurements anyway.
},
};
} : null;

// A hook to get React Stack markers in Systrace.
const reactDebugToolHook = {
const reactDebugToolHook = __DEV__ ? {
onBeforeMountComponent(debugID) {
const ReactComponentTreeHook = require('ReactGlobalSharedState').ReactComponentTreeHook;
const displayName = ReactComponentTreeHook.getDisplayName(debugID);
Expand Down Expand Up @@ -138,15 +134,17 @@ const reactDebugToolHook = {
onEndLifeCycleTimer(debugID, timerType) {
Systrace.endEvent();
},
};
} : null;

const Systrace = {
installReactHook(useFiber: boolean) {
if (_enabled) {
if (useFiber) {
global.performance = userTimingPolyfill;
} else {
require('ReactDebugTool').addHook(reactDebugToolHook);
if (__DEV__) {
if (useFiber) {
global.performance = userTimingPolyfill;
} else {
require('ReactDebugTool').addHook(reactDebugToolHook);
}
}
}
_useFiber = useFiber;
Expand Down

0 comments on commit f11f001

Please sign in to comment.