enhance: cut memory usage

This commit is contained in:
Tienson Qin
2022-01-19 00:24:36 +08:00
parent b0d77e5d7f
commit faea913a40
14 changed files with 216 additions and 305 deletions

View File

@@ -471,11 +471,11 @@
(update :refs (fn [col] (remove nil? col)))))
(defn extract-blocks*
[blocks pre-block-body pre-block-properties encoded-content]
[blocks pre-block-properties encoded-content]
(let [first-block (first blocks)
first-block-start-pos (get-in first-block [:block/meta :start-pos])
blocks (if (or (seq @pre-block-body)
(seq @pre-block-properties))
blocks (if (or (> first-block-start-pos 0)
(empty? blocks))
(cons
(merge
(let [content (utf8/substring encoded-content 0 first-block-start-pos)
@@ -486,7 +486,6 @@
:meta {:start-pos 0
:end-pos (or first-block-start-pos
(utf8/length encoded-content))}
:body @pre-block-body
:properties @pre-block-properties
:properties-order (keys @pre-block-properties)
:refs (->> (get-page-refs-from-properties @pre-block-properties)
@@ -506,11 +505,9 @@
(try
(let [encoded-content (utf8/encode content)
last-pos (utf8/length encoded-content)
pre-block-body (atom nil)
pre-block-properties (atom nil)
blocks
(loop [headings []
block-body []
blocks (reverse blocks)
timestamps {}
properties {}
@@ -527,17 +524,12 @@
(paragraph-timestamp-block? block)
(let [timestamps (extract-timestamps block)
timestamps' (merge timestamps timestamps)
[timestamps others] (split-with #(= "Timestamp" (first %)) (second block))
other-body (->>
(concat
timestamps
(drop-while #(contains? #{"Hard_Break_Line" "Break_Line"} (first %)) others))
(remove nil?))]
(recur headings (conj block-body ["Paragraph" other-body]) (rest blocks) timestamps' properties last-pos last-level children (conj block-all-content block-content)))
[timestamps others] (split-with #(= "Timestamp" (first %)) (second block))]
(recur headings (rest blocks) timestamps' properties last-pos last-level children (conj block-all-content block-content)))
(property/properties-ast? block)
(let [properties (extract-properties (second block))]
(recur headings block-body (rest blocks) timestamps properties last-pos last-level children (conj block-all-content block-content)))
(recur headings (rest blocks) timestamps properties last-pos last-level children (conj block-all-content block-content)))
(heading-block? block)
(let [id (get-custom-id-or-new-id properties)
@@ -568,9 +560,6 @@
block (cond->
(assoc block
:uuid id
:body (vec
(->> (reverse block-body)
(map #(remove-indentations format (:level block) %))))
:refs ref-pages-in-properties
:children (or current-block-children [])
:format format)
@@ -600,20 +589,17 @@
(and updated-at (integer? updated-at))
(assoc :block/updated-at updated-at))]
(recur (conj headings block) [] (rest blocks) {} {} last-pos' (:level block) children []))
(recur (conj headings block) (rest blocks) {} {} last-pos' (:level block) children []))
:else
(let [block-body' (conj block-body block)]
(recur headings block-body' (rest blocks) timestamps properties last-pos last-level children (conj block-all-content block-content)))))
(recur headings (rest blocks) timestamps properties last-pos last-level children (conj block-all-content block-content))))
(do
(when (seq block-body)
(reset! pre-block-body (reverse block-body)))
(when (seq properties)
(let [properties (:properties properties)]
(reset! pre-block-properties properties)))
(-> (reverse headings)
safe-blocks))))]
(extract-blocks* blocks pre-block-body pre-block-properties encoded-content))
(extract-blocks* blocks pre-block-properties encoded-content))
(catch js/Error e
(js/console.error "extract-blocks-failed")
(log/error :exception e))))