-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changes from all commits
54115c5
4739f05
22c0e6b
9569b46
547a3b5
cb4c757
4ce4e28
0a0d10b
0880afb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
import type { GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity'; | ||
import { useGridPrivateApiContext } from '../../utils/useGridPrivateApiContext'; | ||
import { useGridRootProps } from '../../utils/useGridRootProps'; | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really correct? We're not disconnecting on react <= 18? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, ref cleanup is only supported in React 19. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This issue wasn't addressed, I think this might be leaking memory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I forgot about this one. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
How do you verify this? Screen.Recording.2024-11-25.at.17.47.11.movThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try here: https://w8n3kg.csb.app/ 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I clicked the "Collect garbage" button in the video above, but it didn't change anything.
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.movBut it looks like there are no memory leaks from the resize observer, and the DOM node is garbage collected once removed from the DOM. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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) { | ||
|
@@ -596,7 +641,7 @@ export const useGridVirtualScroller = () => { | |
setPanels, | ||
getRows, | ||
getContainerProps: () => ({ | ||
ref: mainRef, | ||
ref: mainRefCallback, | ||
}), | ||
getScrollerProps: () => ({ | ||
ref: scrollerRef, | ||
|
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); |
There was a problem hiding this comment.
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
? 🤔There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 removeimport { reactMajor } from '@mui/internal-test-utils';
as it can be a footgun.