Skip to content

Commit d73b4e1

Browse files
committed
Add doc in lru.mli and improve the add function.
1 parent 6a3c0f4 commit d73b4e1

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

src/irmin/lru.ml

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,39 +92,30 @@ module Make (H : Hashtbl.HashedType) = struct
9292
match tl with
9393
| None -> None
9494
| Some ({ Q.value = k, v; _ } as n) ->
95-
Xt.modify ~xt t.w (fun tw -> tw - 1);
95+
Xt.decr ~xt t.w;
9696
HT.Xt.remove ~xt t.ht k;
9797
Q.detach ~xt t.q n;
9898
Some v
9999

100-
let remove ~xt t k =
101-
match HT.Xt.find_opt ~xt t.ht k with
102-
| None -> ()
103-
| Some n ->
104-
Xt.modify ~xt t.w (fun tw -> tw - 1);
105-
HT.Xt.remove ~xt t.ht k;
106-
Q.detach ~xt t.q n
107-
108-
let add t k v =
109-
let tx ~xt =
110-
let add t k v =
111-
remove ~xt t k;
112-
let n = Q.node (k, v) in
113-
Xt.modify ~xt t.w (fun tw -> tw + 1);
114-
HT.Xt.replace ~xt t.ht k n;
115-
Q.append ~xt t.q n
116-
in
117-
match t.cap with
118-
| Capped c when c = 0 -> ()
119-
| Uncapped -> add t k v
120-
| Capped c ->
121-
add t k v;
122-
if weight ~xt t > c then
123-
let _ = drop ~xt t in
124-
()
100+
let add ~xt t k v =
101+
let add t k v =
102+
let n = Q.node (k, v) in
103+
(match HT.Xt.find_opt ~xt t.ht k with
104+
| Some old -> Q.detach ~xt t.q old
105+
| None -> Xt.incr ~xt t.w);
106+
Q.append ~xt t.q n;
107+
HT.Xt.replace ~xt t.ht k n
125108
in
126-
Xt.commit { tx }
127-
109+
match t.cap with
110+
| Capped c when c = 0 -> ()
111+
| Uncapped -> add t k v
112+
| Capped c ->
113+
add t k v;
114+
if weight ~xt t > c then
115+
let _ = drop ~xt t in
116+
()
117+
118+
let add t k v = Xt.commit { tx = add t k v }
128119
let drop t = Xt.commit { tx = drop t }
129120

130121
let promote ~xt t n =

src/irmin/lru.mli

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,37 @@
1515

1616
(* Extracted from https://github.com/pqwy/lru *)
1717

18+
(** LRU cache implementation *)
1819
module Make (H : Hashtbl.HashedType) : sig
1920
type 'a t
2021

2122
val create : int -> 'a t
23+
(** [create n] returns a new LRU cache with the maximum size of [n]. If [n] is
24+
non-positive, the LRU cache is unbounded and is automatically internally
25+
resized. *)
26+
2227
val add : 'a t -> H.t -> 'a -> unit
28+
(** [add t k v] adds the binding [k -> v] to the cache [t]. If the cache is
29+
full, the least recently used element is evicted. If [k] was already
30+
bound, its previous binding is replaced by [v] and it is marked as most
31+
recently used. *)
32+
2333
val find : 'a t -> H.t -> 'a
34+
(** [find t k] returns the value associated with [k] in the cache [t], and
35+
marks [k] as most recently used. Raises [Not_found] if [k] is not bound in
36+
[t]. *)
37+
2438
val mem : 'a t -> H.t -> bool
39+
(** [mem t k] checks if [k] is bound in the cache [t], and marks [k] as most
40+
recently used if it is. *)
41+
2542
val clear : 'a t -> unit
43+
(** [clear t] removes all bindings from the cache [t]. *)
44+
2645
val iter : 'a t -> (H.t -> 'a -> unit) -> unit
46+
(** [iter t f] calls [f k v] for all bindings in the cache [t]. *)
47+
2748
val drop : 'a t -> 'a option
49+
(** [drop t] removes the least recently used binding from the cache [t] and
50+
returns its value, or [None] if the cache is empty. *)
2851
end

0 commit comments

Comments
 (0)