Răsfoiți Sursa

perf enhancement

1. add :block/tx-id to check whether block has been updated
2. avoid rendering blocks if they're not updated
Tienson Qin 2 ani în urmă
părinte
comite
33f8a24397

+ 3 - 0
deps/db/src/logseq/db/schema.cljs

@@ -99,6 +99,9 @@
    ;; block's file
    :block/file {:db/valueType :db.type/ref}
 
+   ;; latest tx that affected the block
+   :block/tx-id {}
+
    ;; file
    :file/path {:db/unique :db.unique/identity}
    ;; only store the content of logseq's files

+ 49 - 54
src/main/frontend/components/block.cljs

@@ -2925,19 +2925,7 @@
 
 (defn- block-changed?
   [old-block new-block]
-  (let [ks [:block/uuid :block/content :block/collapsed? :block/link
-            :block/properties :block.temp/bottom? :block.temp/top?]]
-    (not
-     (and (= (select-keys old-block ks)
-             (select-keys new-block ks))
-          (= (:db/id (:block/left old-block))
-             (:db/id (:block/left new-block)))
-          (= (:db/id (:block/parent old-block))
-             (:db/id (:block/parent new-block)))
-          (= (map :db/id (:block/_refs old-block))
-             (map :db/id (:block/_refs new-block)))
-          (= (map :db/id (:block/_parent old-block))
-             (map :db/id (:block/_parent new-block)))))))
+  (not= (:block/tx-id old-block) (:block/tx-id new-block)))
 
 (rum/defcs block-container < rum/reactive
   (rum/local false ::show-block-left-menu?)
@@ -2962,16 +2950,6 @@
                       ::control-show? (atom false)
                       ::navigating-block (atom (:block/uuid block))
                       ::blocks-container-id container-id))))
-   :should-update (fn [old-state new-state]
-                    (let [config-compare-keys [:show-cloze? :hide-children? :own-order-list-type :own-order-list-index]
-                          b1                  (second (:rum/args old-state))
-                          b2                  (second (:rum/args new-state))
-                          result              (or
-                                               (block-changed? b1 b2)
-                                               ;; config changed
-                                               (not= (select-keys (first (:rum/args old-state)) config-compare-keys)
-                                                     (select-keys (first (:rum/args new-state)) config-compare-keys)))]
-                      (boolean result)))
    :will-unmount (fn [state]
                    ;; restore root block's collapsed state
                    (let [[config block] (:rum/args state)
@@ -2987,27 +2965,19 @@
         edit-input-id (str "edit-block-" blocks-container-id "-" (:block/uuid block))
         edit? (state/sub [:editor/editing? edit-input-id])
         opts {:edit? edit?
-              :edit-input-id edit-input-id}
-        ref? (:ref? config)
-        custom-query? (boolean (:custom-query? config))]
+              :edit-input-id edit-input-id}]
     (cond
       unloaded?
       [:div.ls-block.flex-1.flex-col.rounded-sm {:style {:width "100%"}}
-     [:div.flex.flex-row
-      [:div.flex.flex-row.items-center.mr-2.ml-1 {:style {:height 24}}
-       [:span.bullet-container.cursor
-        [:span.bullet]]]
-      [:div.flex.flex-1
-       [:span.opacity-70
-        "Loading..."]]]]
+       [:div.flex.flex-row
+        [:div.flex.flex-row.items-center.mr-2.ml-1 {:style {:height 24}}
+         [:span.bullet-container.cursor
+          [:span.bullet]]]
+        [:div.flex.flex-1
+         [:span.opacity-70
+          "Loading..."]]]]
       :else
