Просмотр исходного кода

perf: Reduce usage of remove-nils since it's too slow (#8323)

* reduce usage of remove-nils since it's too slow

Co-authored-by: Tienson Qin <[email protected]>
Andelf 2 лет назад
Родитель
Сommit
193677b46f

+ 2 - 0
deps/graph-parser/.carve/ignore

@@ -38,5 +38,7 @@ logseq.graph-parser/get-blocks-to-delete
 logseq.graph-parser.property/colons-org
 ;; API
 logseq.graph-parser.util.db/resolve-input
+;; TODO: use fast-remove-nils instead
+logseq.graph-parser.util/remove-nils
 ;; API
 logseq.graph-parser.text/get-file-basename

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser.cljs

@@ -122,7 +122,7 @@ Options available:
                          new?
                          ;; TODO: use file system timestamp?
                          (assoc :file/created-at (date-time-util/time-ms)))])
-        tx' (gp-util/remove-nils tx)
+        tx' (gp-util/fast-remove-nils tx)
         result (if skip-db-transact?
                  tx'
                  (d/transact! conn tx' (select-keys options [:new-graph? :from-disk?])))]

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -376,7 +376,7 @@
   [blocks]
   (map (fn [block]
          (if (map? block)
-           (block-keywordize (gp-util/remove-nils block))
+           (block-keywordize (gp-util/remove-nils-non-nested block))
            block))
        blocks))
 

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser/extract.cljc

@@ -109,7 +109,7 @@
         invalid-properties (set (->> (map (comp name first) *invalid-properties)
                                      (concat invalid-properties)))
         page-m (->
-                (gp-util/remove-nils
+                (gp-util/remove-nils-non-nested
                  (assoc
                   (gp-block/page-name->map page false db true date-formatter
                                            :from-page from-page)

+ 12 - 1
deps/graph-parser/src/logseq/graph_parser/util.cljs

@@ -28,7 +28,8 @@
   (.normalize s "NFC"))
 
 (defn remove-nils
-  "remove pairs of key-value that has nil value from a (possibly nested) map."
+  "remove pairs of key-value that has nil value from a (possibly nested) map or
+  coll of maps."
   [nm]
   (walk/postwalk
    (fn [el]
@@ -37,6 +38,16 @@
        el))
    nm))
 
+(defn remove-nils-non-nested
+  "remove pairs of key-value that has nil value from a map (nested not supported)."
+  [nm]
+  (into {} (remove (comp nil? second)) nm))
+
+(defn fast-remove-nils
+  "remove pairs of key-value that has nil value from a coll of maps."
+  [nm]
+  (keep (fn [m] (if (map? m) (remove-nils-non-nested m) m)) nm))
+
 (defn split-first [pattern s]
   (when-let [first-index (string/index-of s pattern)]
     [(subs s 0 first-index)

+ 1 - 2
src/main/frontend/db/utils.cljs

@@ -93,8 +93,7 @@
    (transact! repo-url tx-data nil))
   ([repo-url tx-data tx-meta]
    (when-not config/publishing?
-     (let [tx-data (->> (gp-util/remove-nils tx-data)
-                        (remove nil?))]
+     (let [tx-data (gp-util/fast-remove-nils tx-data)]
        (when (seq tx-data)
          (when-let [conn (conn/get-db repo-url false)]
            (if tx-meta

+ 1 - 2
src/main/frontend/handler/repo.cljs

@@ -24,7 +24,6 @@
             [promesa.core :as p]
             [shadow.resource :as rc]
             [frontend.db.persist :as db-persist]
-            [logseq.graph-parser.util :as gp-util]
             [logseq.graph-parser :as graph-parser]
             [logseq.graph-parser.config :as gp-config]
             [electron.ipc :as ipc]
@@ -302,7 +301,7 @@
                              [])
               add-or-modify-files (some->>
                                    (concat modify-files add-files)
-                                   (gp-util/remove-nils))
+                                   (remove nil?))
               options {:delete-files (concat delete-files delete-pages)
                        :delete-blocks delete-blocks
                        :re-render? true}]