Skip to content

Commit

Permalink
irmin-pack: Change hash tables to kcas
Browse files Browse the repository at this point in the history
  • Loading branch information
clecat committed Feb 23, 2024
1 parent afade3d commit f26c552
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 64 deletions.
46 changes: 10 additions & 36 deletions src/irmin-pack/io/atomic_write.ml
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
open Import
include Irmin_pack.Atomic_write

module UnsafeTbl (K : Irmin.Type.S) = Hashtbl.Make (struct
type t = K.t [@@deriving irmin ~short_hash ~equal]

let hash = short_hash ?seed:None
end)

(** Safe but might be incredibly slow. *)
module Table (K : Irmin.Type.S) = struct
module Unsafe = UnsafeTbl (K)

type 'a t = { lock : Eio.Mutex.t; data : 'a Unsafe.t }

let create n =
let lock = Eio.Mutex.create () in
let data = Unsafe.create n in
{ lock; data }

let add { lock; data } k v =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.add data k v

let mem { lock; data } k =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.mem data k

let find_opt { lock; data } k =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.find_opt data k

let find t k = match find_opt t k with Some v -> v | None -> raise Not_found
module K = (struct
include K

let replace { lock; data } k v =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.replace data k v
type t = K.t [@@deriving irmin ~short_hash ~equal]

let remove { lock; data } k =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.remove data k
let hash = short_hash ?seed:None
let equal = Irmin.Type.(unstage (equal K.t))
end)

let reset { lock; data } =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.reset data
include Kcas_data.Hashtbl

let fold f { lock; data } init =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.fold f data init
let create min_buckets = create ~hashed_type:(module K) ~min_buckets ()
end

module Make_persistent (Io : Io_intf.S) (K : Irmin.Type.S) (V : Value.S) =
Expand All @@ -53,8 +27,8 @@ struct
type watch = W.watch

type t = {
index : int63 Tbl.t;
cache : V.t Tbl.t;
index : (K.t, int63) Tbl.t;
cache : (K.t, V.t) Tbl.t;
block : Io.t;
mutable block_size : int63;
w : W.t;
Expand Down
36 changes: 8 additions & 28 deletions src/irmin-pack/io/pack_store.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,17 @@ exception Dangling_hash
let invalid_read fmt = Fmt.kstr (fun s -> raise (Invalid_read s)) fmt
let corrupted_store fmt = Fmt.kstr (fun s -> raise (Corrupted_store s)) fmt

module UnsafeTbl (K : Irmin.Hash.S) = Hashtbl.Make (struct
type t = K.t

let hash = K.short_hash
let equal = Irmin.Type.(unstage (equal K.t))
end)

(** Safe but might be incredibly slow. *)
module Table (K : Irmin.Hash.S) = struct
module Unsafe = UnsafeTbl (K)

type 'a t = { lock : Eio.Mutex.t; data : 'a Unsafe.t }

let create n =
let lock = Eio.Mutex.create () in
let data = Unsafe.create n in
{ lock; data }

let add { lock; data } k v =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.add data k v

let mem { lock; data } k =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.mem data k
module K = (struct
include K

let find_opt { lock; data } k =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.find_opt data k
let hash = short_hash
let equal = Irmin.Type.(unstage (equal K.t))
end)

let find t k = match find_opt t k with Some v -> v | None -> raise Not_found
include Kcas_data.Hashtbl

let clear { lock; data } =
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.clear data
let create min_buckets = create ~hashed_type:(module K) ~min_buckets ()
end

module Make_without_close_checks
Expand Down Expand Up @@ -86,7 +66,7 @@ struct

type 'a t = {
lru : Lru.t;
staging : Val.t Tbl.t;
staging : (Hash.t, Val.t) Tbl.t;
indexing_strategy : Irmin_pack.Indexing_strategy.t;
fm : Fm.t;
dict : Dict.t;
Expand Down

0 comments on commit f26c552

Please sign in to comment.