Skip to content

Commit 90dce3e

Browse files
committed
perf(filtering): keep quick-filter texts by position for the rows array filterRows searched
Replaces the joined-text cache: joining cost more on the first keystroke than it saved. filterRows now keeps the texts of the last rows array per column lookup and reuses a row's texts when the same object sits at the same index, so an edit re-reads only the changed rows. Matching uses plain loops, and getFormattedValue calls the formatter without a closure per cell.
1 parent c470372 commit 90dce3e

3 files changed

Lines changed: 93 additions & 38 deletions

File tree

‎lib/utils/filtering/filtering.equivalence.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,28 @@ describe('plain columns read cells exactly as columns with a valueGetter', () =>
130130
}
131131
});
132132
});
133+
134+
describe('quick filter after the rows change', () => {
135+
it('a new rows array with replaced, inserted and removed rows searches the current rows', () => {
136+
const rows = makeRows(600, 21);
137+
const lookup = buildColumnLookup(COLUMNS);
138+
const model = { items: [], quickFilterValues: ['ab'] };
139+
filterRows(rows, model, lookup);
140+
const edited = [...rows];
141+
edited[3] = { ...edited[3], a: 'zzab' };
142+
edited[10] = { ...edited[10], a: 'none' };
143+
edited.splice(20, 0, { id: 9999, a: 'xab', b: 1, c: true, d: new Date(2024, 0, 2), e: '' });
144+
edited.splice(500, 5);
145+
for (const current of [edited, rows, edited]) {
146+
expect(filterRows(current, model, lookup).map(r => r.id)).toEqual(refQuickFilter(current, ['ab'], COLUMNS).map(r => r.id));
147+
}
148+
});
149+
150+
it('quick filter combined with items', () => {
151+
const rows = makeRows(600, 22);
152+
const lookup = buildColumnLookup(COLUMNS);
153+
const items: GridFilterItem[] = [{ field: 'c', operator: 'is', value: 'true' }];
154+
const expected = refQuickFilter(rows, ['a'], COLUMNS).filter(r => r.c === true).map(r => r.id);
155+
expect(filterRows(rows, { items, quickFilterValues: ['a'] }, lookup).map(r => r.id)).toEqual(expected);
156+
});
157+
});

