Skip to content

Commit 2969abf

Browse files
authored
optimize horizontal and vertical (#2234)
* optimize horizontal and vertical * generator to list expressions * micro op * another micro-optimization
1 parent 44367a7 commit 2969abf

File tree

6 files changed

+43
-30
lines changed

6 files changed

+43
-30
lines changed

src/textual/_resolve.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ def resolve(
4444

4545
from_float = Fraction.from_float
4646
total_fraction = from_float(
47-
sum(scalar.value for scalar, fraction in resolved if fraction is None)
47+
sum([scalar.value for scalar, fraction in resolved if fraction is None])
4848
)
4949

5050
if total_fraction:
5151
total_gutter = gutter * (len(dimensions) - 1)
52-
consumed = sum(fraction for _, fraction in resolved if fraction is not None)
52+
consumed = sum([fraction for _, fraction in resolved if fraction is not None])
5353
remaining = max(Fraction(0), Fraction(total - total_gutter) - consumed)
5454
fraction_unit = Fraction(remaining, total_fraction)
5555
resolved_fractions = [
@@ -118,23 +118,21 @@ def resolve_box_models(
118118
]
119119

120120
if dimension == "width":
121-
total_remaining = sum(
122-
box_model.width for box_model in box_models if box_model is not None
123-
)
121+
total_remaining = sum([width for width, _, _ in filter(None, box_models)])
124122
remaining_space = max(0, size.width - total_remaining - margin_width)
125123
else:
126-
total_remaining = sum(
127-
box_model.height for box_model in box_models if box_model is not None
128-
)
124+
total_remaining = sum([height for _, height, _ in filter(None, box_models)])
129125
remaining_space = max(0, size.height - total_remaining - margin_height)
130126

131127
fraction_unit = Fraction(
132128
remaining_space,
133129
int(
134130
sum(
135-
dimension.value
136-
for dimension in dimensions
137-
if dimension and dimension.is_fraction
131+
[
132+
dimension.value
133+
for dimension in dimensions
134+
if dimension and dimension.is_fraction
135+
]
138136
)
139137
)
140138
or 1,

src/textual/layouts/horizontal.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
from __future__ import annotations
22

33
from fractions import Fraction
4+
from typing import TYPE_CHECKING
45

56
from .._layout import ArrangeResult, Layout, WidgetPlacement
67
from .._resolve import resolve_box_models
78
from ..geometry import Region, Size
8-
from ..widget import Widget
9+
10+
if TYPE_CHECKING:
11+
from ..geometry import Spacing
12+
from ..widget import Widget
913

1014

1115
class HorizontalLayout(Layout):
@@ -24,17 +28,22 @@ def arrange(
2428
parent_size = parent.outer_size
2529

2630
child_styles = [child.styles for child in children]
27-
box_margins = [styles.margin for styles in child_styles]
31+
box_margins: list[Spacing] = [styles.margin for styles in child_styles]
2832
if box_margins:
2933
resolve_margin = Size(
30-
(
31-
sum(
32-
max(margin1.right, margin2.left)
34+
sum(
35+
[
36+
max(margin1[1], margin2[3])
3337
for margin1, margin2 in zip(box_margins, box_margins[1:])
34-
)
35-
+ (box_margins[0].left + box_margins[-1].right)
38+
]
39+
)
40+
+ (box_margins[0].left + box_margins[-1].right),
41+
max(
42+
[
43+
margin_top + margin_bottom
44+
for margin_top, _, margin_bottom, _ in box_margins
45+
]
3646
),
37-
max(margin.height for margin in box_margins),
3847
)
3948
else:
4049
resolve_margin = Size(0, 0)

src/textual/layouts/vertical.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ..geometry import Region, Size
99

1010
if TYPE_CHECKING:
11+
from ..geometry import Spacing
1112
from ..widget import Widget
1213

1314

@@ -24,17 +25,22 @@ def arrange(
2425
parent_size = parent.outer_size
2526

2627
child_styles = [child.styles for child in children]
27-
box_margins = [styles.margin for styles in child_styles]
28+
box_margins: list[Spacing] = [styles.margin for styles in child_styles]
2829
if box_margins:
2930
resolve_margin = Size(
30-
max([margin.width for margin in box_margins]),
31-
(
32-
sum(
33-
max(margin1.bottom, margin2.top)
34-
for margin1, margin2 in zip(box_margins, box_margins[1:])
35-
)
36-
+ (box_margins[0].top + box_margins[-1].bottom)
31+
max(
32+
[
33+
margin_right + margin_left
34+
for _, margin_right, _, margin_left in box_margins
35+
]
3736
),
37+
sum(
38+
[
39+
max(margin1[2], margin2[0])
40+
for margin1, margin2 in zip(box_margins, box_margins[1:])
41+
]
42+
)
43+
+ (box_margins[0].top + box_margins[-1].bottom),
3844
)
3945
else:
4046
resolve_margin = Size(0, 0)

src/textual/strip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_line_length(segments: Iterable[Segment]) -> int:
2525
Length of line in cells.
2626
"""
2727
_cell_len = cell_len
28-
return sum(_cell_len(text) for text, _, control in segments if not control)
28+
return sum([_cell_len(text) for text, _, control in segments if not control])
2929

3030

3131
class StripRenderable:

src/textual/widget.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ def get_content_height(self, container: Size, viewport: Size, width: int) -> int
10561056
options = self._console.options.update_width(width).update(highlight=False)
10571057
segments = self._console.render(renderable, options)
10581058
# Cheaper than counting the lines returned from render_lines!
1059-
height = sum(text.count("\n") for text, _, _ in segments)
1059+
height = sum([text.count("\n") for text, _, _ in segments])
10601060
self._content_height_cache = (cache_key, height)
10611061

10621062
return height

src/textual/widgets/_text_log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def write(
155155

156156
self.max_width = max(
157157
self.max_width,
158-
max(sum(segment.cell_length for segment in _line) for _line in lines),
158+
max(sum([segment.cell_length for segment in _line]) for _line in lines),
159159
)
160160
strips = Strip.from_lines(lines)
161161
for strip in strips:

0 commit comments

Comments
 (0)