perf: index document history workspace and path identities (#19461)

* perf: index document history workspace and path identities

* docs(browser): note doc-history dedupe must mirror location equality

---------

Co-authored-by: m4air <m4air@m4airs-MacBook-Air.local>
Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
This commit is contained in:
OrcaWin
2026-09-08 19:39:42 -07:00
committed by GitHub
co-authored by m4air Neil
parent 86fe80da25
commit df8a43724f
2 changed files with 44 additions and 4 deletions
+32
View File
@@ -53,4 +53,36 @@ describe('normalizeWorkspaceDocHistoryEntries', () => {
expect(normalizeWorkspaceDocHistoryTitle('', DOC)).toBe('a.html')
expect(normalizeWorkspaceDocHistoryTitle('Report', DOC)).toBe('Report')
})
it('indexes document identity while deduplicating a large legacy history', () => {
let reads = 0
const entries = Array.from({ length: 10_000 }, (_, i) =>
entry({
docLocation: {
kind: 'workspace-doc',
get worktreeId() {
reads++
return 'wt-1'
},
filePath: `/repo/${i % 99}.html`
},
lastVisitedAt: i
})
)
const result = normalizeWorkspaceDocHistoryEntries(entries)
expect(reads).toBeLessThanOrEqual(20_000)
expect(result).toHaveLength(99)
expect(result.map((row) => row.lastVisitedAt)).toEqual(
Array.from({ length: 99 }, (_, i) => 9999 - i)
)
})
it('keeps workspace and file identity separate, including separator-like text', () => {
const locations = [
{ kind: 'workspace-doc' as const, worktreeId: 'a::b', filePath: 'c' },
{ kind: 'workspace-doc' as const, worktreeId: 'a', filePath: 'b::c' },
{ kind: 'workspace-doc' as const, worktreeId: 'a', filePath: 'B::c' }
]
const entries = locations.map((docLocation) => entry({ docLocation }))
expect(normalizeWorkspaceDocHistoryEntries(entries)).toEqual(entries)
})
})
+12 -4
View File
@@ -1,4 +1,3 @@
import { browserPageDocLocationsEqual } from './browser-page-doc-location'
import type { BrowserPageDocLocation } from './browser-workspace-types'
import { isDocPreviewUrl } from './doc-preview-scheme'
@@ -47,6 +46,7 @@ export function normalizeWorkspaceDocHistoryEntries(
entries: readonly WorkspaceDocHistoryEntry[]
): WorkspaceDocHistoryEntry[] {
const normalized: WorkspaceDocHistoryEntry[] = []
const seenPathsByWorktree = new Map<string, Set<string>>()
const candidates = [...entries].sort((a, b) => b.lastVisitedAt - a.lastVisitedAt)
for (const entry of candidates) {
if (
@@ -56,11 +56,19 @@ export function normalizeWorkspaceDocHistoryEntries(
) {
continue
}
if (
normalized.some((kept) => browserPageDocLocationsEqual(kept.docLocation, entry.docLocation))
) {
// Nested, not a joined key: any separator would collide with worktree ids or paths that
// contain it. Must stay equivalent to `browserPageDocLocationsEqual`, which the store's
// doc-history dedupe still uses — divergence would show up as duplicate dropdown rows.
const { worktreeId, filePath } = entry.docLocation
const seenPaths = seenPathsByWorktree.get(worktreeId)
if (seenPaths?.has(filePath)) {
continue
}
if (seenPaths) {
seenPaths.add(filePath)
} else {
seenPathsByWorktree.set(worktreeId, new Set([filePath]))
}
normalized.push({
...entry,
title: normalizeWorkspaceDocHistoryTitle(entry.title, entry.docLocation)