‎lib/utils/filtering/index.ts‎

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -238,55 +238,73 @@ function getQuickFilterTexts(row: GridRowModel, columns: GridColumnLookup | unde
238238

239239
// Search texts per column lookup and row object. The grid keeps one lookup while the columns and
240240
// hidden columns stay the same, so typing in the quick filter reads every cell once, not per keystroke.
241-
//
242-
// A row's texts are stored joined by TEXT_SEPARATOR, one string per row instead of an array of a
243-
// dozen, so the first keystroke allocates less and a term is one `includes` per row. A term that
244-
// contains the separator could match across two texts, so such terms search the split texts; a
245-
// row whose own text contains the separator keeps its array.
246-
const TEXT_SEPARATOR = '\u0000';
247-
type QuickFilterTexts = string | string[];
248-
const quickFilterTextCache = new WeakMap<GridColumnLookup, WeakMap<GridRowModel, QuickFilterTexts>>();
249-
250-
function packQuickFilterTexts(texts: string[]): QuickFilterTexts {
251-
for (const text of texts) if (text.includes(TEXT_SEPARATOR)) return texts;
252-
return texts.join(TEXT_SEPARATOR);
253-
}
241+
const quickFilterTextCache = new WeakMap<GridColumnLookup, WeakMap<GridRowModel, string[]>>();
254242

255-
function getCachedQuickFilterTexts(row: GridRowModel, columns: GridColumnLookup | undefined): QuickFilterTexts {
243+
function getCachedQuickFilterTexts(row: GridRowModel, columns: GridColumnLookup | undefined): string[] {
256244
if (!columns) return getQuickFilterTexts(row, columns);
257245
let byRow = quickFilterTextCache.get(columns);
258246
if (!byRow) {
259247
byRow = new WeakMap();
260248
quickFilterTextCache.set(columns, byRow);
261249
}
262250
let texts = byRow.get(row);
263-
if (texts === undefined) {
264-
texts = packQuickFilterTexts(getQuickFilterTexts(row, columns));
251+
if (!texts) {
252+
texts = getQuickFilterTexts(row, columns);
265253
byRow.set(row, texts);
266254
}
267255
return texts;
268256
}
269257

270-
function textsInclude(texts: QuickFilterTexts, term: string, termHasSeparator: boolean): boolean {
271-
if (typeof texts !== 'string') return texts.some(text => text.includes(term));
272-
if (!termHasSeparator) return texts.includes(term);
273-
// An empty joined string is no texts at all, not one empty text.
274-
return texts !== '' && texts.split(TEXT_SEPARATOR).some(text => text.includes(term));
258+
function quickFilterTerms(values: readonly string[] | undefined): string[] {
259+
return (values ?? [])
260+
.map(value => String(value ?? '').trim().toLowerCase())
261+
.filter(term => term !== '');
262+
}
263+
264+
function textsMatchTerms(texts: readonly string[], terms: readonly string[]): boolean {
265+
// Plain loops: this runs once per row on every keystroke.
266+
for (let t = 0; t < terms.length; t++) {
267+
const term = terms[t];
268+
let found = false;
269+
for (let i = 0; i < texts.length; i++) {
270+
if (texts[i].includes(term)) {
271+
found = true;
272+
break;
273+
}
274+
}
275+
if (!found) return false;
276+
}
277+
return true;
275278
}
276279

277280
function compileQuickFilter(values: readonly string[] | undefined, columns: GridColumnLookup | undefined): RowPredicate | null {
278-
const terms = (values ?? [])
279-
.map(value => String(value ?? '').trim().toLowerCase())
280-
.filter(term => term !== '');
281+
const terms = quickFilterTerms(values);
281282
if (terms.length === 0) return null;
282-
const separated = terms.map(term => term.includes(TEXT_SEPARATOR));
283-
return (row) => {
284-
const texts = getCachedQuickFilterTexts(row, columns);
285-
for (let t = 0; t < terms.length; t++) {
286-
if (!textsInclude(texts, terms[t], separated[t])) return false;
287-
}
288-
return true;
289-
};
283+
return (row) => textsMatchTerms(getCachedQuickFilterTexts(row, columns), terms);
284+
}
285+
286+
// filterRows also keeps the texts of the last rows array it searched, by position, per column
287+
// lookup. Looking a text up by index is much cheaper than one WeakMap entry per row, and after an
288+
// edit (a new array with mostly the same row objects at the same positions) only the rows whose
289+
// object changed are read again.
290+
interface QuickFilterArrayTexts {
291+
rows: readonly GridRowModel[];
292+
texts: string[][];
293+
}
294+
const quickFilterArrayCache = new WeakMap<GridColumnLookup, QuickFilterArrayTexts>();
295+
296+
function getQuickFilterTextsByIndex(rows: readonly GridRowModel[], columns: GridColumnLookup): string[][] {
297+
const previous = quickFilterArrayCache.get(columns);
298+
if (previous && previous.rows === rows) return previous.texts;
299+
const texts = new Array<string[]>(rows.length);
300+
for (let i = 0; i < rows.length; i++) {
301+
const row = rows[i];
302+
texts[i] = previous && i < previous.rows.length && previous.rows[i] === row
303+
? previous.texts[i]
304+
: getQuickFilterTexts(row, columns);
305+
}
306+
quickFilterArrayCache.set(columns, { rows, texts });
307+
return texts;
290308
}
291309

292310
/**
@@ -356,6 +374,17 @@ export function filterRows<R extends GridRowModel>(
356374
filterModel: GridFilterModel,
357375
columns?: GridColumnLookup
358376
): R[] {
377+
const terms = columns && filterModel ? quickFilterTerms(filterModel.quickFilterValues) : [];
378+
if (columns && terms.length > 0) {
379+
// The same result as createRowFilter's predicate: the quick filter and then the items.
380+
const itemsPredicate = compileGroup(filterModel.items ?? [], filterModel.logicOperator ?? 'and', { columns, warned: new Set() });
381+
const texts = getQuickFilterTextsByIndex(rows, columns);
382+
const result: R[] = [];
383+
for (let i = 0; i < rows.length; i++) {
384+
if (textsMatchTerms(texts[i], terms) && (!itemsPredicate || itemsPredicate(rows[i]))) result.push(rows[i]);
385+
}
386+
return result;
387+
}
359388
const predicate = createRowFilter(filterModel, columns);
360389
if (!predicate) return rows;
361390
return rows.filter(row => predicate(row));

‎lib/utils/values.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ export function getFormattedValue<R extends GridRowModel>(
7575
): string | undefined {
7676
const formatter = colDef?.valueFormatter;
7777
if (!formatter) return undefined;
78-
return callSafely(
79-
() => formatter({ value, row, field }),
80-
undefined,
81-
`valueFormatter:${field}`,
82-
`valueFormatter for column "${field}" threw; the value is shown unformatted`,
83-
);
78+
// callSafely inlined: this runs for every cell of the quick filter's first search.
79+
try {
80+
return formatter({ value, row, field });
81+
} catch (error) {
82+
warnCallbackFailure(`valueFormatter:${field}`, `valueFormatter for column "${field}" threw; the value is shown unformatted`, error);
83+
return undefined;
84+
}
8485
}
8586

8687
/**

0 commit comments

Comments
 (0)