mirror of
https://github.com/penpot/penpot.git
synced 2025-05-28 21:46:12 +02:00
⚡ Implement get-handlers in term of internal reduce
That has an average performance improvement of 64% over original impl and reduction of generation of object garbage
This commit is contained in:
parent
7196be2a23
commit
1c77126fe6
4 changed files with 49 additions and 20 deletions
|
@ -34,24 +34,24 @@
|
||||||
(gpt/point (get params cx)
|
(gpt/point (get params cx)
|
||||||
(get params cy)))))
|
(get params cy)))))
|
||||||
|
|
||||||
;; FIXME: rename segments->handlers
|
(defn get-handlers
|
||||||
(defn content->handlers
|
|
||||||
"Retrieve a map where for every point will retrieve a list of
|
"Retrieve a map where for every point will retrieve a list of
|
||||||
the handlers that are associated with that point.
|
the handlers that are associated with that point.
|
||||||
point -> [[index, prefix]]"
|
point -> [[index, prefix]]"
|
||||||
[content]
|
[content]
|
||||||
(->> (d/with-prev content)
|
(let [prev-point* (volatile! nil)
|
||||||
(d/enumerate)
|
vec-conj (fnil conj [])]
|
||||||
(mapcat (fn [[index [cur-segment pre-segment]]]
|
(impl/-reduce content
|
||||||
(if (and pre-segment (= :curve-to (:command cur-segment)))
|
(fn [result index type _ _ _ _ x y]
|
||||||
(let [cur-pos (helpers/segment->point cur-segment)
|
(let [curr-point (gpt/point x y)
|
||||||
pre-pos (helpers/segment->point pre-segment)]
|
prev-point (deref prev-point*)]
|
||||||
(-> [[pre-pos [index :c1]]
|
(vreset! prev-point* curr-point)
|
||||||
[cur-pos [index :c2]]]))
|
(if (and prev-point (= :curve-to type))
|
||||||
[])))
|
(-> result
|
||||||
|
(update prev-point vec-conj [index :c1])
|
||||||
(group-by first)
|
(update curr-point vec-conj [index :c2]))
|
||||||
(d/mapm #(mapv second %2))))
|
result)))
|
||||||
|
{})))
|
||||||
|
|
||||||
(defn point-indices
|
(defn point-indices
|
||||||
[content point]
|
[content point]
|
||||||
|
@ -81,7 +81,7 @@
|
||||||
(helpers/segment->point (nth content index))
|
(helpers/segment->point (nth content index))
|
||||||
(helpers/segment->point (nth content (dec index))))
|
(helpers/segment->point (nth content (dec index))))
|
||||||
|
|
||||||
point->handlers (content->handlers content)
|
point->handlers (get-handlers content)
|
||||||
|
|
||||||
handlers (->> point
|
handlers (->> point
|
||||||
(point->handlers)
|
(point->handlers)
|
||||||
|
@ -350,7 +350,7 @@
|
||||||
(defn make-corner-point
|
(defn make-corner-point
|
||||||
"Changes the content to make a point a 'corner'"
|
"Changes the content to make a point a 'corner'"
|
||||||
[content point]
|
[content point]
|
||||||
(let [handlers (-> (content->handlers content)
|
(let [handlers (-> (get-handlers content)
|
||||||
(get point))
|
(get point))
|
||||||
change-content
|
change-content
|
||||||
(fn [content [index prefix]]
|
(fn [content [index prefix]]
|
||||||
|
@ -395,7 +395,7 @@
|
||||||
;; FIXME: optimize
|
;; FIXME: optimize
|
||||||
(defn is-curve?
|
(defn is-curve?
|
||||||
[content point]
|
[content point]
|
||||||
(let [handlers (-> (content->handlers content)
|
(let [handlers (-> (get-handlers content)
|
||||||
(get point))
|
(get point))
|
||||||
handler-points (map #(get-handler-point content (first %) (second %)) handlers)]
|
handler-points (map #(get-handler-point content (first %) (second %)) handlers)]
|
||||||
(some #(not= point %) handler-points)))
|
(some #(not= point %) handler-points)))
|
||||||
|
|
|
@ -340,3 +340,31 @@
|
||||||
(t/is (= result4 expect1))
|
(t/is (= result4 expect1))
|
||||||
(t/is (= result5 expect2))
|
(t/is (= result5 expect2))
|
||||||
(t/is (= result6 expect3))))
|
(t/is (= result6 expect3))))
|
||||||
|
|
||||||
|
|
||||||
|
(defn get-handlers
|
||||||
|
"Retrieve a map where for every point will retrieve a list of
|
||||||
|
the handlers that are associated with that point.
|
||||||
|
point -> [[index, prefix]].
|
||||||
|
|
||||||
|
Legacy impl"
|
||||||
|
[content]
|
||||||
|
(->> (d/with-prev content)
|
||||||
|
(d/enumerate)
|
||||||
|
(mapcat (fn [[index [cur-segment pre-segment]]]
|
||||||
|
(if (and pre-segment (= :curve-to (:command cur-segment)))
|
||||||
|
(let [cur-pos (path.helpers/segment->point cur-segment)
|
||||||
|
pre-pos (path.helpers/segment->point pre-segment)]
|
||||||
|
(-> [[pre-pos [index :c1]]
|
||||||
|
[cur-pos [index :c2]]]))
|
||||||
|
[])))
|
||||||
|
|
||||||
|
(group-by first)
|
||||||
|
(d/mapm #(mapv second %2))))
|
||||||
|
|
||||||
|
(t/deftest content-to-handlers
|
||||||
|
(let [content (path/content sample-content-large)
|
||||||
|
result1 (get-handlers content)
|
||||||
|
result2 (path.segment/get-handlers content)]
|
||||||
|
|
||||||
|
(t/is (= result1 result2))))
|
||||||
|
|
|
@ -129,7 +129,7 @@
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
(watch [_ state stream]
|
(watch [_ state stream]
|
||||||
(let [content (st/get-path state :content)
|
(let [content (st/get-path state :content)
|
||||||
handlers (-> (path.segment/content->handlers content)
|
handlers (-> (path.segment/get-handlers content)
|
||||||
(get position))
|
(get position))
|
||||||
|
|
||||||
[idx prefix] (when (= (count handlers) 1)
|
[idx prefix] (when (= (count handlers) 1)
|
||||||
|
|
|
@ -300,11 +300,12 @@
|
||||||
(mf/with-memo [content-points]
|
(mf/with-memo [content-points]
|
||||||
(into #{} content-points))
|
(into #{} content-points))
|
||||||
|
|
||||||
last-p (->> content last path.helpers/segment->point)
|
last-p
|
||||||
|
(->> content last path.helpers/segment->point)
|
||||||
|
|
||||||
handlers
|
handlers
|
||||||
(mf/with-memo [content]
|
(mf/with-memo [content]
|
||||||
(path.segment/content->handlers content))
|
(path.segment/get-handlers content))
|
||||||
|
|
||||||
is-path-start
|
is-path-start
|
||||||
(not (some? last-point))
|
(not (some? last-point))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue