From 59df66f6977e2e495044bd60a063f16d02bea6f7 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sun, 7 Apr 2024 11:00:26 +0100 Subject: [PATCH] refactor: small optimization to Fiber.Ivar.{read,fill} Signed-off-by: Rudi Grinberg --- fiber/src/core.ml | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/fiber/src/core.ml b/fiber/src/core.ml index c429000..e45e9cc 100644 --- a/fiber/src/core.ml +++ b/fiber/src/core.ml @@ -217,8 +217,20 @@ module Ivar = struct type 'a t = 'a ivar let create () = { state = Empty } - let read t k = Read_ivar (t, k) - let fill t x k = Fill_ivar (t, x, k) + + let read t k = + match t.state with + | Full x -> k x + | Empty_with_readers _ | Empty -> Read_ivar (t, k) + ;; + + let fill t x k = + match t.state with + | Empty -> + t.state <- Full x; + k () + | Full _ | Empty_with_readers _ -> Fill_ivar (t, x, k) + ;; let peek t k = k