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] Performance: small optimizations #12346

Merged
merged 12 commits into from
Mar 7, 2024
Merged

Conversation

romgrk
Copy link
Contributor

@romgrk romgrk commented Mar 6, 2024

Various small optimizations.

Result: small 2-3% improvement
Benchmark: scroll the grid by a constant offset, record the total CPU time

branch: next
    N           Min           Max        Median           Avg        Stddev
x  10           163           198           178         181.1     11.483805


branch: perf-small-opts
    N           Min           Max        Median           Avg        Stddev
x   9           145           204           175     176.55556     18.500751

@mui-bot
Copy link

mui-bot commented Mar 6, 2024

Deploy preview: https://deploy-preview-12346--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against 90af7a5

@romgrk romgrk added the component: data grid This is the name of the generic UI component, not the React module! label Mar 6, 2024
Comment on lines 430 to 435
{...slotProps?.cell}
pinnedOffset={pinnedOffset}
pinnedPosition={pinnedPosition}
sectionIndex={indexInSection}
sectionLength={sectionLength}
gridHasScrollX={dimensions.hasScrollX}
dimensions={dimensions}
{...slotProps?.cell}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Destructuring in the middle creates 3 objects (_extends({ a: 1}, slotProps.cell, { b: 2 })).

Comment on lines 331 to 342

const cellStyle: React.CSSProperties =
height !== dimensions.rowHeight
? {
'--width': `${width}px`,
'--height': typeof height === 'number' ? `${height}px` : height,
...styleProp,
}
: {
'--width': `${width}px`,
...styleProp,
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Avoid setting --height unless required. .setAttribute is one of our highest costs.

Copy link
Member

Choose a reason for hiding this comment

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

Do we even need --height here? It seems to be always set to rowHeight:

Comment on lines -11 to +17
interface GridPipeGroupCache {
processors: Map<string, GridPipeProcessor<any> | null>;
type Cache = {
[G in GridPipeProcessorGroup]?: GroupCache;
};

type GroupCache = {
processors: Map<string, GridPipeProcessor<any>>;
processorsAsArray: GridPipeProcessor<any>[];
Copy link
Contributor Author

@romgrk romgrk Mar 6, 2024

Choose a reason for hiding this comment

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

Small refactor along with the optimization, the signal-to-noise ratio is high with the long names & prefixes everywhere. This is internal, no need to add the grid prefix.

Comment on lines -126 to +138
const preProcessors = Array.from(processorsCache.current[group]!.processors.values());
return preProcessors.reduce((acc, preProcessor) => {
if (!preProcessor) {
return acc;
}

return preProcessor(acc, context);
}, value);
const processors = cache.current[group]!.processorsAsArray;
let result = value;
for (let i = 0; i < processors.length; i += 1) {
result = processors[i](result, context);
}
return result;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This block is expensive. Map -> Iterator object -> Array.from() -> array.reduce is costly when this is run 2x for each cell.

Comment on lines 54 to 64
const value = apiRef.current.getCellValue(id, field);
const row = apiRef.current.getRow(id);
const rowNode = apiRef.current.getRowNode(id);

if (!row || !rowNode) {
throw new MissingRowIdError(`No row with id #${id} found`);
}

const rawValue = row[colDef.field];
const value = colDef.valueGetter
? colDef.valueGetter(rawValue as never, row, colDef, apiRef)
: rawValue;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

.getCellValue fetches row and rowNode again but we already have them here, and the pattern rowsById[rowId] is expensive when there is a large number of entries.

Comment on lines -152 to 155
const { cache } = cacheContainer;
const cacheArgsInit = cache.get(cacheKey);
const cacheArgs = cacheArgsInit ?? new Map();
const cacheFn = cacheArgs?.get(args);

if (cache.get(cacheKey) && cache.get(cacheKey)?.get(args)) {
if (cacheArgs && cacheFn) {
// We pass the cache key because the called selector might have as
// dependency another selector created with this `createSelector`.
return cache.get(cacheKey)?.get(args)(state, cacheKey);
return cacheFn(state, cacheKey);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're doing map.get 4 times but there are only 2 levels of depth. This is called very often.

@romgrk romgrk marked this pull request as ready for review March 6, 2024 12:59
Comment on lines 331 to 342

const cellStyle: React.CSSProperties =
height !== dimensions.rowHeight
? {
'--width': `${width}px`,
'--height': typeof height === 'number' ? `${height}px` : height,
...styleProp,
}
: {
'--width': `${width}px`,
...styleProp,
};
Copy link
Member

Choose a reason for hiding this comment

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

Do we even need --height here? It seems to be always set to rowHeight:

@romgrk romgrk force-pushed the perf-small-opts branch from 91700c2 to 29d94da Compare March 7, 2024 13:54
@romgrk
Copy link
Contributor Author

romgrk commented Mar 7, 2024

I've addressed the comments. The failing test is a flaky datepicker test. Approved?

Copy link
Member

@cherniavskii cherniavskii left a comment

Choose a reason for hiding this comment

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

🎉

@romgrk romgrk merged commit f68925c into mui:next Mar 7, 2024
14 of 15 checks passed
@romgrk romgrk deleted the perf-small-opts branch March 7, 2024 15:00
thomasmoon pushed a commit to thomasmoon/mui-x that referenced this pull request Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: data grid This is the name of the generic UI component, not the React module! performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants