Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

perf(review): bound initial Shiki diff rendering (#18991)

Innei9bc7a631d6d3
diff --git a/src/features/Conversation/WorkingSidebar/Review/__tests__/reviewTreeNodes.test.ts b/src/features/Conversation/WorkingSidebar/Review/__tests__/reviewTreeNodes.test.ts
index e0eecc9dde1..1302edda04d 100644
--- a/src/features/Conversation/WorkingSidebar/Review/__tests__/reviewTreeNodes.test.ts
+++ b/src/features/Conversation/WorkingSidebar/Review/__tests__/reviewTreeNodes.test.ts
@@ -1,7 +1,7 @@
import type { GitWorkingTreePatch } from '@lobechat/electron-client-ipc';
import { describe, expect, it } from 'vitest';
-import { buildReviewTreeNodes, itemKey } from '../reviewTreeNodes';
+import { buildReviewTreeNodes, getDefaultExpandedKeys, itemKey } from '../reviewTreeNodes';
const makePatch = (filePath: string): GitWorkingTreePatch => ({
additions: 1,
@@ -55,3 +55,37 @@ describe('buildReviewTreeNodes', () => {
expect(nodes).toHaveLength(0);
});
});
+
+describe('getDefaultExpandedKeys', () => {
+ it('bounds the initial Shiki-backed diffs across repo groups', () => {
+ const groups = [
+ group('/repo', 'repo', ['1.ts', '2.ts', '3.ts', '4.ts']),
+ group('/repo/sub', 'sub', ['5.ts', '6.ts', '7.ts', '8.ts']),
+ ];
+
+ expect(getDefaultExpandedKeys(groups)).toEqual([
+ itemKey('/repo', makePatch('1.ts')),
+ itemKey('/repo', makePatch('2.ts')),
+ itemKey('/repo', makePatch('3.ts')),
+ itemKey('/repo', makePatch('4.ts')),
+ itemKey('/repo/sub', makePatch('5.ts')),
+ ]);
+ });
+
+ it('stops before the next diff would exceed the byte budget', () => {
+ const firstPatch = makePatch('small.ts');
+ const largePatch = makePatch('large.ts');
+ firstPatch.patch = 'x'.repeat(100 * 1024);
+ largePatch.patch = 'x';
+
+ expect(
+ getDefaultExpandedKeys([
+ { ...group('/repo', 'repo', []), patches: [firstPatch, largePatch] },
+ ]),
+ ).toEqual([itemKey('/repo', firstPatch)]);
+ });
+
+ it('returns no default expansions when every group is empty', () => {
+ expect(getDefaultExpandedKeys([group('/repo', 'repo', [])])).toEqual([]);
+ });
+});
diff --git a/src/features/Conversation/WorkingSidebar/Review/index.tsx b/src/features/Conversation/WorkingSidebar/Review/index.tsx
index 3c5e71d6fa3..44f184f5dcd 100644
--- a/src/features/Conversation/WorkingSidebar/Review/index.tsx
+++ b/src/features/Conversation/WorkingSidebar/Review/index.tsx
@@ -32,7 +32,7 @@ import type { ComposerTarget } from '../../types';
import FileRow from './FileRow';
import FileTreeNav from './FileTreeNav';
import GroupHeader from './GroupHeader';
-import { itemKey } from './reviewTreeNodes';
+import { getDefaultExpandedKeys, itemKey } from './reviewTreeNodes';
import {
invalidateGitReviewCaches,
type ReviewMode,
@@ -70,11 +70,6 @@ interface ReviewProps {
workingDirectory: string;
}
-// Empirically: ~100KB of patch ≈ 50 small-diff files OR ~2 big refactors;
-// either way keeps Shiki tokenization under ~250ms on first paint.
-const DEFAULT_EXPAND_BYTE_BUDGET = 100 * 1024;
-const DEFAULT_EXPAND_MAX_COUNT = 50;
-
const styles = createStaticStyles(({ css, cssVar }) => ({
caret: css`
flex-shrink: 0;
@@ -355,14 +350,8 @@ const Review = memo<ReviewProps>(
});
};
- // Default-expand by patch-size budget: take entries (across all groups in
- // order) until cumulative patch bytes exceed DEFAULT_EXPAND_BYTE_BUDGET,
- // capped at DEFAULT_EXPAND_MAX_COUNT. Every PatchDiff mounts a Shiki
- // tokenizer synchronously, so expanding too much at once locks the renderer;
- // size-based budget keeps small-diff cases generous while clamping repos
- // with a few large refactors. Re-syncing on signature change auto-expands
- // new entries within the cap; panels the user manually closed earlier stay
- // closed because their key is already absent.
+ // A PatchDiff materialises a large Shiki-backed DOM tree. Keep the initial
+ // expansion to a viewport-sized batch; users can still open any other file.
const signature = useMemo(
() => allEntries.map(({ group, patch }) => itemKey(group.absolutePath, patch)).join('|'),
[allEntries],
@@ -371,16 +360,7 @@ const Review = memo<ReviewProps>(
const [activeKeys, setActiveKeys] = useState<string[]>([]);
if (signature !== seenSignature) {
setSeenSignature(signature);
- const initialKeys: string[] = [];
- let budget = DEFAULT_EXPAND_BYTE_BUDGET;
- for (const { group, patch } of allEntries) {
- if (initialKeys.length >= DEFAULT_EXPAND_MAX_COUNT) break;
- const cost = patch.patch?.length ?? 0;
- if (initialKeys.length > 0 && cost > budget) break;
- initialKeys.push(itemKey(group.absolutePath, patch));
- budget -= cost;
- }
- setActiveKeys(initialKeys);
+ setActiveKeys(getDefaultExpandedKeys(groups));
}
// The file whose diff the tree-nav last scrolled to — highlighted in the rail.
diff --git a/src/features/Conversation/WorkingSidebar/Review/reviewTreeNodes.ts b/src/features/Conversation/WorkingSidebar/Review/reviewTreeNodes.ts
index 6f9b7b5f1b9..4cc8e483e46 100644
--- a/src/features/Conversation/WorkingSidebar/Review/reviewTreeNodes.ts
+++ b/src/features/Conversation/WorkingSidebar/Review/reviewTreeNodes.ts
@@ -18,6 +18,26 @@ export interface ReviewTreeGroup {
patches: GitWorkingTreePatch[];
}
+const DEFAULT_EXPAND_BYTE_BUDGET = 100 * 1024;
+const DEFAULT_EXPAND_MAX_COUNT = 5;
+
+export const getDefaultExpandedKeys = (groups: ReviewTreeGroup[]): string[] => {
+ const keys: string[] = [];
+ let budget = DEFAULT_EXPAND_BYTE_BUDGET;
+
+ for (const group of groups) {
+ for (const patch of group.patches) {
+ if (keys.length >= DEFAULT_EXPAND_MAX_COUNT) return keys;
+ const cost = patch.patch?.length ?? 0;
+ if (keys.length > 0 && cost > budget) return keys;
+ keys.push(itemKey(group.absolutePath, patch));
+ budget -= cost;
+ }
+ }
+
+ return keys;
+};
+
// Node ids are prefixed with the owning repo path so file paths that repeat
// across submodule groups never collide. NUL can't appear in a real path.
const SEP = '\u0000';