From 0707d932bcdbada9a004aac787fb870db3bb9139 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 8 Jun 2023 23:17:39 -0400 Subject: [PATCH] perf: make `with_` inlinable in case there is no collector --- src/trace.ml | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/trace.ml b/src/trace.ml index 08d9548..6078100 100644 --- a/src/trace.ml +++ b/src/trace.ml @@ -22,19 +22,25 @@ let[@inline] exit_span span : unit = | None -> () | Some (module C) -> C.exit_span span -let with_ ?__FUNCTION__ ~__FILE__ ~__LINE__ name f = +let with_collector_ (module C : Collector.S) ?__FUNCTION__ ~__FILE__ ~__LINE__ + name f = + let sp = C.create_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name in + match f sp with + | x -> + C.exit_span sp; + x + | exception exn -> + let bt = Printexc.get_raw_backtrace () in + C.exit_span sp; + Printexc.raise_with_backtrace exn bt + +let[@inline] with_ ?__FUNCTION__ ~__FILE__ ~__LINE__ name f = match A.get collector with - | None -> f Collector.dummy_span - | Some (module C) -> - let sp = C.create_span ?__FUNCTION__ ~__FILE__ ~__LINE__ name in - (match f sp with - | x -> - C.exit_span sp; - x - | exception exn -> - let bt = Printexc.get_raw_backtrace () in - C.exit_span sp; - Printexc.raise_with_backtrace exn bt) + | None -> + (* fast path: no collector, no span *) + f Collector.dummy_span + | Some collector -> + with_collector_ collector ?__FUNCTION__ ~__FILE__ ~__LINE__ name let[@inline] message ?__FUNCTION__ ~__FILE__ ~__LINE__ msg : unit = match A.get collector with