From 2819ea02b152f631a63e758002b2ed943e0c2fc5 Mon Sep 17 00:00:00 2001 From: correctmost <134317971+correctmost@users.noreply.github.com> Date: Mon, 10 Mar 2025 21:10:33 -0400 Subject: [PATCH] Speed up _count_wchars by avoiding east_asian_width calls (#3240) This commit helps avoid ~390,000 duplicate east_asian_width calls when opening the additional packages menu. --- archinstall/lib/output.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index 927c4220..4208cb46 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -5,6 +5,7 @@ import unicodedata from collections.abc import Callable from dataclasses import asdict, is_dataclass from enum import Enum +from functools import lru_cache from pathlib import Path from typing import TYPE_CHECKING, Any @@ -327,9 +328,14 @@ def log( Tui.print(text) +@lru_cache(maxsize=128) +def _is_wide_character(char: str) -> bool: + return unicodedata.east_asian_width(char) in 'FW' + + def _count_wchars(string: str) -> int: "Count the total number of wide characters contained in a string" - return sum(unicodedata.east_asian_width(c) in 'FW' for c in string) + return sum(_is_wide_character(c) for c in string) def unicode_ljust(string: str, width: int, fillbyte: str = ' ') -> str: