Skip to content

Commit 8ae3b5f

Browse files
committed
optimize styles cache render
1 parent 600ae3d commit 8ae3b5f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/textual/_styles_cache.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def __init__(self) -> None:
6363
self._cache: dict[int, Strip] = {}
6464
self._dirty_lines: set[int] = set()
6565
self._width = 1
66+
self._simple_strip: Strip | None = None
67+
"""A simple strip consisting of left border + background + right border, which may be reused in a render."""
6668

6769
def __rich_repr__(self) -> rich.repr.Result:
6870
if self._dirty_lines:
@@ -106,6 +108,7 @@ def render_widget(self, widget: Widget, crop: Region) -> list[Strip]:
106108
"""
107109
border_title = widget._border_title
108110
border_subtitle = widget._border_subtitle
111+
self._simple_strip = None
109112

110113
base_background, background = widget.background_colors
111114
styles = widget.styles
@@ -348,6 +351,7 @@ def post(segments: Iterable[Segment]) -> Iterable[Segment]:
348351
segments = _apply_opacity(segments, base_background, opacity)
349352
return segments
350353

354+
cache_simple_strip: bool = False
351355
line: Iterable[Segment]
352356
# Draw top or bottom borders (A)
353357
if (border_top and y == 0) or (border_bottom and y == height - 1):
@@ -411,11 +415,13 @@ def post(segments: Iterable[Segment]) -> Iterable[Segment]:
411415
label_segments,
412416
label_alignment, # type: ignore
413417
)
414-
415418
# Draw padding (B)
416419
elif (pad_top and y < gutter.top) or (
417420
pad_bottom and y >= height - gutter.bottom
418421
):
422+
if self._simple_strip is not None:
423+
return self._simple_strip
424+
cache_simple_strip = True
419425
background_rich_style = inner.rich_style
420426
left_style = Style(
421427
foreground=base_background + border_left_color.multiply_alpha(opacity)
@@ -498,6 +504,7 @@ def post(segments: Iterable[Segment]) -> Iterable[Segment]:
498504
line = [left, *line]
499505
else:
500506
line = [*line, right]
501-
502507
strip = Strip(post(line), width)
508+
if cache_simple_strip:
509+
self._simple_strip = strip
503510
return strip

0 commit comments

Comments
 (0)