[new] Avoid duplicated trace bodies

Trade off a small performance hit with tracing to avoid duplication
of potentially large expansions, and to help further eliminate potential
issues when embedding within IOT-style macros (`core.async/go`, etc.)
This commit is contained in:
Peter Taoussanis 2024-09-20 11:22:30 +02:00
parent cbab57be66
commit c9e84e8b38

View file

@ -687,12 +687,9 @@
(sig-middleware# signal#) ; Apply signal middleware, can throw (sig-middleware# signal#) ; Apply signal middleware, can throw
(do signal#))))) (do signal#)))))
;; Could avoid double `run-form` expansion with a fn wrap (>0 cost) ;; Trade-off: avoid double `run-form` expansion
;; (let [run-fn-form (when run-form `(fn [] (~run-form)))] run-fn-form (when run-form `(fn [] ~run-form))
;; `(let [~'run-fn-form ~run-fn-form] run-form* (when run-form `(~'__run-fn-form))
;; (if-not ~allow?
;; (run-fn-form)
;; (let [...]))))
into-let-form into-let-form
(enc/cond! (enc/cond!
@ -704,8 +701,8 @@
~(when run-form ~(when run-form
`(let [t0# (enc/now-nano*)] `(let [t0# (enc/now-nano*)]
(enc/try* (enc/try*
(do (RunResult. ~run-form nil (- (enc/now-nano*) t0#))) (do (RunResult. ~run-form* nil (- (enc/now-nano*) t0#)))
(catch :all t# (RunResult. nil t# (- (enc/now-nano*) t0#))))))] (catch :all t# (RunResult. nil t# (- (enc/now-nano*) t0#))))))]
;; Trace without OpenTelemetry ;; Trace without OpenTelemetry
(or cljs? (not enabled:otel-tracing?)) (or cljs? (not enabled:otel-tracing?))
@ -718,8 +715,8 @@
*trace-parent* {:id ~'__id, :uid ~'__uid}] *trace-parent* {:id ~'__id, :uid ~'__uid}]
(let [t0# (enc/now-nano*)] (let [t0# (enc/now-nano*)]
(enc/try* (enc/try*
(do (RunResult. ~run-form nil (- (enc/now-nano*) t0#))) (do (RunResult. ~run-form* nil (- (enc/now-nano*) t0#)))
(catch :all t# (RunResult. nil t# (- (enc/now-nano*) t0#)))))))] (catch :all t# (RunResult. nil t# (- (enc/now-nano*) t0#)))))))]
;; Trace with OpenTelemetry ;; Trace with OpenTelemetry
(and clj? enabled:otel-tracing?) (and clj? enabled:otel-tracing?)
@ -739,19 +736,20 @@
(let [otel-scope# (.makeCurrent ~'__otel-context1) (let [otel-scope# (.makeCurrent ~'__otel-context1)
t0# (enc/now-nano*)] t0# (enc/now-nano*)]
(enc/try* (enc/try*
(do (RunResult. ~run-form nil (- (enc/now-nano*) t0#))) (do (RunResult. ~run-form* nil (- (enc/now-nano*) t0#)))
(catch :all t# (RunResult. nil t# (- (enc/now-nano*) t0#))) (catch :all t# (RunResult. nil t# (- (enc/now-nano*) t0#)))
(finally (.close otel-scope#))))))]) (finally (.close otel-scope#))))))])
final-form final-form
;; Unless otherwise specified, allow errors to throw on call ;; Unless otherwise specified, allow errors to throw on call
`(let [~'__kind ~kind-form `(let [~'__run-fn-form ~run-fn-form
~'__ns ~ns-form ~'__kind ~kind-form
~'__id ~id-form ~'__ns ~ns-form
~'__level ~level-form] ~'__id ~id-form
~'__level ~level-form]
(enc/if-not ~allow? (enc/if-not ~allow?
~run-form ~run-form*
(let [~'__inst ~inst-form (let [~'__inst ~inst-form
~'__thread ~thread-form ~'__thread ~thread-form
~'__root0 ~root-form0 ; ?{:keys [id uid]} ~'__root0 ~root-form0 ; ?{:keys [id uid]}