Skip to content

perf: use deque for lazy widget reveal queue#6398

Closed
giulio-leone wants to merge 1 commit intoTextualize:mainfrom
giulio-leone:fix/lazy-widget-deque
Closed

perf: use deque for lazy widget reveal queue#6398
giulio-leone wants to merge 1 commit intoTextualize:mainfrom
giulio-leone:fix/lazy-widget-deque

Conversation

@giulio-leone
Copy link

Summary

Replace list.pop(0) with collections.deque.popleft() in the lazy-widget reveal timer callback.

Problem

Lazy._reveal() drains the pending-widgets list one element at a time via a recurring timer. Each list.pop(0) call is O(n) because CPython must shift all remaining elements left.

Solution

Switch _widgets from list[Widget] to collections.deque[Widget] and use .popleft() for O(1) front removal.

Compatibility

  • .append(), .copy(), .clear(), truthiness, and iteration are identical on deque
  • No API surface changes; this is an internal-only optimisation
  • Zero behaviour change

Complexity

Operation Before After
pop front O(n) O(1)

Lazy widgets are mounted one-at-a-time via a timer callback that
calls .pop(0) on each tick.  Each .pop(0) is O(n); switching to
collections.deque with .popleft() gives O(1) front removal.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

2 participants