Skip to content

Commit

Permalink
perf(right-sidebar): fix memory+time leak with proper GC of sorted-map
Browse files Browse the repository at this point in the history
…#1239 (#1242)

Co-authored-by: jeff <[email protected]>
  • Loading branch information
datokrat and tangjeff0 committed May 28, 2021
1 parent 7b922a5 commit 32d66f5
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/cljs/athens/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,18 @@
(fn [{:keys [db]} [_ uid is-graph?]]
(let [block (d/pull @db/dsdb '[:node/title :block/string] [:block/uid uid])
new-item (merge block {:open true :index -1 :is-graph? is-graph?})
new-items (assoc (:right-sidebar/items db) uid new-item)
;; Avoid a memory leak by forgetting the comparison function
;; that is stored in the sorted map
;; `(assoc (:right-sidebar/items db) uid new-item)`
new-items (into {}
(assoc (:right-sidebar/items db) uid new-item))
inc-items (reduce-kv (fn [m k v] (assoc m k (update v :index inc)))
{}
new-items)
sorted-items (into (sorted-map-by (fn [k1 k2]
(compare
[(get-in new-items [k1 :index]) k2]
[(get-in new-items [k2 :index]) k1]))) inc-items)]
[(get-in inc-items [k1 :index]) k2]
[(get-in inc-items [k2 :index]) k1]))) inc-items)]
{:db (assoc db :right-sidebar/items sorted-items)
:dispatch-n [(when (not (:right-sidebar/open db)) [:right-sidebar/toggle])
[:right-sidebar/scroll-top]]})))
Expand Down

0 comments on commit 32d66f5

Please sign in to comment.