Skip to content

Commit

Permalink
perf: memorize nodes
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly committed Jul 7, 2024
1 parent e7914b9 commit 5a770a6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
4 changes: 2 additions & 2 deletions ui/src/Node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface NodeProps {
getModuleColor: NodeColorGetter;
}

export const Node: React.FC<NodeProps> = (
export const Node: React.FC<NodeProps> = React.memo((
{
node,
onClick,
Expand Down Expand Up @@ -117,4 +117,4 @@ export const Node: React.FC<NodeProps> = (
</text>
</g>
);
};
});
30 changes: 10 additions & 20 deletions ui/src/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import React, { useEffect, useMemo, useRef, useState } from "react";
import type { Entry } from "./tool/entry.ts";

const Tooltip_marginX = 10;
Expand All @@ -7,10 +7,14 @@ const Tooltip_marginY = 30;
export interface TooltipProps {
node?: Entry;
visible: boolean;
x: number;
y: number;
}

export const Tooltip: React.FC<TooltipProps>
= ({
x,
y,
node,
visible,
}) => {
Expand All @@ -27,13 +31,13 @@ export const Tooltip: React.FC<TooltipProps>
return node?.toString() ?? "";
}, [node]);

const updatePosition = useCallback((mouseCoords: { x: number; y: number }) => {
useEffect(() => {
if (!ref.current)
return;

const pos = {
left: mouseCoords.x + Tooltip_marginX,
top: mouseCoords.y + Tooltip_marginY,
left: x + Tooltip_marginX,
top: y + Tooltip_marginY,
};

const boundingRect = ref.current.getBoundingClientRect();
Expand All @@ -45,25 +49,11 @@ export const Tooltip: React.FC<TooltipProps>

if (pos.top + boundingRect.height > window.innerHeight) {
// Flipping vertically
pos.top = mouseCoords.y - Tooltip_marginY - boundingRect.height;
pos.top = y - Tooltip_marginY - boundingRect.height;
}

setStyle(pos);
}, []);

useEffect(() => {
const handleMouseMove = (event: MouseEvent) => {
updatePosition({
x: event.pageX,
y: event.pageY,
});
};

document.addEventListener("mousemove", handleMouseMove, true);
return () => {
document.removeEventListener("mousemove", handleMouseMove, true);
};
}, [updatePosition]);
}, [x, y]);

return (
<div className={`tooltip ${visible ? "" : "tooltip-hidden"}`} ref={ref} style={style}>
Expand Down
22 changes: 15 additions & 7 deletions ui/src/TreeMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ function TreeMap({ entry }: TreeMapProps) {
setSelectedNode(cur);
}, [hash, root, entry]);

const [showTooltip, setShowTooltip] = useState(true);
const [showTooltip, setShowTooltip] = useState(false);
const [tooltipPosition, setTooltipPosition] = useState<[number, number]>([0, 0]);
const [tooltipNode, setTooltipNode]
= useState<HierarchyRectangularNode<Entry> | undefined>(undefined);

Expand Down Expand Up @@ -197,13 +198,22 @@ function TreeMap({ entry }: TreeMapProps) {
}

setTooltipNode(node);
setTooltipPosition([e.clientX, e.clientY]);

if (!showTooltip) {
setShowTooltip(true);
}
};

document.addEventListener("mousemove", moveListener);
return () => {
document.removeEventListener("mousemove", moveListener);
};
}, [allNodes]);
}, [allNodes, showTooltip]);

const nodeOnClick = useCallback((node: HierarchyRectangularNode<Entry>) => {
setSelectedNodeWithHash(selectedNode?.data?.getID() === node.data.getID() ? null : node);
}, [selectedNode, setSelectedNodeWithHash]);

const nodes = useMemo(() => {
return (
Expand All @@ -216,9 +226,7 @@ function TreeMap({ entry }: TreeMapProps) {
key={node.data.getID()}
node={node}
selected={selectedNode?.data?.getID() === node.data.getID()}
onClick={(node) => {
setSelectedNodeWithHash(selectedNode?.data?.getID() === node.data.getID() ? null : node);
}}
onClick={nodeOnClick}
getModuleColor={getModuleColor}
/>
);
Expand All @@ -227,11 +235,11 @@ function TreeMap({ entry }: TreeMapProps) {
);
})
);
}, [getModuleColor, nestedData, selectedNode?.data, setSelectedNodeWithHash]);
}, [getModuleColor, nestedData, nodeOnClick, selectedNode]);

return (
<>
<Tooltip visible={showTooltip} node={tooltipNode?.data} />
<Tooltip visible={showTooltip} node={tooltipNode?.data} x={tooltipPosition[0]} y={tooltipPosition[1]} />
<svg xmlns="http://www.w3.org/2000/svg" viewBox={`0 0 ${width} ${height}`} ref={svgRef}>
{nodes}
</svg>
Expand Down
2 changes: 1 addition & 1 deletion ui/src/explorer/FileSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function FileSelector({ handler }: {
{pendingFile?.name}
{" "}
has a size of
{formatBytes(pendingFile?.size || 0)}
{` ${formatBytes(pendingFile?.size || 0)}`}
.
It is not recommended to use the wasm version for binary files larger than 30 MB.
</DialogContentText>
Expand Down
8 changes: 4 additions & 4 deletions ui/vite.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import process from "node:process";
import type { BuildOptions, HtmlTagDescriptor, Plugin, PluginOption } from "vite";
import { codecovVitePlugin } from "@codecov/vite-plugin";
import react from "@vitejs/plugin-react-swc";
import MillionLint from "@million/lint";
// import MillionLint from "@million/lint";

export function getSha(): string | undefined {
const envs = process.env;
Expand Down Expand Up @@ -64,9 +64,9 @@ export function codecov(name: string): Plugin[] | undefined {
export function commonPlugin(): (PluginOption[] | Plugin | Plugin[])[] {
return [
react(),
MillionLint.vite({
optimizeDOM: true,
}),
// MillionLint.vite({
// optimizeDOM: true,
// }),
];
}

Expand Down

0 comments on commit 5a770a6

Please sign in to comment.