Skip to content

Commit

Permalink
perf: reduce :block/properties calls on db graphs
Browse files Browse the repository at this point in the history
`:block.temp/property-keys` was added to get all the properties
for given entity.
  • Loading branch information
tiensonqin committed Jan 15, 2025
1 parent 029a3e0 commit 1d9fbc8
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 77 deletions.
9 changes: 9 additions & 0 deletions deps/db/src/logseq/db/frontend/entity_plus.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@
(lookup-entity property :logseq.property/scalar-default-value nil)
(lookup-entity property :logseq.property/default-value nil)))))))))

;; (defonce *id->k-frequency (atom {}))
(defn lookup-kv-then-entity
([e k] (lookup-kv-then-entity e k nil))
([^Entity e k default-value]
(try
(when k
;; (swap! *id->k-frequency update-in [(.-eid e) k] inc)
(let [db (.-db e)]
(case k
:block/raw-title
Expand All @@ -139,6 +141,13 @@
(into {})))
(lookup-entity e :block/properties nil))

:block.temp/property-keys
(if (db-based-graph? db)
(->> (map :a (d/datoms db :eavt (.-eid e)))
distinct
(filter db-property/property?))
(keys (lookup-entity e :block/properties nil)))

;; cache :block/title
:block/title
(or (when-not (get (.-kv e) :block.temp/search?)
Expand Down
16 changes: 8 additions & 8 deletions deps/db/src/logseq/db/frontend/property/util.cljs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns logseq.db.frontend.property.util
"Property related util fns. Fns used in both DB and file graphs should go here"
(:require [logseq.db.frontend.property :as db-property]
[datascript.core :as d]
[logseq.db.sqlite.util :as sqlite-util]
[logseq.db.frontend.property.type :as db-property-type]))
(:require [datascript.core :as d]
[logseq.db.frontend.property :as db-property]
[logseq.db.frontend.property.type :as db-property-type]
[logseq.db.sqlite.util :as sqlite-util]))

(defn get-pid
"Get a built-in property's id (keyword name for file graph and db-ident for db
Expand All @@ -21,18 +21,18 @@

(defn lookup
"Get the property value by a built-in property's db-ident from coll. For file and db graphs"
[repo coll db-ident]
[repo block db-ident]
(if (sqlite-util/db-based-graph? repo)
(let [val (get coll db-ident)]
(let [val (get block db-ident)]
(if (built-in-has-ref-value? db-ident) (db-property/property-value-content val) val))
(get coll (get-pid repo db-ident))))
(get (:block/properties block) (get-pid repo db-ident))))

(defn get-block-property-value
"Get the value of built-in block's property by its db-ident"
[repo db block db-ident]
(when db
(let [block (or (d/entity db (:db/id block)) block)]
(lookup repo (:block/properties block) db-ident))))
(lookup repo block db-ident))))

(defn shape-block?
[repo db block]
Expand Down
22 changes: 11 additions & 11 deletions deps/outliner/src/logseq/outliner/property.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -460,16 +460,16 @@

(defn- property-with-position?
[db property-id block position]
(let [property (entity-plus/entity-memoized db property-id)
schema (:block/schema property)]
(and
(= (:position schema) position)
(not (and (:logseq.property/hide-empty-value property)
(nil? (get block property-id))))
(not (get-in property [:block/schema :hide?]))
(not (and
(= (:position schema) :block-below)
(nil? (get block property-id)))))))
(when-let [property (entity-plus/entity-memoized db property-id)]
(let [schema (:block/schema property)]
(and
(= (:position schema) position)
(not (and (:logseq.property/hide-empty-value property)
(nil? (get block property-id))))
(not (get-in property [:block/schema :hide?]))
(not (and
(= (:position schema) :block-below)
(nil? (get block property-id))))))))

