Merge pull request #7748 from continuedev/nate/rerendering-deep-dive
perf: memoize input box to avoid re-render on each keystroke
This commit is contained in:
commit
8eac486fa9
7 changed files with 212 additions and 44 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import { Editor, JSONContent } from "@tiptap/react";
|
||||
import { ContextItemWithId, InputModifiers, RuleWithSource } from "core";
|
||||
import { useMemo } from "react";
|
||||
import { memo, useMemo } from "react";
|
||||
import { defaultBorderRadius, vscBackground } from "..";
|
||||
import { useAppSelector } from "../../redux/hooks";
|
||||
import { selectSlashCommandComboBoxInputs } from "../../redux/selectors";
|
||||
|
|
@ -69,16 +69,20 @@ function ContinueInputBox(props: ContinueInputBoxProps) {
|
|||
const historyKey = isInEdit ? "edit" : "chat";
|
||||
const placeholder = isInEdit ? "Edit selected code" : undefined;
|
||||
|
||||
const toolbarOptions: ToolbarOptions = isInEdit
|
||||
? {
|
||||
const toolbarOptions: ToolbarOptions = useMemo(() => {
|
||||
if (isInEdit) {
|
||||
return {
|
||||
hideAddContext: false,
|
||||
hideImageUpload: false,
|
||||
hideUseCodebase: true,
|
||||
hideSelectModel: false,
|
||||
enterText:
|
||||
editModeState.applyState.status === "done" ? "Retry" : "Edit",
|
||||
}
|
||||
: {};
|
||||
} as ToolbarOptions;
|
||||
}
|
||||
// Stable empty object to avoid re-renders from identity changes
|
||||
return {} as ToolbarOptions;
|
||||
}, [isInEdit, editModeState.applyState.status]);
|
||||
|
||||
const { appliedRules = [], contextItems = [] } = props;
|
||||
|
||||
|
|
@ -123,4 +127,4 @@ function ContinueInputBox(props: ContinueInputBoxProps) {
|
|||
);
|
||||
}
|
||||
|
||||
export default ContinueInputBox;
|
||||
export default memo(ContinueInputBox);
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import {
|
|||
modelSupportsImages,
|
||||
modelSupportsReasoning,
|
||||
} from "core/llm/autodetect";
|
||||
import { useContext, useRef } from "react";
|
||||
import { memo, useContext, useRef } from "react";
|
||||
import { IdeMessengerContext } from "../../context/IdeMessenger";
|
||||
import { useAppDispatch, useAppSelector } from "../../redux/hooks";
|
||||
import { selectUseActiveFile } from "../../redux/selectors";
|
||||
|
|
@ -242,4 +242,24 @@ function InputToolbar(props: InputToolbarProps) {
|
|||
);
|
||||
}
|
||||
|
||||
export default InputToolbar;
|
||||
function shallowToolbarOptionsEqual(a?: ToolbarOptions, b?: ToolbarOptions) {
|
||||
if (a === b) return true;
|
||||
if (!a || !b) return false;
|
||||
return (
|
||||
a.hideAddContext === b.hideAddContext &&
|
||||
a.hideImageUpload === b.hideImageUpload &&
|
||||
a.hideUseCodebase === b.hideUseCodebase &&
|
||||
a.hideSelectModel === b.hideSelectModel &&
|
||||
a.enterText === b.enterText
|
||||
);
|
||||
}
|
||||
|
||||
export default memo(
|
||||
InputToolbar,
|
||||
(prev, next) =>
|
||||
prev.hidden === next.hidden &&
|
||||
prev.disabled === next.disabled &&
|
||||
prev.isMainInput === next.isMainInput &&
|
||||
prev.activeKey === next.activeKey &&
|
||||
shallowToolbarOptionsEqual(prev.toolbarOptions, next.toolbarOptions),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,14 @@
|
|||
import { Editor, EditorContent, JSONContent } from "@tiptap/react";
|
||||
import { ContextProviderDescription, InputModifiers } from "core";
|
||||
import { modelSupportsImages } from "core/llm/autodetect";
|
||||
import { useCallback, useContext, useEffect, useRef, useState } from "react";
|
||||
import {
|
||||
memo,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
} from "react";
|
||||
import { IdeMessengerContext } from "../../../context/IdeMessenger";
|
||||
import useIsOSREnabled from "../../../hooks/useIsOSREnabled";
|
||||
import useUpdatingRef from "../../../hooks/useUpdatingRef";
|
||||
|
|
@ -38,7 +45,7 @@ export interface TipTapEditorProps {
|
|||
|
||||
export const TIPPY_DIV_ID = "tippy-js-div";
|
||||
|
||||
export function TipTapEditor(props: TipTapEditorProps) {
|
||||
function TipTapEditorInner(props: TipTapEditorProps) {
|
||||
const dispatch = useAppDispatch();
|
||||
const mainEditorContext = useMainEditor();
|
||||
|
||||
|
|
@ -72,12 +79,13 @@ export function TipTapEditor(props: TipTapEditorProps) {
|
|||
return;
|
||||
}
|
||||
const placeholder = getPlaceholderText(props.placeholder, historyLength);
|
||||
|
||||
editor.extensionManager.extensions.filter(
|
||||
(extension) => extension.name === "placeholder",
|
||||
)[0].options["placeholder"] = placeholder;
|
||||
|
||||
editor.view.dispatch(editor.state.tr);
|
||||
const placeholderExt = editor.extensionManager.extensions.find(
|
||||
(e) => e.name === "placeholder",
|
||||
) as any;
|
||||
if (placeholderExt) {
|
||||
placeholderExt.options["placeholder"] = placeholder;
|
||||
editor.view.dispatch(editor.state.tr);
|
||||
}
|
||||
}, [editor, props.placeholder, historyLength]);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
@ -281,3 +289,37 @@ export function TipTapEditor(props: TipTapEditorProps) {
|
|||
</InputBoxDiv>
|
||||
);
|
||||
}
|
||||
|
||||
function toolbarOptionsEqual(a?: ToolbarOptions, b?: ToolbarOptions) {
|
||||
if (a === b) return true;
|
||||
if (!a || !b) return false;
|
||||
return (
|
||||
a.hideAddContext === b.hideAddContext &&
|
||||
a.hideImageUpload === b.hideImageUpload &&
|
||||
a.hideUseCodebase === b.hideUseCodebase &&
|
||||
a.hideSelectModel === b.hideSelectModel &&
|
||||
a.enterText === b.enterText
|
||||
);
|
||||
}
|
||||
|
||||
const MemoInner = memo(
|
||||
TipTapEditorInner,
|
||||
(prev, next) =>
|
||||
prev.isMainInput === next.isMainInput &&
|
||||
prev.placeholder === next.placeholder &&
|
||||
prev.historyKey === next.historyKey &&
|
||||
prev.inputId === next.inputId &&
|
||||
toolbarOptionsEqual(prev.toolbarOptions, next.toolbarOptions) &&
|
||||
(prev.availableContextProviders?.length || 0) ===
|
||||
(next.availableContextProviders?.length || 0) &&
|
||||
(prev.availableSlashCommands?.length || 0) ===
|
||||
(next.availableSlashCommands?.length || 0),
|
||||
);
|
||||
|
||||
export function TipTapEditor(props: TipTapEditorProps) {
|
||||
return (
|
||||
<div className="relative w-full">
|
||||
<MemoInner {...props} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { Editor } from "@tiptap/react";
|
||||
import { KeyboardEvent } from "react";
|
||||
import { KeyboardEvent, useRef } from "react";
|
||||
import { isJetBrains, isMetaEquivalentKeyPressed } from "../../../../util";
|
||||
import {
|
||||
handleJetBrainsOSRMetaKeyIssues,
|
||||
|
|
@ -13,6 +13,7 @@ export function useEditorEventHandlers(options: {
|
|||
setActiveKey: (key: string | null) => void;
|
||||
}) {
|
||||
const { editor, isOSREnabled, editorFocusedRef, setActiveKey } = options;
|
||||
const metaActiveRef = useRef(false);
|
||||
|
||||
/**
|
||||
* This handles various issues with meta key actions
|
||||
|
|
@ -31,10 +32,14 @@ export function useEditorEventHandlers(options: {
|
|||
return;
|
||||
}
|
||||
|
||||
setActiveKey(e.key);
|
||||
|
||||
if (!editorFocusedRef?.current || !isMetaEquivalentKeyPressed(e)) return;
|
||||
|
||||
// Only set activeKey for Meta/Control/Alt to drive toolbar highlighting
|
||||
if (e.key === "Meta" || e.key === "Control" || e.key === "Alt") {
|
||||
setActiveKey(e.key);
|
||||
metaActiveRef.current = true;
|
||||
}
|
||||
|
||||
if (isOSREnabled) {
|
||||
handleJetBrainsOSRMetaKeyIssues(e, editor);
|
||||
} else if (!isJetBrains()) {
|
||||
|
|
@ -42,7 +47,12 @@ export function useEditorEventHandlers(options: {
|
|||
}
|
||||
};
|
||||
|
||||
const handleKeyUp = () => setActiveKey(null);
|
||||
const handleKeyUp = () => {
|
||||
if (metaActiveRef.current) {
|
||||
setActiveKey(null);
|
||||
metaActiveRef.current = false;
|
||||
}
|
||||
};
|
||||
|
||||
return { handleKeyDown, handleKeyUp };
|
||||
}
|
||||
|
|
|
|||
70
gui/src/hooks/useRenderDebug.tsx
Normal file
70
gui/src/hooks/useRenderDebug.tsx
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import { CSSProperties, useEffect, useRef } from "react";
|
||||
|
||||
/**
|
||||
* useRenderDebug
|
||||
*
|
||||
* Developer utility to visualize component re-renders.
|
||||
*
|
||||
* - Returns a `ref` to attach to a container element and a `count` that
|
||||
* increments on every render of the component using this hook.
|
||||
* - On each render, it briefly adds a CSS class (default: `rerender-flash`)
|
||||
* to the element for a quick visual flash.
|
||||
* - Use together with <RenderCountBadge /> to show the render count inline.
|
||||
*
|
||||
* Options:
|
||||
* - enabled: boolean to toggle the effect (default true)
|
||||
* - flashClassName: class added on each render (default `rerender-flash`)
|
||||
* - flashDurationMs: duration of the flash animation in ms (default 180)
|
||||
*/
|
||||
|
||||
export function useRenderDebug(options?: {
|
||||
enabled?: boolean;
|
||||
flashClassName?: string;
|
||||
flashDurationMs?: number;
|
||||
}) {
|
||||
const {
|
||||
enabled = true,
|
||||
flashClassName = "rerender-flash",
|
||||
flashDurationMs = 180,
|
||||
} = options || {};
|
||||
|
||||
const ref = useRef<HTMLElement | null>(null);
|
||||
const countRef = useRef(0);
|
||||
countRef.current += 1;
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || !ref.current) return;
|
||||
const el = ref.current;
|
||||
el.classList.add(flashClassName);
|
||||
const t = setTimeout(
|
||||
() => el.classList.remove(flashClassName),
|
||||
flashDurationMs,
|
||||
);
|
||||
return () => {
|
||||
clearTimeout(t);
|
||||
// Ensure we don't leave the flash class applied on cleanup
|
||||
el.classList.remove(flashClassName);
|
||||
};
|
||||
});
|
||||
|
||||
return { ref, count: countRef.current, countRef } as const;
|
||||
}
|
||||
|
||||
export function RenderCountBadge(props: {
|
||||
count: number;
|
||||
title?: string;
|
||||
style?: CSSProperties;
|
||||
className?: string;
|
||||
}) {
|
||||
// Simple badge for use with useRenderDebug; keep lightweight.
|
||||
const { count, title = `Renders: ${count}`, style, className } = props;
|
||||
return (
|
||||
<span
|
||||
className={`render-count ${className || ""}`}
|
||||
title={title}
|
||||
style={style}
|
||||
>
|
||||
R{count}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
|
@ -119,3 +119,28 @@ body {
|
|||
.scroll-container:is(:hover, :focus)::-webkit-scrollbar {
|
||||
display: block; /* Show scrollbar only on hover or focus */
|
||||
}
|
||||
|
||||
/* Debug: visible flash when component re-renders */
|
||||
@keyframes rerenderFlash {
|
||||
0% {
|
||||
box-shadow: 0 0 0 2px rgba(255, 0, 0, 0.9) inset;
|
||||
}
|
||||
100% {
|
||||
box-shadow: 0 0 0 0 rgba(255, 0, 0, 0) inset;
|
||||
}
|
||||
}
|
||||
|
||||
.rerender-flash {
|
||||
animation: rerenderFlash 180ms ease-out;
|
||||
}
|
||||
|
||||
.render-count {
|
||||
display: inline-block;
|
||||
margin-right: 6px;
|
||||
padding: 0 4px;
|
||||
border-radius: 3px;
|
||||
background: rgba(255, 0, 0, 0.15);
|
||||
color: rgba(255, 0, 0, 0.9);
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.02em;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,8 @@ import { cancelStream } from "../../redux/thunks/cancelStream";
|
|||
import { EmptyChatBody } from "./EmptyChatBody";
|
||||
import { ExploreDialogWatcher } from "./ExploreDialogWatcher";
|
||||
import { useAutoScroll } from "./useAutoScroll";
|
||||
import { useStore } from "react-redux";
|
||||
import { RootState } from "../../redux/store";
|
||||
|
||||
// Helper function to find the index of the latest conversation summary
|
||||
function findLatestSummaryIndex(history: ChatHistoryItem[]): number {
|
||||
|
|
@ -98,13 +100,11 @@ function fallbackRender({ error, resetErrorBoundary }: any) {
|
|||
export function Chat() {
|
||||
const dispatch = useAppDispatch();
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
const reduxStore = useStore<RootState>();
|
||||
const onboardingCard = useOnboardingCard();
|
||||
const showSessionTabs = useAppSelector(
|
||||
(store) => store.config.config.ui?.showSessionTabs,
|
||||
);
|
||||
const selectedModels = useAppSelector(
|
||||
(store) => store.config?.config.selectedModelByRole,
|
||||
);
|
||||
const isStreaming = useAppSelector((state) => state.session.isStreaming);
|
||||
const [stepsOpen] = useState<(boolean | undefined)[]>([]);
|
||||
const mainTextInputRef = useRef<HTMLInputElement>(null);
|
||||
|
|
@ -115,7 +115,6 @@ export function Chat() {
|
|||
(state) => state.config.config.ui?.showChatScrollbar,
|
||||
);
|
||||
const codeToEdit = useAppSelector((state) => state.editModeState.codeToEdit);
|
||||
const mode = useAppSelector((store) => store.session.mode);
|
||||
const isInEdit = useAppSelector((store) => store.session.isInEdit);
|
||||
|
||||
const lastSessionId = useAppSelector((state) => state.session.lastSessionId);
|
||||
|
|
@ -152,9 +151,6 @@ export function Chat() {
|
|||
isStreaming,
|
||||
);
|
||||
|
||||
const pendingToolCalls = useAppSelector(selectPendingToolCalls);
|
||||
const pendingApplyStates = useAppSelector(selectDoneApplyStates);
|
||||
|
||||
const sendInput = useCallback(
|
||||
(
|
||||
editorState: JSONContent,
|
||||
|
|
@ -162,8 +158,16 @@ export function Chat() {
|
|||
index?: number,
|
||||
editorToClearOnSend?: Editor,
|
||||
) => {
|
||||
const stateSnapshot = reduxStore.getState();
|
||||
const latestPendingToolCalls = selectPendingToolCalls(stateSnapshot);
|
||||
const latestPendingApplyStates = selectDoneApplyStates(stateSnapshot);
|
||||
const isCurrentlyInEdit = stateSnapshot.session.isInEdit;
|
||||
const codeToEditSnapshot = stateSnapshot.editModeState.codeToEdit;
|
||||
const selectedModelByRole =
|
||||
stateSnapshot.config.config.selectedModelByRole;
|
||||
|
||||
// Cancel all pending tool calls
|
||||
pendingToolCalls.forEach((toolCallState) => {
|
||||
latestPendingToolCalls.forEach((toolCallState) => {
|
||||
dispatch(
|
||||
cancelToolCall({
|
||||
toolCallId: toolCallState.toolCallId,
|
||||
|
|
@ -172,19 +176,20 @@ export function Chat() {
|
|||
});
|
||||
|
||||
// Reject all pending apply states
|
||||
pendingApplyStates.forEach((applyState) => {
|
||||
latestPendingApplyStates.forEach((applyState) => {
|
||||
if (applyState.status !== "closed") {
|
||||
ideMessenger.post("rejectDiff", applyState);
|
||||
}
|
||||
});
|
||||
const model = isInEdit
|
||||
? (selectedModels?.edit ?? selectedModels?.chat)
|
||||
: selectedModels?.chat;
|
||||
const model = isCurrentlyInEdit
|
||||
? (selectedModelByRole.edit ?? selectedModelByRole.chat)
|
||||
: selectedModelByRole.chat;
|
||||
|
||||
if (!model) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isInEdit && codeToEdit.length === 0) {
|
||||
if (isCurrentlyInEdit && codeToEditSnapshot.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -212,11 +217,11 @@ export function Chat() {
|
|||
// }
|
||||
// }
|
||||
|
||||
if (isInEdit) {
|
||||
if (isCurrentlyInEdit) {
|
||||
void dispatch(
|
||||
streamEditThunk({
|
||||
editorState,
|
||||
codeToEdit,
|
||||
codeToEdit: codeToEditSnapshot,
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
|
|
@ -227,15 +232,7 @@ export function Chat() {
|
|||
}
|
||||
}
|
||||
},
|
||||
[
|
||||
history,
|
||||
selectedModels,
|
||||
mode,
|
||||
isInEdit,
|
||||
codeToEdit,
|
||||
pendingToolCalls,
|
||||
pendingApplyStates,
|
||||
],
|
||||
[dispatch, ideMessenger, reduxStore],
|
||||
);
|
||||
|
||||
useWebviewListener(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue