Repository navigation
fix: draw superseded responses while only the current index changes - #290
hongquanli wants to merge 1 commit into
Conversation
Since pyapp-kit#233, a data response is drawn only if it belongs to the latest request generation. Every slider move issues a new request (and cancels queued ones), so while a slider is dragged faster than one frame loads, every response arrives superseded and is dropped: the image stays frozen until the slider stops. 0.4 drew those responses. Keep the guarantee that motivated the generation check (an old response never replaces newer data on screen, and nothing requested before a data/shape/layout change is drawn), but draw a superseded response when only the current index changed since it was requested: - `_request_data(index_only=...)`: `_apply_changes` passes True when visible axes, channel axis and channel mode are unchanged. Any other request (including `data_changed`) moves `_min_drawable_gen` up to the new generation, as before. - `_on_dims_changed` moves `_min_drawable_gen` past the current request, so responses for the old shape are not drawn as superseded frames. - `_cancel_futures` keeps futures that already started (they cannot be cancelled) so their generation is known when they finish. - `_is_drawable(gen)`: current generation, or not older than `_min_drawable_gen` and not older than the one on screen (`_shown_gen`). Measured in a downstream viewer (3 channels of 2048 x 2048, ~30 ms per fetch, a slider move every 8 ms): 1 of 233 positions drawn before, 104 of 105 after. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #290 +/- ##
==========================================
+ Coverage 85.20% 85.30% +0.10%
==========================================
Files 53 53
Lines 7192 7202 +10
==========================================
+ Hits 6128 6144 +16
+ Misses 1064 1058 -6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| # not older than what is already on screen | ||
| return 0 <= gen and self._min_drawable_gen <= gen and self._shown_gen <= gen |
There was a problem hiding this comment.
| # not older than what is already on screen | |
| return 0 <= gen and self._min_drawable_gen <= gen and self._shown_gen <= gen | |
| # not older than what is already on screen | |
| # and no newer response is already waiting to be drawn | |
| if gen < max(self._min_drawable_gen, self._shown_gen): | |
| return False | |
| return not any( | |
| g > gen and f.done() and not f.cancelled() for f, g in self._futures.items() | |
| ) |
I think we also want to skip a superseded response if a newer one has already finished and is just waiting for its callback to run on the main thread. Otherwise, whenever several requests finish before the event loop gets to them (e.g. a burst of index updates while the main thread is busy), they all pass the _shown_gen <= gen check (since they arrive in order) and we replay the whole backlog.
note: this breaks the last assertion in test_index_change_keeps_earlier_responses_drawable (before isn't drawable once the newer request is done but not yet drawn). I'd just drop that line, the _min_drawable_gen check above it already covers the intent. And maybe add a test for this:
@no_type_check
@_patch_views
def test_superseded_response_skipped_if_newer_done() -> None:
"""A superseded response is skipped when a newer one is already waiting."""
ctrl = ArrayViewer()
ctrl._current_gen = 5
ctrl._min_drawable_gen = 1
newer = _response_future(ctrl, 4, value=4) # done, callback not yet run
ctrl._on_data_response_ready(_response_future(ctrl, 3, value=3))
ctrl._canvas.refresh.assert_not_called()
ctrl._on_data_response_ready(newer)
assert ctrl._shown_gen == 4
ctrl._canvas.refresh.assert_called_once()
tlambert03
left a comment
There was a problem hiding this comment.
thanks @hongquanli ... small change requested
Problem
Since #233,
_on_data_response_readydraws a response only if it belongs to the latest request generation (gen != self._current_gen→ return). Every slider move issues a new request. So when the slider is dragged faster than one frame can be fetched, every response arrives superseded and is dropped. The image freezes until the slider stops, then jumps to the final position. 0.4.x drew those responses, so the image followed the drag.How visible this is depends on fetch time. In a downstream viewer (ndviewer_light, 3 channels of 2048 × 2048 uint16 at ~30 ms per fetch), dragging with a slider move every 8 ms drew 1 of 233 positions on 0.5.0, against 38 of 39 on 0.4.1.
Change
This keeps the guarantee the generation check provides: an older response never replaces newer data on screen, and nothing requested before a data/shape/layout change is ever drawn. It relaxes one case: a superseded response is still drawn if only the current index changed since it was requested.
_request_data(*, index_only=False):_apply_changespassesindex_only=Truewhen visible axes, channel axis and channel mode are unchanged. Every other request (including thedata_changedconnection, which uses the default) sets_min_drawable_gento the new generation, which is exactly the old behaviour._on_dims_changedsets_min_drawable_genpast the current request, so responses fetched for the old shape (possibly other channels or extents) are not drawn as superseded frames. The current request still draws if nothing newer is issued, as before._cancel_futuresremoves only futures that were actually cancelled. Futures already running cannot be cancelled. They stay in_futuresso their generation is known when they finish, and they pop themselves in the done callback as before._is_drawable(gen):gen == _current_gen, or_min_drawable_gen <= genand_shown_gen <= gen._shown_genis updated when a response is drawn.Net effect: during a drag the display shows the newest fetched frame (at most
max_workersfetches are in flight; queued ones are still cancelled), and the final position draws when its fetch completes, as before.Same 8 ms drag as above, with this change applied (as a shim downstream): 104 of 105 positions drawn.
Tests
tests/test_controller.py:test_stale_response_discardnow also sets_min_drawable_gen, so it models a superseded response from before a non-index change, which must still be discarded._cancel_futurescancels queued futures and keeps running ones with their generation;_on_dims_changedmakes earlier generations undrawable but keeps the current one.All 6 fail without the change. Full suite on Windows / Python 3.10 / PyQt6: 159 passed, 24 skipped.
ruff check,ruff format --checkandtyposare clean. mypy and pyright report the same counts as onmainfor_array_viewer.py.🤖 Generated with Claude Code