-      (if (or ref? custom-query? (:lazy? config))
-       (ui/lazy-visible
-        (fn [] (block-container-inner state repo config block opts))
-        {:debug-id (str "block-container-ref " (:db/id block))
-         :fade-in? false
-         :initial-state edit?})
-       (block-container-inner state repo config block opts)))))
+      (block-container-inner state repo config block opts))))
 
 (defn divide-lists
   [[f & l]]
@@ -3377,31 +3347,56 @@
   [config col]
   (map #(markup-element-cp config %) col))
 
-(defn- block-item
-  [config blocks idx item]
+(rum/defc block-item <
+  {:should-update (fn [old-state new-state]
+                    (let [config-compare-keys [:show-cloze? :hide-children? :own-order-list-type :own-order-list-index]
+                          b1                  (second (:rum/args old-state))
+                          b2                  (second (:rum/args new-state))
+                          result              (or
+                                               (block-changed? b1 b2)
+                                               ;; config changed
+                                               (not= (select-keys (first (:rum/args old-state)) config-compare-keys)
+                                                     (select-keys (first (:rum/args new-state)) config-compare-keys)))]
+                      (boolean result)))}
+  [config item {:keys [top? bottom?]}]
   (let [original-block item
         linked-block (:block/link item)
         item (or linked-block item)
         item (cond-> (dissoc item :block/meta)
                (not (:block-children? config))
-               (assoc :block.temp/top? (zero? idx)
-                      :block.temp/bottom? (= (count blocks) (inc idx))))
-        config (assoc config :block/uuid (:block/uuid item)
-                      :idx idx)
+               (assoc :block.temp/top? top?
+                      :block.temp/bottom? bottom?))
+        config (assoc config :block/uuid (:block/uuid item))
         config' (if linked-block
                   (assoc config :original-block original-block)
-                  config)]
-    (rum/with-key (block-container config' item)
-      (str (:blocks-container-id config')
-           "-"
-           (:block/uuid item)
-           (when linked-block
-             (str "-" (:block/uuid original-block)))))))
+                  config)
+        ref? (:ref? config)
+        custom-query? (boolean (:custom-query? config))
+        lazy? (:lazy? config)
+        cp-f (fn []
+               (rum/with-key (block-container config' item)
+                 (str (:blocks-container-id config')
+                      "-"
+                      (:block/uuid item)
+                      (when linked-block
+                        (str "-" (:block/uuid original-block))))))]
+    (if (or ref? custom-query? lazy?)
+      (ui/lazy-visible cp-f
+                       {:debug-id (str "block-container-ref " (:db/id item))
+                        :fade-in? false})
+      (cp-f))))
 
 (defn- block-list
   [config blocks]
   (for [[idx item] (medley/indexed blocks)]
-    (block-item config blocks idx item)))
+    (let [top? (zero? idx)
+          bottom? (= (count blocks) (inc idx))]
+      (rum/with-key
+        (block-item (assoc config :idx idx) item {:top? top?
+                                                  :bottom? bottom?})
+        (str "blocks-" (:blocks-container-id config)
+             "-"
+             (:block/uuid item))))))
 
 (rum/defcs blocks-container <
   {:init (fn [state] (assoc state ::init-blocks-container-id (atom nil)))}

+ 2 - 4
src/main/frontend/components/page.cljs

@@ -147,9 +147,7 @@
 
         (and
          (not block?)
-             (empty? (:block/_parent block))
-
-             )
+             (empty? (:block/_parent block)))
         (dummy-block page-name)
 
         :else
@@ -164,7 +162,7 @@
               hiccup-config (common-handler/config-with-document-mode hiccup-config)
               blocks (if block? [block] (db/sort-by-left (:block/_parent block) block))
               non-collapsed-blocks-count (count (remove :block/collapsed? (:block/_page (db/entity (:db/id page-e)))))
