Skip to content

Add caching for DataTable._get_row_renderables() #5959

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/textual/widgets/_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,10 @@ def __init__(
we need to re-render it. """
self._cell_render_cache: LRUCache[CellCacheKey, SegmentLines] = LRUCache(10000)
"""Cache for individual cells."""
self._row_renderable_cache: LRUCache[tuple[int, int], RowRenderables] = (
LRUCache(1000)
)
"""Caches row renderables - key is (update_count, row_index)"""
self._line_cache: LRUCache[LineCacheKey, Strip] = LRUCache(1000)
"""Cache for lines within rows."""
self._offset_cache: LRUCache[int, list[tuple[RowKey, int]]] = LRUCache(1)
Expand Down Expand Up @@ -1086,6 +1090,7 @@ def get_column_index(self, column_key: ColumnKey | str) -> int:
def _clear_caches(self) -> None:
self._row_render_cache.clear()
self._cell_render_cache.clear()
self._row_renderable_cache.clear()
self._line_cache.clear()
self._styles_cache.clear()
self._offset_cache.clear()
Expand All @@ -1109,6 +1114,7 @@ def notify_style_update(self) -> None:
super().notify_style_update()
self._row_render_cache.clear()
self._cell_render_cache.clear()
self._row_renderable_cache.clear()
self._line_cache.clear()
self._styles_cache.clear()
self._get_styles_to_render_cell.cache_clear()
Expand Down Expand Up @@ -1990,6 +1996,17 @@ def _get_row_renderables(self, row_index: int) -> RowRenderables:
Returns:
A RowRenderables containing the optional label and the rendered cells.
"""
update_count = self._update_count
cache_key = (update_count, row_index)
if cache_key in self._row_renderable_cache:
row_renderables = self._row_renderable_cache[cache_key]
else:
row_renderables = self._compute_row_renderables(row_index)
self._row_renderable_cache[cache_key] = row_renderables
return row_renderables

def _compute_row_renderables(self, row_index: int) -> RowRenderables:
"""Actual computation for _get_row_renderables"""
ordered_columns = self.ordered_columns
if row_index == -1:
header_row: list[RenderableType] = [
Expand Down
Loading