Skip to content

Commit 83a3483

Browse files
committed
Stop marking storage-cached nodes as accessed
1 parent 6e9a252 commit 83a3483

2 files changed

Lines changed: 82 additions & 108 deletions

File tree

‎lib/persistent_sorted_set.ml‎

Lines changed: 53 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type 'a storage = {
1111

1212
module Node = struct
1313
type 'a t =
14-
| Ref of { max_key : 'a; address : string; mutable cached : 'a t option }
14+
| Ref of { max_key : 'a; address : string }
1515
| Leaf of { values : 'a array; len : int; address : string option }
1616
| Branch of {
1717
keys : 'a array;
@@ -65,6 +65,10 @@ let cache_storage settings storage =
6565
Weak.set slot 0 (Some node);
6666
Hashtbl.replace cache address slot
6767
in
68+
let restore_uncached address =
69+
storage.accessed address;
70+
storage.restore_node address
71+
in
6872
match settings.ref_type with
6973
| Strong ->
7074
let cache = Hashtbl.create 128 in
@@ -79,7 +83,7 @@ let cache_storage settings storage =
7983
match Hashtbl.find_opt cache address with
8084
| Some node -> Some node
8185
| None -> (
82-
match storage.restore_node address with
86+
match restore_uncached address with
8387
| Some node ->
8488
Hashtbl.replace cache address node;
8589
Some node
@@ -101,13 +105,13 @@ let cache_storage settings storage =
101105
match Weak.get slot 0 with
102106
| Some node -> Some node
103107
| None -> (
104-
match storage.restore_node address with
108+
match restore_uncached address with
105109
| Some node ->
106110
remember cache address node;
107111
Some node
108112
| None -> None))
109113
| None -> (
110-
match storage.restore_node address with
114+
match restore_uncached address with
111115
| Some node ->
112116
remember cache address node;
113117
Some node
@@ -171,7 +175,6 @@ let storage_required = function
171175
| None -> invalid_arg "storage-backed node requires storage"
172176

173177
let rec materialize_address_with storage address tail =
174-
storage.accessed address;
175178
match storage.restore_node address with
176179
| Some (Leaf values) -> prepend_list values (tail ())
177180
| Some (Branch (_, child_addresses)) ->
@@ -226,12 +229,10 @@ let show_node show_value root =
226229
lines := (String.make (depth * 2) ' ' ^ line) :: !lines
227230
in
228231
let rec loop depth = function
229-
| Node.Ref { max_key; address; cached } ->
232+
| Node.Ref { max_key; address } ->
230233
add_line depth
231-
(Printf.sprintf "Ref(address=%s max_key=%s cached=%s)" address
232-
(show_value max_key)
233-
(match cached with None -> "none" | Some _ -> "some"));
234-
Option.iter (loop (depth + 1)) cached
234+
(Printf.sprintf "Ref(address=%s max_key=%s)" address
235+
(show_value max_key))
235236
| Node.Leaf { values; len; address } ->
236237
add_line depth
237238
(Printf.sprintf "Leaf(address=%s len=%d values=%s)"
@@ -257,7 +258,6 @@ let route_cmp order_cmp equality_cmp value key =
257258
match order_cmp value key with 0 -> equality_cmp value key | n -> n
258259

259260
let restore_stored_node storage address =
260-
storage.accessed address;
261261
match storage.restore_node address with
262262
| Some node -> node
263263
| None -> invalid_arg ("stored node not found: " ^ address)
@@ -499,7 +499,7 @@ let node_of_stored_branch keys child_addresses address =
499499
Array.mapi
500500
(fun index child_address ->
501501
let key = keys.(index) in
502-
Node.Ref { max_key = key; address = child_address; cached = None })
502+
Node.Ref { max_key = key; address = child_address })
503503
child_addresses
504504
in
505505
Node.Branch { keys = Array.copy keys; children; address = Some address }
@@ -513,50 +513,28 @@ let node_of_stored_node storage address =
513513
node_of_stored_branch keys child_addresses address
514514

515515
let force_ref_node storage = function
516-
| Node.Ref ref_ -> (
517-
match ref_.cached with
518-
| Some node -> node
519-
| None ->
520-
let node = node_of_stored_node storage ref_.address in
521-
ref_.cached <- Some node;
522-
node)
516+
| Node.Ref { address; _ } -> node_of_stored_node storage address
523517
| node -> node
524518

525-
type 'a node_edit_mode = Pure_tree | Stored_tree of 'a storage
526-
527-
let storage_of_edit_mode = function
528-
| Stored_tree storage -> storage
529-
| Pure_tree -> invalid_arg "storage-backed node requires storage-aware edit"
530-
531-
let edit_mode_of_storage = function
532-
| Some storage -> Stored_tree storage
533-
| None -> Pure_tree
534-
535-
let node_leaf_values mode = function
519+
let node_leaf_values storage = function
536520
| Node.Leaf { values; len; _ } ->
537521
Some
538522
(if len = Array.length values then values else Array.sub values 0 len)
539523
| Node.Ref _ as node -> (
540-
match mode with
541-
| Pure_tree -> None
542-
| Stored_tree storage -> (
543-
match force_ref_node storage node with
544-
| Node.Leaf { values; len; _ } ->
545-
Some
546-
(if len = Array.length values then values
547-
else Array.sub values 0 len)
548-
| Node.Ref _ | Node.Branch _ -> None))
524+
match force_ref_node (storage_required storage) node with
525+
| Node.Leaf { values; len; _ } ->
526+
Some
527+
(if len = Array.length values then values
528+
else Array.sub values 0 len)
529+
| Node.Ref _ | Node.Branch _ -> None)
549530
| Node.Branch _ -> None
550531

551-
let node_branch_parts mode = function
532+
let node_branch_parts storage = function
552533
| Node.Branch { keys; children; _ } -> Some (keys, children)
553534
| Node.Ref _ as node -> (
554-
match mode with
555-
| Pure_tree -> None
556-
| Stored_tree storage -> (
557-
match force_ref_node storage node with
558-
| Node.Branch { keys; children; _ } -> Some (keys, children)
559-
| Node.Ref _ | Node.Leaf _ -> None))
535+
match force_ref_node (storage_required storage) node with
536+
| Node.Branch { keys; children; _ } -> Some (keys, children)
537+
| Node.Ref _ | Node.Leaf _ -> None)
560538
| Node.Leaf _ -> None
561539

562540
let add_leaf_refs settings inserted =
@@ -580,8 +558,8 @@ let branch_replace_range settings keys children start remove_count replacements
580558
let keys, children = ref_arrays_of_list !refs in
581559
branch_refs_of_arrays settings keys children
582560

583-
let rebalance_leaf_child mode settings keys children index child =
584-
match node_leaf_values mode child with
561+
let rebalance_leaf_child storage settings keys children index child =
562+
match node_leaf_values storage child with
585563
| None -> None
586564
| Some values -> (
587565
let minimum = min_child_occupancy settings in
@@ -590,7 +568,7 @@ let rebalance_leaf_child mode settings keys children index child =
590568
let rebalance_with_right () =
591569
if index + 1 >= Array.length children then None
592570
else
593-
match node_leaf_values mode children.(index + 1) with
571+
match node_leaf_values storage children.(index + 1) with
594572
| None -> None
595573
| Some right ->
596574
let combined_length =
@@ -618,7 +596,7 @@ let rebalance_leaf_child mode settings keys children index child =
618596
let rebalance_with_left () =
619597
if index = 0 then None
620598
else
621-
match node_leaf_values mode children.(index - 1) with
599+
match node_leaf_values storage children.(index - 1) with
622600
| None -> None
623601
| Some left ->
624602
let combined_length = Array.length left + Array.length values in
@@ -644,8 +622,8 @@ let rebalance_leaf_child mode settings keys children index child =
644622
| Some changed -> Some changed
645623
| None -> rebalance_with_left ())
646624

647-
let rebalance_branch_child mode settings keys children index child =
648-
match node_branch_parts mode child with
625+
let rebalance_branch_child storage settings keys children index child =
626+
match node_branch_parts storage child with
649627
| None -> None
650628
| Some (child_keys, child_children) -> (
651629
let minimum = min_child_occupancy settings in
@@ -654,7 +632,7 @@ let rebalance_branch_child mode settings keys children index child =
654632
let rebalance_with_right () =
655633
if index + 1 >= Array.length children then None
656634
else
657-
match node_branch_parts mode children.(index + 1) with
635+
match node_branch_parts storage children.(index + 1) with
658636
| None -> None
659637
| Some (right_keys, right_children) ->
660638
let combined_length =
@@ -697,7 +675,7 @@ let rebalance_branch_child mode settings keys children index child =
697675
let rebalance_with_left () =
698676
if index = 0 then None
699677
else
700-
match node_branch_parts mode children.(index - 1) with
678+
match node_branch_parts storage children.(index - 1) with
701679
| None -> None
702680
| Some (left_keys, left_children) ->
703681
let combined_length =
@@ -742,24 +720,21 @@ let rec add_to_address storage settings order_cmp equality_cmp key_cmp value
742720
address =
743721
match restore_stored_node storage address with
744722
| Leaf values ->
745-
add_to_node (Stored_tree storage) settings order_cmp equality_cmp key_cmp
746-
value
723+
add_to_node (Some storage) settings order_cmp equality_cmp key_cmp value
747724
(Node.Leaf
748725
{
749726
values = Array.of_list values;
750727
len = List.length values;
751728
address = Some address;
752729
})
753730
| Branch (keys, child_addresses) ->
754-
add_to_node (Stored_tree storage) settings order_cmp equality_cmp key_cmp
755-
value
731+
add_to_node (Some storage) settings order_cmp equality_cmp key_cmp value
756732
(node_of_stored_branch keys child_addresses address)
757733

758-
and add_to_node mode settings order_cmp equality_cmp key_cmp value = function
734+
and add_to_node storage settings order_cmp equality_cmp key_cmp value = function
759735
| Node.Ref _ as node ->
760-
let storage = storage_of_edit_mode mode in
761-
add_to_node mode settings order_cmp equality_cmp key_cmp value
762-
(force_ref_node storage node)
736+
add_to_node storage settings order_cmp equality_cmp key_cmp value
737+
(force_ref_node (storage_required storage) node)
763738
| Node.Leaf { values; len; _ } -> (
764739
match find_insert_index_len order_cmp equality_cmp value values len with
765740
| `Found _ -> Tree_edit_unchanged
@@ -769,7 +744,7 @@ and add_to_node mode settings order_cmp equality_cmp key_cmp value = function
769744
| Node.Branch { keys; children; _ } -> (
770745
let index = find_child_index key_cmp value keys in
771746
match
772-
add_to_node mode settings order_cmp equality_cmp key_cmp value
747+
add_to_node storage settings order_cmp equality_cmp key_cmp value
773748
children.(index)
774749
with
775750
| Tree_edit_unchanged -> Tree_edit_unchanged
@@ -785,25 +760,24 @@ let rec remove_from_address storage settings order_cmp equality_cmp key_cmp
785760
value address =
786761
match restore_stored_node storage address with
787762
| Leaf values ->
788-
remove_from_node (Stored_tree storage) settings order_cmp equality_cmp
789-
key_cmp value
763+
remove_from_node (Some storage) settings order_cmp equality_cmp key_cmp
764+
value
790765
(Node.Leaf
791766
{
792767
values = Array.of_list values;
793768
len = List.length values;
794769
address = Some address;
795770
})
796771
| Branch (keys, child_addresses) ->
797-
remove_from_node (Stored_tree storage) settings order_cmp equality_cmp
798-
key_cmp value
772+
remove_from_node (Some storage) settings order_cmp equality_cmp key_cmp
773+
value
799774
(node_of_stored_branch keys child_addresses address)
800775

801-
and remove_from_node mode settings order_cmp equality_cmp key_cmp value =
776+
and remove_from_node storage settings order_cmp equality_cmp key_cmp value =
802777
function
803778
| Node.Ref _ as node ->
804-
let storage = storage_of_edit_mode mode in
805-
remove_from_node mode settings order_cmp equality_cmp key_cmp value
806-
(force_ref_node storage node)
779+
remove_from_node storage settings order_cmp equality_cmp key_cmp value
780+
(force_ref_node (storage_required storage) node)
807781
| Node.Leaf { values; len; _ } -> (
808782
match find_remove_index_len order_cmp equality_cmp value values len with
809783
| None -> Tree_edit_unchanged
@@ -820,19 +794,20 @@ and remove_from_node mode settings order_cmp equality_cmp key_cmp value =
820794
| Node.Branch { keys; children; _ } -> (
821795
let index = find_child_index key_cmp value keys in
822796
match
823-
remove_from_node mode settings order_cmp equality_cmp key_cmp value
797+
remove_from_node storage settings order_cmp equality_cmp key_cmp value
824798
children.(index)
825799
with
826800
| Tree_edit_unchanged -> Tree_edit_unchanged
827801
| Tree_edit_changed [ (key, child) ] ->
828802
let changed =
829803
match
830-
rebalance_leaf_child mode settings keys children index child
804+
rebalance_leaf_child storage settings keys children index child
831805
with
832806
| Some changed -> changed
833807
| None -> (
834808
match
835-
rebalance_branch_child mode settings keys children index child
809+
rebalance_branch_child storage settings keys children index
810+
child
836811
with
837812
| Some changed -> changed
838813
| None ->
@@ -866,9 +841,8 @@ let add value set =
866841
)
867842
| Tree root -> (
868843
match
869-
add_to_node
870-
(edit_mode_of_storage set.set_storage)
871-
set.set_settings set.cmp equality_cmp key_cmp value root
844+
add_to_node set.set_storage set.set_settings set.cmp equality_cmp
845+
key_cmp value root
872846
with
873847
| Tree_edit_unchanged -> set
874848
| Tree_edit_changed changed ->
@@ -960,9 +934,8 @@ let remove value set =
960934
)
961935
| Tree root -> (
962936
match
963-
remove_from_node
964-
(edit_mode_of_storage set.set_storage)
965-
set.set_settings set.cmp equality_cmp key_cmp value root
937+
remove_from_node set.set_storage set.set_settings set.cmp equality_cmp
938+
key_cmp value root
966939
with
967940
| Tree_edit_unchanged -> set
968941
| Tree_edit_changed changed ->
@@ -1104,7 +1077,6 @@ let validate_invariants set =
11041077
type search_step = Found | Stop | Continue
11051078

11061079
let rec search_deferred storage order_cmp equality_cmp value address =
1107-
storage.accessed address;
11081080
match storage.restore_node address with
11091081
| Some (Leaf values) ->
11101082
let values = Array.of_list values in
@@ -1163,7 +1135,6 @@ let mem value set =
11631135
set.cmp set.cmp value address
11641136

11651137
let rec fold_address storage f init address =
1166-
storage.accessed address;
11671138
match storage.restore_node address with
11681139
| Some (Leaf values) -> List.fold_left f init values
11691140
| Some (Branch (_, child_addresses)) ->
@@ -1265,7 +1236,6 @@ let reverse_slice_array_into_len cmp from_ to_ values len acc =
12651236
loop acc (len - 1)
12661237

12671238
let rec slice_deferred_into storage cmp from_ to_ address acc =
1268-
storage.accessed address;
12691239
match storage.restore_node address with
12701240
| Some (Leaf values) ->
12711241
List.rev_append (slice_values cmp from_ to_ values) acc
@@ -1291,7 +1261,6 @@ let slice_deferred storage cmp from_ to_ address =
12911261
List.rev (slice_deferred_into storage cmp from_ to_ address [])
12921262

12931263
let rec reverse_slice_deferred_into storage cmp from_ to_ address acc =
1294-
storage.accessed address;
12951264
match storage.restore_node address with
12961265
| Some (Leaf values) ->
12971266
List.rev_append (reverse_slice_values cmp from_ to_ values) acc
@@ -1463,7 +1432,6 @@ let cursor_children_of_nodes children =
14631432
Array.map (fun child -> Cursor_node child) children
14641433

14651434
let restore_cursor_child storage address =
1466-
storage.accessed address;
14671435
match storage.restore_node address with
14681436
| Some (Leaf values) ->
14691437
let values = Array.of_list values in

0 commit comments

Comments
 (0)