Performance improvements

This commit is contained in:
alonso.torres 2023-02-13 17:33:41 +01:00
parent 94e87f8a7d
commit 2030f987db
3 changed files with 77 additions and 52 deletions

View file

@ -46,7 +46,8 @@
:expr (or (nil? ids) (set? ids)) :expr (or (nil? ids) (set? ids))
:hint (dm/str "tree sequence from not set: " ids)) :hint (dm/str "tree sequence from not set: " ids))
(letfn [(get-tree-root ;; Finds the tree root for the current id (let [get-tree-root
(fn ;; Finds the tree root for the current id
[id] [id]
(loop [current id (loop [current id
@ -59,18 +60,21 @@
;; Frame found, but not layout we return the last layout found (or the id) ;; Frame found, but not layout we return the last layout found (or the id)
(and (= :frame (:type parent)) (and (= :frame (:type parent))
(not (ctl/layout? parent))) (not (ctl/any-layout? parent)))
result result
;; Layout found. We continue upward but we mark this layout ;; Layout found. We continue upward but we mark this layout
(ctl/layout? parent) (ctl/any-layout? parent)
(recur (:id parent) (:id parent)) (recur (:id parent) (:id parent))
;; If group or boolean or other type of group we continue with the last result ;; If group or boolean or other type of group we continue with the last result
:else :else
(recur (:id parent) result))))) (recur (:id parent) result)))))
(calculate-common-roots ;; Given some roots retrieves the minimum number of tree roots is-child? #(cph/is-child? objects %1 %2)
calculate-common-roots
(fn ;; Given some roots retrieves the minimum number of tree roots
[result id] [result id]
(if (= id uuid/zero) (if (= id uuid/zero)
result result
@ -78,11 +82,12 @@
;; Remove the children from the current root ;; Remove the children from the current root
result result
(into #{} (remove #(cph/is-child? objects root %)) result) (if (cph/has-children? objects root)
(into #{} (remove #(is-child? root %)) result)
contains-parent? result)
(some #(cph/is-child? objects % root) result)]
root-parents (cph/get-parent-ids objects root)
contains-parent? (some #(contains? result %) root-parents)]
(cond-> result (cond-> result
(not contains-parent?) (not contains-parent?)
(conj root)))))] (conj root)))))]

View file

@ -76,6 +76,12 @@
(and (not (frame-shape? shape)) (and (not (frame-shape? shape))
(= (:frame-id shape) uuid/zero))) (= (:frame-id shape) uuid/zero)))
(defn has-children?
([objects id]
(has-children? (get objects id)))
([shape]
(d/not-empty? (:shapes shape))))
(defn get-children-ids (defn get-children-ids
[objects id] [objects id]
(letfn [(get-children-ids-rec (letfn [(get-children-ids-rec
@ -487,8 +493,17 @@
(defn is-child? (defn is-child?
[objects parent-id candidate-child-id] [objects parent-id candidate-child-id]
(let [parents (get-parent-ids objects candidate-child-id)] (loop [cur-id candidate-child-id]
(some? (d/seek #(= % parent-id) parents)))) (let [cur-parent-id (dm/get-in objects [cur-id :parent-id])]
(cond
(= parent-id cur-parent-id)
true
(or (= cur-parent-id uuid/zero) (nil? cur-parent-id))
false
:else
(recur cur-parent-id)))))
(defn reduce-objects (defn reduce-objects
([objects reducer-fn init-val] ([objects reducer-fn init-val]

View file

@ -51,16 +51,19 @@
(defn apply-modifiers-to-selected (defn apply-modifiers-to-selected
[selected objects text-modifiers modifiers] [selected objects text-modifiers modifiers]
(into [] (reduce
(comp (fn [objects id]
(keep (d/getf objects)) (update
(map (fn [{:keys [id] :as shape}] objects id
(fn [shape]
(cond-> shape (cond-> shape
(and (cph/text-shape? shape) (contains? text-modifiers id)) (and (cph/text-shape? shape) (contains? text-modifiers id))
(dwm/apply-text-modifier (get text-modifiers id)) (dwm/apply-text-modifier (get text-modifiers id))
(contains? modifiers id) (contains? modifiers id)
(gsh/transform-shape (dm/get-in modifiers [id :modifiers])))))) (gsh/transform-shape (dm/get-in modifiers [id :modifiers]))))))
objects
selected)) selected))
(mf/defc viewport (mf/defc viewport
@ -97,8 +100,11 @@
modifiers (mf/deref refs/workspace-modifiers) modifiers (mf/deref refs/workspace-modifiers)
text-modifiers (mf/deref refs/workspace-text-modifier) text-modifiers (mf/deref refs/workspace-text-modifier)
objects-modified (mf/with-memo [base-objects modifiers] objects-modified (mf/with-memo
(gsh/apply-objects-modifiers base-objects modifiers selected)) [base-objects text-modifiers modifiers]
(apply-modifiers-to-selected selected base-objects text-modifiers modifiers))
selected-shapes (->> selected (keep (d/getf objects-modified)))
background (get options :background clr/canvas) background (get options :background clr/canvas)
@ -138,7 +144,6 @@
drawing-tool (:tool drawing) drawing-tool (:tool drawing)
drawing-obj (:object drawing) drawing-obj (:object drawing)
selected-shapes (apply-modifiers-to-selected selected base-objects text-modifiers modifiers)
selected-frames (into #{} (map :frame-id) selected-shapes) selected-frames (into #{} (map :frame-id) selected-shapes)