Skip to content

Conversation

@philcunliffe
Copy link
Contributor

Playing around with an internal cache, ran into some inconveniences I need to think through

@philcunliffe philcunliffe requested a review from severo September 3, 2025 20:18
Copy link
Contributor

@severo severo left a comment

Choose a reason for hiding this comment

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

I put some random thoughts as comments.

I think this proposal helps shape a better DataFrame, with a decoupled cache.

}
} finally {
data.eventTarget?.removeEventListener('resolve', callback)
// No cleanup needed
Copy link
Contributor

Choose a reason for hiding this comment

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

ok, so the try finally structure can be removed

//
// For static data frames, eventTarget can be undefined.
eventTarget?: CustomEventTarget<DataFrameEvents>
// Optional cache listener for dataframes that support caching
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add some more comments to make it clear that the returned function is used to remove the listener and should be called when the user dismisses the data frame.

// Simulate async data fetching
const value = await fetchCellData(row, column)
// store the fetched data in the cache
setCell(row, column, value)
Copy link
Contributor

Choose a reason for hiding this comment

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

you have to call setRowNumber too, otherwise, the row numbers will never appear (if I understand well)

Copy link
Contributor

Choose a reason for hiding this comment

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

good idea to provide a cache API + an implementation

getRowNumber: ({ row }) => ({ value: rowIndex }),
getCell: ({ row, col }) => cache.get(col).get(row),
getRowNumber: createGetRowNumber({ numRows: 1000000 }),
getCell: ({ row, column }) => cache.get(column)?.get(row),
Copy link
Contributor

Choose a reason for hiding this comment

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

well spot (col -> column)

/**
* Get all cached rows for a specific column.
*/
getCachedRowsForColumn(column: string): number[] {
Copy link
Contributor

Choose a reason for hiding this comment

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

same for the row numbers?

*/
getCachedRowsForColumn(column: string): number[] {
const columnCache = cellCache.get(column)
return columnCache ? Array.from(columnCache.keys()) : []
Copy link
Contributor

Choose a reason for hiding this comment

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

If we use an array, should we sort?

Or we might prefer to return a Set

const { cacheKey, numRows, columnDescriptors, fetch, getRowNumber: customGetRowNumber } = options

// Use a ref to store the cache so it persists across re-renders but can be replaced when cacheKey changes
const cacheRef = useRef<{ key: string | undefined, cache: DataFrameCache, version: number } | null>(null)
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure of my comment, but... instead of relying on a React ref + a hook, maybe we could transform this file into a simpler function that transform a DataFrame into a cached DataFrame (as we had initially in hightable), optionally passing a cache as an argument/option? The optional cache could still be stored in a React state, or Ref, but it would decouple a bit more.


// Default getRowNumber implementation
const defaultGetRowNumber = useCallback((row: number): ResolvedValue<number> => {
if (row < 0 || row >= numRows || !Number.isInteger(row)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we have a helper function for that

Comment on lines +97 to +99
setCell,
setRowNumber,
clearCache,
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe include these fields as a super interface called CachedDataFrame, that would extend DataFrame?

the returned object is also very similar to DataFrameCache... I think we could simplify and combine these three interfaces: DataFrame, DataFrameCache and this unnamed return type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants