Skip to content

Commit

Permalink
Fix peek and peek_opt
Browse files Browse the repository at this point in the history
There was a trivial bug using a wrong variable.

Additionally, it turns out the OCaml 5 compiler incorrectly optimized away a
repeated `Atomic.get`.  This commit works around that compiler bug by using
`Sys.opaque_identity` to provent the compiler from figuring out that the
location has been read before.
  • Loading branch information
polytypic committed Nov 3, 2023
1 parent c336ffd commit dc55688
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/cue.ml
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ let rec peek t =
release_all t.tail_waiters;
raise exn);
peek t
| Node r as node ->
| Node r ->
let value = r.value in
if Atomic.get t.head != node then peek t else value
(* The [Sys.opaque_identity] below prevents OCaml 5 from optimizing the
repeated load away. *)
if Atomic.get (Sys.opaque_identity t.head) != old_head then peek t
else value

let[@inline] peek t = peek t

Expand All @@ -119,9 +122,12 @@ let rec peek_opt t =
let head = Atomic.get t.head in
match fenceless_get_next head with
| Null -> None
| Node r as node ->
| Node r ->
let value = r.value in
if Atomic.get t.head != node then peek_opt t else Some value
(* The [Sys.opaque_identity] below prevents OCaml 5 from optimizing the
repeated load away. *)
if Atomic.get (Sys.opaque_identity t.head) != head then peek_opt t
else Some value

let[@inline] peek_opt t = peek_opt t

Expand Down

0 comments on commit dc55688

Please sign in to comment.