perf(rendering): cache entry.level in render_log_html (#46)

* perf(rendering): cache entry.level and is_thinking in render_log_html

Avoid repeated attribute access and redundant strip() calls
by caching level and is_thinking as local variables.

* fix: remove stale noqa and use set membership test

The refactoring reduced complexity below C901/PLR0912 thresholds,
making the noqa directive unused. Use `in` set test per PLR1714.

* Add blackbox benchmark VM infra

D2s_v5 (non-burstable, 2 vCPU, 8 GB) with cloud-init provisioning,
CPU-pinned benchmarks, and A/B comparison scripts.

---------

Co-authored-by: codeflash[bot] <codeflash[bot]@users.noreply.github.com>
This commit is contained in:
Kevin Turcios 2026-04-29 03:22:47 -05:00 committed by GitHub
parent 41edcf06e1
commit e4fcbb5b83
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -121,7 +121,7 @@ def tool_call_html(preview: str) -> str:
)
def render_log_html(entry: LogEntry) -> str: # noqa: C901, PLR0912
def render_log_html(entry: LogEntry) -> str:
ts = fmt_time(entry.timestamp)
src_cls = SOURCE_CLASSES.get(entry.source, "bg-gray-600")
src_label = SOURCE_LABELS.get(entry.source, entry.source[:3].upper())
@ -138,31 +138,32 @@ def render_log_html(entry: LogEntry) -> str: # noqa: C901, PLR0912
badge_cls = src_cls
badge_label = src_label
if entry.source == "user" and entry.level == "info":
level = entry.level
is_thinking = level == "assistant" and entry.message.strip() == "(thinking)"
if entry.source == "user" and level == "info":
msg = f'<span class="text-green-300 font-medium">{esc_md(entry.message)}</span>'
elif entry.level == "tool_call":
elif level == "tool_call":
preview = entry.data.get("input_preview", "")
msg = tool_call_html(preview)
elif entry.level == "tool_result":
elif level == "tool_result":
text = entry.message[:500]
if len(entry.message) > 500:
text += "..."
msg = f'<span class="text-gray-500">{esc(shorten_paths(text))}</span>'
elif entry.level == "assistant":
if entry.message.strip() == "(thinking)":
elif level == "assistant":
if is_thinking:
msg = f'<span class="text-gray-500 italic">{esc(entry.message)}</span>'
else:
msg = f'<span class="text-gray-100">{esc_md(entry.message)}</span>'
elif entry.level == "error":
elif level == "error":
msg = f'<span class="text-red-400 font-medium">{esc(entry.message)}</span>'
else:
msg = f'<span class="text-gray-300">{esc(entry.message)}</span>'
extra_div_classes = ""
if entry.level == "assistant" and entry.message.strip() == "(thinking)":
extra_div_classes = " border-t border-surface-800 mt-2 pt-1"
extra_div_classes = " border-t border-surface-800 mt-2 pt-1" if is_thinking else ""
is_tool = entry.level in ("tool_call", "tool_result")
is_tool = level in {"tool_call", "tool_result"}
opacity = " opacity-60" if is_tool else ""
indent = " pl-4" if is_tool else ""