Преглед изворни кода

fix: performance degradation for 0.8.0

Tienson Qin пре 3 година
родитељ
комит
d6abc839ec

+ 3 - 3
src/main/frontend/components/journal.cljs

@@ -19,7 +19,7 @@
 
 (rum/defc blocks-cp < rum/reactive db-mixins/query
   {}
-  [repo page _format]
+  [repo page]
   (when-let [page-e (db/pull [:block/name (util/page-name-sanity-lc page)])]
     (page/page-blocks-cp repo page-e {})))
 
@@ -56,9 +56,9 @@
         (gp-util/capitalize-all title)]]
 
       (if today?
-        (blocks-cp repo page format)
+        (blocks-cp repo page)
         (ui/lazy-visible
-         (fn [] (blocks-cp repo page format))
+         (fn [] (blocks-cp repo page))
          {:debug-id (str "journal-blocks " page)}))
 
       {})

+ 12 - 17
src/main/frontend/db/model.cljs

@@ -1172,21 +1172,19 @@
    (get-page-referenced-blocks-full (state/get-current-repo) page options))
   ([repo page options]
    (when repo
-     (when (conn/get-db repo)
+     (when-let [db (conn/get-db repo)]
        (let [page-id (:db/id (db-utils/entity [:block/name (util/safe-page-name-sanity-lc page)]))
              pages (page-alias-set repo page)
              aliases (set/difference pages #{page-id})]
          (->>
-          (react/q repo
-            [:frontend.db.react/page<-blocks-or-block<-blocks page-id]
-            {}
+          (d/q
             '[:find [(pull ?block ?block-attrs) ...]
               :in $ [?ref-page ...] ?block-attrs
               :where
               [?block :block/path-refs ?ref-page]]
+            db
             pages
             (butlast block-attrs))
-          react
           (remove (fn [block] (= page-id (:db/id (:block/page block)))))
           db-utils/group-by-page
           (map (fn [[k blocks]]
@@ -1209,8 +1207,7 @@
          (->>
           (react/q repo
             [:frontend.db.react/page<-blocks-or-block<-blocks page-id]
-            {:use-cache? false
-             :query-fn (fn []
+            {:query-fn (fn []
                          (let [entities (mapcat (fn [id]
                                                   (:block/_path-refs (db-utils/entity id))) pages)
                                blocks (map (fn [e] {:block/parent (:block/parent e)
@@ -1311,8 +1308,6 @@
              (sort-by-left-recursive)
              db-utils/group-by-page)))))
 
-;; TODO: Replace recursive queries with datoms index implementation
-;; see https://github.com/tonsky/datascript/issues/130#issuecomment-169520434
 (defn get-block-referenced-blocks
   ([block-uuid]
    (get-block-referenced-blocks block-uuid {}))
@@ -1322,14 +1317,14 @@
        (let [block (db-utils/entity [:block/uuid block-uuid])
              query-result (->> (react/q repo [:frontend.db.react/page<-blocks-or-block<-blocks
                                               (:db/id block)]
-                                        {:use-cache? false}
-                                        '[:find [(pull ?ref-block ?block-attrs) ...]
-                                          :in $ ?block-uuid ?block-attrs
-                                          :where
-                                          [?block :block/uuid ?block-uuid]
-                                          [?ref-block :block/refs ?block]]
-                                        block-uuid
-                                        block-attrs)
+                                 {}
+                                 '[:find [(pull ?ref-block ?block-attrs) ...]
+                                   :in $ ?block-uuid ?block-attrs
+                                   :where
+                                   [?block :block/uuid ?block-uuid]
+                                   [?ref-block :block/refs ?block]]
+                                 block-uuid
+                                 block-attrs)
                                react
                                (sort-by-left-recursive))]
          (db-utils/group-by-page query-result))))))

+ 14 - 30
src/main/frontend/db/react.cljs

@@ -24,13 +24,9 @@
 ;; get block&children react-query
 (s/def ::block-and-children (s/tuple #(= ::block-and-children %) uuid?))
 
-(s/def ::block-direct-children (s/tuple #(= ::block-direct-children %) uuid?))
 ;; ::journals
 ;; get journal-list react-query
 (s/def ::journals (s/tuple #(= ::journals %)))
-;; ::page->pages
-;; get PAGES referenced by PAGE
-(s/def ::page->pages (s/tuple #(= ::page->pages %) int?))
 ;; ::page<-pages
 ;; get PAGES referencing PAGE
 (s/def ::page<-pages (s/tuple #(= ::page<-pages %) int?))
@@ -40,22 +36,17 @@
   (s/tuple #(= ::page<-blocks-or-block<-blocks %) int?))
 ;; FIXME: this react-query has performance issues
 (s/def ::page-unlinked-refs (s/tuple #(= ::page-unlinked-refs %) int?))
-;; ::block<-block-ids
-;; get BLOCK-IDS referencing BLOCK
-(s/def ::block<-block-ids (s/tuple #(= ::block<-block-ids %) int?))
+
 ;; custom react-query
 (s/def ::custom any?)
 
 (s/def ::react-query-keys (s/or :block ::block
                                 :page-blocks ::page-blocks
                                 :block-and-children ::block-and-children
-                                :block-direct-children ::block-direct-children
                                 :journals ::journals
-                                :page->pages ::page->pages
                                 :page<-pages ::page<-pages
                                 :page<-blocks-or-block<-blocks ::page<-blocks-or-block<-blocks
                                 :page-unlinked-refs ::page-unlinked-refs
-                                :block<-block-ids ::block<-block-ids
                                 :custom ::custom))
 
 (s/def ::affected-keys (s/coll-of ::react-query-keys))
@@ -245,21 +236,12 @@
                                              (:db/id (:block/page block)))
                                     blocks [[::block (:db/id block)]]
                                     others (when page-id
-                                             (let [db-after-parent-uuid (:block/uuid (:block/parent block))
-                                                   db-before-parent-uuid (:block/uuid (:block/parent (d/entity db-before
-                                                                                                               [:block/uuid (:block/uuid block)])))]
-                                               [[::page-blocks page-id]
-                                                [::page->pages page-id]
-                                                [::block-direct-children db-after-parent-uuid]
-                                                (when (and db-before-parent-uuid
-                                                           (not= db-before-parent-uuid db-after-parent-uuid))
-                                                  [::block-direct-children db-before-parent-uuid])]))]
+                                             [[::page-blocks page-id]])]
                                 (concat blocks others)))))
                         blocks)
 
                        (when-let [current-page-id (:db/id (get-current-page))]
-                         [[::page->pages current-page-id]
-                          [::page<-pages current-page-id]])
+                         [[::page<-pages current-page-id]])
 
                        (map (fn [ref]
                               (let [entity (db-utils/entity ref)]
@@ -336,15 +318,17 @@
                        kv?))
               (let [{:keys [query query-fn]} cache
                     query-or-refs? (state/edit-in-query-or-refs-component)]
-                (when (or query query-fn)
-                  (try
-                    (let [f #(execute-query! repo-url db k tx cache {:skip-query-time-check? query-or-refs?})]
-                      ;; Detects whether user is editing in a custom query, if so, execute the query immediately
-                      (if (or query-or-refs? (not custom?))
-                        (f)
-                        (async/put! (state/get-reactive-custom-queries-chan) [f query])))
-                    (catch js/Error e
-                      (js/console.error e))))))))))))
+                (util/profile
+                 (str "refresh! " (rest k))
+                 (when (or query query-fn)
+                   (try
+                     (let [f #(execute-query! repo-url db k tx cache {:skip-query-time-check? query-or-refs?})]
+                       ;; Detects whether user is editing in a custom query, if so, execute the query immediately
+                       (if (or query-or-refs? (not custom?))
+                         (f)
+                         (async/put! (state/get-reactive-custom-queries-chan) [f query])))
+                     (catch js/Error e
+                       (js/console.error e)))))))))))))
 
 (defn set-key-value
   [repo-url key value]

+ 14 - 23
src/main/frontend/modules/outliner/tree.cljs

@@ -66,8 +66,8 @@
                  root-block (assoc root-block :block/children result)]
              [root-block])))))))
 
