Skip to content

Commit 677ae5c

Browse files
committed
perf: fast path for Moonpool_lwt.run_in_lwt_and_await
1 parent 4e19719 commit 677ae5c

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/lwt/moonpool_lwt.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,16 @@ let spawn_lwt f : _ Lwt.t =
295295
lwt_fut
296296

297297
let spawn_lwt_ignore f = ignore (spawn_lwt f : unit Lwt.t)
298-
let run_in_lwt_and_await (f : unit -> 'a) : 'a = await_lwt @@ spawn_lwt f
299298
let on_lwt_thread = Main_state.on_lwt_thread
300299

300+
let run_in_lwt_and_await (f : unit -> 'a) : 'a =
301+
let st = Main_state.get_st () in
302+
if Scheduler_state.on_lwt_thread_ st then
303+
(* run immediately *)
304+
f ()
305+
else
306+
await_lwt @@ spawn_lwt f
307+
301308
let lwt_main (f : _ -> 'a) : 'a =
302309
let st = setup () in
303310
(* make sure to cleanup *)

0 commit comments

Comments
 (0)