Revert some memoizes on page/helpers.

And improves base performance of get-children and
remove duplicated code. Also optimize the use
of get-children on react components with corresponding
use-memo hook.
This commit is contained in:
Andrey Antukh 2022-01-18 23:12:41 +01:00 committed by Andrés Moya
parent 1b3b3b0ee6
commit 072e4a4f98
5 changed files with 111 additions and 107 deletions

View file

@ -6,7 +6,9 @@
(ns app.common.geom.align
(:require
[app.common.data :as d]
[app.common.geom.shapes :as gsh]
[app.common.pages.helpers :refer [get-children]]
[clojure.spec.alpha :as s]))
;; --- Alignment
@ -15,23 +17,13 @@
(declare calc-align-pos)
;; TODO: revisit on how to reuse code and dont have this function
;; duplicated because the implementation right now differs from the
;; original function.
;; Duplicated from pages/helpers to remove cyclic dependencies
(defn- get-children [id objects]
(let [shapes (vec (get-in objects [id :shapes]))]
(if shapes
(into shapes (mapcat #(get-children % objects)) shapes)
[])))
(defn- recursive-move
"Move the shape and all its recursive children."
[shape dpoint objects]
(let [children-ids (get-children (:id shape) objects)
children (map #(get objects %) children-ids)]
(map #(gsh/move % dpoint) (cons shape children))))
(->> (get-children (:id shape) objects)
(map (d/getf objects))
(cons shape)
(map #(gsh/move % dpoint))))
(defn align-to-rect
"Move the shape so that it is aligned with the given rectangle

View file

@ -412,7 +412,7 @@
{:rotation angle
:displacement displacement}))
(defn merge-modifiers*
(defn merge-modifiers
[objects modifiers]
(let [set-modifier
@ -422,8 +422,6 @@
(->> modifiers
(reduce set-modifier objects))))
(def merge-modifiers (memoize merge-modifiers*))
(defn modifiers->transform
([modifiers]
(modifiers->transform nil modifiers))

View file

@ -99,37 +99,10 @@
[component]
(get-in component [:objects (:id component)]))
;; Implemented with transient for performance
(defn get-children*
"Retrieve all children ids recursively for a given object. The
children's order will be breadth first."
[id objects]
(loop [result (transient [])
pending (transient [])
next id]
(let [children (get-in objects [next :shapes] [])
[result pending]
;; Iterate through children and add them to the result
;; also add them in pending to check for their children
(loop [result result
pending pending
current (first children)
children (rest children)]
(if current
(recur (conj! result current)
(conj! pending current)
(first children)
(rest children))
[result pending]))
;; If we have still pending, advance the iterator
length (count pending)]
(if (pos? length)
(let [next (get pending (dec length))]
(recur result (pop! pending) next))
(persistent! result)))))
(def get-children (memoize get-children*))
(defn get-children [id objects]
(if-let [shapes (-> (get objects id) :shapes (some-> vec))]
(into shapes (mapcat #(get-children % objects)) shapes)
[]))
(defn get-children-objects
"Retrieve all children objects recursively for a given object"
@ -175,7 +148,7 @@
shape
(get objects (:frame-id shape))))
(defn clean-loops*
(defn clean-loops
"Clean a list of ids from circular references."
[objects ids]
@ -192,8 +165,6 @@
(reduce add-element (d/ordered-set) ids)))
(def clean-loops (memoize clean-loops*))
(defn calculate-invalid-targets
[shape-id objects]
(let [result #{shape-id}