Skip to content

Commit

Permalink
[new] NB completely rewrite core
Browse files Browse the repository at this point in the history
Refreshes the core implementation as I'd write it today.

Changes include:

  - Major code simplifications
  - Significant performance improvements
  • Loading branch information
ptaoussanis committed Sep 7, 2023
1 parent 4375025 commit 7247824
Show file tree
Hide file tree
Showing 3 changed files with 404 additions and 404 deletions.
51 changes: 24 additions & 27 deletions src/taoensso/tufte.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@
#?(:clj (:import [taoensso.tufte.impl PStats]))
#?(:cljs (:require-macros [taoensso.tufte :refer [profiled]])))

(enc/assert-min-encore-version [3 66 0])
(comment (remove-ns 'taoensso.tufte))

(enc/assert-min-encore-version [3 67 0])

;;;; TODO
;; - Use new `enc/ctx-filter` and config utils for filtering
;; - Use new Telemere-style handler registration, with per-handler opts?

;;;; Level filtering
;; Terminology note: we distinguish between call/form and min levels to ensure
Expand Down Expand Up @@ -175,9 +181,10 @@
(not (string? ns-str-form)) ; Not a compile-time ns-str const
(may-profile-ns? compile-time-ns-filter ns-str-form))))))

;;;; Output handlers
;; Handlers are used for `profile` output, let us nicely decouple stat
;; creation and consumption.
;;;; `profile` handlers
;; Handlers decouples stat creation and consumption

#?(:clj (enc/defalias impl/handler-runner-opts))

(defrecord HandlerVal [ns-str level ?id ?data pstats pstats-str_ ?file ?line])

Expand Down Expand Up @@ -311,16 +318,14 @@
(impl/new-pdata-local nmax))))

(comment
@@(new-pdata)

;; Note that dynamic pdata with non-dynamic `with-profiling` is fine:
@((new-pdata))
(let [pd (new-pdata)
t0 (System/nanoTime)]
;; Note that dynamic pdata with non-dynamic `with-profiling` is fine:
(with-profiling pd {}
(p :foo (Thread/sleep 100))
(capture-time! pd :bar (- t0 (System/nanoTime))))
@pd) ; => pstats
)
(p :pid1 (Thread/sleep 100))
(pd :pid2 (- t0 (System/nanoTime)) nil))
@(pd)))

#?(:clj
(defmacro with-profiling
Expand Down Expand Up @@ -349,17 +354,17 @@
See `new-pdata` for more info on low-level primitives.
See also `capture-time!*`."
([pdata id nano-secs-elapsed] `(impl/capture-time! ~pdata ~id ~nano-secs-elapsed ~(enc/get-source &form &env)))
([pdata id nano-secs-elapsed] `(~pdata ~id ~nano-secs-elapsed ~(enc/get-source &form &env)))
([ id nano-secs-elapsed]
`(when-let [~'pd (or impl/*pdata* (impl/pdata-local-get))]
(impl/capture-time! ~'pd ~id ~nano-secs-elapsed ~(enc/get-source &form &env))))))
(~'pd ~id ~nano-secs-elapsed ~(enc/get-source &form &env))))))

(defn capture-time!*
"Like `capture-time!` but: a function, and does not collect callsite location info."
([pdata id nano-secs-elapsed] (impl/capture-time! pdata id nano-secs-elapsed nil))
([pdata id nano-secs-elapsed] (pdata id nano-secs-elapsed nil))
([ id nano-secs-elapsed]
(when-let [pd (or impl/*pdata* (impl/pdata-local-get))]
(impl/capture-time! pd id nano-secs-elapsed nil))))
(pd id nano-secs-elapsed nil))))

(comment
@(second
Expand All @@ -370,14 +375,14 @@
(capture-time! :foo (- t1 t0)))))

(let [pd (new-pdata)]
(enc/qb 1e6 (capture-time! pd :foo 100))
@@pd)
(enc/qb 1e6 (pd :foo 100 nil))
@(pd))

(let [pd (new-pdata)]
(with-profiling pd {}
(p :foo (Thread/sleep 100))
(p :bar (Thread/sleep 200)))
@@pd))
@(pd)))

;;;; Core macros

Expand Down Expand Up @@ -631,7 +636,7 @@
;; ~(println [id-form (:line loc)]) ; Debug location info

;; Note that `capture-time!` expense is excl. from p time
(impl/capture-time! ~'__pd ~id-form (- ~'__t1 ~'__t0) ~loc )
(~'__pd ~id-form (- ~'__t1 ~'__t0) ~loc )
~'__result)

(do ~@body)))))))
Expand Down Expand Up @@ -993,11 +998,3 @@
(format-pstats
(second
(profiled {} (p :foo (Thread/sleep 100)))))))

(comment ; Disjoint time union
(let [[_ ps1] (profiled {} (p :foo (Thread/sleep 100)))
_ (Thread/sleep 500)
[_ ps2] (profiled {} (p :foo (Thread/sleep 100)))]
(println (format-pstats (merge-pstats ps2 ps1)))
;;@(merge-pstats ps2 ps1)
))
Loading

0 comments on commit 7247824

Please sign in to comment.