(defn property-with-other-position?
[property]
Expand All @@ -479,7 +479,7 @@
(defn get-block-positioned-properties
[db eid position]
(let [block (d/entity db eid)
own-properties (keys (:block/properties block))]
own-properties (:block.temp/property-keys block)]
(->> (:classes-properties (get-block-classes-properties db eid))
(map :db/ident)
(concat own-properties)
Expand Down
75 changes: 42 additions & 33 deletions src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,9 +1233,8 @@
(let [block (db/entity [:block/uuid block-id])
db-id (:db/id block)
block (when db-id (db/sub-block db-id))
properties (:block/properties block)
block-type (keyword (pu/lookup properties :logseq.property/ls-type))
hl-type (pu/lookup properties :logseq.property.pdf/hl-type)
block-type (keyword (pu/lookup block :logseq.property/ls-type))
hl-type (pu/lookup block :logseq.property.pdf/hl-type)
repo (state/get-current-repo)
stop-inner-events? (= block-type :whiteboard-shape)]
(if (and block (:block/title block))
Expand Down Expand Up @@ -2062,14 +2061,13 @@
(block-list config' children))]])))

(defn- block-content-empty?
[{:block/keys [properties] :as block}]
[block]
(let [ast-title (:block.temp/ast-title block)
ast-body (:block.temp/ast-body block)]
ast-body (:block.temp/ast-body block)
db-based? (config/db-based-graph? (state/get-current-repo))]
(and
(or
(empty? properties)
(and (not (config/db-based-graph? (state/get-current-repo)))
(property-file/properties-hidden? properties)))
(or db-based?
(property-file/properties-hidden? (:block/properties block)))

(empty? ast-title)

Expand Down Expand Up @@ -2196,8 +2194,11 @@
(declare src-cp)

(rum/defc ^:large-vars/cleanup-todo text-block-title
[config {:block/keys [format marker pre-block? properties] :as block}]
(let [format (or format :markdown)
[config block]
(let [db-based? (config/db-based-graph? (state/get-current-repo))
format (if db-based? :markdown (or (:block/format block) :markdown))
pre-block? (if db-based? false (:block/pre-block? block))
marker (when-not db-based? (:block/marker block))
block (if-not (:block.temp/ast-title block)
(merge block (block/parse-title-and-body uuid format pre-block?
(:block/title block)))
Expand All @@ -2207,16 +2208,16 @@
level (:level config)
slide? (boolean (:slide? config))
block-ref? (:block-ref? config)
block-type (or (keyword (pu/lookup properties :logseq.property/ls-type)) :default)
block-type (or (keyword (pu/lookup block :logseq.property/ls-type)) :default)
html-export? (:html-export? config)
bg-color (pu/lookup properties :logseq.property/background-color)
bg-color (pu/lookup block :logseq.property/background-color)
;; `heading-level` is for backward compatibility, will remove it in later releases
heading-level (:block/heading-level block)
heading (or
(and heading-level
(<= heading-level 6)
heading-level)
(pu/lookup properties :logseq.property/heading))
(pu/lookup block :logseq.property/heading))
heading (if (true? heading) (min (inc level) 6) heading)
elem (if heading
(keyword (str "h" heading ".block-title-wrap.as-heading"
Expand All @@ -2225,7 +2226,7 @@
(->elem
elem
(merge
{:data-hl-type (pu/lookup properties :logseq.property.pdf/hl-type)}
{:data-hl-type (pu/lookup block :logseq.property.pdf/hl-type)}
(when (and marker
(not (string/blank? marker))
(not= "nil" marker))
Expand All @@ -2239,7 +2240,7 @@
:class "px-1 with-bg-color"})))

;; children
(let [area? (= :area (keyword (pu/lookup properties :logseq.property.pdf/hl-type)))
(let [area? (= :area (keyword (pu/lookup block :logseq.property.pdf/hl-type)))
hl-ref #(when (not (#{:default :whiteboard-shape} block-type))
[:div.prefix-link
{:on-pointer-down
Expand All @@ -2259,12 +2260,15 @@
[:span.hl-page
[:strong.forbid-edit
(str "P"
(or (pu/lookup properties :logseq.property.pdf/hl-page)
(or (pu/lookup block :logseq.property.pdf/hl-page)
"?"))]]

(when (and area?
(or (:hl-stamp properties)
(:logseq.property.pdf/hl-image properties)))
(or
;; db graphs
(:logseq.property.pdf/hl-image block)
;; file graphs
(get-in block [:block/properties :hl-stamp])))
(pdf-assets/area-display block))])]
(remove-nils
(concat
Expand Down Expand Up @@ -2769,11 +2773,14 @@
(clock/seconds->days:hours:minutes:seconds time-spent))]))))

(rum/defc ^:large-vars/cleanup-todo block-content < rum/reactive
[config {:block/keys [uuid properties scheduled deadline format pre-block?] :as block} edit-input-id block-id slide?]
(let [format (or format :markdown)
collapsed? (:collapsed? config)
repo (state/get-current-repo)
[config {:block/keys [uuid] :as block} edit-input-id block-id slide?]
(let [repo (state/get-current-repo)
db-based? (config/db-based-graph? (state/get-current-repo))
scheduled (when-not db-based? (:block/scheduled block))
deadline (when-not db-based? (:block/deadline block))
format (if db-based? :markdown (or (:block/format block) :markdown))
pre-block? (when-not db-based? (:block/pre-block? block))
collapsed? (:collapsed? config)
content (if db-based?
(:block/raw-title block)
(property-util/remove-built-in-properties format (:block/raw-title block)))
Expand All @@ -2786,7 +2793,7 @@
stop-events? (:stop-events? config)
block-ref-with-title? (and block-ref? (not (state/show-full-blocks?)) (seq ast-title))
block-type (or
(pu/lookup properties :logseq.property/ls-type)
(pu/lookup block :logseq.property/ls-type)
:default)
content (if (string? content) (string/trim content) "")
mouse-down-key (if (util/ios?)
Expand All @@ -2802,9 +2809,9 @@
:pointer-events (when stop-events? "none")}}

(not (string/blank?
(pu/lookup properties :logseq.property.pdf/hl-color)))
(pu/lookup block :logseq.property.pdf/hl-color)))
(assoc :data-hl-color
(pu/lookup properties :logseq.property.pdf/hl-color))
(pu/lookup block :logseq.property.pdf/hl-color))

(not block-ref?)
(assoc mouse-down-key (fn [e]
Expand Down Expand Up @@ -2862,13 +2869,13 @@
(when-let [invalid-properties (:block/invalid-properties block)]
(invalid-properties-cp invalid-properties)))

(when (and (seq properties)
(let [hidden? (property-file/properties-hidden? properties)]
(when (and (not (config/db-based-graph? repo))
(seq (:block/properties block))
(let [hidden? (property-file/properties-hidden? (:block/properties block))]
(not hidden?))
(not (and block-ref? (or (seq ast-title) (seq ast-body))))
(not (:slide? config))
(not= block-type :whiteboard-shape)
(not (config/db-based-graph? repo)))
(not= block-type :whiteboard-shape))
(properties-cp config block))

(block-content-inner config block ast-body plugin-slotted? collapsed? block-ref-with-title?)
Expand Down Expand Up @@ -2928,8 +2935,10 @@
(assoc state
::hide-block-refs? (atom default-hide?)
::refs-count *refs-count)))}
[state config {:block/keys [uuid format] :as block} {:keys [edit-input-id block-id edit? hide-block-refs-count?]}]
(let [format (or format :markdown)
[state config {:block/keys [uuid] :as block} {:keys [edit-input-id block-id edit? hide-block-refs-count?]}]
(let [format (if (config/db-based-graph? (state/get-current-repo))
:markdown
(or (:block/format block) :markdown))
*hide-block-refs? (get state ::hide-block-refs?)
*refs-count (get state ::refs-count)
hide-block-refs? (rum/react *hide-block-refs?)
Expand Down Expand Up @@ -3470,7 +3479,7 @@
[:div.block-main-container.flex.flex-row.gap-1
{:style (when (and db-based? (:page-title? config))
{:margin-left -30})
:data-has-heading (some-> block :block/properties (pu/lookup :logseq.property/heading))
:data-has-heading (some-> block (pu/lookup :logseq.property/heading))
:on-touch-start (fn [event uuid] (block-handler/on-touch-start event uuid))
:on-touch-move (fn [event]
(block-handler/on-touch-move event block uuid editing? *show-left-menu? *show-right-menu?))
Expand Down
3 changes: 1 addition & 2 deletions src/main/frontend/components/content.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@
(let [repo (state/get-current-repo)
db? (config/db-based-graph? repo)]
(when-let [block (db/entity [:block/uuid block-id])]
(let [properties (:block/properties block)
heading (or (pu/lookup properties :logseq.property/heading)
(let [heading (or (pu/lookup block :logseq.property/heading)
false)]
[:<>
(ui/menu-background-color #(property-handler/set-block-property! repo block-id
Expand Down
7 changes: 3 additions & 4 deletions src/main/frontend/components/property.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
[frontend.handler.notification :as notification]
[frontend.handler.property.util :as pu]
[frontend.handler.route :as route-handler]
[frontend.hooks :as hooks]
[frontend.mixins :as mixins]
[frontend.modules.shortcut.core :as shortcut]
[frontend.state :as state]
Expand All @@ -32,8 +33,7 @@
[logseq.outliner.property :as outliner-property]
[logseq.shui.ui :as shui]
[promesa.core :as p]
[rum.core :as rum]
[frontend.hooks :as hooks]))
[rum.core :as rum]))

(defn- <add-property-from-dropdown
"Adds an existing or new property from dropdown. Used from a block or page context."
Expand Down Expand Up @@ -584,8 +584,7 @@
_ (doseq [class (::classes state)]
(db/sub-block (:db/id class)))
class? (ldb/class? block)
block-properties (:block/properties block)
properties block-properties
properties (:block/properties block)
remove-built-in-or-other-position-properties
(fn [properties]
(remove (fn [property]
Expand Down
6 changes: 2 additions & 4 deletions src/main/frontend/handler/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@
(defn get-idx-of-order-list-block
[block order-list-type]
(let [order-block-fn? (fn [block]
(let [properties (:block/properties block)
type (pu/lookup properties :logseq.property/order-list-type)]
(let [type (pu/lookup block :logseq.property/order-list-type)]
(= type order-list-type)))
prev-block-fn #(some-> (db/entity (:db/id %)) ldb/get-left-sibling)
prev-block (prev-block-fn block)]
Expand Down Expand Up @@ -146,8 +145,7 @@

(defn attach-order-list-state
[config block]
(let [properties (:block/properties block)
type (pu/lookup properties :logseq.property/order-list-type)
(let [type (pu/lookup block :logseq.property/order-list-type)
own-order-list-type (some-> type str string/lower-case)
own-order-list-index (some->> own-order-list-type (get-idx-of-order-list-block block))]
(assoc config :own-order-list-type own-order-list-type
Expand Down
12 changes: 6 additions & 6 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@

(defn get-block-own-order-list-type
[block]
(let [properties (:block/properties block)]
(pu/lookup properties :logseq.property/order-list-type)))
(pu/lookup block :logseq.property/order-list-type))

(defn set-block-own-order-list-type!
[block type]
Expand Down Expand Up @@ -283,12 +282,13 @@
([block value
{:keys [force?]
:as opts}]
(let [{:block/keys [uuid format repo title properties]} block
(let [{:block/keys [uuid format repo title]} block
format (or format :markdown)
repo (or repo (state/get-current-repo))
format (or format (state/get-preferred-format))
block-id (when (and (not (config/db-based-graph? repo)) (map? properties))
(get properties :id))
block-id (let [properties (:block/properties block)]
(when (and (not (config/db-based-graph? repo)) (map? properties))
(get properties :id)))
content (if (config/db-based-graph? repo)
(:block/title (db/entity (:db/id block)))
(-> (property-file/remove-built-in-properties-when-file-based repo format title)
Expand Down Expand Up @@ -3467,7 +3467,7 @@
[block]
(let [class-properties (:classes-properties (outliner-property/get-block-classes-properties (db/get-db) (:db/id block)))
db (db/get-db)
properties (->> (map :a (d/datoms db :eavt (:db/id block)))
properties (->> (:block.temp/property-keys block)
(map (partial entity-plus/entity-memoized db))
(concat class-properties)
(remove (fn [e] (db-property/db-attribute-properties (:db/ident e))))
Expand Down
Loading

0 comments on commit 1d9fbc8

Please sign in to comment.