From bd3a42ed23308450655c97ee42d0c92a8e354b6d Mon Sep 17 00:00:00 2001 From: KJ7LNW <93454819+KJ7LNW@users.noreply.github.com> Date: Sun, 4 May 2025 13:20:33 -0700 Subject: [PATCH] perf: optimize code block rendering performance (#3135) feat: optimize code block rendering performance Memoize CodeBlock components to prevent unnecessary re-renders: - Add MemoizedCodeContent for syntax highlighted HTML - Add MemoizedStyledPre for container element - Properly type all component props - Reduce React reconciliation work for complex code blocks Signed-off-by: Eric Wheeler Co-authored-by: Eric Wheeler --- .../src/components/common/CodeBlock.tsx | 50 ++++++++++++++++--- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/webview-ui/src/components/common/CodeBlock.tsx b/webview-ui/src/components/common/CodeBlock.tsx index afd87c248f..1b295e7d1a 100644 --- a/webview-ui/src/components/common/CodeBlock.tsx +++ b/webview-ui/src/components/common/CodeBlock.tsx @@ -604,16 +604,15 @@ const CodeBlock = memo( return ( - updateCodeBlockButtonPosition(true)} - onMouseUp={() => updateCodeBlockButtonPosition(false)}> -
- + highlightedCode={highlightedCode} + updateCodeBlockButtonPosition={updateCodeBlockButtonPosition} + /> {!isSelecting && (
) + +// Memoized StyledPre component +const MemoizedStyledPre = memo( + ({ + preRef, + preStyle, + wordWrap, + windowShade, + collapsedHeight, + highlightedCode, + updateCodeBlockButtonPosition, + }: { + preRef: React.RefObject + preStyle?: React.CSSProperties + wordWrap: boolean + windowShade: boolean + collapsedHeight?: number + highlightedCode: string + updateCodeBlockButtonPosition: (forceHide?: boolean) => void + }) => ( + updateCodeBlockButtonPosition(true)} + onMouseUp={() => updateCodeBlockButtonPosition(false)}> + + + ), +) + export default CodeBlock