Skip to content

Commit

Permalink
sum and map is faster
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Oct 31, 2024
1 parent 9e7f363 commit 46150cd
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions rich/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*map(chr, range(0x2500, 0x25FF + 1)),
]
)

_is_single_cell_widths = _SINGLE_CELLS.issuperset


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


def cell_len(text: str, _cell_len: Callable[[str], int] = cached_cell_len) -> int:
Expand All @@ -45,9 +46,9 @@ def cell_len(text: str, _cell_len: Callable[[str], int] = cached_cell_len) -> in
"""
if len(text) < 512:
return _cell_len(text)
_get_size = get_character_cell_size
total_size = sum(_get_size(character) for character in text)
return total_size
if _is_single_cell_widths(text):
return len(text)
return sum(map(get_character_cell_size, text))


@lru_cache(maxsize=4096)
Expand Down

0 comments on commit 46150cd

Please sign in to comment.