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: compile filter applier with eval #9635

Merged
merged 4 commits into from
Jul 18, 2023
Merged
Changes from 1 commit
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 @@ -212,6 +212,8 @@ const getFilterCallbackFromItem = (
};
};

let filterItemsApplierId = 1;
romgrk marked this conversation as resolved.
Show resolved Hide resolved

/**
* Generates a method to easily check if a row is matching the current filter model.
* @param {GridRowIdGetter | undefined} getRowId The getter for row's id.
Expand All @@ -234,20 +236,48 @@ export const buildAggregatedFilterItemsApplier = (
return null;
}

return (row, shouldApplyFilter) => {
Copy link
Member

Choose a reason for hiding this comment

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

Can you share a benchmark compared to the master branch?
Curious to learn what is the benefit in this PR on its own 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Filtering 100,000 rows on my device:

Case Result
Without 70.2 +- 3.8
With 62.6 +- 4.7

The improvement is consistenly around 11% for a realistic number of rows, but with a small improvement as N grows.

Copy link
Member

Choose a reason for hiding this comment

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

Great, thanks!

const resultPerItemId: GridFilterItemResult = {};

for (let i = 0; i < appliers.length; i += 1) {
const applier = appliers[i];
if (!shouldApplyFilter || shouldApplyFilter(applier.item.field)) {
resultPerItemId[applier.item.id!] = applier.v7
? applier.fn(row)
: applier.fn(getRowId ? getRowId(row) : row.id);
}
}

return resultPerItemId;
};
// Original logic:
// return (row, shouldApplyFilter) => {
// const resultPerItemId: GridFilterItemResult = {};
//
// for (let i = 0; i < appliers.length; i += 1) {
// const applier = appliers[i];
// if (!shouldApplyFilter || shouldApplyFilter(applier.item.field)) {
// resultPerItemId[applier.item.id!] = applier.v7
// ? applier.fn(row)
// : applier.fn(getRowId ? getRowId(row) : row.id);
// }
// }
//
// return resultPerItemId;
// };
romgrk marked this conversation as resolved.
Show resolved Hide resolved

// We generate a new function with `eval()` to avoid expensive patterns for JS engines
// such as a dynamic object assignment, e.g. `{ [dynamicKey]: value }`.
const filterItemTemplate =
`(function filterItem$$(row, shouldApplyFilter) {
${appliers.map((applier, i) =>
`const shouldApply${i} = !shouldApplyFilter || shouldApplyFilter(${JSON.stringify(applier.item.field)});`
).join('\n')}

const result$$ = {
${appliers.map((applier, i) =>
`${applier.item.id !== undefined ? parseInt(String(applier.item.id)) : 'undefined'}:
!shouldApply${i} ?
false :
${applier.v7 ?
`appliers[${i}].fn(row)` :
`appliers[${i}].fn(${getRowId ? 'getRowId(row)' : 'row.id'})`
},
`).join('\n')}};

return result$$;
})`;

const filterItem = eval(filterItemTemplate.replaceAll('$$', String(filterItemsApplierId))) as GridFilterItemApplierNotAggregated;
filterItemsApplierId += 1;

return filterItem;
};

/**
Expand Down