Skip to content

Commit

Permalink
remove RelayProfiler.instrument
Browse files Browse the repository at this point in the history
Summary: Removes `RelayProfiler`'s `instrument` and the related `attachAggregateHandler` and `detachAggregateHandler` functions used to instrument them as they're unused.

Reviewed By: jstejada

Differential Revision: D27571316

fbshipit-source-id: 221432ec13535f3f6da6603929dea2d212e94082
  • Loading branch information
kassens authored and facebook-github-bot committed Apr 6, 2021
1 parent 1bfa996 commit cc2758b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 361 deletions.
110 changes: 0 additions & 110 deletions packages/relay-runtime/util/RelayProfiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@

'use strict';

type Handler = (name: string, callback: () => void) => void;
type ProfileHandler = (name: string, state?: any) => (error?: Error) => void;

const aggregateHandlersByName: {[name: string]: Array<Handler>, ...} = {
'*': [],
};
const profileHandlersByName: {[name: string]: Array<ProfileHandler>, ...} = {
'*': [],
};

const NOT_INVOKED = {};
const defaultProfiler = {
stop() {},
};
Expand Down Expand Up @@ -56,111 +51,6 @@ const defaultProfiler = {
* });
*/
const RelayProfiler = {
/**
* Wraps the supplied function with one that provides the `attachHandler` and
* `detachHandler` methods. Example usage:
*
* const printRelayQuery =
* RelayProfiler.instrument('printRelayQuery', printRelayQuery);
*
* printRelayQuery.attachHandler(...);
*
* NOTE: The instrumentation assumes that no handlers are attached or detached
* in the course of executing another handler.
*/
instrument<T: Function>(name: string, originalFunction: T): T {
if (!aggregateHandlersByName.hasOwnProperty(name)) {
aggregateHandlersByName[name] = [];
}
const catchallHandlers = aggregateHandlersByName['*'];
const aggregateHandlers = aggregateHandlersByName[name];
const handlers: Array<Handler> = [];
const contexts: Array<[number, number, number, any, any, any]> = [];
const invokeHandlers = function() {
const context = contexts[contexts.length - 1];
if (context[0]) {
context[0]--;
catchallHandlers[context[0]](name, invokeHandlers);
} else if (context[1]) {
context[1]--;
aggregateHandlers[context[1]](name, invokeHandlers);
} else if (context[2]) {
context[2]--;
handlers[context[2]](name, invokeHandlers);
} else {
context[5] = originalFunction.apply(context[3], context[4]);
}
};
const instrumentedCallback = function() {
let returnValue;
if (
aggregateHandlers.length === 0 &&
handlers.length === 0 &&
catchallHandlers.length === 0
) {
returnValue = originalFunction.apply(this, arguments);
} else {
contexts.push([
catchallHandlers.length,
aggregateHandlers.length,
handlers.length,
this,
arguments,
NOT_INVOKED,
]);
invokeHandlers();
const context = contexts.pop();
returnValue = context[5];
if (returnValue === NOT_INVOKED) {
throw new Error(
'RelayProfiler: Handler did not invoke original function.',
);
}
}
return returnValue;
};
instrumentedCallback.attachHandler = function(handler: Handler): void {
handlers.push(handler);
};
instrumentedCallback.detachHandler = function(handler: Handler): void {
removeFromArray(handlers, handler);
};
instrumentedCallback.displayName = '(instrumented ' + name + ')';
return (instrumentedCallback: any);
},

/**
* Attaches a handler to all methods instrumented with the supplied name.
*
* function createRenderer() {
* return RelayProfiler.instrument('render', function() {...});
* }
* const renderA = createRenderer();
* const renderB = createRenderer();
*
* // Only profiles `renderA`.
* renderA.attachHandler(...);
*
* // Profiles both `renderA` and `renderB`.
* RelayProfiler.attachAggregateHandler('render', ...);
*
*/
attachAggregateHandler(name: string, handler: Handler): void {
if (!aggregateHandlersByName.hasOwnProperty(name)) {
aggregateHandlersByName[name] = [];
}
aggregateHandlersByName[name].push(handler);
},

/**
* Detaches a handler attached via `attachAggregateHandler`.
*/
detachAggregateHandler(name: string, handler: Handler): void {
if (aggregateHandlersByName.hasOwnProperty(name)) {
removeFromArray(aggregateHandlersByName[name], handler);
}
},

/**
* Instruments profiling for arbitrarily asynchronous code by a name.
*
Expand Down
7 changes: 0 additions & 7 deletions packages/relay-runtime/util/__mocks__/RelayProfiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
'use strict';

const RelayProfiler = {
instrument: jest.fn((name, handler) => {
handler.attachHandler = () => {};
handler.detachHandler = () => {};
return handler;
}),
attachAggregateHandler: jest.fn(),
detachAggregateHandler: jest.fn(),
profile: jest.fn(() => {
return {
stop: jest.fn(),
Expand Down
Loading

0 comments on commit cc2758b

Please sign in to comment.