From 990674f7e36bdf7153ed33f3a35b3fdbbd0671f2 Mon Sep 17 00:00:00 2001 From: OrcaWin Date: Mon, 7 Sep 2026 22:21:52 -0700 Subject: [PATCH] perf: sum omitted workspace sizes without intermediate objects (#19491) Co-authored-by: m4air --- src/shared/workspace-space-compaction.test.ts | 50 +++++++++++++++++++ src/shared/workspace-space-compaction.ts | 25 ++++------ 2 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 src/shared/workspace-space-compaction.test.ts diff --git a/src/shared/workspace-space-compaction.test.ts b/src/shared/workspace-space-compaction.test.ts new file mode 100644 index 00000000000..bf774645fbb --- /dev/null +++ b/src/shared/workspace-space-compaction.test.ts @@ -0,0 +1,50 @@ +import { expect, it, vi } from 'vitest' +import { compactWorkspaceSpaceItems } from './workspace-space-compaction' +import type { WorkspaceSpaceItem } from './workspace-space-types' + +it('sums omitted sizes without constructing a replacement object per omitted item', () => { + const items: WorkspaceSpaceItem[] = Array.from({ length: 10000 }, (_, index) => ({ + name: String(index), + path: String(index), + kind: 'file', + sizeBytes: index + })) + const original = Array.prototype.reduce + let objectAccumulators = 0 + const spy = vi.spyOn(Array.prototype, 'reduce').mockImplementation(function ( + this: unknown[], + callback, + initial: unknown + ) { + if (initial && typeof initial === 'object' && 'name' in initial && initial.name === 'Other') { + objectAccumulators += this.length + } + return Reflect.apply(original, this, [callback, initial]) + }) + let result: ReturnType + try { + result = compactWorkspaceSpaceItems(items) + } finally { + spy.mockRestore() + } + expect(objectAccumulators).toBe(0) + expect(result!.topLevelItems).toHaveLength(48) + expect(result!.omittedTopLevelItemCount).toBe(9953) + expect(result!.omittedTopLevelSizeBytes).toBe((9952 * 9953) / 2) + expect(result!.topLevelItems[0]).toBe(items[9999]) + expect(items[0].sizeBytes).toBe(0) +}) + +it('preserves small-list size ties and empty results', () => { + expect(compactWorkspaceSpaceItems([]).topLevelItems).toEqual([]) + const items: WorkspaceSpaceItem[] = ['b', 'a'].map((name) => ({ + name, + path: name, + kind: 'file', + sizeBytes: 1 + })) + expect(compactWorkspaceSpaceItems(items).topLevelItems.map((item) => item.name)).toEqual([ + 'a', + 'b' + ]) +}) diff --git a/src/shared/workspace-space-compaction.ts b/src/shared/workspace-space-compaction.ts index 9b3ab739e92..b2b6bf99fb2 100644 --- a/src/shared/workspace-space-compaction.ts +++ b/src/shared/workspace-space-compaction.ts @@ -19,23 +19,20 @@ export function compactWorkspaceSpaceItems(items: WorkspaceSpaceItem[]): { } const visible = sorted.slice(0, WORKSPACE_SPACE_MAX_TOP_LEVEL_ITEMS - 1) - const omitted = sorted.slice(WORKSPACE_SPACE_MAX_TOP_LEVEL_ITEMS - 1) - const other = omitted.reduce( - (acc, item) => ({ - ...acc, - sizeBytes: acc.sizeBytes + item.sizeBytes - }), - { - name: 'Other', - path: '', - kind: 'other', - sizeBytes: 0 - } - ) + let omittedSizeBytes = 0 + for (let index = visible.length; index < sorted.length; index += 1) { + omittedSizeBytes += sorted[index].sizeBytes + } + const other: WorkspaceSpaceItem = { + name: 'Other', + path: '', + kind: 'other', + sizeBytes: omittedSizeBytes + } return { topLevelItems: [...visible, other], - omittedTopLevelItemCount: omitted.length, + omittedTopLevelItemCount: sorted.length - visible.length, omittedTopLevelSizeBytes: other.sizeBytes } }