-(defn- tree [flat-nodes root-id]
-  (let [children (group-by :block/parent flat-nodes)
+(defn- tree [parent->children root]
+  (let [root-id (:db/id root)
         nodes (fn nodes [parent-id level]
                 (mapv (fn [b]
                         (let [b' (assoc b :block/level (inc level))
@@ -75,8 +75,12 @@
                           (if (seq children)
                             (assoc b' :block/children children)
                             b')))
-                      (get children {:db/id parent-id})))]
-    (nodes root-id 1)))
+                      (get parent->children {:db/id parent-id})))
+        children (nodes root-id 1)
+        root' (assoc root :block/level 1)]
+    (if (seq children)
+      (assoc root' :block/children children)
+      root')))
 
 (defn non-consecutive-blocks->vec-tree
   "`blocks` need to be in the same page."
@@ -85,25 +89,12 @@
                              :block/uuid (:block/uuid e)
                              :block/parent {:db/id (:db/id (:block/parent e))}
                              :block/page {:db/id (:db/id (:block/page e))}}) blocks)
-        blocks (model/sort-page-random-blocks blocks)
-        id->parent (zipmap (map :db/id blocks)
-                           (map (comp :db/id :block/parent) blocks))
-        top-level-ids (set (remove #(id->parent (id->parent %)) (map :db/id blocks)))
-        ;; Separate blocks into parent and children groups [parent-children, parent-children]
-        blocks' (loop [blocks blocks
-                       result []]
-                  (if-let [block (first blocks)]
-                    (if (top-level-ids (:db/id block))
-                      (let [block' (assoc block :block/level 1)]
-                        (recur (rest blocks) (conj result [block'])))
-                      (recur (rest blocks) (conj (vec (butlast result))
-                                                 (conj (last result) block))))
-                    result))]
-    (map (fn [[parent & children]]
-           (if (seq children)
-             (assoc parent :block/children
-                    (tree children (:db/id parent)))
-             parent)) blocks')))
+        parent->children (group-by :block/parent blocks)
+        id->blocks (zipmap (map :db/id blocks) blocks)
+        top-level-blocks (filter #(nil?
+                                   (id->blocks
+                                    (:db/id (:block/parent (id->blocks (:db/id %)))))) blocks)]
+    (map #(tree parent->children %) top-level-blocks)))
 
 (defn- sort-blocks-aux
   [parents parent-groups]