Skip to content
Merged
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
41 changes: 41 additions & 0 deletions table/src/plugins/-private/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from '@ember/debug';
import { createCache, getValue } from '@glimmer/tracking/primitives/cache';

import {
COLUMN_META_KEY,
Expand Down Expand Up @@ -309,6 +310,19 @@ export const preferences = {
* This works recursively up the plugin tree up until a plugin has no requirements, and then
* all columns from the table are returned.
*/
/**
* One cache per table and requester. `createCache` participates in
* autotracking, so the list is recomputed when the columns, their order or
* their visibility change, and reused otherwise.
*
* Without it, reading the list once per rendered row costs O(rows x columns)
* per render, and nothing at the call site says so.
*/
const COLUMNS_CACHE = new WeakMap<
object,
Map<unknown, ReturnType<typeof createCache<unknown>>>
>();

function columnsFor<
DataType = any,
ColumnMeta = unknown,
Expand All @@ -317,6 +331,33 @@ function columnsFor<
>(
table: Table<DataType, ColumnMeta, Meta, CellArgs>,
requester?: Plugin<any>,
): Column<DataType, ColumnMeta, Meta, CellArgs>[] {
let byRequester = COLUMNS_CACHE.get(table);

if (byRequester === undefined) {
byRequester = new Map();
COLUMNS_CACHE.set(table, byRequester);
}

const key: unknown = requester ?? COLUMNS_CACHE;
let cache = byRequester.get(key);

if (cache === undefined) {
cache = createCache(() => uncachedColumnsFor(table, requester));
byRequester.set(key, cache);
}

return getValue(cache) as Column<DataType, ColumnMeta, Meta, CellArgs>[];
}

function uncachedColumnsFor<
DataType = any,
ColumnMeta = unknown,
Meta = unknown,
CellArgs = unknown,
>(
table: Table<DataType, ColumnMeta, Meta, CellArgs>,
requester?: Plugin<any>,
): Column<DataType, ColumnMeta, Meta, CellArgs>[] {
// Plugins hold columns of this same table, so they have its meta.
return resolveColumns(table, requester) as Column<
Expand Down
Loading