Chrome DevTools for coding agents
perf: Load heap snapshots in parallel in compare_heapsnapshots (#3023)
Dominik Inführ20c1391b89f9
diff --git a/src/McpContext.ts b/src/McpContext.tsindex 52e21f15..06b361d6 100644--- a/src/McpContext.ts+++ b/src/McpContext.ts@@ -867,7 +867,7 @@ export class McpContext implements Context {}async closeHeapSnapshot(filePath: string): Promise<boolean> {- return this.#heapSnapshotManager.disposeSnapshot(filePath);+ return await this.#heapSnapshotManager.disposeSnapshot(filePath);}hasHeapSnapshots(): boolean {diff --git a/src/processors/HeapSnapshotManager.ts b/src/processors/HeapSnapshotManager.tsindex 6c6ab502..fcd7a326 100644--- a/src/processors/HeapSnapshotManager.ts+++ b/src/processors/HeapSnapshotManager.ts@@ -48,6 +48,41 @@ export type HeapQueryOptions =export type HeapEdgesQueryOptions =DevTools.HeapSnapshotModel.HeapSnapshotModel.HeapEdgesQueryOptions;++export class HeapSnapshot {+ proxy: DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotProxy;+ worker: DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotWorkerProxy;+ // 1-indexed array where index is the class ID.+ #idToClassKey: string[] = [''];+ #classKeyToId = new Map<string, number>();++ constructor(+ proxy: DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotProxy,+ worker: DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotWorkerProxy,+ aggregates: Record<+ string,+ DevTools.HeapSnapshotModel.HeapSnapshotModel.AggregatedInfo+ >,+ ) {+ this.proxy = proxy;+ this.worker = worker;++ for (const key of Object.keys(aggregates)) {+ const id = this.#idToClassKey.length;+ this.#classKeyToId.set(key, id);+ this.#idToClassKey.push(key);+ }+ }++ resolveIdFromClassKey(classKey: string): number | undefined {+ return this.#classKeyToId.get(classKey);+ }++ resolveClassKeyFromId(id: number): string | undefined {+ return this.#idToClassKey[id];+ }+}+const VALID_EXTENSIONS: readonly string[] = ['.heapsnapshot', '.heaptimeline'];function hasValidHeapSnapshotExtension(filePath: string): boolean {@@ -56,50 +91,23 @@ function hasValidHeapSnapshotExtension(filePath: string): boolean {export class HeapSnapshotManager {#snapshotIdGenerator = createIdGenerator();- #snapshots = new Map<- string,- {- snapshot: DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotProxy;- worker: DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotWorkerProxy;- // 1-indexed array where index is the class ID.- idToClassKey: string[];- classKeyToId: Map<string, number>;- }- >();+ #snapshots = new Map<string, Promise<HeapSnapshot>>();- async getSnapshot(- filePath: string,- ): Promise<DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotProxy> {+ async getSnapshot(filePath: string): Promise<HeapSnapshot> {if (!hasValidHeapSnapshotExtension(filePath)) {throw new Error(`File ${filePath} must have a .heapsnapshot or .heaptimeline extension.`,);}const absolutePath = path.resolve(filePath);- const cached = this.#snapshots.get(absolutePath);- if (cached) {- return cached.snapshot;- }-- const uid = this.#snapshotIdGenerator();- const {snapshot, worker} = await this.#loadSnapshot(absolutePath, uid);- this.#snapshots.set(absolutePath, {- snapshot,- worker,- idToClassKey: [''],- classKeyToId: new Map<string, number>(),- });- // Assign ids to all aggregates of this snapshot. This ensures ids are available- // even without calling getAggregates() and deterministic (do not depend on- // the filter used on the getAggregates() call).- const aggregates = await snapshot.aggregatesWithFilter(- new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter(),- );- for (const key of Object.keys(aggregates)) {- this.getOrCreateIdForClassKey(filePath, key);+ let cached = this.#snapshots.get(absolutePath);+ if (!cached) {+ const uid = this.#snapshotIdGenerator();+ cached = this.#loadSnapshot(absolutePath, uid);+ this.#snapshots.set(absolutePath, cached);}- return snapshot;+ return await cached;}async #applyNodeFilter(@@ -132,14 +140,14 @@ export class HeapSnapshotManager {const snapshot = await this.getSnapshot(filePath);const filter =new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter();- await this.#applyNodeFilter(snapshot, filter, filterName, objectId);+ await this.#applyNodeFilter(snapshot.proxy, filter, filterName, objectId);const aggregates: Record<string, AggregatedInfoWithId> =- await snapshot.aggregatesWithFilter(filter);+ await snapshot.proxy.aggregatesWithFilter(filter);let objectCount = 0;let totalSelfSize = 0;for (const [key, aggregate] of Object.entries(aggregates)) {- const id = this.getOrCreateIdForClassKey(filePath, key);+ const id = snapshot.resolveIdFromClassKey(key);aggregate[stableIdSymbol] = id;objectCount += aggregate.count;totalSelfSize += aggregate.self;@@ -156,39 +164,28 @@ export class HeapSnapshotManager {filePath: string,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.Statistics> {const snapshot = await this.getSnapshot(filePath);- return await snapshot.getStatistics();+ return await snapshot.proxy.getStatistics();}async getStaticData(filePath: string,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.StaticData | null> {const snapshot = await this.getSnapshot(filePath);- return snapshot.staticData;+ return snapshot.proxy.staticData;}async getNativeContextSizes(filePath: string,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.NativeContextSizes> {const snapshot = await this.getSnapshot(filePath);- return await snapshot.getNativeContextSizes();+ return await snapshot.proxy.getNativeContextSizes();}async getRetainedByContextSummary(filePath: string,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.RetainedByContextSummary> {const snapshot = await this.getSnapshot(filePath);- return await snapshot.getRetainedByContextSummary();- }-- getOrCreateIdForClassKey(filePath: string, classKey: string): number {- const cached = this.#getCachedSnapshot(filePath);- let id = cached.classKeyToId.get(classKey);- if (!id) {- id = cached.idToClassKey.length;- cached.classKeyToId.set(classKey, id);- cached.idToClassKey.push(classKey);- }- return id;+ return await snapshot.proxy.getRetainedByContextSummary();}async getNodesById(@@ -200,12 +197,15 @@ export class HeapSnapshotManager {const snapshot = await this.getSnapshot(filePath);const filter =new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter();- await this.#applyNodeFilter(snapshot, filter, filterName, objectId);- const className = this.resolveClassKeyFromId(filePath, id);+ await this.#applyNodeFilter(snapshot.proxy, filter, filterName, objectId);+ const className = snapshot.resolveClassKeyFromId(id);if (!className) {throw new Error(`Class with ID ${id} not found in heap snapshot`);}- const provider = snapshot.createNodesProviderForClass(className, filter);+ const provider = snapshot.proxy.createNodesProviderForClass(+ className,+ filter,+ );return await provider.serializeItemsRange(0, Infinity);}@@ -215,11 +215,11 @@ export class HeapSnapshotManager {nodeId: number,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.ItemsRange> {const snapshot = await this.getSnapshot(filePath);- const nodeIndex = await snapshot.nodeIndexForId(nodeId);+ const nodeIndex = await snapshot.proxy.nodeIndexForId(nodeId);if (nodeIndex === undefined) {throw new Error(`Node with ID ${nodeId} not found`);}- const provider = snapshot.createRetainingEdgesProvider(nodeIndex);+ const provider = snapshot.proxy.createRetainingEdgesProvider(nodeIndex);return await provider.serializeItemsRange(0, Infinity);}@@ -228,18 +228,18 @@ export class HeapSnapshotManager {nodeId: number,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.ObjectInfo> {const snapshot = await this.getSnapshot(filePath);- const nodeIndex = await snapshot.nodeIndexForId(nodeId);+ const nodeIndex = await snapshot.proxy.nodeIndexForId(nodeId);if (nodeIndex === undefined) {throw new Error(`Node with ID ${nodeId} not found`);}- return await snapshot.getObjectInfo(nodeIndex);+ return await snapshot.proxy.getObjectInfo(nodeIndex);}async analyzeContexts(filePath: string,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.ContextAnalysisResult> {const snapshot = await this.getSnapshot(filePath);- return await snapshot.analyzeContexts();+ return await snapshot.proxy.analyzeContexts();}async getRetainingPaths(@@ -250,11 +250,11 @@ export class HeapSnapshotManager {maxSiblings?: number,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.RetainingPaths> {const snapshot = await this.getSnapshot(filePath);- const nodeIndex = await snapshot.nodeIndexForId(nodeId);+ const nodeIndex = await snapshot.proxy.nodeIndexForId(nodeId);if (nodeIndex === undefined) {throw new Error(`Node with ID ${nodeId} not found`);}- return await snapshot.getRetainingPaths(+ return await snapshot.proxy.getRetainingPaths(nodeIndex,maxDepth,maxNodes,@@ -267,11 +267,11 @@ export class HeapSnapshotManager {nodeId: number,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.DominatorChain> {const snapshot = await this.getSnapshot(filePath);- const nodeIndex = await snapshot.nodeIndexForId(nodeId);+ const nodeIndex = await snapshot.proxy.nodeIndexForId(nodeId);if (nodeIndex === undefined) {throw new Error(`Node with ID ${nodeId} not found`);}- return await snapshot.getDominatorsOf(nodeIndex);+ return await snapshot.proxy.getDominatorsOf(nodeIndex);}async getEdges(@@ -280,11 +280,11 @@ export class HeapSnapshotManager {options?: HeapEdgesQueryOptions,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.ItemsRange> {const snapshot = await this.getSnapshot(filePath);- const nodeIndex = await snapshot.nodeIndexForId(nodeId);+ const nodeIndex = await snapshot.proxy.nodeIndexForId(nodeId);if (nodeIndex === undefined) {throw new Error(`Node with ID ${nodeId} not found`);}- const provider = snapshot.createEdgesProvider(nodeIndex, options);+ const provider = snapshot.proxy.createEdgesProvider(nodeIndex, options);return await provider.serializeItemsRange(0, Infinity);}@@ -337,37 +337,26 @@ export class HeapSnapshotManager {};}- #getCachedSnapshot(filePath: string): {- snapshot: DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotProxy;- worker: DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotWorkerProxy;- idToClassKey: string[];- classKeyToId: Map<string, number>;- } {- const absolutePath = path.resolve(filePath);- const cached = this.#snapshots.get(absolutePath);- if (!cached) {- throw new Error(`Snapshot not loaded for ${filePath}`);- }- return cached;- }-async #getSortedRawClassDiffs(baseFilePath: string,currentFilePath: string,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.Diff[]> {- const baseSnapshot = await this.getSnapshot(baseFilePath);- const currentSnapshot = await this.getSnapshot(currentFilePath);+ const [baseSnapshot, currentSnapshot] = await Promise.all([+ this.getSnapshot(baseFilePath),+ this.getSnapshot(currentFilePath),+ ]);- const interfaceDefinitions = await currentSnapshot.interfaceDefinitions();+ const interfaceDefinitions =+ await currentSnapshot.proxy.interfaceDefinitions();const aggregatesForDiff =- await baseSnapshot.aggregatesForDiff(interfaceDefinitions);- const baseSnapshotId = baseSnapshot.uid;+ await baseSnapshot.proxy.aggregatesForDiff(interfaceDefinitions);+ const baseSnapshotId = baseSnapshot.proxy.uid;if (baseSnapshotId === undefined) {throw new Error('Base snapshot UID is undefined');}// DevTools calculateSnapshotDiff uses the first parameter (baseSnapshotId)// as a cache key. We pass the unique UID of the base snapshot.- const rawDiffs = await currentSnapshot.calculateSnapshotDiff(+ const rawDiffs = await currentSnapshot.proxy.calculateSnapshotDiff(baseSnapshotId,aggregatesForDiff,);@@ -380,18 +369,10 @@ export class HeapSnapshotManager {.sort((a, b) => b.sizeDelta - a.sizeDelta);}- resolveClassKeyFromId(filePath: string, id: number): string | undefined {- const cached = this.#getCachedSnapshot(filePath);- return cached.idToClassKey[id];- }-async #loadSnapshot(absolutePath: string,uid: number,- ): Promise<{- snapshot: DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotProxy;- worker: DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotWorkerProxy;- }> {+ ): Promise<HeapSnapshot> {const workerProxy =new DevTools.HeapSnapshotModel.HeapSnapshotProxy.HeapSnapshotWorkerProxy(() => {@@ -421,12 +402,18 @@ export class HeapSnapshotManager {await loaderProxy.close();const snapshot = await snapshotPromise;- return {snapshot, worker: workerProxy};+ // Assign ids to all aggregates of this snapshot. This ensures ids are available+ // even without calling getAggregates() and deterministic (do not depend on+ // the filter used on the getAggregates() call).+ const aggregates = await snapshot.aggregatesWithFilter(+ new DevTools.HeapSnapshotModel.HeapSnapshotModel.NodeFilter(),+ );+ return new HeapSnapshot(snapshot, workerProxy, aggregates);} catch (error) {- // The worker is created before the read, and a failed load never reaches- // the #snapshots map, so dispose()/disposeAll() can never clean it up.- // Dispose it here to avoid leaking a worker on every failed load (e.g. a- // missing or invalid .heapsnapshot path).+ // Remove the rejected promise from #snapshots and dispose the worker to+ // avoid leaking it on a failed load (e.g. a missing or invalid+ // .heapsnapshot path).+ this.#snapshots.delete(absolutePath);workerProxy.dispose();throw error;}@@ -434,7 +421,7 @@ export class HeapSnapshotManager {async getDuplicateStrings(filePath: string): Promise<DuplicateStringGroup[]> {const snapshot = await this.getSnapshot(filePath);- return await snapshot.getDuplicateStrings();+ return await snapshot.proxy.getDuplicateStrings();}async queryObjects(@@ -442,7 +429,7 @@ export class HeapSnapshotManager {options: HeapQueryOptions,): Promise<DevTools.HeapSnapshotModel.HeapSnapshotModel.ItemsRange> {const snapshot = await this.getSnapshot(filePath);- const provider = snapshot.queryObjects(options);+ const provider = snapshot.proxy.queryObjects(options);return await provider.serializeItemsRange(0, Infinity);}@@ -450,21 +437,25 @@ export class HeapSnapshotManager {return this.#snapshots.size > 0;}- disposeSnapshot(filePath: string): boolean {+ async disposeSnapshot(filePath: string): Promise<boolean> {const absolutePath = path.resolve(filePath);const cached = this.#snapshots.get(absolutePath);if (cached) {- cached.worker.dispose();this.#snapshots.delete(absolutePath);+ const {worker} = await cached;+ worker.dispose();return true;}return false;}dispose(): void {- for (const cached of this.#snapshots.values()) {- cached.worker.dispose();- }+ void Promise.allSettled(+ this.#snapshots.values().map(async cached => {+ const {worker} = await cached;+ worker.dispose();+ }),+ );this.#snapshots.clear();}}diff --git a/tests/processors/HeapSnapshotManager.test.ts b/tests/processors/HeapSnapshotManager.test.tsindex dd62a013..ad95f0df 100644--- a/tests/processors/HeapSnapshotManager.test.ts+++ b/tests/processors/HeapSnapshotManager.test.ts@@ -184,8 +184,8 @@ describe('HeapSnapshotManager', () => {assert.ok(staticData.nodeCount > 0);assert.strictEqual(manager.hasSnapshots(), true);- assert.strictEqual(manager.disposeSnapshot(filePath), true);- assert.strictEqual(manager.disposeSnapshot(filePath), false);+ assert.strictEqual(await manager.disposeSnapshot(filePath), true);+ assert.strictEqual(await manager.disposeSnapshot(filePath), false);assert.strictEqual(manager.hasSnapshots(), false);} finally {manager.dispose();