From 0e61398d67163e696b7e0ab2f93a4778ff36d0ab Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 9 Apr 2025 10:27:24 +0200 Subject: [PATCH] :zap: Optimize handler->point path segment helper fn More or les x2 speed improvement and reduced the generation of objects garbage. --- common/src/app/common/types/path/segment.cljc | 20 +++++----- .../common_tests/types/path_data_test.cljc | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/common/src/app/common/types/path/segment.cljc b/common/src/app/common/types/path/segment.cljc index b49432e5b8..fe9c746734 100644 --- a/common/src/app/common/types/path/segment.cljc +++ b/common/src/app/common/types/path/segment.cljc @@ -102,21 +102,19 @@ (->> (d/enumerate content) (filterv (fn [[_ segment]] (= (helpers/segment->point segment) point))))) -;; FIXME: candidate to be optimized with native data type operation (defn handler->point [content index prefix] (when (and (some? index) (some? prefix)) - (when (and (<= 0 index) - (< index (count content))) - (let [segment (nth content index) - params (get segment :params)] - (if (= :curve-to (:command segment)) - (let [[cx cy] (helpers/prefix->coords prefix)] - (gpt/point (get params cx) - (get params cy))) - (gpt/point (get params :x) - (get params :y))))))) + (impl/-lookup content index + (fn [command c1x c1y c2x c2y x y] + (let [prefix (if (= :curve-to command) + prefix + nil)] + (case prefix + :c1 (gpt/point c1x c1y) + :c2 (gpt/point c2x c2y) + (gpt/point x y))))))) ;; FIXME: revisit this function (defn handler->node diff --git a/common/test/common_tests/types/path_data_test.cljc b/common/test/common_tests/types/path_data_test.cljc index 92cdb4f9ee..e3ae2b2be2 100644 --- a/common/test/common_tests/types/path_data_test.cljc +++ b/common/test/common_tests/types/path_data_test.cljc @@ -303,3 +303,40 @@ :index 6}]] (t/is (= result expect)))) + +(defn handler->point + "A legacy impl of handler point, used as reference for test" + [content index prefix] + (when (and (some? index) + (some? prefix)) + (when (and (<= 0 index) + (< index (count content))) + (let [segment (nth content index) + params (get segment :params)] + (if (= :curve-to (:command segment)) + (let [[cx cy] (path.helpers/prefix->coords prefix)] + (gpt/point (get params cx) + (get params cy))) + (gpt/point (get params :x) + (get params :y))))))) + +(t/deftest handler-to-point + (let [content (path/content sample-content-2) + result1 (handler->point content 3 :c1) + result2 (handler->point content 1 :c1) + result3 (handler->point content 0 :c1) + + expect1 (gpt/point 3.0 7.0) + expect2 (gpt/point 439.0 802.0) + expect3 (gpt/point 480.0 839.0) + + result4 (path.segment/handler->point content 3 :c1) + result5 (path.segment/handler->point content 1 :c1) + result6 (path.segment/handler->point content 0 :c1)] + + (t/is (= result1 expect1)) + (t/is (= result2 expect2)) + (t/is (= result3 expect3)) + (t/is (= result4 expect1)) + (t/is (= result5 expect2)) + (t/is (= result6 expect3))))