Skip to content

Perf: eliminate O(N^2) sidebar row tones, cache notification_references and scroll cover clip - #2342

Merged
trissim merged 1 commit into
mainfrom
perf/sidebar-tones-scroll-covers-and-notif-cache
Nov 28, 2026
Merged

trissim merged 1 commit into
mainfrom
perf/sidebar-tones-scroll-covers-and-notif-cache

Conversation

@trissim

@trissim trissim commented Nov 28, 2026

Copy link
Copy Markdown

Summary

Measured with FrameProbe on live #comms (412x915 Pixel 8a viewport) and fixed three verified hotspots:

  1. Eliminated O(N^2) sidebar row-tone storm (src/toad/qt/window.py:199,694-715,861-890,958-962)

    • ConversationRows connected rowsInserted/rowsRemoved directly to _paint_children_tones(parent, first), which walked range(first, childCount()) and called child.setBackground(0, brush) unconditionally on every insertChild/takeChild during bulk sidebar updates and TreeItemRows.order_rows. Each setBackground emitted dataChanged -> MultilinePreviewItemDelegate.forget_range -> full StyledTextRenderer.prepare re-layout.
    • Added ConversationRows.batching_row_tones() (used by ConversationRows.redraw, TreeItemRows.reconcile_keys, and TreeItemRows.order_rows) to coalesce per-parent tone updates into one O(N) pass at batch exit, and tracked SidebarItem._painted_tone so rows whose tone brush did not change skip setBackground altogether.
  2. Cached notification_references on BusMessageHistoryFragment / WireMessage (src/toad/client/fragments.py:518,679)

    • Converted notification_references from @property to @cached_property so scroll notification checks stop re-minting MessageId / SentMessageReference objects on every visible-fragment scan.
  3. Cached steady overlay scroll clip/dirty regions in VirtualRowCanvas.scrollContentsBy (src/toad/qt/virtual_scroll.py:911,1240-1272)

    • Cached (vp_rect, covers, opaque) -> (clear, dirty) so consecutive scroll frames with unchanged sticky_header/jump_bar rectangles skip uncovered() and repeated QRegion allocations/subtractions.

Live FrameProbe Before vs After (#comms, 412x915)

  • Sidebar populate / #comms open:
    • _paint_children_tones: 3.5% -> 0.1% (35x reduction)
    • MultilinePreviewItemDelegate.forget_range: 1.8% -> 0.3% (6x reduction)
    • slow MetaCall/_UiThreadCallProxy stall: 354 ms -> 258 ms (worst_stall_ms 391.4 ms -> 290.7 ms)
  • 5 s #comms scroll fling:
    • WireMessage.notification_references / MessageId.mint: 0.9% -> 0.0%
    • VirtualRowCanvas.scrollContentsBy + uncovered: 2.2% -> 0.7%
    • Frames in 5 s: 462 -> 479 (worst_stall_ms 9.9 ms -> 8.1 ms, Timer/_SimpleTimer 934 ms -> 839 ms)

@trissim
trissim merged commit 3baafd1 into main Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant