Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DevTools] Add Component Highlighting to Profiler #18745

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import NoCommitData from './NoCommitData';
import CommitFlamegraphListItem from './CommitFlamegraphListItem';
import HoveredFiberInfo from './HoveredFiberInfo';
import {scale} from './utils';
import {useHighlightNativeElement} from '../hooks';
import {StoreContext} from '../context';
import {SettingsContext} from '../Settings/SettingsContext';
import Tooltip from './Tooltip';
Expand All @@ -28,7 +29,8 @@ import type {CommitTree} from './types';

export type ItemData = {|
chartData: ChartData,
hoverFiber: (fiberData: TooltipFiberData | null) => void,
onElementMouseEnter: (fiberData: TooltipFiberData) => void,
onElementMouseLeave: () => void,
scaleX: (value: number, fallbackValue: number) => number,
selectedChartNode: ChartNode | null,
selectedChartNodeIndex: number,
Expand Down Expand Up @@ -96,11 +98,16 @@ type Props = {|
|};

function CommitFlamegraph({chartData, commitTree, height, width}: Props) {
const [hoveredFiberData, hoverFiber] = useState<TooltipFiberData | null>(
null,
);
const [
hoveredFiberData,
setHoveredFiberData,
] = useState<TooltipFiberData | null>(null);
const {lineHeight} = useContext(SettingsContext);
const {selectFiber, selectedFiberID} = useContext(ProfilerContext);
const {
highlightNativeElement,
clearHighlightNativeElement,
} = useHighlightNativeElement();

const selectedChartNodeIndex = useMemo<number>(() => {
if (selectedFiberID === null) {
Expand All @@ -123,10 +130,24 @@ function CommitFlamegraph({chartData, commitTree, height, width}: Props) {
return null;
}, [chartData, selectedFiberID, selectedChartNodeIndex]);

const handleElementMouseEnter = useCallback(
({id, name}) => {
highlightNativeElement(id); // Highlight last hovered element.
setHoveredFiberData({id, name}); // Set hovered fiber data for tooltip
},
[highlightNativeElement],
);

const handleElementMouseLeave = useCallback(() => {
clearHighlightNativeElement(); // clear highlighting of element on mouse leave
setHoveredFiberData(null); // clear hovered fiber data for tooltip
}, [clearHighlightNativeElement]);

const itemData = useMemo<ItemData>(
() => ({
chartData,
hoverFiber,
onElementMouseEnter: handleElementMouseEnter,
onElementMouseLeave: handleElementMouseLeave,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no point to useMemo if we're passing in functions that get recreated inline every time we render.

Since these functions are just attached as mouse event handlers, normally it would not be important to memoize them- but in this case, not memoizing means that we could be doing a lot of otherwise unnecessary re-renders.

scaleX: scale(
0,
selectedChartNode !== null
Expand All @@ -142,7 +163,8 @@ function CommitFlamegraph({chartData, commitTree, height, width}: Props) {
}),
[
chartData,
hoverFiber,
handleElementMouseEnter,
handleElementMouseLeave,
selectedChartNode,
selectedChartNodeIndex,
selectFiber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ type Props = {
function CommitFlamegraphListItem({data, index, style}: Props) {
const {
chartData,
hoverFiber,
onElementMouseEnter,
onElementMouseLeave,
scaleX,
selectedChartNode,
selectedChartNodeIndex,
Expand All @@ -49,11 +50,11 @@ function CommitFlamegraphListItem({data, index, style}: Props) {

const handleMouseEnter = (nodeData: ChartNodeType) => {
const {id, name} = nodeData;
hoverFiber({id, name});
onElementMouseEnter({id, name});
};

const handleMouseLeave = () => {
hoverFiber(null);
onElementMouseLeave();
};

// List items are absolutely positioned using the CSS "top" attribute.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import HoveredFiberInfo from './HoveredFiberInfo';
import {scale} from './utils';
import {StoreContext} from '../context';
import {SettingsContext} from '../Settings/SettingsContext';
import {useHighlightNativeElement} from '../hooks';
import Tooltip from './Tooltip';

import styles from './CommitRanked.css';
Expand All @@ -28,7 +29,8 @@ import type {CommitTree} from './types';

export type ItemData = {|
chartData: ChartData,
hoverFiber: (fiberData: TooltipFiberData | null) => void,
onElementMouseEnter: (fiberData: TooltipFiberData) => void,
onElementMouseLeave: () => void,
scaleX: (value: number, fallbackValue: number) => number,
selectedFiberID: number | null,
selectedFiberIndex: number,
Expand Down Expand Up @@ -94,28 +96,55 @@ type Props = {|
|};

function CommitRanked({chartData, commitTree, height, width}: Props) {
const [hoveredFiberData, hoverFiber] = useState<TooltipFiberData | null>(
null,
);
const [
hoveredFiberData,
setHoveredFiberData,
] = useState<TooltipFiberData | null>(null);
const {lineHeight} = useContext(SettingsContext);
const {selectedFiberID, selectFiber} = useContext(ProfilerContext);
const {
highlightNativeElement,
clearHighlightNativeElement,
} = useHighlightNativeElement();

const selectedFiberIndex = useMemo(
() => getNodeIndex(chartData, selectedFiberID),
[chartData, selectedFiberID],
);

const handleElementMouseEnter = useCallback(
({id, name}) => {
highlightNativeElement(id); // Highlight last hovered element.
setHoveredFiberData({id, name}); // Set hovered fiber data for tooltip
},
[highlightNativeElement],
);

const handleElementMouseLeave = useCallback(() => {
clearHighlightNativeElement(); // clear highlighting of element on mouse leave
setHoveredFiberData(null); // clear hovered fiber data for tooltip
}, [clearHighlightNativeElement]);

const itemData = useMemo<ItemData>(
() => ({
chartData,
hoverFiber,
onElementMouseEnter: handleElementMouseEnter,
onElementMouseLeave: handleElementMouseLeave,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above wrt memoization.

scaleX: scale(0, chartData.nodes[selectedFiberIndex].value, 0, width),
selectedFiberID,
selectedFiberIndex,
selectFiber,
width,
}),
[chartData, selectedFiberID, selectedFiberIndex, selectFiber, width],
[
chartData,
handleElementMouseEnter,
handleElementMouseLeave,
selectedFiberID,
selectedFiberIndex,
selectFiber,
width,
],
);

// Tooltip used to show summary of fiber info on hover
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type Props = {
function CommitRankedListItem({data, index, style}: Props) {
const {
chartData,
hoverFiber,
onElementMouseEnter,
onElementMouseLeave,
scaleX,
selectedFiberIndex,
selectFiber,
Expand All @@ -49,11 +50,11 @@ function CommitRankedListItem({data, index, style}: Props) {

const handleMouseEnter = () => {
const {id, name} = node;
hoverFiber({id, name});
onElementMouseEnter({id, name});
};

const handleMouseLeave = () => {
hoverFiber(null);
onElementMouseLeave();
};

// List items are absolutely positioned using the CSS "top" attribute.
Expand Down