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] Filtering performance: cache values #9284

Merged
merged 7 commits into from
Jun 13, 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 @@ -85,6 +85,7 @@ export const filterRowTreeFromGroupingColumns = (
const { rowTree, isRowMatchingFilters, filterModel } = params;
const filteredRowsLookup: Record<GridRowId, boolean> = {};
const filteredDescendantCountLookup: Record<GridRowId, number> = {};
const filterCache = {};

const filterTreeNode = (
node: GridTreeNode,
Expand Down Expand Up @@ -133,6 +134,7 @@ export const filterRowTreeFromGroupingColumns = (
allResults.map((result) => result.passingQuickFilterValues),
filterModel,
params.apiRef,
filterCache,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const filterRowTreeFromTreeData = (
const { rowTree, disableChildrenFiltering, isRowMatchingFilters } = params;
const filteredRowsLookup: Record<GridRowId, boolean> = {};
const filteredDescendantCountLookup: Record<GridRowId, number> = {};
const filterCache = {};

const filterTreeNode = (
node: GridTreeNode,
Expand All @@ -52,6 +53,7 @@ export const filterRowTreeFromTreeData = (
[passingQuickFilterValues],
params.filterModel,
params.apiRef,
filterCache,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,36 +295,44 @@ export const buildAggregatedFilterApplier = (
});
};

const isNotNull = <T>(result: null | T): result is T => result != null;
Copy link
Member

@Janpot Janpot Jun 14, 2023

Choose a reason for hiding this comment

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

This would make it work for type checking at well

Suggested change
const isNotNull = <T>(result: null | T): result is T => result != null;
const isNotNull = <T>(result: T): result is NonNullable<T> => result != null;

Not the most important, but it just caught my eye


type FilterCache = {
cleanedFilterItems?: GridFilterItem[];
};

const filterModelItems = (
cache: FilterCache,
apiRef: React.MutableRefObject<GridApiCommunity>,
items: GridFilterItem[],
) => {
if (!cache.cleanedFilterItems) {
cache.cleanedFilterItems = items.filter(
(item) => getFilterCallbackFromItem(item, apiRef) !== null,
);
}
return cache.cleanedFilterItems;
};

export const passFilterLogic = (
allFilterItemResults: (null | GridFilterItemResult)[],
allQuickFilterResults: (null | GridQuickFilterValueResult)[],
filterModel: GridFilterModel,
apiRef: React.MutableRefObject<GridApiCommunity>,
cache: FilterCache,
): boolean => {
const cleanedFilterItems = filterModel.items.filter(
(item) => getFilterCallbackFromItem(item, apiRef) !== null,
);

const cleanedAllFilterItemResults = allFilterItemResults.filter(
(result): result is GridFilterItemResult => result != null,
);

const cleanedAllQuickFilterResults = allQuickFilterResults.filter(
(result): result is GridQuickFilterValueResult => result != null,
);

// Defaultize operators
const quickFilterLogicOperator =
filterModel.quickFilterLogicOperator ?? getDefaultGridFilterModel().quickFilterLogicOperator;
const logicOperator = filterModel.logicOperator ?? getDefaultGridFilterModel().logicOperator;
const cleanedFilterItems = filterModelItems(cache, apiRef, filterModel.items);
const cleanedFilterItemResults = allFilterItemResults.filter(isNotNull);
const cleanedQuickFilterResults = allQuickFilterResults.filter(isNotNull);

// get result for filter items model
if (cleanedAllFilterItemResults.length > 0) {
if (cleanedFilterItemResults.length > 0) {
// Return true if the item pass with one of the rows
const filterItemPredicate = (item: GridFilterItem) => {
return cleanedAllFilterItemResults.some((filterItemResult) => filterItemResult[item.id!]);
return cleanedFilterItemResults.some((filterItemResult) => filterItemResult[item.id!]);
};

const logicOperator = filterModel.logicOperator ?? getDefaultGridFilterModel().logicOperator;
if (logicOperator === GridLogicOperator.And) {
const passesAllFilters = cleanedFilterItems.every(filterItemPredicate);
if (!passesAllFilters) {
Expand All @@ -339,14 +347,16 @@ export const passFilterLogic = (
}

// get result for quick filter model
if (cleanedAllQuickFilterResults.length > 0 && filterModel.quickFilterValues != null) {
if (cleanedQuickFilterResults.length > 0 && filterModel.quickFilterValues != null) {
// Return true if the item pass with one of the rows
const quickFilterValuePredicate = (value: string) => {
return cleanedAllQuickFilterResults.some(
return cleanedQuickFilterResults.some(
(quickFilterValueResult) => quickFilterValueResult[value],
);
};

const quickFilterLogicOperator =
filterModel.quickFilterLogicOperator ?? getDefaultGridFilterModel().quickFilterLogicOperator;
if (quickFilterLogicOperator === GridLogicOperator.And) {
const passesAllQuickFilterValues =
filterModel.quickFilterValues.every(quickFilterValuePredicate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ export const useGridFilter = (
const tree = gridRowTreeSelector(apiRef);
const rowIds = (tree[GRID_ROOT_GROUP_ID] as GridGroupNode).children;
const filteredRowsLookup: Record<GridRowId, boolean> = {};
const filterCache = {};

for (let i = 0; i < rowIds.length; i += 1) {
const rowId = rowIds[i];
let isRowPassing;
Expand All @@ -403,6 +405,7 @@ export const useGridFilter = (
[passingQuickFilterValues],
params.filterModel,
apiRef,
filterCache,
);
}
filteredRowsLookup[rowId] = isRowPassing;
Expand Down