@@ -20,25 +20,25 @@ module Make (Data : Binable.S) = struct
2020 { env : Rw .t
2121 ; db : Rw .holder
2222 ; counter : int ref
23- ; garbage : int Hash_set .t
23+ ; reusable_keys : int Queue .t
2424 (* * A list of ids that are no longer reachable from OCaml's side *)
2525 }
2626
27- (* * How big can the above hashset be before we do a cleanup *)
28- let garbage_size_limit = 512
27+ (* * How big can the queue [reusable_keys] be before we do a cleanup *)
28+ let reuse_size_limit = 512
2929
3030 let initialize path ~logger =
3131 Async.Deferred.Result. map (Disk_cache_utils. initialize_dir path ~logger )
3232 ~f: (fun path ->
3333 let env, db = Rw. create path in
34- { env; db; counter = ref 0 ; garbage = Hash_set . create (module Int ) } )
34+ { env; db; counter = ref 0 ; reusable_keys = Queue . create () } )
3535
3636 type id = { idx : int }
3737
3838 let get ({ env; db; _ } : t ) ({ idx } : id ) : Data.t =
3939 Rw. get ~env db idx |> Option. value_exn
4040
41- let put ({ env; db; counter; garbage } : t ) (x : Data.t ) : id =
41+ let put ({ env; db; counter; reusable_keys } : t ) (x : Data.t ) : id =
4242 (* TODO: we may reuse IDs by pulling them from the `garbage` hash set *)
4343 let idx = ! counter in
4444 incr counter ;
@@ -49,10 +49,10 @@ module Make (Data : Binable.S) = struct
4949 critical section. LMDB critical section then will be re-entered if
5050 it's invoked directly in a GC hook.
5151 This causes mutex double-acquiring and node freezes. *)
52- Hash_set. add garbage idx ) ;
53- if Hash_set . length garbage > = garbage_size_limit then (
54- Hash_set . iter garbage ~f: (fun to_remove -> Rw. remove ~env db to_remove) ;
55- Hash_set . clear garbage ) ;
52+ Queue. enqueue reusable_keys idx ) ;
53+ if Queue . length reusable_keys > = reuse_size_limit then (
54+ Queue . iter reusable_keys ~f: (fun to_remove -> Rw. remove ~env db to_remove) ;
55+ Queue . clear reusable_keys ) ;
5656 Rw. set ~env db idx x ;
5757 res
5858
0 commit comments