perf(graph): window the commit history rows
This commit is contained in:
1 parent
2237f3787b
commit
0a4a94c704
5 files changed
+226
-6
No files matched your search
@@ -7,7 +7,7 @@
|
||||
* Mounted by {@link GitLens} on the repo/worktree target the lens has already
|
||||
* selected; it owns its own bounded page (`git.graph`) and reports the
|
||||
* selected commit upward for the tab's bottom pane (`details` tab). */
|
||||
import { useEffect, useMemo, useRef, useState, type MouseEvent } from 'react'
|
||||
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, type MouseEvent } from 'react'
|
||||
import {
|
||||
Button, IconBranchOutlineRegular, IconCheckOutlineRegular, IconGlobeOutlineRegular,
|
||||
} from '@deepseek-ai/dsh-client-ui-primitives'
|
||||
@@ -17,6 +17,8 @@ import { relativeTime, t } from '../locales.ts'
|
||||
import { Notice } from '../ui/index.ts'
|
||||
import { AnchoredPopover } from '../AnchoredPopover.tsx'
|
||||
import { edgePath, layoutGraph } from './graph-layout.ts'
|
||||
import { createFrameBatcher } from '../frame-batcher.ts'
|
||||
import { windowedRange } from './git-graph-windowing.ts'
|
||||
import css from './graph.module.css'
|
||||
|
||||
type GraphPage = Awaited<ReturnType<typeof api.gitGraph>>
|
||||
@@ -138,6 +140,38 @@ export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onP
|
||||
const appending = useRef(false)
|
||||
const scrollRef = useRef<HTMLDivElement | null>(null)
|
||||
const sentinelRef = useRef<HTMLDivElement | null>(null)
|
||||
/**
|
||||
* The scroll container's measured viewport, and the per-frame coalescer that
|
||||
* keeps it in step with a scroll stream (a trackpad fires several scroll
|
||||
* events per display frame; one commit per frame is enough to move the
|
||||
* window, and each commit reads layout once).
|
||||
*/
|
||||
const [view, setView] = useState({ scrollTop: 0, height: 0 })
|
||||
const scrollBatcher = useRef(createFrameBatcher()).current
|
||||
const syncWindow = useCallback((): void => {
|
||||
const node = scrollRef.current
|
||||
if (node === null) return
|
||||
const scrollTop = node.scrollTop
|
||||
const height = node.clientHeight
|
||||
setView(current => (current.scrollTop === scrollTop && current.height === height
|
||||
? current
|
||||
: { scrollTop, height }))
|
||||
}, [])
|
||||
// A page append/shrink changes the content under the scroll position; the
|
||||
// browser clamps a stale scrollTop only after the DOM commit, so re-read it
|
||||
// in the layout phase. The pure range function clamps anyway.
|
||||
useLayoutEffect(() => { syncWindow() }, [syncWindow, page?.entries.length])
|
||||
// A panel resize (the details pane opening, a window resize, the sidebar
|
||||
// width drag) changes how many rows fit: re-measure so the mounted window
|
||||
// still covers the viewport instead of leaving a blank band.
|
||||
useEffect(() => {
|
||||
const node = scrollRef.current
|
||||
if (node === null || typeof ResizeObserver === 'undefined') return
|
||||
const observer = new ResizeObserver(() => { syncWindow() })
|
||||
observer.observe(node)
|
||||
return () => { observer.disconnect() }
|
||||
}, [syncWindow])
|
||||
useEffect(() => () => { scrollBatcher.dispose() }, [scrollBatcher])
|
||||
/** Row hover preview: the middle information rung between the one-line row
|
||||
* (pills + subject) and the details card (everything). It opens only after
|
||||
* a short dwell so scrolling past rows never flashes cards; the pointer
|
||||
@@ -227,6 +261,12 @@ export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onP
|
||||
|
||||
const rows = useMemo(() => layoutGraph(page?.entries ?? []), [page?.entries])
|
||||
const width = Math.max(28, ...rows.map(row => laneX(row.laneCount) + 2))
|
||||
const range = windowedRange({
|
||||
count: page?.entries.length ?? 0,
|
||||
rowHeight: ROW_HEIGHT,
|
||||
scrollTop: view.scrollTop,
|
||||
viewportHeight: view.height,
|
||||
})
|
||||
const remotes = useMemo(() => remoteRefNames(branches), [branches])
|
||||
const selectedIndex = selectedRef?.kind === 'commit'
|
||||
&& selectedRef.worktree === worktree
|
||||
@@ -262,7 +302,8 @@ export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onP
|
||||
setPage(null)
|
||||
}
|
||||
|
||||
return <section className={css.root} aria-label={t('gitGraph')} data-git-graph>
|
||||
return <section className={css.root} aria-label={t('gitGraph')} data-git-graph
|
||||
data-graph-count={page?.entries.length ?? 0}>
|
||||
<div className={css.toolbar}>
|
||||
<select className={css.select} aria-label={t('graphBranchFilter')}
|
||||
value={branch} onChange={event => { chooseBranch(event.target.value) }}>
|
||||
@@ -271,14 +312,21 @@ export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onP
|
||||
{branch && !branches.some(item => item.ref === branch) && <option value={branch}>{branch}</option>}
|
||||
</select>
|
||||
</div>
|
||||
<div className={css.scroll} aria-busy={loading} ref={scrollRef}>
|
||||
<div className={css.scroll} aria-busy={loading} ref={scrollRef} data-graph-scroll
|
||||
onScroll={() => {
|
||||
// The window may unmount the row the hover card is anchored to.
|
||||
if (hover !== null || hoverTimer.current !== null) cancelHover()
|
||||
scrollBatcher.schedule(syncWindow)
|
||||
}}>
|
||||
{error !== null && <div className={css.notice} role="alert">
|
||||
<Notice kind="warn">{stale ? t('graphStale') : t('graphLoadError')}{!stale && `: ${error}`}</Notice>
|
||||
<Button onClick={() => { if (stale || !failedAppend.current) setRetry(value => value + 1); else void request(true) }}>{t('retry')}</Button>
|
||||
</div>}
|
||||
{loading && <div className={css.notice} role="status">{t('loading')}</div>}
|
||||
{!loading && !error && page !== null && page.entries.length === 0 && <div className={css.notice}>{t(page.isRepo ? 'graphEmpty' : 'notRepo')}</div>}
|
||||
{page?.entries.map((entry, index) => {
|
||||
{range.top > 0 && <div aria-hidden="true" data-graph-spacer="top" style={{ height: range.top }} />}
|
||||
{page?.entries.slice(range.start, range.end).map((entry, offset) => {
|
||||
const index = range.start + offset
|
||||
const row = rows[index]!
|
||||
const slot = colorSlot(row.colorIndex)
|
||||
return <button key={entry.hashFull} type="button" className={css.row}
|
||||
@@ -302,6 +350,7 @@ export function GitGraphSection({ scope, worktree, visible, refreshTick = 0, onP
|
||||
<span className={css.subject}>{entry.subject}</span>
|
||||
</button>
|
||||
})}
|
||||
{range.bottom > 0 && <div aria-hidden="true" data-graph-spacer="bottom" style={{ height: range.bottom }} />}
|
||||
{page?.hasMore && !stale && <div className={css.notice} ref={sentinelRef} data-graph-sentinel>
|
||||
<Button disabled={loading} onClick={() => { void request(true) }}>{t('graphLoadMore')}</Button>
|
||||
</div>}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* The visible slice of a fixed-height row list (the commit graph's history).
|
||||
*
|
||||
* `page.entries` grows by one infinite-scroll page at a time (50 rows; the
|
||||
* host caps the walk at 10000) and every row carries an SVG rail, so mounting
|
||||
* the whole history made each append and each scroll cost O(history). Only
|
||||
* the viewport window (± overscan) is mounted; the two spacer heights keep the
|
||||
* real scrollbar, the end sentinel's position and the `data-graph-count` page
|
||||
* size intact.
|
||||
*
|
||||
* The arithmetic is pure and clamped on purpose: the list can shrink under the
|
||||
* scroll position (a branch switch, a refresh collapses the page), and the
|
||||
* browser only clamps `scrollTop` afterwards — a stale `scrollTop` must never
|
||||
* translate into an out-of-range window (that would render the wrong rows
|
||||
* while the scrollbar sits at the top).
|
||||
*/
|
||||
export interface WindowedRange {
|
||||
/** First mounted row (inclusive). */
|
||||
start: number
|
||||
/** One past the last mounted row. */
|
||||
end: number
|
||||
/** Pixel height of the rows above the window. */
|
||||
top: number
|
||||
/** Pixel height of the rows below the window. */
|
||||
bottom: number
|
||||
}
|
||||
|
||||
export interface WindowedRangeOptions {
|
||||
/** Total number of rows in the list. */
|
||||
count: number
|
||||
/** Height of one row in pixels (the graph's rows are a fixed 28). */
|
||||
rowHeight: number
|
||||
/** The scroll container's current `scrollTop`. */
|
||||
scrollTop: number
|
||||
/** The scroll container's client height; 0 before it is measured. */
|
||||
viewportHeight: number
|
||||
/** Extra rows kept mounted above and below the viewport (default 8). */
|
||||
overscan?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the mounted row range for one scroll position.
|
||||
* @param options - row count/height, scroll position, viewport height and overscan.
|
||||
* @returns The clamped half-open range plus the two spacer heights.
|
||||
*/
|
||||
export function windowedRange(options: WindowedRangeOptions): WindowedRange {
|
||||
const { count, rowHeight, scrollTop, viewportHeight } = options
|
||||
const overscan = Math.max(0, options.overscan ?? 8)
|
||||
if (count <= 0 || rowHeight <= 0) return { start: 0, end: 0, top: 0, bottom: 0 }
|
||||
// A container that is not measured yet still mounts one row plus the
|
||||
// overscan band, so the first paint is not empty and the height feedback
|
||||
// loop (measure → render) has content to measure.
|
||||
const rowsInView = Math.max(1, Math.ceil(Math.max(0, viewportHeight) / rowHeight))
|
||||
const visible = rowsInView + overscan * 2
|
||||
const first = Math.floor(Math.max(0, scrollTop) / rowHeight) - overscan
|
||||
const start = Math.min(Math.max(0, first), count - 1)
|
||||
const end = Math.min(count, start + visible)
|
||||
return {
|
||||
start,
|
||||
end,
|
||||
top: start * rowHeight,
|
||||
bottom: (count - end) * rowHeight,
|
||||
}
|
||||
}
|
||||
@@ -93,7 +93,9 @@ test('read-only Git Graph renders merge history and filters without checkout', a
|
||||
expect(await page.locator('[data-scm-section="graph"]').evaluate(el => Math.round(el.getBoundingClientRect().height))).toBe(28)
|
||||
await graphHeader.click()
|
||||
await expect(graph).toBeVisible()
|
||||
await expect(graph.locator('[data-graph-commit]')).toHaveCount(50)
|
||||
// The row window is viewport-bound now; the PAGE size is what the state
|
||||
// carries, so assert it on the section rather than counting mounted rows.
|
||||
await expect(graph).toHaveAttribute('data-graph-count', '50')
|
||||
await expectGraphFillsHeight()
|
||||
|
||||
// Stress the outer scroller only after verifying graph interactions are read-only.
|
||||
@@ -102,7 +104,9 @@ test('read-only Git Graph renders merge history and filters without checkout', a
|
||||
await changes.getByRole('button', { name: 'Refresh', exact: true }).click()
|
||||
await expect(changes.locator('[data-row="change-39.txt"]')).toHaveCount(1)
|
||||
await expect(graphScroll).toHaveAttribute('aria-busy', 'false')
|
||||
await expect(graph.locator('[data-graph-commit]')).toHaveCount(50)
|
||||
// The row window is viewport-bound now; the PAGE size is what the state
|
||||
// carries, so assert it on the section rather than counting mounted rows.
|
||||
await expect(graph).toHaveAttribute('data-graph-count', '50')
|
||||
const outer = page.locator('[data-git-scroll]')
|
||||
expect(await outer.evaluate(el => el.scrollHeight > el.clientHeight)).toBe(true)
|
||||
expect(await graph.evaluate(el => Math.round(el.getBoundingClientRect().height))).toBe(220)
|
||||
|
||||
@@ -507,3 +507,50 @@ describe('graph end-sentinel auto-load', () => {
|
||||
} finally { hidden.unmount(); io.restore() }
|
||||
})
|
||||
})
|
||||
|
||||
/** One spacer's pixel height (absent spacer = 0). */
|
||||
function spacerHeight(scroll: HTMLElement, which: 'top' | 'bottom'): number {
|
||||
const node = scroll.querySelector<HTMLElement>(`[data-graph-spacer="${which}"]`)
|
||||
return node === null ? 0 : Number.parseFloat(node.style.height)
|
||||
}
|
||||
|
||||
describe('graph history windowing', () => {
|
||||
it('mounts only the viewport window and keeps the spacers in step with the scroll position', async () => {
|
||||
// The host caps the walk at 10000 rows; 200 is already enough that the DOM
|
||||
// used to grow with the history instead of the viewport.
|
||||
const many = Array.from({ length: 200 }, (_, index) => entry(`${index.toString(16).padStart(8, '0')}${'f'.repeat(32)}`))
|
||||
vi.spyOn(api, 'gitGraph').mockResolvedValue(graphPage(many))
|
||||
const mounted = renderRoot(createElement(GitGraphSection, defaults))
|
||||
try {
|
||||
await flush()
|
||||
const graph = mounted.container.querySelector<HTMLElement>('[data-git-graph]')!
|
||||
const scroll = mounted.container.querySelector<HTMLElement>('[data-graph-scroll]')!
|
||||
expect(scroll).not.toBeNull()
|
||||
expect(graph.getAttribute('data-graph-count')).toBe('200')
|
||||
// The row height is the component's own constant: read it off the rail
|
||||
// so the test cannot drift from it.
|
||||
const rowHeight = Number(scroll.querySelector('svg')!.getAttribute('height'))
|
||||
expect(rowHeight).toBeGreaterThan(0)
|
||||
|
||||
// jsdom has no layout: clientHeight is 0, so one row plus the 8-row
|
||||
// overscan band on each side mounts, and the spacers keep the height.
|
||||
expect(mounted.container.querySelectorAll('[data-graph-commit]')).toHaveLength(17)
|
||||
expect(spacerHeight(scroll, 'top')).toBe(0)
|
||||
expect(spacerHeight(scroll, 'bottom')).toBe((200 - 17) * rowHeight)
|
||||
|
||||
// Scroll five pages down: only the viewport (20 rows) plus overscan mounts.
|
||||
Object.defineProperty(scroll, 'clientHeight', { configurable: true, value: 20 * rowHeight })
|
||||
scroll.scrollTop = 50 * rowHeight
|
||||
await act(async () => {
|
||||
scroll.dispatchEvent(new Event('scroll'))
|
||||
await new Promise<void>(resolve => requestAnimationFrame(() => resolve()))
|
||||
})
|
||||
const rendered = [...mounted.container.querySelectorAll('[data-graph-commit]')]
|
||||
expect(rendered).toHaveLength(36)
|
||||
expect(rendered[0]!.getAttribute('data-graph-commit')).toBe(many[42]!.hashFull)
|
||||
expect(rendered.at(-1)!.getAttribute('data-graph-commit')).toBe(many[77]!.hashFull)
|
||||
expect(spacerHeight(scroll, 'top')).toBe(42 * rowHeight)
|
||||
expect(spacerHeight(scroll, 'bottom')).toBe((200 - 78) * rowHeight)
|
||||
} finally { mounted.unmount() }
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* The commit graph mounts only the viewport window of its history rows (the
|
||||
* list is unbounded: infinite scroll appends 50 at a time up to the host's
|
||||
* 10000 cap, and every row carries an SVG rail). This is the pure arithmetic
|
||||
* behind it — the component test in `git-graph-ui.spec.tsx` pins the wiring.
|
||||
*
|
||||
* The clamping cases matter: the list SHRINKS under the scroll position when
|
||||
* the branch selection changes or a refresh drops the page, and the browser
|
||||
* clamps `scrollTop` a frame later. A stale scrollTop must never point the
|
||||
* window past the end of the new list.
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { windowedRange } from '../src/client/changes/git-graph-windowing.ts'
|
||||
|
||||
const ROW = 28
|
||||
|
||||
describe('windowedRange', () => {
|
||||
it('renders nothing for an empty list', () => {
|
||||
expect(windowedRange({ count: 0, rowHeight: ROW, scrollTop: 0, viewportHeight: 560 }))
|
||||
.toEqual({ start: 0, end: 0, top: 0, bottom: 0 })
|
||||
})
|
||||
|
||||
it('mounts the whole list when it is shorter than the viewport', () => {
|
||||
expect(windowedRange({ count: 10, rowHeight: ROW, scrollTop: 0, viewportHeight: 560 }))
|
||||
.toEqual({ start: 0, end: 10, top: 0, bottom: 0 })
|
||||
})
|
||||
|
||||
it('mounts the first page plus overscan before the viewport is measured', () => {
|
||||
// viewportHeight 0 (jsdom, or a hidden panel): one row of view plus the
|
||||
// 8-row band on each side.
|
||||
expect(windowedRange({ count: 200, rowHeight: ROW, scrollTop: 0, viewportHeight: 0 }))
|
||||
.toEqual({ start: 0, end: 17, top: 0, bottom: 183 * ROW })
|
||||
})
|
||||
|
||||
it('follows the scroll position with one overscan band on each side', () => {
|
||||
// 560px viewport = 20 rows; scrolled to row 50.
|
||||
expect(windowedRange({ count: 200, rowHeight: ROW, scrollTop: 50 * ROW, viewportHeight: 560 }))
|
||||
.toEqual({ start: 42, end: 78, top: 42 * ROW, bottom: 122 * ROW })
|
||||
})
|
||||
|
||||
it('stops at the end of the list (no spacer below the last row)', () => {
|
||||
expect(windowedRange({ count: 200, rowHeight: ROW, scrollTop: 199 * ROW, viewportHeight: 560 }))
|
||||
.toEqual({ start: 191, end: 200, top: 191 * ROW, bottom: 0 })
|
||||
})
|
||||
|
||||
it('clamps a stale scrollTop that outlived a shrinking list', () => {
|
||||
// Branch switch: 200 rows -> 5 rows, the browser has not clamped yet.
|
||||
expect(windowedRange({ count: 5, rowHeight: ROW, scrollTop: 100_000, viewportHeight: 560 }))
|
||||
.toEqual({ start: 4, end: 5, top: 4 * ROW, bottom: 0 })
|
||||
})
|
||||
|
||||
it('honours an explicit overscan (0 = exactly the viewport)', () => {
|
||||
expect(windowedRange({ count: 200, rowHeight: ROW, scrollTop: 50 * ROW, viewportHeight: 560, overscan: 0 }))
|
||||
.toEqual({ start: 50, end: 70, top: 50 * ROW, bottom: 130 * ROW })
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user