From 161a55e16639b4c9914ba8115d1636fb22cd2ab4 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 17 Jan 2024 23:40:15 +0100 Subject: [PATCH] :zap: Optimize general case of without-nils Performance gains up to x6 --- common/src/app/common/data.cljc | 11 +++++++++-- common/src/app/common/types/shape.cljc | 7 ++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/common/src/app/common/data.cljc b/common/src/app/common/data.cljc index d814b14392..c01cdb3d65 100644 --- a/common/src/app/common/data.cljc +++ b/common/src/app/common/data.cljc @@ -216,12 +216,19 @@ [coll] (into [] (remove nil?) coll)) + (defn without-nils "Given a map, return a map removing key-value pairs when value is `nil`." - ([] (remove (comp nil? val))) + ([] + (remove (comp nil? val))) ([data] - (into {} (without-nils) data))) + (reduce-kv (fn [data k v] + (if (nil? v) + (dissoc data k) + data)) + data + data))) (defn without-qualified ([] diff --git a/common/src/app/common/types/shape.cljc b/common/src/app/common/types/shape.cljc index 28289219d4..2397d58458 100644 --- a/common/src/app/common/types/shape.cljc +++ b/common/src/app/common/types/shape.cljc @@ -491,7 +491,12 @@ the shape. The props must have :x :y :width :height." [{:keys [type] :as props}] (let [shape (make-minimal-shape type) - shape (merge shape (d/without-nils props)) + + ;; The props can be custom records that does not + ;; work properly with without-nils, so we first make + ;; it plain map for proceed + props (d/without-nils (into {} props)) + shape (merge shape (d/without-nils (into {} props))) shape (case (:type shape) (:bool :path) (setup-path shape) :image (-> shape setup-rect setup-image)