Skip to content

Commit

Permalink
perf: reduce size of Fut again
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Jan 30, 2024
1 parent 4abc334 commit 8d83d5b
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/fut.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ type 'a waiter = 'a or_error -> unit

type 'a state =
| Done of 'a or_error
| Waiting of { waiters: 'a waiter list }

type 'a t = {
st: 'a state A.t;
name: string;
}
| Waiting of {
waiters: 'a waiter list;
name: string;
}

type 'a t = { st: 'a state A.t } [@@unboxed]
type 'a promise = 'a t

let[@inline] get_name_ (self : _ t) =
match A.get self.st with
| Done _ -> ""
| Waiting { name; _ } -> name

let make ?(name = "") () =
let fut = { st = A.make (Waiting { waiters = [] }); name } in
let fut = { st = A.make (Waiting { waiters = []; name }) } in
fut, fut

let[@inline] of_result x : _ t = { st = A.make (Done x); name = "" }
let[@inline] of_result x : _ t = { st = A.make (Done x) }
let[@inline] return x : _ t = of_result (Ok x)
let[@inline] fail e bt : _ t = of_result (Error (e, bt))

Expand Down Expand Up @@ -57,8 +61,8 @@ let on_result (self : _ t) (f : _ waiter) : unit =
| Done x ->
f x;
false
| Waiting { waiters = l } ->
not (A.compare_and_set self.st st (Waiting { waiters = f :: l }))
| Waiting { waiters = l; name } ->
not (A.compare_and_set self.st st (Waiting { waiters = f :: l; name }))
do
Domain_.relax ()
done
Expand All @@ -71,7 +75,7 @@ let fulfill (self : _ t) (r : _ result) : unit =
let st = A.get self.st in
match st with
| Done _ -> raise Already_fulfilled
| Waiting { waiters = l } ->
| Waiting { waiters = l; name = _ } ->
let did_swap = A.compare_and_set self.st st (Done r) in
if did_swap then (
(* success, now call all the waiters *)
Expand Down Expand Up @@ -135,7 +139,7 @@ let map ?on ~f fut : _ t =
| Error e_bt -> Error e_bt
in

let name = fut.name in
let name = get_name_ fut in
match peek fut, get_runner_ ?on () with
| Some res, None -> of_result @@ map_immediate_ res
| Some res, Some runner ->
Expand All @@ -159,7 +163,7 @@ let join (fut : 'a t t) : 'a t =
| Some (Ok f) -> f
| Some (Error (e, bt)) -> fail e bt
| None ->
let fut2, promise = make ~name:fut.name () in
let fut2, promise = make ~name:(get_name_ fut) () in
on_result fut (function
| Ok sub_fut -> on_result sub_fut (fulfill promise)
| Error _ as e -> fulfill promise e);
Expand All @@ -182,7 +186,7 @@ let bind ?on ~f fut : _ t =
on_result f_res_fut (fun r -> fulfill promise r)
in

let name = fut.name in
let name = get_name_ fut in
match peek fut, get_runner_ ?on () with
| Some res, Some runner ->
let fut2, promise = make ~name () in
Expand Down Expand Up @@ -219,7 +223,7 @@ let both a b : _ t =
| Some (Ok x), Some (Ok y) -> return (x, y)
| Some (Error (e, bt)), _ | _, Some (Error (e, bt)) -> fail e bt
| _ ->
let fut, promise = make ~name:a.name () in
let fut, promise = make ~name:(get_name_ a) () in

let st = A.make `Neither in
on_result a (function
Expand Down Expand Up @@ -252,7 +256,7 @@ let choose a b : _ t =
| _, Some (Ok y) -> return (Either.Right y)
| Some (Error (e, bt)), Some (Error _) -> fail e bt
| _ ->
let fut, promise = make ~name:a.name () in
let fut, promise = make ~name:(get_name_ a) () in

let one_failure = A.make false in
on_result a (function
Expand All @@ -275,7 +279,7 @@ let choose_same a b : _ t =
| _, Some (Ok y) -> return y
| Some (Error (e, bt)), Some (Error _) -> fail e bt
| _ ->
let fut, promise = make ~name:a.name () in
let fut, promise = make ~name:(get_name_ a) () in

let one_failure = A.make false in
on_result a (function
Expand Down

0 comments on commit 8d83d5b

Please sign in to comment.