mirror of
https://github.com/penpot/penpot.git
synced 2026-11-18 16:12:24 +00:00
✨ Delay building schemas in check-fn in release builds (#12711)
* ✨ Delay building schemas in check-fn check-fn eagerly builds its schemas which contributes to the startup time. But we can delay this to when the check function is actually called, which seems to be when an edit is made. On the Travel A2B - Locofy Sample project file this reduced the time to the first viewport render by ~15%. * ✨ Make schema resolution lazy only on production builds Add a lazy-delay macro that defers the expression only on builds that set the app.common.data.macros/lazy-delay? closure define, and use it in check-fn to resolve the schema. The previous change made the schema resolution unconditionally lazy, which hid wrong schema definitions until the first check call. With this change development builds, tests and storybook resolve the schema when the namespace loads, so a bad definition fails early, and production builds keep the deferred resolution and the startup win. The define is enabled in the release builds of frontend (main and worker), exporter and library. On the JVM the expression is always evaluated immediately. AI-assisted-by: deepseek-v4.1-flash --------- Co-authored-by: Andrey Antukh <niwi@niwi.nz>
This commit is contained in:
parent
dfc2ff6b56
commit
d563444985
@ -26,6 +26,12 @@ Respect the intended abstraction direction in new/refactored code:
|
||||
|
||||
Some legacy code violates this layering; do not copy those violations into new code when a focused refactor is practical.
|
||||
|
||||
## Build-mode dependent laziness
|
||||
|
||||
`app.common.data.macros/lazy-delay` returns a delay whose expression is evaluated eagerly by default. Only the builds that set the `app.common.data.macros/lazy-delay?` closure define to `true` defer it to the first deref; every production build does (frontend `main` and `worker`, exporter, library), so the Closure compiler replaces the constant and drops the eager branch. On the JVM it is always eager.
|
||||
|
||||
`app.common.schema/check-fn` resolves its schema through it: an invalid schema definition fails when the namespace loads in development and in tests, and only on the first check call in production. The other lazily built parts of `check-fn` (validator and explainer) are always deferred.
|
||||
|
||||
## Focused memory routing
|
||||
|
||||
Model, schema, and persistence shape:
|
||||
|
||||
@ -123,6 +123,27 @@
|
||||
(list 'js* (c/str "(~{}?." (str/snake prop) "?? ~{})") obj (list 'cljs.core/get obj prop))
|
||||
(list `c/get obj prop)))
|
||||
|
||||
#?(:cljs (goog-define lazy-delay? false))
|
||||
|
||||
(defmacro lazy-delay
|
||||
"Like `delay`, but only lazy when the build asks for it.
|
||||
|
||||
With the default value of `lazy-delay?` the expression is evaluated
|
||||
immediately, so errors raised while computing it (an invalid schema
|
||||
definition, for instance) surface when the namespace is loaded instead of
|
||||
on the first deref. Production builds set `lazy-delay?` to `true` through
|
||||
`:closure-defines`; then the expression is fully deferred and the compiler
|
||||
drops the eager branch. On the JVM the expression is always evaluated
|
||||
immediately."
|
||||
[expr]
|
||||
(if (:ns &env)
|
||||
`(if lazy-delay?
|
||||
(delay ~expr)
|
||||
(let [value# ~expr]
|
||||
(delay value#)))
|
||||
`(let [value# ~expr]
|
||||
(delay value#))))
|
||||
|
||||
(defn runtime-assert
|
||||
[hint f]
|
||||
(try
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
#?(:clj [malli.dev.pretty :as mdp])
|
||||
#?(:clj [malli.dev.virhe :as v])
|
||||
[app.common.data :as d]
|
||||
[app.common.data.macros :as dm]
|
||||
[app.common.i18n :as i18n :refer [tr]]
|
||||
[app.common.json :as json]
|
||||
[app.common.math :as mth]
|
||||
@ -320,25 +321,32 @@
|
||||
([s] (lookup sr/default-registry s))
|
||||
([registry s] (schema (mr/schema registry s))))
|
||||
|
||||
(defn check-fn
|
||||
"Create a predefined check function"
|
||||
[s & {:keys [hint type code]}]
|
||||
(let [s #?(:clj
|
||||
(schema s)
|
||||
:cljs
|
||||
(try
|
||||
(schema s)
|
||||
(catch :default cause
|
||||
(let [data (ex-data cause)]
|
||||
(if (= :malli.core/invalid-schema (:type data))
|
||||
(throw (ex-info
|
||||
(str "Invalid schema\n"
|
||||
(pp/pprint-str (:data data)))
|
||||
{}))
|
||||
(throw cause))))))
|
||||
(defn- resolve-schema
|
||||
"Resolves the schema definition. On ClojureScript it rewrites the malli
|
||||
invalid schema error with a human readable message."
|
||||
[s]
|
||||
#?(:clj (schema s)
|
||||
:cljs (try
|
||||
(schema s)
|
||||
(catch :default cause
|
||||
(let [data (ex-data cause)]
|
||||
(if (= :malli.core/invalid-schema (:type data))
|
||||
(throw (ex-info
|
||||
(str "Invalid schema\n"
|
||||
(pp/pprint-str (:data data)))
|
||||
{}))
|
||||
(throw cause)))))))
|
||||
|
||||
validator* (delay (m/validator s))
|
||||
explainer* (delay (m/explainer s))
|
||||
(defn check-fn
|
||||
"Create a predefined check function.
|
||||
|
||||
The schema definition is resolved lazily on production builds, and
|
||||
eagerly on development ones, so invalid schemas are detected early
|
||||
while developing. See `app.common.data.macros/lazy-delay`."
|
||||
[s & {:keys [hint type code]}]
|
||||
(let [s* (dm/lazy-delay (resolve-schema s))
|
||||
validator* (delay (m/validator @s*))
|
||||
explainer* (delay (m/explainer @s*))
|
||||
hint (or ^boolean hint "check error")
|
||||
type (or ^boolean type :assertion)
|
||||
code (or ^boolean code :data-validation)]
|
||||
|
||||
33
common/test/common_tests/data_macros_test.cljc
Normal file
33
common/test/common_tests/data_macros_test.cljc
Normal file
@ -0,0 +1,33 @@
|
||||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
||||
|
||||
(ns common-tests.data-macros-test
|
||||
(:require
|
||||
[app.common.data.macros :as dm]
|
||||
[clojure.test :as t]))
|
||||
|
||||
(defn- eager?
|
||||
"Mirrors the build mode check performed by `dm/lazy-delay`."
|
||||
[]
|
||||
#?(:clj true
|
||||
:cljs (not dm/lazy-delay?)))
|
||||
|
||||
(t/deftest lazy-delay-returns-a-delay-with-the-value
|
||||
(let [value (dm/lazy-delay (str "foo" "bar"))]
|
||||
(t/is (delay? value))
|
||||
(t/is (= "foobar" @value))))
|
||||
|
||||
(t/deftest lazy-delay-evaluates-immediately-by-default
|
||||
(let [calls (atom 0)
|
||||
_ (dm/lazy-delay (swap! calls inc))]
|
||||
(t/is (= (if (eager?) 1 0) @calls))))
|
||||
|
||||
(t/deftest lazy-delay-evaluates-the-expression-only-once
|
||||
(let [calls (atom 0)
|
||||
value (dm/lazy-delay (swap! calls inc))]
|
||||
(t/is (= 1 @value))
|
||||
(t/is (= 1 @value))
|
||||
(t/is (= 1 @calls))))
|
||||
@ -17,6 +17,7 @@
|
||||
[common-tests.attrs-test]
|
||||
[common-tests.buffer-test]
|
||||
[common-tests.colors-test]
|
||||
[common-tests.data-macros-test]
|
||||
[common-tests.data-test]
|
||||
[common-tests.exceptions-test]
|
||||
[common-tests.files-builder-test]
|
||||
@ -135,6 +136,7 @@
|
||||
'common-tests.attrs-test
|
||||
'common-tests.buffer-test
|
||||
'common-tests.colors-test
|
||||
'common-tests.data-macros-test
|
||||
'common-tests.data-test
|
||||
'common-tests.exceptions-test
|
||||
'common-tests.files-builder-test
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
(ns common-tests.schema-test
|
||||
(:require
|
||||
[app.common.data :as d]
|
||||
[app.common.data.macros :as dm]
|
||||
[app.common.schema :as sm]
|
||||
[app.common.schema.generators :as sg]
|
||||
[app.common.uuid :as uuid]
|
||||
@ -283,3 +284,43 @@
|
||||
|
||||
(t/testing "generate"
|
||||
(t/is (true? (sm/validate ::sm/user-provided-uuid (sg/generate ::sm/user-provided-uuid)))))))
|
||||
|
||||
(defn- catch-error
|
||||
[f]
|
||||
(try
|
||||
(f)
|
||||
nil
|
||||
(catch #?(:clj Throwable :cljs :default) cause
|
||||
cause)))
|
||||
|
||||
(defn- lazy-resolution?
|
||||
"Mirrors the build mode check performed by `app.common.data.macros/lazy-delay`."
|
||||
[]
|
||||
#?(:clj false
|
||||
:cljs dm/lazy-delay?))
|
||||
|
||||
(t/deftest test-check-fn
|
||||
(t/testing "returns the value when it is valid"
|
||||
(let [check (sm/check-fn ::sm/uuid)]
|
||||
(t/is (= uuid/zero (check uuid/zero)))))
|
||||
|
||||
(t/testing "raises with the explain data when the value is invalid"
|
||||
(let [check (sm/check-fn ::sm/uuid :hint "invalid uuid"
|
||||
:type :custom :code :custom-code)
|
||||
cause (catch-error #(check "not-a-uuid"))
|
||||
data (ex-data cause)]
|
||||
(t/is (some? cause))
|
||||
(t/is (= "invalid uuid" (ex-message cause)))
|
||||
(t/is (= :custom (:type data)))
|
||||
(t/is (= :custom-code (:code data)))
|
||||
(t/is (= "invalid uuid" (:hint data)))
|
||||
(t/is (map? (::sm/explain data)))))
|
||||
|
||||
(t/testing "resolves the schema definition according to the build mode"
|
||||
;; A schema that does not exist raises only when it is resolved, so this
|
||||
;; checks when the resolution happens: on `check-fn` on non production
|
||||
;; builds, on the first call of the returned function on production ones.
|
||||
(let [check (catch-error #(sm/check-fn ::unknown-schema))]
|
||||
(if (lazy-resolution?)
|
||||
(t/is (some? (catch-error #(check ::anything))))
|
||||
(t/is (some? check))))))
|
||||
|
||||
@ -24,7 +24,8 @@
|
||||
:output-wrapper false}
|
||||
|
||||
:release
|
||||
{:compiler-options
|
||||
{:closure-defines {app.common.data.macros/lazy-delay? true}
|
||||
:compiler-options
|
||||
{:fn-invoke-direct true
|
||||
:source-map true
|
||||
:optimizations #shadow/env ["PENPOT_BUILD_OPTIMIZATIONS" :as :keyword :default :simple]
|
||||
|
||||
@ -76,6 +76,7 @@
|
||||
:release
|
||||
{:closure-defines {goog.DEBUG false
|
||||
goog.debug.LOGGING_ENABLED true
|
||||
app.common.data.macros/lazy-delay? true
|
||||
app.config/compiled-version-tag #shadow/env ["VERSION_TAG" :default "develop"]}
|
||||
:compiler-options
|
||||
{:fn-invoke-direct true
|
||||
@ -115,7 +116,8 @@
|
||||
:warnings {:fn-deprecated false}}
|
||||
|
||||
:release
|
||||
{:compiler-options
|
||||
{:closure-defines {app.common.data.macros/lazy-delay? true}
|
||||
:compiler-options
|
||||
{:fn-invoke-direct true
|
||||
:optimizations #shadow/env ["PENPOT_BUILD_OPTIMIZATIONS" :as :keyword :default :advanced]
|
||||
:output-wrapper true
|
||||
|
||||
@ -35,7 +35,8 @@
|
||||
:warnings {:fn-deprecated false}}
|
||||
|
||||
:release
|
||||
{:compiler-options
|
||||
{:closure-defines {app.common.data.macros/lazy-delay? true}
|
||||
:compiler-options
|
||||
{:fn-invoke-direct true
|
||||
:optimizations #shadow/env ["PENPOT_BUILD_OPTIMIZATIONS" :as :keyword :default :advanced]
|
||||
:pretty-print false
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user