-              lazy? (> non-collapsed-blocks-count 300)
+              lazy? (> non-collapsed-blocks-count 50)
               hiccup (component-block/->hiccup blocks (assoc hiccup-config :lazy? lazy?) {})]
           [:div
            (page-blocks-inner page-name block hiccup sidebar? whiteboard? block-id)

+ 4 - 4
src/main/frontend/db/react.cljs

@@ -227,10 +227,10 @@
                           (map :e))
         blocks (-> (concat blocks other-blocks) distinct)
         block-entities (keep (fn [block-id]
-                              (let [block-id (if (and (string? block-id) (util/uuid-string? block-id))
-                                               [:block/uuid block-id]
-                                               block-id)]
-                                (db-utils/entity block-id))) blocks)
+                               (let [block-id (if (and (string? block-id) (util/uuid-string? block-id))
+                                                [:block/uuid block-id]
+                                                block-id)]
+                                 (db-utils/entity block-id))) blocks)
         affected-keys (concat
                        (mapcat
                         (fn [block]

+ 9 - 1
src/main/frontend/modules/outliner/pipeline.cljs

@@ -114,7 +114,15 @@
           (react/refresh! repo tx-report'))
 
         (when (and (config/db-based-graph? repo) (not (:skip-persist? tx-meta)))
-          (let [upsert-blocks (outliner-pipeline/build-upsert-blocks blocks deleted-block-uuids (:db-after tx-report'))]
+          (let [upsert-blocks (outliner-pipeline/build-upsert-blocks blocks deleted-block-uuids (:db-after tx-report'))
+                updated-blocks (remove (fn [b] (contains? (set deleted-block-uuids)  (:block/uuid b))) blocks)
+                tx-id (get-in tx-report' [:tempids :db/current-tx])
+                update-tx-ids (map (fn [b]
+                                     (when-let [db-id (:db/id b)]
+                                       {:db/id db-id
+                                        :block/tx-id tx-id})) updated-blocks)]
+            (when (seq update-tx-ids)
+              (db/transact! repo update-tx-ids {:replace? true}))
             (p/let [_transact-result (persist-db/<transact-data repo upsert-blocks deleted-block-uuids)
                     _ipc-result (comment ipc/ipc :db-transact-data repo
                                          (pr-str

+ 3 - 3
src/main/frontend/state.cljs

@@ -140,7 +140,7 @@
 
       :editor/code-block-context             {}
 
-      :db/last-transact-time                 {}
+      :db/last-transact-time                 (atom {})
       ;; whether database is persisted
       :db/persisted?                         {}
       :cursor-range                          nil
@@ -1687,7 +1687,7 @@ Similar to re-frame subscriptions"
 
 (defn set-last-transact-time!
   [repo time]
-  (set-state! [:db/last-transact-time repo] time)
+  (set-state! :db/last-transact-time time :path-in-sub-atom repo)
 
   ;; THINK: new block, indent/outdent, drag && drop, etc.
   (set-editor-last-input-time! repo time))
@@ -1699,7 +1699,7 @@ Similar to re-frame subscriptions"
 (defn db-idle?
   [repo]
   (when repo
-    (when-let [last-time (get-in @state [:db/last-transact-time repo])]
+    (when-let [last-time (get (:db/last-transact-time @state) repo)]
       (let [now (util/time-ms)]
         (>= (- now last-time) 3000)))))
 

+ 12 - 9
src/main/frontend/ui.cljs

@@ -1118,15 +1118,18 @@
 
 (rum/defc lazy-loading-placeholder
   [height]
-  [:div.shadow.rounded-md.p-4.w-full.mx-auto.mb-5.opacity-70 {:style {:height height}}
-   [:div.animate-pulse.flex.space-x-4
-    [:div.flex-1.space-y-3.py-1
-     [:div.h-2.rounded]
-     [:div.space-y-3
-      [:div.grid.grid-cols-3.gap-4
-       [:div.h-2.rounded.col-span-2]
-       [:div.h-2.rounded.col-span-1]]
-      [:div.h-2.rounded]]]]])
+  [:div {:style {:height height}}]
+  ;; [:div.shadow.rounded-md.p-4.w-full.mx-auto.mb-5.opacity-70 {:style {:height height}}
+  ;;  ;; [:div.animate-pulse.flex.space-x-4
+  ;;  ;;  [:div.flex-1.space-y-3.py-1
+  ;;  ;;   [:div.h-2.rounded]
+  ;;  ;;   [:div.space-y-3
+  ;;  ;;    [:div.grid.grid-cols-3.gap-4
+  ;;  ;;     [:div.h-2.rounded.col-span-2]
+  ;;  ;;     [:div.h-2.rounded.col-span-1]]
+  ;;  ;;    [:div.h-2.rounded]]]]
+  ;;  ]
+  )
 
 (rum/defc lazy-visible-inner
   [visible? content-fn ref fade-in?]