From 656a05de0d7bef67fc8e6aa4fcd57001215db5a0 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 8 Nov 2026 05:05:19 -0800 Subject: [PATCH] perf(main): overlap independent fs probes; skip per-entry copies (#39729) * perf(main): overlap independent fs probes on git status, agent detection and orcad startup * perf: skip per-entry copies in git change list and tab hydration; drop unused activity barrel --- .../agent-hooks/local-agent-cli-presence.ts | 18 ++++++------- src/main/git/huge-folder-ignore.ts | 25 +++++++++--------- .../worktree-symlink-detection.ts | 26 +++++++++---------- src/main/orcad/orcad-startup-qualification.ts | 12 ++++++--- .../activity/ActivityPrototypePage.tsx | 2 -- .../activity-prototype-page-exports.ts | 17 ------------ .../src/store/slices/tabs/tabs-hydration.ts | 16 +++++------- src/shared/git/diff/git-change-list.ts | 6 ++++- 8 files changed, 56 insertions(+), 66 deletions(-) delete mode 100644 src/renderer/src/components/activity/activity-prototype-page-exports.ts diff --git a/src/main/agent-hooks/local-agent-cli-presence.ts b/src/main/agent-hooks/local-agent-cli-presence.ts index 77fd339e019..76d44cf188b 100644 --- a/src/main/agent-hooks/local-agent-cli-presence.ts +++ b/src/main/agent-hooks/local-agent-cli-presence.ts @@ -10,6 +10,7 @@ import { } from '../../shared/managed-agent-command-token' import type { ManagedAgentHookTarget } from '../../shared/managed-agent-hook-targets' import type { GlobalSettings } from '../../shared/global-settings-types' +import { mapWithConcurrency } from '../../shared/map-with-concurrency' import { hydrateShellPath, mergePathSegments } from '../startup/hydrate-shell-path' import { mergePersistedWindowsPathAsync } from '../pty/environment/windows-environment-path' import { resolvePathEnvKey } from '../pty/environment/windows-path-segment-merge' @@ -50,6 +51,7 @@ type DetectOptions = { type CommandOverrideSettings = Partial> | null | undefined const DEFAULT_WINDOWS_EXTENSIONS = ['.COM', '.EXE', '.BAT', '.CMD'] +const CANDIDATE_PROBE_CONCURRENCY = 8 function pathApiForPlatform(platform: NodeJS.Platform) { return platform === 'win32' ? path.win32 : path.posix @@ -212,17 +214,15 @@ export async function detectLocalManagedAgentCliPresence( candidates.add(override) } } + // Candidates probe independently; each keeps its own ordered PATH walk so first-match wins. + const uniqueCandidates = [...candidates] + const probed = await mapWithConcurrency(uniqueCandidates, CANDIDATE_PROBE_CONCURRENCY, (c) => + probePathCandidate(c, dirs, platform, fileProbe, options.pathExt) + ) const found = new Map() - for (const candidate of candidates) { - const executablePath = await probePathCandidate( - candidate, - dirs, - platform, - fileProbe, - options.pathExt - ) + for (const [index, executablePath] of probed.entries()) { if (executablePath) { - found.set(candidate, executablePath) + found.set(uniqueCandidates[index], executablePath) } } const result: LocalCliPresenceByAgent = {} diff --git a/src/main/git/huge-folder-ignore.ts b/src/main/git/huge-folder-ignore.ts index a9e6fd0a064..15cdb5f03f0 100644 --- a/src/main/git/huge-folder-ignore.ts +++ b/src/main/git/huge-folder-ignore.ts @@ -11,18 +11,19 @@ export async function findKnownHugeFolderPathsToIgnore( worktreePath: string, options: GitRuntimeOptions = {} ): Promise { - const existing: string[] = [] - for (const name of KNOWN_HUGE_FOLDER_NAMES) { - const full = path.join(worktreePath, name) - if (!existsSync(full)) { - continue - } - try { - if ((await stat(full)).isDirectory()) { - existing.push(name) - } - } catch {} - } + const candidates = KNOWN_HUGE_FOLDER_NAMES.filter((name) => + existsSync(path.join(worktreePath, name)) + ) + // Fixed six-name allowlist, so the independent stats can all run at once. + const isDirectory = await Promise.all( + candidates.map((name) => + stat(path.join(worktreePath, name)).then( + (stats) => stats.isDirectory(), + () => false + ) + ) + ) + const existing = candidates.filter((_, index) => isDirectory[index]) if (existing.length === 0) { return [] } diff --git a/src/main/git/worktrees/content-paths/worktree-symlink-detection.ts b/src/main/git/worktrees/content-paths/worktree-symlink-detection.ts index b9e71f794b9..9ce37f54ad7 100644 --- a/src/main/git/worktrees/content-paths/worktree-symlink-detection.ts +++ b/src/main/git/worktrees/content-paths/worktree-symlink-detection.ts @@ -1,6 +1,7 @@ import { lstat } from 'node:fs/promises' import { resolve, sep } from 'node:path' import { resolveWorktreeHostPath } from '../../../../shared/git/metadata/git-metadata-path' +import { mapWithConcurrency } from '../../../../shared/map-with-concurrency' // Keep read-only symlink detection independent of APFS materialization and child-process imports. @@ -8,6 +9,7 @@ export type SafeRelativePathResult = { safe: true; rel: string } | { safe: false // isAbsolute misses drive-relative C:foo, which still escapes the worktree on Windows. const WINDOWS_DRIVE_DESIGNATOR = /^[a-zA-Z]:/ +const SYMLINK_PROBE_CONCURRENCY = 8 export function getSafeRelativePath(rawPath: string): SafeRelativePathResult { // Strip either separator style before validating so host syntax cannot change the verdict. @@ -44,19 +46,17 @@ export async function findExistingWorktreeSymlinkPaths( ): Promise { // Node filesystem reads need host spelling even when WSL Git reported guest paths. const hostWorktreePath = resolveWorktreeHostPath(worktreePath, options) ?? worktreePath - const symlinkPaths: string[] = [] - for (const rawPath of paths) { + const safePaths = paths.flatMap((rawPath) => { const safePath = getSafeRelativePath(rawPath) - if (!safePath.safe) { - continue - } - try { - if ((await lstat(resolve(hostWorktreePath, safePath.rel))).isSymbolicLink()) { - symlinkPaths.push(safePath.rel) - } - } catch { + return safePath.safe ? [safePath.rel] : [] + }) + // Independent lstats: overlap them so slow WSL/UNC round-trips don't serialize git status. + const isSymlink = await mapWithConcurrency(safePaths, SYMLINK_PROBE_CONCURRENCY, (rel) => + lstat(resolve(hostWorktreePath, rel)).then( + (stats) => stats.isSymbolicLink(), // Why: only a positively identified symlink may bypass dirty preflight. - } - } - return symlinkPaths + () => false + ) + ) + return safePaths.filter((_, index) => isSymlink[index]) } diff --git a/src/main/orcad/orcad-startup-qualification.ts b/src/main/orcad/orcad-startup-qualification.ts index 4ca7643bf34..8236f357a99 100644 --- a/src/main/orcad/orcad-startup-qualification.ts +++ b/src/main/orcad/orcad-startup-qualification.ts @@ -6,10 +6,12 @@ import { isAbsolute, join, relative } from 'node:path' import process from 'node:process' import { ORCAD_MANAGED_ACTIVATION_ROOT_ENV } from '../../shared/orcad-managed-launch-env' import { ORCAD_NODE_RUNTIME_IDENTITY } from '../../shared/orcad-node-runtime-identity' +import { mapWithConcurrency } from '../../shared/map-with-concurrency' import { listOrcadArtifactFilenames } from './orcad-artifact-identity' import { detectNativeHostAbi } from './native-host-abi' export const ORCAD_STARTUP_QUALIFIED_FILENAME = '.orcad-startup-qualified' +const ARTIFACT_STAT_CONCURRENCY = 16 // Launch environment that changes how Node, its native modules or the probe's temp files behave. const ORCAD_STARTUP_QUALIFICATION_ENV = [ @@ -47,10 +49,14 @@ export async function orcadStartupQualificationKey( artifactVersion: string, env: NodeJS.ProcessEnv = process.env ): Promise { + const filenames = await listOrcadArtifactFilenames(directory) + // Startup path: overlap the independent stats; hash in listing order so the key is unchanged. + const stats = await mapWithConcurrency(filenames, ARTIFACT_STAT_CONCURRENCY, (filename) => + stat(join(directory, filename)) + ) const files = createHash('sha256') - for (const filename of await listOrcadArtifactFilenames(directory)) { - const { size, mtimeMs, ctimeMs, ino } = await stat(join(directory, filename)) - files.update(JSON.stringify([filename, size, mtimeMs, ctimeMs, ino])) + for (const [index, { size, mtimeMs, ctimeMs, ino }] of stats.entries()) { + files.update(JSON.stringify([filenames[index], size, mtimeMs, ctimeMs, ino])) } const launchEnv = createHash('sha256') .update(JSON.stringify(ORCAD_STARTUP_QUALIFICATION_ENV.map((name) => env[name] ?? null))) diff --git a/src/renderer/src/components/activity/ActivityPrototypePage.tsx b/src/renderer/src/components/activity/ActivityPrototypePage.tsx index 889872ca834..f822f763156 100644 --- a/src/renderer/src/components/activity/ActivityPrototypePage.tsx +++ b/src/renderer/src/components/activity/ActivityPrototypePage.tsx @@ -28,8 +28,6 @@ import { selectWorktreesByRepo } from '@/store/workspaces/catalog/workspace-record-fields' -export * from './activity-prototype-page-exports' - export default function ActivityPrototypePage(): React.JSX.Element { const [query, setQuery] = useState('') const activityFilterInputRef = useRef(null) diff --git a/src/renderer/src/components/activity/activity-prototype-page-exports.ts b/src/renderer/src/components/activity/activity-prototype-page-exports.ts deleted file mode 100644 index 82eebb3ea06..00000000000 --- a/src/renderer/src/components/activity/activity-prototype-page-exports.ts +++ /dev/null @@ -1,17 +0,0 @@ -export { buildActivityEvents } from './activity-event-builder' -export { buildAgentPaneThreads } from './activity-thread-builder' -export { activityThreadResponseRenderPreview } from './activity-thread-presentation' -export { - ACTIVITY_SEARCH_QUERY_MAX_BYTES, - activityThreadMatchesSearchQuery, - buildActivityThreadGroups, - getActivityThreadGroup, - isActivitySearchQueryTooLarge -} from './activity-thread-grouping' -export { - handleActivityFilterFocusShortcut, - isActivityFilterFocusShortcut, - shouldIgnoreActivityFilterFocusShortcutTarget -} from './activity-filter-focus-shortcut' -export { ActivityThreadOptionsMenu, ThreadAgentStateIndicator } from './activity-thread-controls' -export { useActivityTerminalPortalStatus } from './activity-terminal-portal-status' diff --git a/src/renderer/src/store/slices/tabs/tabs-hydration.ts b/src/renderer/src/store/slices/tabs/tabs-hydration.ts index 04bfbbf8118..67f16258704 100644 --- a/src/renderer/src/store/slices/tabs/tabs-hydration.ts +++ b/src/renderer/src/store/slices/tabs/tabs-hydration.ts @@ -106,22 +106,20 @@ export function buildHydratedTabState( .filter((tab) => tab.aiVaultTitle) .map((tab) => [tab.id, tab.aiVaultTitle!]) ) - const hydratedTabs = [...tabs] - .map((tab) => ({ - ...tab, - entityId: tab.entityId ?? tab.id - })) + const hydratedTabs = tabs .map((tab) => { + const entityId = tab.entityId ?? tab.id if (tab.contentType !== 'terminal') { - return tab + return { ...tab, entityId } } const quickCommandLabel = tab.quickCommandLabel?.trim() ? tab.quickCommandLabel.trim() - : quickCommandLabelByTerminalId.get(tab.entityId) - const generatedLabel = generatedTitleByTerminalId.get(tab.entityId) - const aiVaultTitle = tab.aiVaultTitle ?? aiVaultTitleByTerminalId.get(tab.entityId) + : quickCommandLabelByTerminalId.get(entityId) + const generatedLabel = generatedTitleByTerminalId.get(entityId) + const aiVaultTitle = tab.aiVaultTitle ?? aiVaultTitleByTerminalId.get(entityId) return { ...tab, + entityId, ...(quickCommandLabel ? { quickCommandLabel } : {}), ...(aiVaultTitle ? { aiVaultTitle } : {}), ...(!tab.generatedLabel?.trim() && generatedLabel ? { generatedLabel } : {}) diff --git a/src/shared/git/diff/git-change-list.ts b/src/shared/git/diff/git-change-list.ts index a91c0b04bd9..c86bb0549d0 100644 --- a/src/shared/git/diff/git-change-list.ts +++ b/src/shared/git/diff/git-change-list.ts @@ -59,5 +59,9 @@ export function parseGitChangeList( return entries } const statsByPath = parseNumstat(stdout.slice(offset)) - return entries.map((entry) => ({ ...entry, ...statsByPath.get(entry.path) })) + // Why in place: entries are local records built above, so mutation is unobservable. + for (const entry of entries) { + Object.assign(entry, statsByPath.get(entry.path)) + } + return entries }