Skip to content

Commit 46150cd

Browse files
committed
sum and map is faster
1 parent 9e7f363 commit 46150cd

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

rich/cells.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*map(chr, range(0x2500, 0x25FF + 1)),
1414
]
1515
)
16+
1617
_is_single_cell_widths = _SINGLE_CELLS.issuperset
1718

1819

@@ -29,9 +30,9 @@ def cached_cell_len(text: str) -> int:
2930
Returns:
3031
int: Get the number of cells required to display text.
3132
"""
32-
_get_size = get_character_cell_size
33-
total_size = sum(_get_size(character) for character in text)
34-
return total_size
33+
if _is_single_cell_widths(text):
34+
return len(text)
35+
return sum(map(get_character_cell_size, text))
3536

3637

3738
def cell_len(text: str, _cell_len: Callable[[str], int] = cached_cell_len) -> int:
@@ -45,9 +46,9 @@ def cell_len(text: str, _cell_len: Callable[[str], int] = cached_cell_len) -> in
4546
"""
4647
if len(text) < 512:
4748
return _cell_len(text)
48-
_get_size = get_character_cell_size
49-
total_size = sum(_get_size(character) for character in text)
50-
return total_size
49+
if _is_single_cell_widths(text):
50+
return len(text)
51+
return sum(map(get_character_cell_size, text))
5152

5253

5354
@lru_cache(maxsize=4096)

0 commit comments

Comments
 (0)