Add general performance micro optimizations

This commit is contained in:
Andrey Antukh 2023-05-14 18:55:11 +02:00 committed by Alonso Torres
parent 78f62cc5e1
commit b7e1e54a92
4 changed files with 45 additions and 25 deletions

View file

@ -41,7 +41,7 @@
:transform transform})) :transform transform}))
path? (some? (.-d props))] path? (some? (.-d props))]
[:clipPath.frame-clip-def {:id (frame-clip-id shape render-id) :class "frame-clip"} [:clipPath.frame-clip-def {:id (frame-clip-id shape render-id) :class "frame-clip"}
(if path? (if ^boolean path?
[:> :path props] [:> :path props]
[:> :rect props])]))) [:> :rect props])])))
@ -51,36 +51,36 @@
{::mf/wrap-props false} {::mf/wrap-props false}
[props] [props]
(let [shape (obj/get props "shape") (let [shape (unchecked-get props "shape")
children (obj/get props "children") children (unchecked-get props "children")
{:keys [x y width height show-content]} shape {:keys [x y width height show-content]} shape
transform (gsh/transform-str shape) transform (gsh/transform-str shape)
render-id (mf/use-ctx muc/render-id) render-id (mf/use-ctx muc/render-id)
props (-> (attrs/extract-style-attrs shape render-id) props (-> (attrs/extract-style-attrs shape render-id)
(obj/merge! (obj/merge!
#js {:x x #js {:x x
:y y :y y
:transform transform :transform transform
:width width :width width
:height height :height height
:className "frame-background"})) :className "frame-background"}))
path? (some? (.-d props))] path? (some? (.-d props))]
[:* [:*
[:g {:clip-path (when (not show-content) (frame-clip-url shape render-id))} [:g {:clip-path (when (not show-content) (frame-clip-url shape render-id))}
[:& frame-clip-def {:shape shape :render-id render-id}] [:& frame-clip-def {:shape shape :render-id render-id}]
[:& shape-fills {:shape shape} [:& shape-fills {:shape shape}
(if path? (if ^boolean path?
[:> :path props] [:> :path props]
[:> :rect props])] [:> :rect props])]
children] children]
[:& shape-strokes {:shape shape} [:& shape-strokes {:shape shape}
(if path? (if ^boolean path?
[:> :path props] [:> :path props]
[:> :rect props])]])) [:> :rect props])]]))

View file

@ -31,11 +31,10 @@
(if (.isArray js/Array value) (if (.isArray js/Array value)
(->> value (->> value
(remove (fn [change] (remove (fn [change]
(or (or (= "data-loading" (.-attributeName change))
(= "data-loading" (.-attributeName change)) (and (= "attributes" (.-type change))
(and (= "attributes" (.-type change)) (= "href" (.-attributeName change))
(= "href" (.-attributeName change)) (str/starts-with? (.-oldValue change) "http"))))))
(str/starts-with? (.-oldValue change) "http"))))))
[value])) [value]))
(defn- create-svg-blob-uri-from (defn- create-svg-blob-uri-from

View file

@ -48,7 +48,7 @@
(defn duration (defn duration
[o] [o]
(cond (cond
(integer? o) (.fromMillis Duration o) (number? o) (.fromMillis Duration o)
(duration? o) o (duration? o) o
(string? o) (.fromISO Duration o) (string? o) (.fromISO Duration o)
(map? o) (.fromObject Duration (clj->js o)) (map? o) (.fromObject Duration (clj->js o))
@ -239,6 +239,25 @@
(when v (when v
(let [v (if (datetime? v) (format v :date) v) (let [v (if (datetime? v) (format v :date) v)
locale (obj/get locales locale) locale (obj/get locales locale)
f (-> (.-formatLong ^js locale) f (.date (.-formatLong locale) v)]
(.date v))] (->> #js {:locale locale}
(dateFnsFormat v f #js {:locale locale}))))) (dateFnsFormat v f))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Measurement Helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn tpoint
"Create a measurement checkpoint for time measurement of potentially
asynchronous flow."
[]
(let [p1 (.now js/performance)]
#(duration (- (.now js/performance) p1))))
(defn tpoint-ms
"Create a measurement checkpoint for time measurement of potentially
asynchronous flow."
[]
(let [p1 (.now js/performance)]
#(- (.now js/performance) p1)))

View file

@ -7,12 +7,14 @@
(ns app.worker.thumbnails (ns app.worker.thumbnails
(:require (:require
["react-dom/server" :as rds] ["react-dom/server" :as rds]
[app.common.data.macros :as dm]
[app.common.logging :as log] [app.common.logging :as log]
[app.common.uri :as u] [app.common.uri :as u]
[app.config :as cf] [app.config :as cf]
[app.main.fonts :as fonts] [app.main.fonts :as fonts]
[app.main.render :as render] [app.main.render :as render]
[app.util.http :as http] [app.util.http :as http]
[app.util.time :as ts]
[app.worker.impl :as impl] [app.worker.impl :as impl]
[beicon.core :as rx] [beicon.core :as rx]
[debug :refer [debug?]] [debug :refer [debug?]]
@ -21,7 +23,6 @@
(log/set-level! :trace) (log/set-level! :trace)
(defn- handle-response (defn- handle-response
[{:keys [body status] :as response}] [{:keys [body status] :as response}]
(cond (cond
@ -135,14 +136,15 @@
(defmethod impl/handler :thumbnails/render-offscreen-canvas (defmethod impl/handler :thumbnails/render-offscreen-canvas
[_ ibpm] [_ ibpm]
(let [canvas (js/OffscreenCanvas. (.-width ^js ibpm) (.-height ^js ibpm)) (let [canvas (js/OffscreenCanvas. (.-width ^js ibpm) (.-height ^js ibpm))
ctx (.getContext ^js canvas "bitmaprenderer")] ctx (.getContext ^js canvas "bitmaprenderer")
tp (ts/tpoint-ms)]
(.transferFromImageBitmap ^js ctx ibpm) (.transferFromImageBitmap ^js ctx ibpm)
(->> (.convertToBlob ^js canvas #js {:type "image/png"}) (->> (.convertToBlob ^js canvas #js {:type "image/png"})
(p/fmap (fn [blob] (p/fmap (fn [blob]
(js/console.log "[worker]: generated thumbnail")
{:result (.createObjectURL js/URL blob)})) {:result (.createObjectURL js/URL blob)}))
(p/fnly (fn [_] (p/fnly (fn [_]
(log/debug :hint "generated thumbnail" :elapsed (dm/str (tp) "ms"))
(.close ^js ibpm)))))) (.close ^js ibpm))))))