perf: stop filesystem authorization at the first matching root (#19438)

Co-authored-by: m4air <m4air@m4airs-MacBook-Air.local>
This commit is contained in:
OrcaWin
2026-09-07 22:09:56 -07:00
committed by GitHub
co-authored by m4air
parent 32510c9041
commit 919087897e
2 changed files with 59 additions and 21 deletions
@@ -8,6 +8,7 @@ import { listRepoWorktreeGraph } from '../repo-worktrees'
import type * as ProjectGroupsModule from '../../shared/project-groups'
import { buildProjectGroupChildIndex, getProjectGroupSubtreeIds } from '../../shared/project-groups'
import { isPathInsideOrEqual } from '../../shared/cross-platform-path'
import type * as CrossPlatformPathModule from '../../shared/cross-platform-path'
import { getWorktreeMirrorDistro } from '../project-runtime-git-options'
import type { FolderWorkspace } from '../../shared/folder-workspace-types'
import type { ProjectGroup } from '../../shared/project-group-types'
@@ -32,6 +33,13 @@ vi.mock('../../shared/project-groups', async () => {
}
})
vi.mock('../../shared/cross-platform-path', async () => {
const actual = await vi.importActual<typeof CrossPlatformPathModule>(
'../../shared/cross-platform-path'
)
return { ...actual, isPathInsideOrEqual: vi.fn(actual.isPathInsideOrEqual) }
})
type StoreFixture = {
repos: Repo[]
projects: Project[]
@@ -257,6 +265,42 @@ beforeEach(() => {
})
describe('getAllowedRoots', () => {
it('stops scanning repositories when a local candidate settles each folder scope', () => {
const fixture: StoreFixture = {
repos: Array.from({ length: 1_000 }, (_, index) =>
makeRepo({ id: `repo-${index}`, path: `/folders/root/repo-${index}` })
),
projects: [],
projectGroups: [],
folderWorkspaces: Array.from({ length: 100 }, (_, index) =>
makeWorkspace({ id: `folder-${index}`, folderPath: '/folders/root' })
)
}
const { store } = makeCountingStore(fixture)
vi.mocked(isPathInsideOrEqual).mockClear()
const actual = getAllowedRoots(store)
expect(isPathInsideOrEqual).toHaveBeenCalledTimes(100)
vi.mocked(isPathInsideOrEqual).mockClear()
expect(actual).toEqual(referenceAllowedRoots(store))
expect(isPathInsideOrEqual).toHaveBeenCalledTimes(100_000)
})
it('preserves empty, remote-only, mixed and explicit remote folder scopes in any repo order', () => {
const fixture = makeMixedFixture()
fixture.repos.push(
makeRepo({
id: 'local-in-remote-group',
path: '/local/mixed',
projectGroupId: 'group-remote'
})
)
for (let index = 0; index < fixture.repos.length; index += 1) {
fixture.repos.push(fixture.repos.shift()!)
const { store } = makeCountingStore(fixture)
expect(getAllowedRoots(store)).toEqual(referenceAllowedRoots(store))
}
})
it('produces the same roots as the pre-change implementation', () => {
const { store } = makeCountingStore(makeMixedFixture())
+15 -21
View File
@@ -27,20 +27,6 @@ export function getLocalRepos(store: Store) {
return filterLocalRepos(store.getRepos())
}
function getFolderScopeCandidateRepos(
folderPath: string,
projectGroupId: string,
childGroupIndex: ProjectGroupChildIndex,
repos: readonly Repo[]
): Repo[] {
const groupIds = collectProjectGroupSubtreeIds(childGroupIndex, projectGroupId)
return repos.filter(
(repo) =>
(typeof repo.projectGroupId === 'string' && groupIds.has(repo.projectGroupId)) ||
isPathInsideOrEqual(folderPath, repo.path)
)
}
function isRemoteOnlyFolderScope(
folderPath: string,
projectGroupId: string,
@@ -51,13 +37,21 @@ function isRemoteOnlyFolderScope(
if (connectionId) {
return true
}
const candidates = getFolderScopeCandidateRepos(
folderPath,
projectGroupId,
childGroupIndex,
repos
)
return candidates.length > 0 && candidates.every((repo) => Boolean(repo.connectionId))
const groupIds = collectProjectGroupSubtreeIds(childGroupIndex, projectGroupId)
let hasRemoteCandidate = false
for (const repo of repos) {
if (
(typeof repo.projectGroupId === 'string' && groupIds.has(repo.projectGroupId)) ||
isPathInsideOrEqual(folderPath, repo.path)
) {
// One local candidate settles the scope without scanning the remaining repositories.
if (!repo.connectionId) {
return false
}
hasRemoteCandidate = true
}
}
return hasRemoteCandidate
}
function getFolderWorkspaceConnectionId(