Skip to content

Commit f26c552

Browse files
committed
irmin-pack: Change hash tables to kcas
1 parent afade3d commit f26c552

File tree

2 files changed

+18
-64
lines changed

2 files changed

+18
-64
lines changed

src/irmin-pack/io/atomic_write.ml

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,19 @@
11
open Import
22
include Irmin_pack.Atomic_write
33

4-
module UnsafeTbl (K : Irmin.Type.S) = Hashtbl.Make (struct
5-
type t = K.t [@@deriving irmin ~short_hash ~equal]
6-
7-
let hash = short_hash ?seed:None
8-
end)
9-
10-
(** Safe but might be incredibly slow. *)
114
module Table (K : Irmin.Type.S) = struct
12-
module Unsafe = UnsafeTbl (K)
13-
14-
type 'a t = { lock : Eio.Mutex.t; data : 'a Unsafe.t }
15-
16-
let create n =
17-
let lock = Eio.Mutex.create () in
18-
let data = Unsafe.create n in
19-
{ lock; data }
20-
21-
let add { lock; data } k v =
22-
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.add data k v
23-
24-
let mem { lock; data } k =
25-
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.mem data k
26-
27-
let find_opt { lock; data } k =
28-
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.find_opt data k
29-
30-
let find t k = match find_opt t k with Some v -> v | None -> raise Not_found
5+
module K = (struct
6+
include K
317

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

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

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

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

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

5529
type t = {
56-
index : int63 Tbl.t;
57-
cache : V.t Tbl.t;
30+
index : (K.t, int63) Tbl.t;
31+
cache : (K.t, V.t) Tbl.t;
5832
block : Io.t;
5933
mutable block_size : int63;
6034
w : W.t;

src/irmin-pack/io/pack_store.ml

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,17 @@ exception Dangling_hash
2424
let invalid_read fmt = Fmt.kstr (fun s -> raise (Invalid_read s)) fmt
2525
let corrupted_store fmt = Fmt.kstr (fun s -> raise (Corrupted_store s)) fmt
2626

27-
module UnsafeTbl (K : Irmin.Hash.S) = Hashtbl.Make (struct
28-
type t = K.t
29-
30-
let hash = K.short_hash
31-
let equal = Irmin.Type.(unstage (equal K.t))
32-
end)
33-
34-
(** Safe but might be incredibly slow. *)
3527
module Table (K : Irmin.Hash.S) = struct
36-
module Unsafe = UnsafeTbl (K)
37-
38-
type 'a t = { lock : Eio.Mutex.t; data : 'a Unsafe.t }
39-
40-
let create n =
41-
let lock = Eio.Mutex.create () in
42-
let data = Unsafe.create n in
43-
{ lock; data }
44-
45-
let add { lock; data } k v =
46-
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.add data k v
47-
48-
let mem { lock; data } k =
49-
Eio.Mutex.use_rw ~protect:true lock @@ fun () -> Unsafe.mem data k
28+
module K = (struct
29+
include K
5030

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

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

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

6040
module Make_without_close_checks
@@ -86,7 +66,7 @@ struct
8666

8767
type 'a t = {
8868
lru : Lru.t;
89-
staging : Val.t Tbl.t;
69+
staging : (Hash.t, Val.t) Tbl.t;
9070
indexing_strategy : Irmin_pack.Indexing_strategy.t;
9171
fm : Fm.t;
9272
dict : Dict.t;

0 commit comments

Comments
 (0)