Skip to content

Commit

Permalink
list-view: avoid more work when keys don't change (#58)
Browse files Browse the repository at this point in the history
* list-view: avoid more work when keys don't change

According to existing discussion:
- The removal loop does not trigger the inner conditional when the keys
  haven't changed anyway, because (member old-k new-keys) should always
  be true.
- The call to change-children is redundant when the keys haven't changed
  because the list of children should also be the same.

Thus we perform some work (looping, conditionals, and importantly a call
to change-children) that is unnecessary: for performance, skip it.

Close #57

Best-viewed-with: --ignore-space-change

* list: avoid computing keys twice

---------

Co-authored-by: Bogdan Popa <bogdan@defn.io>
  • Loading branch information
benknoble and Bogdanp authored Jan 8, 2025
1 parent 4d61012 commit fd37922
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions gui-easy-lib/gui/easy/private/view/list.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@
[stretchable-height h-s?]))
(define keys-to-children
(get-keys-to-children the-panel))
(define entries (peek @entries))
(define keys (map key-proc entries))
(send the-panel set-context 'current-keys keys)
(begin0 the-panel
(with-container-sequence the-panel
(for ([e (in-list (peek @entries))])
(define k (key-proc e))
(for ([e (in-list entries)]
[k (in-list keys)])
(define v (make-view k (make-keyed-obs k e)))
(define w (send v create the-panel))
(define w (send v create the-panel)) ;; noqa
(add-child-handlers! the-panel v)
(add-child the-panel v w)
(hash-set! keys-to-children k v)))))
Expand All @@ -122,8 +125,10 @@
(add-child-handlers! v child-v)
(add-child v child-v child-w)
(hash-set! keys-to-children k child-v)))))
(for ([(old-k old-v) (in-hash keys-to-children)])
(unless (member old-k new-keys)
(unless (equal? new-keys (send v get-context 'current-keys null))
(send v set-context 'current-keys new-keys)
(for ([(old-k old-v) (in-hash keys-to-children)]
#:unless (member old-k new-keys))
(define old-w (get-child v old-v))
(define focused? (send old-w has-focus?))
(send old-v destroy old-w)
Expand All @@ -135,11 +140,11 @@
(define children (send v get-children))
(cond
[(null? children) (send v focus)]
[else (send (last children) focus)]))))
(send v change-children
(λ (_)
(for/list ([k (in-list new-keys)])
(get-child v (hash-ref keys-to-children k))))))]
[else (send (last children) focus)])))
(send v change-children
(λ (_)
(for/list ([k (in-list new-keys)])
(get-child v (hash-ref keys-to-children k)))))))]
[@alignment
(send/apply v set-alignment val)]
[@enabled?
Expand Down

0 comments on commit fd37922

Please sign in to comment.