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] Avoid allocations in hydrateRowsMeta #9121

Merged
merged 2 commits into from
May 29, 2023
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 @@ -288,17 +288,16 @@ export const useGridDetailPanel = (
const addDetailHeight = React.useCallback<GridPipeProcessor<'rowHeight'>>(
(initialValue, row) => {
if (!expandedRowIds || expandedRowIds.length === 0 || !expandedRowIds.includes(row.id)) {
return { ...initialValue, detail: 0 };
initialValue.detail = 0;
return initialValue;
}

updateCachesIfNeeded();

const heightCache = gridDetailPanelExpandedRowsHeightCacheSelector(apiRef);

return {
...initialValue,
detail: heightCache[row.id] ?? 0, // Fallback to zero because the cache might not be ready yet (e.g. page was changed)
};
initialValue.detail = heightCache[row.id] ?? 0; // Fallback to zero because the cache might not be ready yet (e.g. page was changed)
return initialValue;
},
[apiRef, expandedRowIds, updateCachesIfNeeded],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,14 @@ export const useGridRowsMeta = (
rowsHeightLookup.current[row.id].needsFirstMeasurement = false;
}

const existingBaseSizes = Object.entries(sizes).reduce<Record<string, number>>(
(acc, [key, size]) => {
if (/^base[A-Z]/.test(key)) {
acc[key] = size;
}
return acc;
},
{},
);

// We use an object to make simple to check if a height is already added or not
const initialHeights: Record<string, number> = {
...existingBaseSizes,
baseCenter: baseRowHeight,
};
const initialHeights = {} as Record<string, number>;
/* eslint-disable-next-line no-restricted-syntax */
for (const key in sizes) {
if (/^base[A-Z]/.test(key)) {
initialHeights[key] = sizes[key];
}
}
initialHeights.baseCenter = baseRowHeight;
Comment on lines -147 to +154
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I love functionnal programming, but FP is always slower in JS :|


if (getRowSpacing) {
const indexRelativeToCurrentPage = apiRef.current.getRowIndexRelativeToVisibleRows(row.id);
Expand Down Expand Up @@ -193,13 +186,15 @@ export const useGridRowsMeta = (
let otherSizes = 0;

const processedSizes = calculateRowProcessedSizes(row);
Object.entries(processedSizes).forEach(([size, value]) => {
if (/^base[A-Z]/.test(size)) {
/* eslint-disable-next-line no-restricted-syntax, guard-for-in */
for (const key in processedSizes) {
const value = processedSizes[key];
if (/^base[A-Z]/.test(key)) {
maximumBaseSize = value > maximumBaseSize ? value : maximumBaseSize;
} else {
otherSizes += value;
}
});
}

return acc + maximumBaseSize + otherSizes;
}, 0);
Expand Down