mirror of
https://github.com/ocaml-tracing/ocaml-opentelemetry.git
synced 2026-03-09 20:33:36 -04:00
reduce allocations in push
This commit is contained in:
parent
026465f770
commit
b778ffdac3
1 changed files with 10 additions and 4 deletions
|
|
@ -72,8 +72,16 @@ let pop_if_ready ?(force = false) ~now (self : _ t) : _ list option =
|
||||||
let append_with_count ~(elems : 'a list) ~(q : 'a list) : int * 'a list =
|
let append_with_count ~(elems : 'a list) ~(q : 'a list) : int * 'a list =
|
||||||
elems |> List.fold_left (fun (count, q') x -> succ count, x :: q') (0, q)
|
elems |> List.fold_left (fun (count, q') x -> succ count, x :: q') (0, q)
|
||||||
|
|
||||||
|
let rec push_unprotected (self : _ t) ~(elems : _ list) : unit =
|
||||||
|
match elems with
|
||||||
|
| [] -> ()
|
||||||
|
| x :: xs ->
|
||||||
|
self.q <- x :: self.q;
|
||||||
|
self.size <- 1 + self.size;
|
||||||
|
push_unprotected self ~elems:xs
|
||||||
|
|
||||||
let push (self : _ t) elems : [ `Dropped | `Ok ] =
|
let push (self : _ t) elems : [ `Dropped | `Ok ] =
|
||||||
protect self.mutex @@ fun () ->
|
protect_mutex self.mutex @@ fun () ->
|
||||||
if self.size >= self.high_watermark then
|
if self.size >= self.high_watermark then
|
||||||
(* drop this to prevent queue from growing too fast *)
|
(* drop this to prevent queue from growing too fast *)
|
||||||
`Dropped
|
`Dropped
|
||||||
|
|
@ -82,9 +90,7 @@ let push (self : _ t) elems : [ `Dropped | `Ok ] =
|
||||||
(* current batch starts now *)
|
(* current batch starts now *)
|
||||||
self.start <- Mtime_clock.now ();
|
self.start <- Mtime_clock.now ();
|
||||||
|
|
||||||
let count, q' = append_with_count ~elems ~q:self.q in
|
|
||||||
(* add to queue *)
|
(* add to queue *)
|
||||||
self.size <- self.size + count;
|
push_unprotected self ~elems;
|
||||||
self.q <- q';
|
|
||||||
`Ok
|
`Ok
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue