Skip to content

Commit ce31491

Browse files
committed
Use GADT to avoid a bit of code duplication
1 parent 0228553 commit ce31491

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/kcas/kcas.ml

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -772,40 +772,39 @@ module Xt = struct
772772
let post_commit ~xt:(Xt xt_r : _ t) action =
773773
xt_r.post_commit <- Action.append action xt_r.post_commit
774774

775-
let validate ~xt loc =
776-
let loc = Loc.to_loc loc in
777-
let x = loc.id in
778-
match !(tree_as_ref xt) with
779-
| T Leaf -> ()
780-
| T (Node { loc = a; lt = T Leaf; _ }) when x < a.id -> ()
781-
| T (Node { loc = a; gt = T Leaf; _ }) when a.id < x -> ()
782-
| T (Node { loc = a; state; _ }) when Obj.magic a == loc ->
783-
validate_one xt a state
784-
| tree -> begin
785-
match splay ~hit_parent:true x tree with
786-
| lt, T (Node node_r), gt ->
787-
tree_as_ref xt := T (Node { node_r with lt; gt; awaiters = [] });
788-
if Obj.magic node_r.loc == loc then
789-
validate_one xt node_r.loc node_r.state
790-
| _, T Leaf, _ -> impossible ()
791-
end
775+
type _ op = Validate : unit op | Is_in_log : bool op
792776

793-
let is_in_log ~xt loc =
777+
let do_op : type r. xt:'x t -> 'a Loc.t -> r op -> r =
778+
fun ~xt loc op ->
794779
let loc = Loc.to_loc loc in
795780
let x = loc.id in
796781
match !(tree_as_ref xt) with
797-
| T Leaf -> false
798-
| T (Node { loc = a; lt = T Leaf; _ }) when x < a.id -> false
799-
| T (Node { loc = a; gt = T Leaf; _ }) when a.id < x -> false
800-
| T (Node { loc = a; _ }) when Obj.magic a == loc -> true
782+
| T Leaf -> begin match op with Validate -> () | Is_in_log -> false end
783+
| T (Node { loc = a; lt = T Leaf; _ }) when x < a.id -> begin
784+
match op with Validate -> () | Is_in_log -> false
785+
end
786+
| T (Node { loc = a; gt = T Leaf; _ }) when a.id < x -> begin
787+
match op with Validate -> () | Is_in_log -> false
788+
end
789+
| T (Node { loc = a; state; _ }) when Obj.magic a == loc -> begin
790+
match op with Validate -> validate_one xt a state | Is_in_log -> true
791+
end
801792
| tree -> begin
802793
match splay ~hit_parent:true x tree with
803-
| lt, T (Node node_r), gt ->
794+
| lt, T (Node node_r), gt -> begin
804795
tree_as_ref xt := T (Node { node_r with lt; gt; awaiters = [] });
805-
Obj.magic node_r.loc == loc
796+
match op with
797+
| Validate ->
798+
if Obj.magic node_r.loc == loc then
799+
validate_one xt node_r.loc node_r.state
800+
| Is_in_log -> Obj.magic node_r.loc == loc
801+
end
806802
| _, T Leaf, _ -> impossible ()
807803
end
808804

805+
let[@inline] validate ~xt loc = do_op ~xt loc Validate
806+
let[@inline] is_in_log ~xt loc = do_op ~xt loc Is_in_log
807+
809808
let rec rollback which tree_snap tree =
810809
if tree_snap == tree then tree
811810
else

0 commit comments

Comments
 (0)