perf(graph): window the commit history rows

This commit is contained in:
Menghuan1918 committed 2026-10-24 02:17:10 +08:00
1 parent 2237f3787b
commit 0a4a94c704
5 files changed
+226 -6

No files matched your search

+53 -4
View File
@@ -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>}
+64
View File
@@ -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,
}
}