From 46150cdbf61426c4683c59a0e4f45dca23d38202 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Thu, 31 Oct 2024 18:45:13 +0000 Subject: [PATCH] sum and map is faster --- rich/cells.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/rich/cells.py b/rich/cells.py index 06a57b2c..3fa58c3e 100644 --- a/rich/cells.py +++ b/rich/cells.py @@ -13,6 +13,7 @@ _SINGLE_CELLS = frozenset( *map(chr, range(0x2500, 0x25FF + 1)), ] ) + _is_single_cell_widths = _SINGLE_CELLS.issuperset @@ -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: @@ -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)