perf: Cache rendered message rows to avoid redundant processing.

This commit is contained in:
2026-03-14 18:26:18 -07:00
parent 030c75f30d
commit b8b38cd0ad

View File

@@ -826,6 +826,7 @@ def stream(items):
MSG_COLOR = "\033[1;38;5;87m" # sky cyan MSG_COLOR = "\033[1;38;5;87m" # sky cyan
MSG_META = "\033[38;5;245m" # cool grey MSG_META = "\033[38;5;245m" # cool grey
MSG_BORDER = "\033[2;38;5;37m" # dim teal MSG_BORDER = "\033[2;38;5;37m" # dim teal
_msg_cache = (None, None) # (cache_key, rendered_rows)
while queued < HEADLINE_LIMIT or active: while queued < HEADLINE_LIMIT or active:
t0 = time.monotonic() t0 = time.monotonic()
@@ -846,11 +847,16 @@ def stream(items):
if msg_active: if msg_active:
# ── MESSAGE state: freeze scroll, render message ── # ── MESSAGE state: freeze scroll, render message ──
buf = [] buf = []
# Render message text with OTF font # Render message text with OTF font (cached across frames)
display_text = m_body or m_title or "(empty)" display_text = m_body or m_title or "(empty)"
display_text = re.sub(r"\s+", " ", display_text.upper()) display_text = re.sub(r"\s+", " ", display_text.upper())
msg_rows = _big_wrap(display_text, w - 4) cache_key = (display_text, w)
msg_rows = _lr_gradient(msg_rows) if _msg_cache[0] != cache_key:
msg_rows = _big_wrap(display_text, w - 4)
msg_rows = _lr_gradient(msg_rows)
_msg_cache = (cache_key, msg_rows)
else:
msg_rows = _msg_cache[1]
# Center vertically in scroll zone # Center vertically in scroll zone
total_h = len(msg_rows) + 4 # +4 for border + meta + padding total_h = len(msg_rows) + 4 # +4 for border + meta + padding
y_off = max(0, (sh - total_h) // 2) y_off = max(0, (sh - total_h) // 2)