Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

perf(jarvisbar): one Tk photo per bar — each frame pastes pixels instead of building a PhotoImage

rubenluetke10-beepd7a5be020330
diff --git a/jarvis/ui/jarvisbar/overlay.py b/jarvis/ui/jarvisbar/overlay.py
index c1d5cb641e..7fbb0ba8d0 100644
--- a/jarvis/ui/jarvisbar/overlay.py
+++ b/jarvis/ui/jarvisbar/overlay.py
@@ -331,6 +331,18 @@ def _win32_force_topmost(root: Any, *, user32: Any | None = None) -> bool:
return False
+def _photo_size(photo: Any) -> tuple[int, int] | None:
+ """``(width, height)`` of a Tk photo image, or ``None`` when it cannot say.
+
+ ``None`` never matches a PIL image's ``size``, so a photo that cannot report
+ its geometry is simply rebuilt — the pre-existing path, never a crash.
+ """
+ try:
+ return (int(photo.width()), int(photo.height()))
+ except Exception: # noqa: BLE001 - a stand-in or a torn-down interpreter
+ return None
+
+
def _win32_topmost_band_is_healthy(root: Any, *, user32: Any | None = None) -> bool | None:
"""Check whether any ordinary visible HWND sits above the Jarvis Bar.
@@ -1335,11 +1347,33 @@ class JarvisBarOverlay:
# the empty backing rectangle on screen. Supplying the actual
# consumer also stays correct for the single-root Windows/Linux
# paths.
- self._photo = ImageTk.PhotoImage(img, master=self._root)
- if self._image_id is None:
- self._image_id = self._canvas.create_image(0, 0, anchor="nw", image=self._photo)
+ #
+ # ONE Tk photo per bar, new pixels per frame. Building a fresh
+ # ``PhotoImage`` every tick means a Tcl ``image create photo``,
+ # an ``itemconfig`` that swaps the canvas item's image, and the
+ # ``image delete`` of the previous one when Python drops it —
+ # measured at 2.1-4.0 ms per frame against 0.8-1.0 ms for
+ # ``paste()`` into the existing photo (same layered window,
+ # same renderer, 25 fps; CPU diet 2026-08-23). The pixels are
+ # identical; only the bookkeeping is gone. A photo of another
+ # size (the bar was resized) or a stand-in without ``paste``
+ # (tests) takes the original create-and-swap path.
+ photo = self._photo
+ if (
+ photo is not None
+ and self._image_id is not None
+ and callable(getattr(photo, "paste", None))
+ and _photo_size(photo) == img.size
+ ):
+ photo.paste(img)
else:
- self._canvas.itemconfig(self._image_id, image=self._photo)
+ self._photo = ImageTk.PhotoImage(img, master=self._root)
+ if self._image_id is None:
+ self._image_id = self._canvas.create_image(
+ 0, 0, anchor="nw", image=self._photo
+ )
+ else:
+ self._canvas.itemconfig(self._image_id, image=self._photo)
except Exception: # noqa: BLE001 — one bad frame must never freeze the bar
log.exception("JarvisBar frame render failed — dropping one frame")
finally:
diff --git a/tests/unit/ui/jarvisbar/test_frame_pacing.py b/tests/unit/ui/jarvisbar/test_frame_pacing.py
index 136fe9663b..9127b3bf7c 100644
--- a/tests/unit/ui/jarvisbar/test_frame_pacing.py
+++ b/tests/unit/ui/jarvisbar/test_frame_pacing.py
@@ -392,3 +392,103 @@ def test_target_frame_is_25fps_not_60():
from the old 16 ms target on a layered window whose DWM ceiling was ~31/s,
and costs a third of the wake-ups."""
assert TARGET_FRAME_MS == 40
+
+
+# --------------------------------------------------------- one photo per bar
+
+
+class _SizedImage:
+ """Stands in for the PIL frame: only its ``size`` matters to the loop."""
+
+ def __init__(self, size: tuple[int, int]) -> None:
+ self.size = size
+
+
+class _SizedRenderer:
+ def __init__(self, size: tuple[int, int]) -> None:
+ self.size = size
+ self.calls = 0
+
+ def render(self, *a: object, **k: object) -> _SizedImage: # noqa: ANN201
+ self.calls += 1
+ return _SizedImage(self.size)
+
+
+class _FakePhoto:
+ """A Tk photo that remembers its geometry and counts in-place pastes."""
+
+ created = 0
+
+ def __init__(self, img: _SizedImage, **_kwargs: object) -> None:
+ _FakePhoto.created += 1
+ self._w, self._h = img.size
+ self.pastes = 0
+
+ def width(self) -> int:
+ return self._w
+
+ def height(self) -> int:
+ return self._h
+
+ def paste(self, img: _SizedImage) -> None:
+ assert img.size == (self._w, self._h)
+ self.pastes += 1
+
+
+def test_an_audible_bar_reuses_one_tk_photo_and_pastes_new_pixels(monkeypatch):
+ """Building a PhotoImage per frame cost 2-4 ms a tick on the real layered
+ window (a Tcl image create + canvas swap + image delete); pasting the new
+ pixels into the one photo the bar owns is ~1 ms for identical output.
+ The loop therefore creates the photo once and pastes thereafter."""
+ monkeypatch.setattr("PIL.ImageTk.PhotoImage", _FakePhoto)
+ _FakePhoto.created = 0
+ renderer_obj = _SizedRenderer((82, 35))
+ bar, _root, canvas = _bare_bar(renderer_obj, mode="listen")
+ bar._ext_level = 0.6
+ monkeypatch.setattr(renderer, "effective_ext_level", lambda level, age: level, raising=True)
+
+ for _ in range(12):
+ bar._schedule_frame()
+
+ assert renderer_obj.calls == 12
+ assert _FakePhoto.created == 1, "a new Tk photo was built for a same-size frame"
+ assert bar._photo.pastes == 11
+ assert canvas.create_image_calls == 1
+ assert canvas.itemconfig_calls == 0, "the canvas item never needs re-pointing"
+
+
+def test_a_resized_bar_gets_a_fresh_tk_photo(monkeypatch):
+ """``paste`` needs matching geometry: after a size change (the Bar-size
+ slider) the next frame rebuilds the photo and re-points the canvas item,
+ exactly the pre-existing path."""
+ monkeypatch.setattr("PIL.ImageTk.PhotoImage", _FakePhoto)
+ _FakePhoto.created = 0
+ renderer_obj = _SizedRenderer((82, 35))
+ bar, _root, canvas = _bare_bar(renderer_obj, mode="listen")
+ bar._ext_level = 0.6
+ monkeypatch.setattr(renderer, "effective_ext_level", lambda level, age: level, raising=True)
+ for _ in range(3):
+ bar._schedule_frame()
+ first = bar._photo
+
+ renderer_obj.size = (120, 50)
+ bar._schedule_frame()
+
+ assert _FakePhoto.created == 2
+ assert bar._photo is not first
+ assert (bar._photo.width(), bar._photo.height()) == (120, 50)
+ assert canvas.itemconfig_calls == 1
+ assert first.pastes == 2
+
+
+def test_a_photo_stand_in_without_paste_takes_the_create_path(monkeypatch):
+ """The other tests in this file replace PhotoImage with a passthrough that
+ has no ``paste``; the loop must keep working with such a stand-in."""
+ renderer_obj = _CountingRenderer()
+ bar, _root, canvas = _bare_bar(renderer_obj, mode="listen")
+ bar._ext_level = 0.6
+ monkeypatch.setattr(renderer, "effective_ext_level", lambda level, age: level, raising=True)
+ for _ in range(4):
+ bar._schedule_frame()
+ assert canvas.create_image_calls == 1
+ assert canvas.itemconfig_calls == 3