perf: reduce streaming text processing and classic CLI redraws (#5728)

* perf: skip tag parsing for ordinary streamed text

* perf: coalesce classic CLI stream refreshes
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-15 11:48:40 +08:00
1 parent 1c3c68262f
commit aa257a705e
3 files changed
+114 -2

No files matched your search

+32 -2
View File
@@ -9,7 +9,9 @@ that plagued earlier approaches.
from __future__ import annotations
import asyncio
import sys
import time
from contextlib import contextmanager, nullcontext
from typing import Literal
@@ -18,6 +20,8 @@ from rich.live import Live
from rich.markdown import Markdown
from rich.text import Text
_STREAM_REFRESH_INTERVAL = 0.05
def _clear_current_line(console: Console) -> None:
"""Erase a transient status line before printing persistent output."""
@@ -107,6 +111,8 @@ class StreamRenderer:
self.streamed = False
self._console = _make_console()
self._live: Live | None = None
self._refresh_handle: asyncio.TimerHandle | None = None
self._next_refresh_at = 0.0
self._spinner: ThinkingSpinner | None = None
self._header_printed = False
self._start_spinner()
@@ -160,6 +166,7 @@ class StreamRenderer:
"""Context manager: temporarily stop transient output for clean trace lines."""
@contextmanager
def _pause():
self._cancel_refresh()
live_was_active = self._live is not None
if self._live:
# Trace/reasoning can arrive after answer streaming has started.
@@ -191,11 +198,33 @@ class StreamRenderer:
transient=True,
)
self._live.start()
else:
self._live.update(self._renderable())
self._live.refresh()
self._next_refresh_at = time.monotonic() + _STREAM_REFRESH_INTERVAL
elif self._refresh_handle is None:
delay = max(0.0, self._next_refresh_at - time.monotonic())
if delay == 0:
self._refresh_stream()
else:
self._refresh_handle = asyncio.get_running_loop().call_later(
delay, self._refresh_stream,
)
def _refresh_stream(self) -> None:
self._refresh_handle = None
if self._live is None:
return
self._live.update(self._renderable())
self._live.refresh()
# Leave a quiet interval even when rendering itself exceeds the frame budget.
self._next_refresh_at = time.monotonic() + _STREAM_REFRESH_INTERVAL
def _cancel_refresh(self) -> None:
if self._refresh_handle is not None:
self._refresh_handle.cancel()
self._refresh_handle = None
async def on_end(self, *, resuming: bool = False) -> None:
self._cancel_refresh()
if self._live:
# Double-refresh to sync _shape before stop() calls refresh().
self._live.refresh()
@@ -225,6 +254,7 @@ class StreamRenderer:
async def close(self) -> None:
"""Stop spinner/live without rendering a final streamed round."""
self._cancel_refresh()
if self._live:
self._live.stop()
self._live = None
+7
View File
@@ -191,6 +191,9 @@ def strip_think(text: str) -> str:
tokens mid-text would silently rewrite any message where a user or the
assistant discusses the tokens themselves.
"""
# Every supported control tag contains '<'; ordinary text only needs trimming.
if "<" not in text:
return text.strip()
# Well-formed blocks first.
text = re.sub(rf"<(?P<tag>{_THINKING_TAG})>[\s\S]*?</(?P=tag)>", "", text)
text = re.sub(rf"^\s*<{_THINKING_TAG}>[\s\S]*$", "", text)
@@ -224,6 +227,8 @@ def strip_reasoning_tags(text: object) -> str:
"""Remove wrapper tags from text that is already known to be reasoning."""
if not isinstance(text, str):
return ""
if "<" not in text:
return text.strip()
text = re.sub(rf"^\s*<{_THINKING_TAG}/>\s*", "", text)
text = re.sub(rf"\s*<{_THINKING_TAG}/>\s*$", "", text)
text = re.sub(rf"^\s*<{_THINKING_TAG}>\s*", "", text)
@@ -239,6 +244,8 @@ def extract_think(text: str) -> tuple[str | None, str]:
extracted; unclosed streaming prefixes are stripped from the cleaned
text but not surfaced — :func:`strip_think` handles that case.
"""
if "<" not in text:
return None, text.strip()
parts: list[str] = []
for m in re.finditer(rf"<(?P<tag>{_THINKING_TAG})>([\s\S]*?)</(?P=tag)>", text):
parts.append(m.group(2).strip())