micro optimizations and cache fix

This commit is contained in:
Will McGugan
2022-12-10 11:58:07 +00:00
parent 8263d5c7f0
commit 7a42f5ec48
4 changed files with 15 additions and 32 deletions

View File

@@ -9,6 +9,7 @@ when setting and getting.
from __future__ import annotations
from operator import attrgetter
from typing import TYPE_CHECKING, Generic, Iterable, NamedTuple, TypeVar, cast
import rich.repr
@@ -262,10 +263,7 @@ class BoxProperty:
A ``tuple[EdgeType, Style]`` containing the string type of the box and
it's style. Example types are "rounded", "solid", and "dashed".
"""
box_type, color = obj.get_rule(self.name) or ("", self._default_color)
if box_type in {"none", "hidden"}:
box_type = ""
return (box_type, color)
return obj.get_rule(self.name) or ("", self._default_color)
def __set__(self, obj: Styles, border: tuple[EdgeType, str | Color] | None):
"""Set the box property
@@ -286,6 +284,8 @@ class BoxProperty:
obj.refresh(layout=True)
else:
_type, color = border
if _type in ("none", "hidden"):
_type = ""
new_value = border
if isinstance(color, str):
try:
@@ -367,6 +367,7 @@ class BorderProperty:
f"{name}_bottom",
f"{name}_left",
)
self._get_properties = attrgetter(*self._properties)
def __get__(
self, obj: StylesBase, objtype: type[StylesBase] | None = None
@@ -380,13 +381,8 @@ class BorderProperty:
Returns:
An ``Edges`` object describing the type and style of each edge.
"""
top, right, bottom, left = self._properties
return Edges(
getattr(obj, top),
getattr(obj, right),
getattr(obj, bottom),
getattr(obj, left),
)
return Edges(*self._get_properties(obj))
def __set__(
self,
@@ -411,27 +407,14 @@ class BorderProperty:
_rich_traceback_omit = True
top, right, bottom, left = self._properties
border_spacing = Edges(
getattr(obj, top),
getattr(obj, right),
getattr(obj, bottom),
getattr(obj, left),
).spacing
border_spacing = Edges(*self._get_properties(obj)).spacing
def check_refresh() -> None:
"""Check if an update requires a layout"""
if not self._layout:
obj.refresh()
else:
layout = (
Edges(
getattr(obj, top),
getattr(obj, right),
getattr(obj, bottom),
getattr(obj, left),
).spacing
!= border_spacing
)
layout = Edges(*self._get_properties(obj)).spacing != border_spacing
obj.refresh(layout=layout)
if border is None:

View File

@@ -351,8 +351,7 @@ class StylesBase(ABC):
Returns:
Spacing: Space around widget content.
"""
spacing = self.padding + self.border.spacing
return spacing
return self.padding + self.border.spacing
@property
def auto_dimensions(self) -> bool:
@@ -993,9 +992,10 @@ class RenderStyles(StylesBase):
Returns:
Spacing: Space around widget content.
"""
# This is (surprisingly) a bit of a bottleneck
if self._gutter is not None:
cache_key, gutter = self._gutter
if cache_key == self._updates:
if cache_key == self._cache_key:
return gutter
gutter = self.padding + self.border.spacing
self._gutter = (self._cache_key, gutter)

View File

@@ -691,7 +691,8 @@ class Region(NamedTuple):
Returns:
Region: New region.
"""
if not any(margin):
return self
top, right, bottom, left = margin
x, y, width, height = self
return Region(

View File

@@ -973,10 +973,9 @@ class Widget(DOMNode):
Returns:
Spacing: Scrollbar gutter spacing.
"""
gutter = Spacing(
return Spacing(
0, self.scrollbar_size_vertical, self.scrollbar_size_horizontal, 0
)
return gutter
@property
def gutter(self) -> Spacing: