@player-devtools/profiler-plugin 1.0.1-next.0 → 1.1.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ProfilerDevtoolsPlugin.native.js +127 -53
- package/dist/ProfilerDevtoolsPlugin.native.js.map +1 -1
- package/dist/cjs/index.cjs +98 -23
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/index.legacy-esm.js +93 -18
- package/dist/index.mjs +93 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/__snapshots__/plugin.test.ts.snap +319 -319
- package/src/__tests__/plugin.test.ts +145 -1
- package/src/helpers/__tests__/profiler.test.ts +49 -0
- package/src/helpers/profiler.ts +30 -1
- package/src/plugin.ts +118 -17
- package/types/helpers/profiler.d.ts +12 -1
- package/types/plugin.d.ts +19 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -27,7 +27,7 @@ module.exports = __toCommonJS(src_exports);
|
|
|
27
27
|
// ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/plugin.ts
|
|
28
28
|
var import_plugin2 = require("@player-devtools/plugin");
|
|
29
29
|
var import_profiler_plugin_content = require("@player-devtools/profiler-plugin-content");
|
|
30
|
-
var
|
|
30
|
+
var import_dset = require("dset");
|
|
31
31
|
var import_immer = require("immer");
|
|
32
32
|
|
|
33
33
|
// ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/isRecordType.ts
|
|
@@ -47,11 +47,25 @@ var isMatchingPaths = (path1, path2) => {
|
|
|
47
47
|
// ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/profiler.ts
|
|
48
48
|
var import_plugin = require("@player-devtools/plugin");
|
|
49
49
|
var Profiler = class {
|
|
50
|
-
constructor(onUpdate) {
|
|
50
|
+
constructor(onUpdate, options = {}) {
|
|
51
51
|
this.onUpdate = onUpdate;
|
|
52
|
+
this.options = options;
|
|
52
53
|
this.rootNodes = [];
|
|
53
54
|
this.stack = [];
|
|
54
55
|
}
|
|
56
|
+
/** Drop the oldest finished top-level nodes until we are back within `maxRootNodes`. */
|
|
57
|
+
trimRootNodes() {
|
|
58
|
+
const { maxRootNodes } = this.options;
|
|
59
|
+
if (maxRootNodes === void 0) return;
|
|
60
|
+
const excess = this.rootNodes.length - maxRootNodes;
|
|
61
|
+
let drop = 0;
|
|
62
|
+
while (drop < excess && this.rootNodes[drop].endTime !== void 0) {
|
|
63
|
+
drop++;
|
|
64
|
+
}
|
|
65
|
+
if (drop > 0) {
|
|
66
|
+
this.rootNodes.splice(0, drop);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
55
69
|
start() {
|
|
56
70
|
this.rootNodes = [];
|
|
57
71
|
this.stack = [];
|
|
@@ -96,6 +110,7 @@ var Profiler = class {
|
|
|
96
110
|
this.stack[this.stack.length - 1].children.push(node);
|
|
97
111
|
} else {
|
|
98
112
|
this.rootNodes.push(node);
|
|
113
|
+
this.trimRootNodes();
|
|
99
114
|
}
|
|
100
115
|
this.stack.push(node);
|
|
101
116
|
}
|
|
@@ -225,6 +240,9 @@ var wrapInRoot = (nodes) => {
|
|
|
225
240
|
};
|
|
226
241
|
var pluginData = import_profiler_plugin_content.ProfilerPluginData;
|
|
227
242
|
var pluginID = pluginData.id;
|
|
243
|
+
var MIN_PUBLISH_INTERVAL_MS = 250;
|
|
244
|
+
var PUBLISH_BACKOFF_FACTOR = 10;
|
|
245
|
+
var MAX_ROOT_NODES = 1e3;
|
|
228
246
|
var ProfilerDevtoolsPlugin = class extends import_plugin2.DevtoolsPlugin {
|
|
229
247
|
constructor(options) {
|
|
230
248
|
super({
|
|
@@ -232,29 +250,78 @@ var ProfilerDevtoolsPlugin = class extends import_plugin2.DevtoolsPlugin {
|
|
|
232
250
|
pluginData
|
|
233
251
|
});
|
|
234
252
|
this.name = "ProfilerDevtoolsPlugin";
|
|
253
|
+
/** The profiler tree changed since the last publish */
|
|
254
|
+
this.publishPending = false;
|
|
255
|
+
/** True while a publish is running, to ignore updates that it triggers itself */
|
|
256
|
+
this.publishing = false;
|
|
257
|
+
/** Earliest time (Date.now) at which the next publish may run */
|
|
258
|
+
this.nextPublishAt = 0;
|
|
235
259
|
this.interactionMap = /* @__PURE__ */ new Map([
|
|
236
260
|
[import_profiler_plugin_content.INTERACTIONS.START_PROFILING, () => this.startProfiler()],
|
|
237
261
|
[import_profiler_plugin_content.INTERACTIONS.STOP_PROFILING, () => this.stopProfiler()],
|
|
238
262
|
[import_profiler_plugin_content.INTERACTIONS.RESET_PROFILING, () => this.clearProfiler()]
|
|
239
263
|
]);
|
|
240
|
-
this.profilerObj = new Profiler(() => {
|
|
241
|
-
|
|
242
|
-
const newState = this.produceState(
|
|
243
|
-
[
|
|
244
|
-
["plugins", pluginID, "flow", "data", "rootNode"],
|
|
245
|
-
transformProfilerData(wrapInRoot(rootNodes))
|
|
246
|
-
],
|
|
247
|
-
[["plugins", pluginID, "flow", "data", "rawNodes"], rootNodes]
|
|
248
|
-
);
|
|
249
|
-
this.store.dispatch(
|
|
250
|
-
(0, import_plugin2.genDataChangeTransaction)({
|
|
251
|
-
playerID: this.playerID,
|
|
252
|
-
data: newState.plugins[pluginID]?.flow.data,
|
|
253
|
-
pluginID
|
|
254
|
-
})
|
|
255
|
-
);
|
|
264
|
+
this.profilerObj = new Profiler(() => this.requestPublish(), {
|
|
265
|
+
maxRootNodes: MAX_ROOT_NODES
|
|
256
266
|
});
|
|
257
267
|
}
|
|
268
|
+
/** Clone the recorded tree and send it to devtools. Cost grows with the size of the tree. */
|
|
269
|
+
publishSnapshot() {
|
|
270
|
+
const { rootNodes } = this.profilerObj.getSnapshot();
|
|
271
|
+
const newState = this.produceState(
|
|
272
|
+
[
|
|
273
|
+
["plugins", pluginID, "flow", "data", "rootNode"],
|
|
274
|
+
transformProfilerData(wrapInRoot(rootNodes))
|
|
275
|
+
],
|
|
276
|
+
[["plugins", pluginID, "flow", "data", "rawNodes"], rootNodes]
|
|
277
|
+
);
|
|
278
|
+
this.store.dispatch(
|
|
279
|
+
(0, import_plugin2.genDataChangeTransaction)({
|
|
280
|
+
playerID: this.playerID,
|
|
281
|
+
data: newState.plugins[pluginID]?.flow.data,
|
|
282
|
+
pluginID
|
|
283
|
+
})
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* The recorded tree changed. Publish it now if we are allowed to, otherwise make sure a single
|
|
288
|
+
* trailing publish happens once we are, so the latest state is never left unpublished.
|
|
289
|
+
*/
|
|
290
|
+
requestPublish() {
|
|
291
|
+
this.publishPending = true;
|
|
292
|
+
if (this.publishing || this.publishTimer !== void 0) return;
|
|
293
|
+
const wait = this.nextPublishAt - Date.now();
|
|
294
|
+
if (wait <= 0) {
|
|
295
|
+
this.flushPublish();
|
|
296
|
+
} else if (typeof setTimeout === "function") {
|
|
297
|
+
this.publishTimer = setTimeout(() => {
|
|
298
|
+
this.publishTimer = void 0;
|
|
299
|
+
this.flushPublish();
|
|
300
|
+
}, wait);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/** Publish now if anything changed since the last publish, and set when the next one may run. */
|
|
304
|
+
flushPublish() {
|
|
305
|
+
if (!this.publishPending || this.publishing) return;
|
|
306
|
+
this.cancelScheduledPublish();
|
|
307
|
+
this.publishPending = false;
|
|
308
|
+
this.publishing = true;
|
|
309
|
+
const start = Date.now();
|
|
310
|
+
try {
|
|
311
|
+
this.publishSnapshot();
|
|
312
|
+
} finally {
|
|
313
|
+
this.publishing = false;
|
|
314
|
+
}
|
|
315
|
+
const end = Date.now();
|
|
316
|
+
this.nextPublishAt = end + Math.max(MIN_PUBLISH_INTERVAL_MS, PUBLISH_BACKOFF_FACTOR * (end - start));
|
|
317
|
+
if (this.publishPending) this.requestPublish();
|
|
318
|
+
}
|
|
319
|
+
cancelScheduledPublish() {
|
|
320
|
+
if (this.publishTimer !== void 0) {
|
|
321
|
+
clearTimeout(this.publishTimer);
|
|
322
|
+
this.publishTimer = void 0;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
258
325
|
/**
|
|
259
326
|
* Produces a new store state with each `[path, value]` pair written into the
|
|
260
327
|
* draft via `dset`, leaving the live store untouched.
|
|
@@ -262,15 +329,16 @@ var ProfilerDevtoolsPlugin = class extends import_plugin2.DevtoolsPlugin {
|
|
|
262
329
|
produceState(...updates) {
|
|
263
330
|
return (0, import_immer.produce)(this.store.getState(), (draft) => {
|
|
264
331
|
updates.forEach(([path, value]) => {
|
|
265
|
-
(0,
|
|
332
|
+
(0, import_dset.dset)(draft, path, value);
|
|
266
333
|
});
|
|
267
334
|
});
|
|
268
335
|
}
|
|
269
336
|
startProfiler() {
|
|
270
337
|
this.profilerObj.start();
|
|
338
|
+
this.flushPublish();
|
|
271
339
|
const newState = (0, import_immer.produce)(this.store.getState(), (draft) => {
|
|
272
|
-
(0,
|
|
273
|
-
(0,
|
|
340
|
+
(0, import_dset.dset)(draft, ["plugins", pluginID, "flow", "data", "profiling"], true);
|
|
341
|
+
(0, import_dset.dset)(
|
|
274
342
|
draft,
|
|
275
343
|
["plugins", pluginID, "flow", "data", "displayFlameGraph"],
|
|
276
344
|
false
|
|
@@ -286,6 +354,7 @@ var ProfilerDevtoolsPlugin = class extends import_plugin2.DevtoolsPlugin {
|
|
|
286
354
|
}
|
|
287
355
|
clearProfiler() {
|
|
288
356
|
this.profilerObj.clear();
|
|
357
|
+
this.flushPublish();
|
|
289
358
|
}
|
|
290
359
|
stopProfiler() {
|
|
291
360
|
const result = this.profilerObj.stopProfiler();
|
|
@@ -306,6 +375,8 @@ var ProfilerDevtoolsPlugin = class extends import_plugin2.DevtoolsPlugin {
|
|
|
306
375
|
pluginID
|
|
307
376
|
})
|
|
308
377
|
);
|
|
378
|
+
this.publishPending = false;
|
|
379
|
+
this.cancelScheduledPublish();
|
|
309
380
|
return result;
|
|
310
381
|
}
|
|
311
382
|
apply(player) {
|
|
@@ -316,8 +387,8 @@ var ProfilerDevtoolsPlugin = class extends import_plugin2.DevtoolsPlugin {
|
|
|
316
387
|
addProfilerInterceptorsToHooks(player, this.profilerObj);
|
|
317
388
|
this.profilerObj.start();
|
|
318
389
|
const initialState = (0, import_immer.produce)(this.store.getState(), (draft) => {
|
|
319
|
-
(0,
|
|
320
|
-
(0,
|
|
390
|
+
(0, import_dset.dset)(draft, ["plugins", pluginID, "flow", "data", "profiling"], true);
|
|
391
|
+
(0, import_dset.dset)(
|
|
321
392
|
draft,
|
|
322
393
|
["plugins", pluginID, "flow", "data", "displayFlameGraph"],
|
|
323
394
|
false
|
|
@@ -338,6 +409,10 @@ var ProfilerDevtoolsPlugin = class extends import_plugin2.DevtoolsPlugin {
|
|
|
338
409
|
} = interaction;
|
|
339
410
|
this.interactionMap.get(type)?.();
|
|
340
411
|
}
|
|
412
|
+
teardown() {
|
|
413
|
+
this.cancelScheduledPublish();
|
|
414
|
+
super.teardown();
|
|
415
|
+
}
|
|
341
416
|
};
|
|
342
417
|
// Annotate the CommonJS export names for ESM import in node:
|
|
343
418
|
0 && (module.exports = {
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/index.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/plugin.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/isRecordType.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/hasHooks.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/isMatchingPaths.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/profiler.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/transformProfilerData.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/addProfilerInterceptorsToHooks.ts"],"sourcesContent":["export * from \"./plugin\";\n","import {\n DevtoolsPlugin,\n genDataChangeTransaction,\n type DevtoolsPluginOptions,\n} from \"@player-devtools/plugin\";\nimport type {\n DevtoolsPluginInteractionEvent,\n DevtoolsPluginsStore,\n} from \"@player-devtools/types\";\nimport type { Player } from \"@player-ui/player\";\nimport {\n INTERACTIONS,\n ProfilerPluginData,\n} from \"@player-devtools/profiler-plugin-content\";\nimport { dset } from \"dset/merge\";\nimport { produce } from \"immer\";\nimport { Profiler, transformProfilerData } from \"./helpers\";\nimport type { ProfilerNode } from \"./types\";\nimport { addProfilerInterceptorsToHooks } from \"./addProfilerInterceptorsToHooks\";\n\nconst wrapInRoot = (nodes: ProfilerNode[]): ProfilerNode => {\n const startTime =\n nodes.reduce<number | undefined>(\n (min, n) =>\n n.startTime !== undefined && (min === undefined || n.startTime < min)\n ? n.startTime\n : min,\n undefined,\n ) ?? 0;\n const endTime =\n nodes.reduce<number | undefined>(\n (max, n) =>\n n.endTime !== undefined && (max === undefined || n.endTime > max)\n ? n.endTime\n : max,\n undefined,\n ) ?? startTime;\n return {\n name: \"root\",\n startTime,\n endTime,\n value: Math.ceil((endTime - startTime) * 1000),\n children: nodes,\n };\n};\n\nconst pluginData = ProfilerPluginData;\n\nconst pluginID = pluginData.id;\n\nexport class ProfilerDevtoolsPlugin extends DevtoolsPlugin {\n name = \"ProfilerDevtoolsPlugin\";\n\n private readonly profilerObj: Profiler;\n\n private readonly interactionMap: Map<string, () => void> = new Map([\n [INTERACTIONS.START_PROFILING, () => this.startProfiler()],\n [INTERACTIONS.STOP_PROFILING, () => this.stopProfiler()],\n [INTERACTIONS.RESET_PROFILING, () => this.clearProfiler()],\n ]);\n\n constructor(options: Omit<DevtoolsPluginOptions, \"pluginData\">) {\n super({\n ...options,\n pluginData,\n });\n\n this.profilerObj = new Profiler(() => {\n const { rootNodes } = this.profilerObj.getSnapshot();\n const newState = this.produceState(\n [\n [\"plugins\", pluginID, \"flow\", \"data\", \"rootNode\"],\n transformProfilerData(wrapInRoot(rootNodes)),\n ],\n [[\"plugins\", pluginID, \"flow\", \"data\", \"rawNodes\"], rootNodes],\n );\n this.store.dispatch(\n genDataChangeTransaction({\n playerID: this.playerID,\n data: newState.plugins[pluginID]?.flow.data,\n pluginID,\n }),\n );\n });\n }\n\n /**\n * Produces a new store state with each `[path, value]` pair written into the\n * draft via `dset`, leaving the live store untouched.\n */\n private produceState(\n ...updates: Array<[path: Array<string>, value: unknown]>\n ): DevtoolsPluginsStore {\n return produce(this.store.getState(), (draft) => {\n updates.forEach(([path, value]) => {\n dset(draft, path, value);\n });\n });\n }\n\n private startProfiler(): void {\n this.profilerObj.start();\n const newState = produce(this.store.getState(), (draft) => {\n dset(draft, [\"plugins\", pluginID, \"flow\", \"data\", \"profiling\"], true);\n dset(\n draft,\n [\"plugins\", pluginID, \"flow\", \"data\", \"displayFlameGraph\"],\n false,\n );\n });\n this.store.dispatch(\n genDataChangeTransaction({\n playerID: this.playerID,\n data: newState.plugins[pluginID]?.flow.data,\n pluginID,\n }),\n );\n }\n\n private clearProfiler(): void {\n this.profilerObj.clear();\n }\n\n private stopProfiler(): ReturnType<Profiler[\"stopProfiler\"]> {\n const result = this.profilerObj.stopProfiler();\n const { rootNodes } = result;\n const newState = this.produceState(\n [\n [\"plugins\", pluginID, \"flow\", \"data\", \"rootNode\"],\n transformProfilerData(wrapInRoot(rootNodes)),\n ],\n [[\"plugins\", pluginID, \"flow\", \"data\", \"rawNodes\"], rootNodes],\n [[\"plugins\", pluginID, \"flow\", \"data\", \"profiling\"], false],\n [[\"plugins\", pluginID, \"flow\", \"data\", \"displayFlameGraph\"], true],\n );\n this.store.dispatch(\n genDataChangeTransaction({\n playerID: this.playerID,\n data: newState.plugins[pluginID]?.flow.data,\n pluginID,\n }),\n );\n return result;\n }\n\n apply(player: Player): void {\n if (!this.checkIfDevtoolsIsActive()) {\n return;\n }\n\n super.apply(player);\n\n // Hook once for the lifetime of this Player instance\n addProfilerInterceptorsToHooks(player, this.profilerObj);\n\n // Start profiling and dispatch initial state\n this.profilerObj.start();\n const initialState = produce(this.store.getState(), (draft) => {\n dset(draft, [\"plugins\", pluginID, \"flow\", \"data\", \"profiling\"], true);\n dset(\n draft,\n [\"plugins\", pluginID, \"flow\", \"data\", \"displayFlameGraph\"],\n false,\n );\n });\n\n this.store.dispatch(\n genDataChangeTransaction({\n playerID: this.playerID,\n data: initialState.plugins[pluginID]?.flow.data,\n pluginID,\n }),\n );\n }\n\n processInteraction(interaction: DevtoolsPluginInteractionEvent): void {\n super.processInteraction(interaction);\n\n const {\n payload: { type },\n } = interaction;\n\n this.interactionMap.get(type)?.();\n }\n}\n","export const isRecordType = <T>(obj: unknown): obj is Record<PropertyKey, T> =>\n typeof obj === \"object\" && obj !== null && !Array.isArray(obj);\n","import { isRecordType } from \"./isRecordType\";\n\nexport type ObjectWithHooks = {\n hooks: Record<PropertyKey, unknown>;\n};\n\nexport const hasHooks = (obj: unknown): obj is ObjectWithHooks => {\n return isRecordType(obj) && \"hooks\" in obj && isRecordType(obj.hooks);\n};\n","export const isMatchingPaths = (path1: string[], path2: string[]): boolean => {\n if (path1.length !== path2.length) return false;\n\n return path1.every((val, idx) => val === path2[idx]);\n};\n","import type { ProfilerNode } from \"../types\";\nimport { getNowTime } from \"@player-devtools/plugin\";\n\nexport class Profiler {\n private rootNodes: ProfilerNode[] = [];\n private stack: ProfilerNode[] = [];\n\n constructor(private readonly onUpdate?: () => void) {}\n\n start(): void {\n this.rootNodes = [];\n this.stack = [];\n this.onUpdate?.();\n }\n\n clear(): void {\n const now = getNowTime();\n // Reset each in-progress node: fresh startTime, no accumulated children\n for (const node of this.stack) {\n node.startTime = now;\n node.children = node.children.filter((x) => x.endTime === undefined);\n }\n // Re-wire parent→child links through the stack chain\n for (let i = 1; i < this.stack.length; i++) {\n this.stack[i - 1]!.children = [this.stack[i]!];\n }\n // rootNodes keeps only the outermost in-progress node (if any)\n this.rootNodes = this.stack.slice(0, 1);\n this.onUpdate?.();\n }\n\n private cloneNode(node: ProfilerNode, snapshotTime?: number): ProfilerNode {\n const children = node.children.map((c) => this.cloneNode(c, snapshotTime));\n const endTime =\n node.endTime ?? (snapshotTime !== undefined ? snapshotTime : undefined);\n const value =\n node.value ??\n (endTime !== undefined && node.startTime !== undefined\n ? Math.ceil((endTime - node.startTime) * 1000)\n : children.reduce((prev, current) => prev + (current?.value ?? 0), 0));\n\n return {\n ...node,\n endTime,\n value,\n children,\n };\n }\n\n getSnapshot(): { rootNodes: ProfilerNode[] } {\n const now = getNowTime();\n return {\n rootNodes: this.rootNodes.map((n) => this.cloneNode(n, now)),\n };\n }\n\n startTimer(hookName: string): void {\n const node: ProfilerNode = {\n name: hookName,\n startTime: getNowTime(),\n children: [],\n };\n\n if (this.stack.length > 0) {\n this.stack[this.stack.length - 1]!.children.push(node);\n } else {\n this.rootNodes.push(node);\n }\n\n this.stack.push(node);\n }\n\n private finalizeNode(node: ProfilerNode, endTime: number): void {\n const duration =\n node.startTime !== undefined ? endTime - node.startTime : 0.01;\n node.endTime = endTime;\n node.value = Math.ceil(duration * 1000);\n node.tooltip = `${node.name}, ${duration.toFixed(4)} (ms)`;\n }\n\n endTimer({ hookName }: { hookName: string }): void {\n const idx = [...this.stack].reverse().findIndex((n) => n.name === hookName);\n\n if (idx === -1) {\n console.warn(`endTimer: '${hookName}' not found in stack, ignoring`);\n return;\n }\n\n // stack index of the target (reverse idx → forward idx)\n const targetIdx = this.stack.length - 1 - idx;\n const endTime = getNowTime();\n\n // Pop and finalize everything above the target, from top down\n for (let i = this.stack.length - 1; i > targetIdx; i--) {\n const orphan = this.stack[i]!;\n console.warn(\n `endTimer: popping '${orphan.name}' — timer was never explicitly ended`,\n );\n this.finalizeNode(orphan, endTime);\n }\n\n // Finalize the target\n this.finalizeNode(this.stack[targetIdx]!, endTime);\n\n // Truncate stack\n this.stack.length = targetIdx;\n\n this.onUpdate?.();\n }\n\n stopProfiler(): { rootNodes: ProfilerNode[] } {\n return { rootNodes: [...this.rootNodes] };\n }\n}\n","import type { ProfilerNode } from \"../types\";\n\nconst createSpacer = (gap: number) => ({\n name: \"(work)\",\n value: Math.ceil(gap * 1000),\n children: [],\n backgroundColor: \"#000000\",\n color: \"#000000\",\n tooltip: \"Placeholder time between hooks\",\n});\n\nexport const transformProfilerData = (root: ProfilerNode): ProfilerNode => {\n return {\n ...root,\n children: transformProfilerDataHelper(root.children, root.startTime),\n };\n};\n\nconst transformProfilerDataHelper = (\n nodes: ProfilerNode[],\n parentStart: number = 0,\n): ProfilerNode[] => {\n const merged: ProfilerNode[] = [];\n let cursor = parentStart;\n\n for (const node of nodes) {\n if (\n node.startTime === undefined ||\n node.endTime === undefined ||\n node.value === undefined ||\n node.value <= 0\n ) {\n continue;\n }\n\n const next = {\n ...node,\n children: transformProfilerDataHelper(node.children, node.startTime),\n };\n\n const gap = node.startTime - cursor;\n if (gap > 0) {\n merged.push(createSpacer(gap));\n }\n cursor = node.endTime;\n\n merged.push(next);\n }\n\n return merged;\n};\n","import {\n AsyncParallelBailHook,\n AsyncParallelHook,\n AsyncSeriesBailHook,\n AsyncSeriesHook,\n AsyncSeriesLoopHook,\n AsyncSeriesWaterfallHook,\n SyncBailHook,\n SyncHook,\n SyncLoopHook,\n SyncWaterfallHook,\n} from \"tapable-ts\";\nimport { Profiler, hasHooks, isMatchingPaths, isRecordType } from \"./helpers\";\n\n/* Paths to hooks to ignore.\n * Currently ignoring \"view\" hook on player since it acts as a shortcut to the viewController's view hook. Including it would duplicate a lot of profiling work.\n */\nconst IGNORED_PATHS = [[\"view\"]];\n\n// Would love to just check if things are `Hook` but tapable-ts doesn't export the base class ;-;\ntype AnyHook =\n | AsyncParallelBailHook<unknown[], unknown>\n | AsyncParallelHook<unknown[]>\n | AsyncSeriesBailHook<unknown[], unknown>\n | AsyncSeriesHook<unknown[]>\n | AsyncSeriesLoopHook<unknown[]>\n | AsyncSeriesWaterfallHook<unknown[]>\n | SyncBailHook<unknown[], unknown>\n | SyncHook<unknown[]>\n | SyncLoopHook<unknown[]>\n | SyncWaterfallHook<unknown[]>;\n\n// Note: cannot use instanceof to check against the hook classes due to how JS is loaded in swift and kotlin\nconst isAnyHook = (obj: unknown): obj is AnyHook => {\n return (\n isRecordType(obj) &&\n \"intercept\" in obj &&\n typeof obj.intercept === \"function\"\n );\n};\n\n/** Recursively add profiler interceptors to each hook in the \"hooks\" property of obj. */\nexport const addProfilerInterceptorsToHooks = (\n obj: unknown,\n profiler: Profiler,\n currentPath: string[] = [],\n intercepted: WeakSet<object> = new WeakSet(),\n): void => {\n if (!hasHooks(obj)) {\n return;\n }\n\n Object.entries(obj.hooks).forEach(([key, value]) => {\n const nextPath = [...currentPath, key];\n if (\n !isAnyHook(value) ||\n IGNORED_PATHS.some((path) => isMatchingPaths(path, nextPath)) ||\n intercepted.has(value)\n ) {\n return;\n }\n\n intercepted.add(value);\n\n value.intercept({\n call: (...args) => {\n if (args.length > 0) {\n addProfilerInterceptorsToHooks(\n args[0],\n profiler,\n nextPath,\n intercepted,\n );\n }\n\n profiler.startTimer(key);\n },\n done: () => {\n profiler.endTimer({ hookName: key });\n },\n result: () => {\n profiler.endTimer({ hookName: key });\n },\n error: () => {\n profiler.endTimer({ hookName: key });\n },\n });\n });\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,iBAIO;AAMP,qCAGO;AACP,mBAAqB;AACrB,mBAAwB;;;ACfjB,IAAM,eAAe,CAAI,QAC9B,OAAO,QAAQ,YAAY,QAAQ,QAAQ,CAAC,MAAM,QAAQ,GAAG;;;ACKxD,IAAM,WAAW,CAAC,QAAyC;AAChE,SAAO,aAAa,GA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D,6BAAK,OAAO,CAAC,WAAW,UAAU,QAAQ,QAAQ,WAAW,GAAG,IAAI;AACpE;AAAA,QACE;AAAA,QACA,CAAC,WAAW,UAAU,QAAQ,QAAQ,mBAAmB;AAAA,QACzD;AAAA,MACF;AAAA,IACF,CAAC;AAED,SAAK,MAAM;AAAA,UACT,yCAAyB;AAAA,QACvB,UAAU,KAAK;AAAA,QACf,MAAM,aAAa,QAAQ,QAAQ,GAAG,KAAK;AAAA,QAC3C;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAEA,mBAAmB,aAAmD;AACpE,UAAM,mBAAmB,WAAW;AAEpC,UAAM;AAAA,MACJ,SAAS,EAAE,KAAK;AAAA,IAClB,IAAI;AAEJ,SAAK,eAAe,IAAI,IAAI,IAAI;AAAA,EAClC;AACF;","names":["import_plugin"]}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/index.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/plugin.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/isRecordType.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/hasHooks.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/isMatchingPaths.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/profiler.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/transformProfilerData.ts","../../../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/addProfilerInterceptorsToHooks.ts"],"sourcesContent":["export * from \"./plugin\";\n","import {\n DevtoolsPlugin,\n genDataChangeTransaction,\n type DevtoolsPluginOptions,\n} from \"@player-devtools/plugin\";\nimport type {\n DevtoolsPluginInteractionEvent,\n DevtoolsPluginsStore,\n} from \"@player-devtools/types\";\nimport type { Player } from \"@player-ui/player\";\nimport {\n INTERACTIONS,\n ProfilerPluginData,\n} from \"@player-devtools/profiler-plugin-content\";\n// Not `dset/merge`: merging arrays by index never shrinks them, which leaves stale nodes behind\n// whenever a shorter tree replaces a longer one (reset, restart, dropped root nodes).\nimport { dset } from \"dset\";\nimport { produce } from \"immer\";\nimport { Profiler, transformProfilerData } from \"./helpers\";\nimport type { ProfilerNode } from \"./types\";\nimport { addProfilerInterceptorsToHooks } from \"./addProfilerInterceptorsToHooks\";\n\nconst wrapInRoot = (nodes: ProfilerNode[]): ProfilerNode => {\n const startTime =\n nodes.reduce<number | undefined>(\n (min, n) =>\n n.startTime !== undefined && (min === undefined || n.startTime < min)\n ? n.startTime\n : min,\n undefined,\n ) ?? 0;\n const endTime =\n nodes.reduce<number | undefined>(\n (max, n) =>\n n.endTime !== undefined && (max === undefined || n.endTime > max)\n ? n.endTime\n : max,\n undefined,\n ) ?? startTime;\n return {\n name: \"root\",\n startTime,\n endTime,\n value: Math.ceil((endTime - startTime) * 1000),\n children: nodes,\n };\n};\n\nconst pluginData = ProfilerPluginData;\n\nconst pluginID = pluginData.id;\n\n/**\n * Publishing the tree to devtools clones and re-merges all of it, so a publish costs time\n * proportional to everything recorded so far. Recording itself is cheap. Publishing after every\n * hook made the total work quadratic: minutes of main-thread time on iOS, where JavaScriptCore\n * runs without a JIT. So recording happens on every hook, but publishing is rate limited:\n */\n\n/** Never publish more often than this (ms), however many hooks fire. */\nconst MIN_PUBLISH_INTERVAL_MS = 250;\n\n/**\n * After a publish, wait at least this many times as long as it took before publishing again.\n * Publishing then uses at most ~1/(1 + factor) of the main thread, however big the tree gets.\n */\nconst PUBLISH_BACKOFF_FACTOR = 10;\n\n/** How many top-level nodes to keep, so a profiler that is never stopped can't grow forever. */\nconst MAX_ROOT_NODES = 1000;\n\nexport class ProfilerDevtoolsPlugin extends DevtoolsPlugin {\n name = \"ProfilerDevtoolsPlugin\";\n\n private readonly profilerObj: Profiler;\n\n /** The profiler tree changed since the last publish */\n private publishPending = false;\n\n /** True while a publish is running, to ignore updates that it triggers itself */\n private publishing = false;\n\n /** Earliest time (Date.now) at which the next publish may run */\n private nextPublishAt = 0;\n\n /** Trailing publish for updates that arrived while we were waiting */\n private publishTimer: ReturnType<typeof setTimeout> | undefined;\n\n private readonly interactionMap: Map<string, () => void> = new Map([\n [INTERACTIONS.START_PROFILING, () => this.startProfiler()],\n [INTERACTIONS.STOP_PROFILING, () => this.stopProfiler()],\n [INTERACTIONS.RESET_PROFILING, () => this.clearProfiler()],\n ]);\n\n constructor(options: Omit<DevtoolsPluginOptions, \"pluginData\">) {\n super({\n ...options,\n pluginData,\n });\n\n this.profilerObj = new Profiler(() => this.requestPublish(), {\n maxRootNodes: MAX_ROOT_NODES,\n });\n }\n\n /** Clone the recorded tree and send it to devtools. Cost grows with the size of the tree. */\n private publishSnapshot(): void {\n const { rootNodes } = this.profilerObj.getSnapshot();\n const newState = this.produceState(\n [\n [\"plugins\", pluginID, \"flow\", \"data\", \"rootNode\"],\n transformProfilerData(wrapInRoot(rootNodes)),\n ],\n [[\"plugins\", pluginID, \"flow\", \"data\", \"rawNodes\"], rootNodes],\n );\n this.store.dispatch(\n genDataChangeTransaction({\n playerID: this.playerID,\n data: newState.plugins[pluginID]?.flow.data,\n pluginID,\n }),\n );\n }\n\n /**\n * The recorded tree changed. Publish it now if we are allowed to, otherwise make sure a single\n * trailing publish happens once we are, so the latest state is never left unpublished.\n */\n private requestPublish(): void {\n this.publishPending = true;\n\n if (this.publishing || this.publishTimer !== undefined) return;\n\n const wait = this.nextPublishAt - Date.now();\n if (wait <= 0) {\n this.flushPublish();\n } else if (typeof setTimeout === \"function\") {\n this.publishTimer = setTimeout(() => {\n this.publishTimer = undefined;\n this.flushPublish();\n }, wait);\n }\n }\n\n /** Publish now if anything changed since the last publish, and set when the next one may run. */\n private flushPublish(): void {\n if (!this.publishPending || this.publishing) return;\n\n this.cancelScheduledPublish();\n this.publishPending = false;\n this.publishing = true;\n\n const start = Date.now();\n try {\n this.publishSnapshot();\n } finally {\n this.publishing = false;\n }\n const end = Date.now();\n\n this.nextPublishAt =\n end +\n Math.max(MIN_PUBLISH_INTERVAL_MS, PUBLISH_BACKOFF_FACTOR * (end - start));\n\n // Something changed while we were publishing\n if (this.publishPending) this.requestPublish();\n }\n\n private cancelScheduledPublish(): void {\n if (this.publishTimer !== undefined) {\n clearTimeout(this.publishTimer);\n this.publishTimer = undefined;\n }\n }\n\n /**\n * Produces a new store state with each `[path, value]` pair written into the\n * draft via `dset`, leaving the live store untouched.\n */\n private produceState(\n ...updates: Array<[path: Array<string>, value: unknown]>\n ): DevtoolsPluginsStore {\n return produce(this.store.getState(), (draft) => {\n updates.forEach(([path, value]) => {\n dset(draft, path, value);\n });\n });\n }\n\n private startProfiler(): void {\n this.profilerObj.start();\n // Explicit user action: show the reset tree now rather than when the rate limit allows\n this.flushPublish();\n const newState = produce(this.store.getState(), (draft) => {\n dset(draft, [\"plugins\", pluginID, \"flow\", \"data\", \"profiling\"], true);\n dset(\n draft,\n [\"plugins\", pluginID, \"flow\", \"data\", \"displayFlameGraph\"],\n false,\n );\n });\n this.store.dispatch(\n genDataChangeTransaction({\n playerID: this.playerID,\n data: newState.plugins[pluginID]?.flow.data,\n pluginID,\n }),\n );\n }\n\n private clearProfiler(): void {\n this.profilerObj.clear();\n // Explicit user action: show the cleared tree now rather than when the rate limit allows\n this.flushPublish();\n }\n\n private stopProfiler(): ReturnType<Profiler[\"stopProfiler\"]> {\n const result = this.profilerObj.stopProfiler();\n const { rootNodes } = result;\n const newState = this.produceState(\n [\n [\"plugins\", pluginID, \"flow\", \"data\", \"rootNode\"],\n transformProfilerData(wrapInRoot(rootNodes)),\n ],\n [[\"plugins\", pluginID, \"flow\", \"data\", \"rawNodes\"], rootNodes],\n [[\"plugins\", pluginID, \"flow\", \"data\", \"profiling\"], false],\n [[\"plugins\", pluginID, \"flow\", \"data\", \"displayFlameGraph\"], true],\n );\n this.store.dispatch(\n genDataChangeTransaction({\n playerID: this.playerID,\n data: newState.plugins[pluginID]?.flow.data,\n pluginID,\n }),\n );\n // This publish already contains everything that was recorded, so nothing is left pending\n this.publishPending = false;\n this.cancelScheduledPublish();\n return result;\n }\n\n apply(player: Player): void {\n if (!this.checkIfDevtoolsIsActive()) {\n return;\n }\n\n super.apply(player);\n\n // Hook once for the lifetime of this Player instance\n addProfilerInterceptorsToHooks(player, this.profilerObj);\n\n // Start profiling and dispatch initial state\n this.profilerObj.start();\n const initialState = produce(this.store.getState(), (draft) => {\n dset(draft, [\"plugins\", pluginID, \"flow\", \"data\", \"profiling\"], true);\n dset(\n draft,\n [\"plugins\", pluginID, \"flow\", \"data\", \"displayFlameGraph\"],\n false,\n );\n });\n\n this.store.dispatch(\n genDataChangeTransaction({\n playerID: this.playerID,\n data: initialState.plugins[pluginID]?.flow.data,\n pluginID,\n }),\n );\n }\n\n processInteraction(interaction: DevtoolsPluginInteractionEvent): void {\n super.processInteraction(interaction);\n\n const {\n payload: { type },\n } = interaction;\n\n this.interactionMap.get(type)?.();\n }\n\n teardown(): void {\n this.cancelScheduledPublish();\n super.teardown();\n }\n}\n","export const isRecordType = <T>(obj: unknown): obj is Record<PropertyKey, T> =>\n typeof obj === \"object\" && obj !== null && !Array.isArray(obj);\n","import { isRecordType } from \"./isRecordType\";\n\nexport type ObjectWithHooks = {\n hooks: Record<PropertyKey, unknown>;\n};\n\nexport const hasHooks = (obj: unknown): obj is ObjectWithHooks => {\n return isRecordType(obj) && \"hooks\" in obj && isRecordType(obj.hooks);\n};\n","export const isMatchingPaths = (path1: string[], path2: string[]): boolean => {\n if (path1.length !== path2.length) return false;\n\n return path1.every((val, idx) => val === path2[idx]);\n};\n","import type { ProfilerNode } from \"../types\";\nimport { getNowTime } from \"@player-devtools/plugin\";\n\nexport interface ProfilerOptions {\n /**\n * Upper bound on the number of top-level nodes that are kept. Once it is exceeded the oldest\n * finished top-level nodes are dropped, so a profiler that is never stopped can't grow forever.\n * A node that is still in progress is never dropped. Unbounded when not set.\n */\n maxRootNodes?: number;\n}\n\nexport class Profiler {\n private rootNodes: ProfilerNode[] = [];\n private stack: ProfilerNode[] = [];\n\n constructor(\n private readonly onUpdate?: () => void,\n private readonly options: ProfilerOptions = {},\n ) {}\n\n /** Drop the oldest finished top-level nodes until we are back within `maxRootNodes`. */\n private trimRootNodes(): void {\n const { maxRootNodes } = this.options;\n if (maxRootNodes === undefined) return;\n\n const excess = this.rootNodes.length - maxRootNodes;\n let drop = 0;\n while (drop < excess && this.rootNodes[drop]!.endTime !== undefined) {\n drop++;\n }\n\n if (drop > 0) {\n this.rootNodes.splice(0, drop);\n }\n }\n\n start(): void {\n this.rootNodes = [];\n this.stack = [];\n this.onUpdate?.();\n }\n\n clear(): void {\n const now = getNowTime();\n // Reset each in-progress node: fresh startTime, no accumulated children\n for (const node of this.stack) {\n node.startTime = now;\n node.children = node.children.filter((x) => x.endTime === undefined);\n }\n // Re-wire parent→child links through the stack chain\n for (let i = 1; i < this.stack.length; i++) {\n this.stack[i - 1]!.children = [this.stack[i]!];\n }\n // rootNodes keeps only the outermost in-progress node (if any)\n this.rootNodes = this.stack.slice(0, 1);\n this.onUpdate?.();\n }\n\n private cloneNode(node: ProfilerNode, snapshotTime?: number): ProfilerNode {\n const children = node.children.map((c) => this.cloneNode(c, snapshotTime));\n const endTime =\n node.endTime ?? (snapshotTime !== undefined ? snapshotTime : undefined);\n const value =\n node.value ??\n (endTime !== undefined && node.startTime !== undefined\n ? Math.ceil((endTime - node.startTime) * 1000)\n : children.reduce((prev, current) => prev + (current?.value ?? 0), 0));\n\n return {\n ...node,\n endTime,\n value,\n children,\n };\n }\n\n getSnapshot(): { rootNodes: ProfilerNode[] } {\n const now = getNowTime();\n return {\n rootNodes: this.rootNodes.map((n) => this.cloneNode(n, now)),\n };\n }\n\n startTimer(hookName: string): void {\n const node: ProfilerNode = {\n name: hookName,\n startTime: getNowTime(),\n children: [],\n };\n\n if (this.stack.length > 0) {\n this.stack[this.stack.length - 1]!.children.push(node);\n } else {\n this.rootNodes.push(node);\n this.trimRootNodes();\n }\n\n this.stack.push(node);\n }\n\n private finalizeNode(node: ProfilerNode, endTime: number): void {\n const duration =\n node.startTime !== undefined ? endTime - node.startTime : 0.01;\n node.endTime = endTime;\n node.value = Math.ceil(duration * 1000);\n node.tooltip = `${node.name}, ${duration.toFixed(4)} (ms)`;\n }\n\n endTimer({ hookName }: { hookName: string }): void {\n const idx = [...this.stack].reverse().findIndex((n) => n.name === hookName);\n\n if (idx === -1) {\n console.warn(`endTimer: '${hookName}' not found in stack, ignoring`);\n return;\n }\n\n // stack index of the target (reverse idx → forward idx)\n const targetIdx = this.stack.length - 1 - idx;\n const endTime = getNowTime();\n\n // Pop and finalize everything above the target, from top down\n for (let i = this.stack.length - 1; i > targetIdx; i--) {\n const orphan = this.stack[i]!;\n console.warn(\n `endTimer: popping '${orphan.name}' — timer was never explicitly ended`,\n );\n this.finalizeNode(orphan, endTime);\n }\n\n // Finalize the target\n this.finalizeNode(this.stack[targetIdx]!, endTime);\n\n // Truncate stack\n this.stack.length = targetIdx;\n\n this.onUpdate?.();\n }\n\n stopProfiler(): { rootNodes: ProfilerNode[] } {\n return { rootNodes: [...this.rootNodes] };\n }\n}\n","import type { ProfilerNode } from \"../types\";\n\nconst createSpacer = (gap: number) => ({\n name: \"(work)\",\n value: Math.ceil(gap * 1000),\n children: [],\n backgroundColor: \"#000000\",\n color: \"#000000\",\n tooltip: \"Placeholder time between hooks\",\n});\n\nexport const transformProfilerData = (root: ProfilerNode): ProfilerNode => {\n return {\n ...root,\n children: transformProfilerDataHelper(root.children, root.startTime),\n };\n};\n\nconst transformProfilerDataHelper = (\n nodes: ProfilerNode[],\n parentStart: number = 0,\n): ProfilerNode[] => {\n const merged: ProfilerNode[] = [];\n let cursor = parentStart;\n\n for (const node of nodes) {\n if (\n node.startTime === undefined ||\n node.endTime === undefined ||\n node.value === undefined ||\n node.value <= 0\n ) {\n continue;\n }\n\n const next = {\n ...node,\n children: transformProfilerDataHelper(node.children, node.startTime),\n };\n\n const gap = node.startTime - cursor;\n if (gap > 0) {\n merged.push(createSpacer(gap));\n }\n cursor = node.endTime;\n\n merged.push(next);\n }\n\n return merged;\n};\n","import {\n AsyncParallelBailHook,\n AsyncParallelHook,\n AsyncSeriesBailHook,\n AsyncSeriesHook,\n AsyncSeriesLoopHook,\n AsyncSeriesWaterfallHook,\n SyncBailHook,\n SyncHook,\n SyncLoopHook,\n SyncWaterfallHook,\n} from \"tapable-ts\";\nimport { Profiler, hasHooks, isMatchingPaths, isRecordType } from \"./helpers\";\n\n/* Paths to hooks to ignore.\n * Currently ignoring \"view\" hook on player since it acts as a shortcut to the viewController's view hook. Including it would duplicate a lot of profiling work.\n */\nconst IGNORED_PATHS = [[\"view\"]];\n\n// Would love to just check if things are `Hook` but tapable-ts doesn't export the base class ;-;\ntype AnyHook =\n | AsyncParallelBailHook<unknown[], unknown>\n | AsyncParallelHook<unknown[]>\n | AsyncSeriesBailHook<unknown[], unknown>\n | AsyncSeriesHook<unknown[]>\n | AsyncSeriesLoopHook<unknown[]>\n | AsyncSeriesWaterfallHook<unknown[]>\n | SyncBailHook<unknown[], unknown>\n | SyncHook<unknown[]>\n | SyncLoopHook<unknown[]>\n | SyncWaterfallHook<unknown[]>;\n\n// Note: cannot use instanceof to check against the hook classes due to how JS is loaded in swift and kotlin\nconst isAnyHook = (obj: unknown): obj is AnyHook => {\n return (\n isRecordType(obj) &&\n \"intercept\" in obj &&\n typeof obj.intercept === \"function\"\n );\n};\n\n/** Recursively add profiler interceptors to each hook in the \"hooks\" property of obj. */\nexport const addProfilerInterceptorsToHooks = (\n obj: unknown,\n profiler: Profiler,\n currentPath: string[] = [],\n intercepted: WeakSet<object> = new WeakSet(),\n): void => {\n if (!hasHooks(obj)) {\n return;\n }\n\n Object.entries(obj.hooks).forEach(([key, value]) => {\n const nextPath = [...currentPath, key];\n if (\n !isAnyHook(value) ||\n IGNORED_PATHS.some((path) => isMatchingPaths(path, nextPath)) ||\n intercepted.has(value)\n ) {\n return;\n }\n\n intercepted.add(value);\n\n value.intercept({\n call: (...args) => {\n if (args.length > 0) {\n addProfilerInterceptorsToHooks(\n args[0],\n profiler,\n nextPath,\n intercepted,\n );\n }\n\n profiler.startTimer(key);\n },\n done: () => {\n profiler.endTimer({ hookName: key });\n },\n result: () => {\n profiler.endTimer({ hookName: key });\n },\n error: () => {\n profiler.endTimer({ hookName: key });\n },\n });\n });\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,iBAIO;AAMP,qCAGO;AAGP,kBAAqB;AACrB,mBAAwB;;;ACjBjB,IAAM,eAAe,CAAI,QAC9B,OAAO,QAAQ,YAAY,QAAQ,QAAQ,CAAC,MAAM,QAAQ,GAAG;;;ACKxD,IAAM,WAAW,CAAC,QAAyC;AAChE,SAAO,aAAa,GAAG,KAAK,WAAW,OAAO,aAAa,IAAI,KAAK;AACtE;;;ACRO,IAAM,kBAAkB,CAAC,OAAiB,UAA6B;AAC5E,MAAI,MAAM,WAAW,MAAM,OAAQ,QAAO;AAE1C,SAAO,MAAM,MAAM,CAAC,KAAK,QAAQ,QAAQ,MAAM,GAAG,CAAC;AACrD;;;ACHA,oBAA2B;AAWpB,IAAM,WAAN,MAAe;AAAA,EAIpB,YACmB,UACA,UAA2B,CAAC,GAC7C;AAFiB;AACA;AALnB,SAAQ,YAA4B,CAAC;AACrC,SAAQ,QAAwB,CAAC;AAAA,EAK9B;AAAA;AAAA,EAGK,gBAAsB;AAC5B,UAAM,EAAE,aAAa,IAAI,KAAK;AAC9B,QAAI,iBAAiB,OAAW;AAEhC,UAAM,SAAS,KAAK,UAAU,SAAS;AACvC,QAAI,OAAO;AACX,WAAO,OAAO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AAa,QAAQ,QAAQ,GAAG,KAAK;AAAA,QAC3C;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAEA,mBAAmB,aAAmD;AACpE,UAAM,mBAAmB,WAAW;AAEpC,UAAM;AAAA,MACJ,SAAS,EAAE,KAAK;AAAA,IAClB,IAAI;AAEJ,SAAK,eAAe,IAAI,IAAI,IAAI;AAAA,EAClC;AAAA,EAEA,WAAiB;AACf,SAAK,uBAAuB;AAC5B,UAAM,SAAS;AAAA,EACjB;AACF;","names":["import_plugin"]}
|
package/dist/index.legacy-esm.js
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
INTERACTIONS,
|
|
8
8
|
ProfilerPluginData
|
|
9
9
|
} from "@player-devtools/profiler-plugin-content";
|
|
10
|
-
import { dset } from "dset
|
|
10
|
+
import { dset } from "dset";
|
|
11
11
|
import { produce } from "immer";
|
|
12
12
|
|
|
13
13
|
// ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/isRecordType.ts
|
|
@@ -27,11 +27,25 @@ var isMatchingPaths = (path1, path2) => {
|
|
|
27
27
|
// ../../../../../../../../../../../../execroot/_main/bazel-out/k8-fastbuild/bin/devtools/plugins/profiler/core/src/helpers/profiler.ts
|
|
28
28
|
import { getNowTime } from "@player-devtools/plugin";
|
|
29
29
|
var Profiler = class {
|
|
30
|
-
constructor(onUpdate) {
|
|
30
|
+
constructor(onUpdate, options = {}) {
|
|
31
31
|
this.onUpdate = onUpdate;
|
|
32
|
+
this.options = options;
|
|
32
33
|
this.rootNodes = [];
|
|
33
34
|
this.stack = [];
|
|
34
35
|
}
|
|
36
|
+
/** Drop the oldest finished top-level nodes until we are back within `maxRootNodes`. */
|
|
37
|
+
trimRootNodes() {
|
|
38
|
+
const { maxRootNodes } = this.options;
|
|
39
|
+
if (maxRootNodes === void 0) return;
|
|
40
|
+
const excess = this.rootNodes.length - maxRootNodes;
|
|
41
|
+
let drop = 0;
|
|
42
|
+
while (drop < excess && this.rootNodes[drop].endTime !== void 0) {
|
|
43
|
+
drop++;
|
|
44
|
+
}
|
|
45
|
+
if (drop > 0) {
|
|
46
|
+
this.rootNodes.splice(0, drop);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
35
49
|
start() {
|
|
36
50
|
this.rootNodes = [];
|
|
37
51
|
this.stack = [];
|
|
@@ -76,6 +90,7 @@ var Profiler = class {
|
|
|
76
90
|
this.stack[this.stack.length - 1].children.push(node);
|
|
77
91
|
} else {
|
|
78
92
|
this.rootNodes.push(node);
|
|
93
|
+
this.trimRootNodes();
|
|
79
94
|
}
|
|
80
95
|
this.stack.push(node);
|
|
81
96
|
}
|
|
@@ -205,6 +220,9 @@ var wrapInRoot = (nodes) => {
|
|
|
205
220
|
};
|
|
206
221
|
var pluginData = ProfilerPluginData;
|
|
207
222
|
var pluginID = pluginData.id;
|
|
223
|
+
var MIN_PUBLISH_INTERVAL_MS = 250;
|
|
224
|
+
var PUBLISH_BACKOFF_FACTOR = 10;
|
|
225
|
+
var MAX_ROOT_NODES = 1e3;
|
|
208
226
|
var ProfilerDevtoolsPlugin = class extends DevtoolsPlugin {
|
|
209
227
|
constructor(options) {
|
|
210
228
|
super({
|
|
@@ -212,29 +230,78 @@ var ProfilerDevtoolsPlugin = class extends DevtoolsPlugin {
|
|
|
212
230
|
pluginData
|
|
213
231
|
});
|
|
214
232
|
this.name = "ProfilerDevtoolsPlugin";
|
|
233
|
+
/** The profiler tree changed since the last publish */
|
|
234
|
+
this.publishPending = false;
|
|
235
|
+
/** True while a publish is running, to ignore updates that it triggers itself */
|
|
236
|
+
this.publishing = false;
|
|
237
|
+
/** Earliest time (Date.now) at which the next publish may run */
|
|
238
|
+
this.nextPublishAt = 0;
|
|
215
239
|
this.interactionMap = /* @__PURE__ */ new Map([
|
|
216
240
|
[INTERACTIONS.START_PROFILING, () => this.startProfiler()],
|
|
217
241
|
[INTERACTIONS.STOP_PROFILING, () => this.stopProfiler()],
|
|
218
242
|
[INTERACTIONS.RESET_PROFILING, () => this.clearProfiler()]
|
|
219
243
|
]);
|
|
220
|
-
this.profilerObj = new Profiler(() => {
|
|
221
|
-
|
|
222
|
-
const newState = this.produceState(
|
|
223
|
-
[
|
|
224
|
-
["plugins", pluginID, "flow", "data", "rootNode"],
|
|
225
|
-
transformProfilerData(wrapInRoot(rootNodes))
|
|
226
|
-
],
|
|
227
|
-
[["plugins", pluginID, "flow", "data", "rawNodes"], rootNodes]
|
|
228
|
-
);
|
|
229
|
-
this.store.dispatch(
|
|
230
|
-
genDataChangeTransaction({
|
|
231
|
-
playerID: this.playerID,
|
|
232
|
-
data: newState.plugins[pluginID]?.flow.data,
|
|
233
|
-
pluginID
|
|
234
|
-
})
|
|
235
|
-
);
|
|
244
|
+
this.profilerObj = new Profiler(() => this.requestPublish(), {
|
|
245
|
+
maxRootNodes: MAX_ROOT_NODES
|
|
236
246
|
});
|
|
237
247
|
}
|
|
248
|
+
/** Clone the recorded tree and send it to devtools. Cost grows with the size of the tree. */
|
|
249
|
+
publishSnapshot() {
|
|
250
|
+
const { rootNodes } = this.profilerObj.getSnapshot();
|
|
251
|
+
const newState = this.produceState(
|
|
252
|
+
[
|
|
253
|
+
["plugins", pluginID, "flow", "data", "rootNode"],
|
|
254
|
+
transformProfilerData(wrapInRoot(rootNodes))
|
|
255
|
+
],
|
|
256
|
+
[["plugins", pluginID, "flow", "data", "rawNodes"], rootNodes]
|
|
257
|
+
);
|
|
258
|
+
this.store.dispatch(
|
|
259
|
+
genDataChangeTransaction({
|
|
260
|
+
playerID: this.playerID,
|
|
261
|
+
data: newState.plugins[pluginID]?.flow.data,
|
|
262
|
+
pluginID
|
|
263
|
+
})
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* The recorded tree changed. Publish it now if we are allowed to, otherwise make sure a single
|
|
268
|
+
* trailing publish happens once we are, so the latest state is never left unpublished.
|
|
269
|
+
*/
|
|
270
|
+
requestPublish() {
|
|
271
|
+
this.publishPending = true;
|
|
272
|
+
if (this.publishing || this.publishTimer !== void 0) return;
|
|
273
|
+
const wait = this.nextPublishAt - Date.now();
|
|
274
|
+
if (wait <= 0) {
|
|
275
|
+
this.flushPublish();
|
|
276
|
+
} else if (typeof setTimeout === "function") {
|
|
277
|
+
this.publishTimer = setTimeout(() => {
|
|
278
|
+
this.publishTimer = void 0;
|
|
279
|
+
this.flushPublish();
|
|
280
|
+
}, wait);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/** Publish now if anything changed since the last publish, and set when the next one may run. */
|
|
284
|
+
flushPublish() {
|
|
285
|
+
if (!this.publishPending || this.publishing) return;
|
|
286
|
+
this.cancelScheduledPublish();
|
|
287
|
+
this.publishPending = false;
|
|
288
|
+
this.publishing = true;
|
|
289
|
+
const start = Date.now();
|
|
290
|
+
try {
|
|
291
|
+
this.publishSnapshot();
|
|
292
|
+
} finally {
|
|
293
|
+
this.publishing = false;
|
|
294
|
+
}
|
|
295
|
+
const end = Date.now();
|
|
296
|
+
this.nextPublishAt = end + Math.max(MIN_PUBLISH_INTERVAL_MS, PUBLISH_BACKOFF_FACTOR * (end - start));
|
|
297
|
+
if (this.publishPending) this.requestPublish();
|
|
298
|
+
}
|
|
299
|
+
cancelScheduledPublish() {
|
|
300
|
+
if (this.publishTimer !== void 0) {
|
|
301
|
+
clearTimeout(this.publishTimer);
|
|
302
|
+
this.publishTimer = void 0;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
238
305
|
/**
|
|
239
306
|
* Produces a new store state with each `[path, value]` pair written into the
|
|
240
307
|
* draft via `dset`, leaving the live store untouched.
|
|
@@ -248,6 +315,7 @@ var ProfilerDevtoolsPlugin = class extends DevtoolsPlugin {
|
|
|
248
315
|
}
|
|
249
316
|
startProfiler() {
|
|
250
317
|
this.profilerObj.start();
|
|
318
|
+
this.flushPublish();
|
|
251
319
|
const newState = produce(this.store.getState(), (draft) => {
|
|
252
320
|
dset(draft, ["plugins", pluginID, "flow", "data", "profiling"], true);
|
|
253
321
|
dset(
|
|
@@ -266,6 +334,7 @@ var ProfilerDevtoolsPlugin = class extends DevtoolsPlugin {
|
|
|
266
334
|
}
|
|
267
335
|
clearProfiler() {
|
|
268
336
|
this.profilerObj.clear();
|
|
337
|
+
this.flushPublish();
|
|
269
338
|
}
|
|
270
339
|
stopProfiler() {
|
|
271
340
|
const result = this.profilerObj.stopProfiler();
|
|
@@ -286,6 +355,8 @@ var ProfilerDevtoolsPlugin = class extends DevtoolsPlugin {
|
|
|
286
355
|
pluginID
|
|
287
356
|
})
|
|
288
357
|
);
|
|
358
|
+
this.publishPending = false;
|
|
359
|
+
this.cancelScheduledPublish();
|
|
289
360
|
return result;
|
|
290
361
|
}
|
|
291
362
|
apply(player) {
|
|
@@ -318,6 +389,10 @@ var ProfilerDevtoolsPlugin = class extends DevtoolsPlugin {
|
|
|
318
389
|
} = interaction;
|
|
319
390
|
this.interactionMap.get(type)?.();
|
|
320
391
|
}
|
|
392
|
+
teardown() {
|
|
393
|
+
this.cancelScheduledPublish();
|
|
394
|
+
super.teardown();
|
|
395
|
+
}
|
|
321
396
|
};
|
|
322
397
|
export {
|
|
323
398
|
ProfilerDevtoolsPlugin
|