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

[DataGrid] Improve resize performance #15549

Merged
merged 9 commits into from
Nov 25, 2024
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
2 changes: 1 addition & 1 deletion packages/x-data-grid/src/hooks/core/useGridRefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const useGridRefs = <PrivateApi extends GridPrivateApiCommon>(
apiRef: React.MutableRefObject<PrivateApi>,
) => {
const rootElementRef = React.useRef<HTMLDivElement>(null);
const mainElementRef = React.useRef<HTMLDivElement>(null);
const mainElementRef = React.useRef<HTMLDivElement | null>(null);
const virtualScrollerRef = React.useRef<HTMLDivElement>(null);
const virtualScrollbarVerticalRef = React.useRef<HTMLDivElement>(null);
const virtualScrollbarHorizontalRef = React.useRef<HTMLDivElement>(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
} from '@mui/utils';
import useLazyRef from '@mui/utils/useLazyRef';
import useTimeout from '@mui/utils/useTimeout';
import { useResizeObserver } from '@mui/x-internals/useResizeObserver';
import { useRtl } from '@mui/system/RtlProvider';
import reactMajor from '@mui/x-internals/reactMajor';
Copy link
Member

Choose a reason for hiding this comment

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

Just noticed this while working on React 19 bump.
@mui/xgrid Was there a reason to create a local utility, when everywhere else it is imported from @mui/internal-test-utils? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

We needed this in the data grid package itself, not in the test files.

Copy link
Member

Choose a reason for hiding this comment

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

A possible solution mui/base-ui#1047. We could argue that if we can do import reactMajor from '@mui/x-internals/reactMajor'; then we should remove import { reactMajor } from '@mui/internal-test-utils'; as it can be a footgun.

import type { GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity';
import { useGridPrivateApiContext } from '../../utils/useGridPrivateApiContext';
import { useGridRootProps } from '../../utils/useGridRootProps';
Expand Down Expand Up @@ -135,7 +135,57 @@ export const useGridVirtualScroller = () => {
const columnsTotalWidth = dimensions.columnsTotalWidth;
const hasColSpan = useGridSelector(apiRef, gridHasColSpanSelector);

useResizeObserver(mainRef, () => apiRef.current.resize());
const mainRefCallback = React.useCallback(
(node: HTMLDivElement | null) => {
mainRef.current = node;

if (!node) {
return undefined;
}

const initialRect = node.getBoundingClientRect();
let lastSize = {
width: initialRect.width,
height: initialRect.height,
};

apiRef.current.publishEvent('resize', lastSize);

if (typeof ResizeObserver === 'undefined') {
return undefined;
}

const observer = new ResizeObserver((entries) => {
const entry = entries[0];
if (!entry) {
return;
}

const newSize = {
width: entry.contentRect.width,
height: entry.contentRect.height,
};

if (newSize.width === lastSize.width && newSize.height === lastSize.height) {
return;
}

apiRef.current.publishEvent('resize', newSize);
lastSize = newSize;
});

observer.observe(node);

if (reactMajor >= 19) {
return () => {
mainRef.current = null;
observer.disconnect();
};
}
return undefined;
Comment on lines +177 to +185
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this really correct? We're not disconnecting on react <= 18?

Copy link
Member

Choose a reason for hiding this comment

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

Right, ref cleanup is only supported in React 19.
Also, I'm checking the React major because React 18 logs this error: https://github.com/facebook/react/blob/f1338f8080abd1386454a10bbf93d67bfe37ce85/packages/react-reconciler/src/ReactFiberCommitWork.new.js#L292-L295
I can add a cleanup effect for older versions.

Copy link
Contributor

Choose a reason for hiding this comment

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

This issue wasn't addressed, I think this might be leaking memory.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I forgot about this one.
I'll open a PR with a cleanup effect.

Copy link
Contributor

Choose a reason for hiding this comment

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

Btw I would skip the version checking and use a single strategy, to keep the code simple.

Copy link
Member

Choose a reason for hiding this comment

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

Verify that no detached nodes are left behind.

How do you verify this?
I see retained detached nodes in Chrome Devtools, even with the resize observer commented out in useGridVirtualScroller:

Screen.Recording.2024-11-25.at.17.47.11.mov

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Try here: https://w8n3kg.csb.app/
Source: https://codesandbox.io/p/sandbox/w8n3kg

Whole Datagrid was a bad example perhaps. There may or may not be a memory leak somewhere else there. I've spent good deal of time trying to debug this in our own app, and I'm not sure at this point. Could be that it's very slow to garbage collect (which I don't understand why – (is it the DOM tree size?) and running a profile too quickly creates a reference to the detached node, which in turn prevents garbage collection). But not sure either if and when it would automatically get garbage collected. Bugs in devotools doesn't make it a joy to debug.

See: #15459 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Could be that it's very slow to garbage collect (which I don't understand why – (is it the DOM tree size?) and running a profile too quickly creates a reference to the detached node

I clicked the "Collect garbage" button in the video above, but it didn't change anything.

Bugs in devotools doesn't make it a joy to debug.

Yep, I get different results and I'm not sure it's reliable at this point:

Screen.Recording.2024-11-25.at.18.25.25.mov

But it looks like there are no memory leaks from the resize observer, and the DOM node is garbage collected once removed from the DOM.
I think we shouldn't bother with cleanups here and take a look at #15459 instead

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you confident that everything is GC'ed correctly across all browsers? I would tend to add cleanup just to be safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can never be sure about that, but from quick checks with Safari and Firefox, it seems to be the case. But let's add it to be extra safe.

},
[apiRef, mainRef],
);

/*
* Scroll context logic
Expand Down Expand Up @@ -549,11 +599,6 @@ export const useGridVirtualScroller = () => {
apiRef.current.publishEvent('virtualScrollerContentSizeChange');
}, [apiRef, contentSize]);

useEnhancedEffect(() => {
// FIXME: Is this really necessary?
apiRef.current.resize();
}, [apiRef, rowsMeta.currentPageTotalHeight]);

useEnhancedEffect(() => {
// TODO a scroll reset should not be necessary
if (enabledForColumns) {
Expand Down Expand Up @@ -596,7 +641,7 @@ export const useGridVirtualScroller = () => {
setPanels,
getRows,
getContainerProps: () => ({
ref: mainRef,
ref: mainRefCallback,
}),
getScrollerProps: () => ({
ref: scrollerRef,
Expand Down
2 changes: 1 addition & 1 deletion packages/x-data-grid/src/models/api/gridCoreApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface GridCorePrivateApi<
/**
* The React ref of the grid main container div element.
*/
mainElementRef: React.RefObject<HTMLDivElement>;
mainElementRef: React.MutableRefObject<HTMLDivElement | null>;
/**
* The React ref of the grid's virtual scroller container element.
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/x-internals/src/reactMajor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as React from 'react';

export default parseInt(React.version, 10);