diff --git a/src/core/span_id.ml b/src/core/span_id.ml index f7d926fc..6285fe94 100644 --- a/src/core/span_id.ml +++ b/src/core/span_id.ml @@ -11,10 +11,13 @@ let create () : t = Bytes.set b 0 (Char.unsafe_chr (Char.code (Bytes.get b 0) lor 1)); b +(* dark magic, woo. We have an [assert] below to do the bound checks once *) +external unsafe_b_get64 : bytes -> int -> int64 = "%caml_bytes_get64u" + let[@inline] is_zero (self : t) : bool = (* try to reduce branches *) assert (Bytes.length self = 8); - let n1 = Bytes.get_int64_ne self 0 in + let n1 = unsafe_b_get64 self 0 in n1 = 0L let[@inline] is_valid self = not (is_zero self) diff --git a/src/core/trace_id.ml b/src/core/trace_id.ml index 999eb3af..e82539c5 100644 --- a/src/core/trace_id.ml +++ b/src/core/trace_id.ml @@ -17,11 +17,14 @@ let[@inline] of_bytes b = else invalid_arg "trace ID must be 16 bytes in length" +(* dark magic, woo. We have an [assert] below to do the bound checks once *) +external unsafe_b_get64 : bytes -> int -> int64 = "%caml_bytes_get64u" + let[@inline] is_zero (self : t) : bool = (* try to reduce branches *) - assert (Bytes.length self = 1); - let n1 = Bytes.get_int64_ne self 0 in - let n2 = Bytes.get_int64_ne self 8 in + assert (Bytes.length self = 16); + let n1 = unsafe_b_get64 self 0 in + let n2 = unsafe_b_get64 self 8 in n1 = 0L && n2 = 0L let[@inline] is_valid self = not (is_zero self) @@ -37,4 +40,12 @@ let[@inline] of_hex_substring s off = let pp fmt t = Format.fprintf fmt "%s" (to_hex t) +let compare = Bytes.compare + +module Map = Map.Make (struct + type nonrec t = t + + let compare = compare +end) + let k_trace_id : t Hmap.key = Hmap.Key.create () diff --git a/src/core/trace_id.mli b/src/core/trace_id.mli index 487c901b..d3c45f9b 100644 --- a/src/core/trace_id.mli +++ b/src/core/trace_id.mli @@ -8,6 +8,8 @@ val create : unit -> t val dummy : t +val compare : t -> t -> int + val pp : Format.formatter -> t -> unit val is_valid : t -> bool @@ -24,6 +26,8 @@ val of_hex : string -> t val of_hex_substring : string -> int -> t +module Map : Map.S with type key = t + val k_trace_id : t Hmap.key (** Hmap key to carry around a {!Trace_id.t}, to remember what the current trace is.