updated caches to handle exceptions

This commit is contained in:
Simon Cruanes 2013-11-20 12:33:59 +01:00
parent 20ac91f532
commit c829d1f7fe
2 changed files with 65 additions and 31 deletions

View file

@ -47,11 +47,16 @@ module type S = sig
(** Clear content of the cache *) (** Clear content of the cache *)
val with_cache : 'a t -> (key -> 'a) -> key -> 'a val with_cache : 'a t -> (key -> 'a) -> key -> 'a
(** Wrap the function with the cache *) (** Wrap the function with the cache. This means that
[with_cache cache f x] always returns the same value as
[f x], if [f x] returns, or raise the same exception.
However, [f] may not be called if [x] is in the cache. *)
val with_cache_rec : 'a t -> ((key -> 'a) -> key -> 'a) -> key -> 'a val with_cache_rec : 'a t -> ((key -> 'a) -> key -> 'a) -> key -> 'a
(** Partially apply the given function with a cached version of itself. (** Partially apply the given function with a cached version of itself.
It returns the specialized function. *) It returns the specialized function.
[with_cache_rec cache f] applies [f] to a cached version of [f],
called [f'], so that [f' x = f f' x]. *)
end end
(** Signature of a cache for pairs of values *) (** Signature of a cache for pairs of values *)
@ -106,7 +111,7 @@ end
module Linear(X : EQ) = struct module Linear(X : EQ) = struct
type 'a t = 'a bucket array type 'a t = 'a bucket array
and 'a bucket = Empty | Pair of key * 'a and 'a bucket = Empty | Pair of key * 'a | PairRaise of key * exn
and key = X.t and key = X.t
let create size = let create size =
@ -116,12 +121,12 @@ module Linear(X : EQ) = struct
let clear cache = let clear cache =
Array.fill cache 0 (Array.length cache) Empty Array.fill cache 0 (Array.length cache) Empty
(** Insert the binding (x -> y) into the cache *) (** Insert the bucket [b] into the cache *)
let insert cache x y = let insert cache b =
let n = Array.length cache in let n = Array.length cache in
(* shift other values toward the end *) (* shift other values toward the end *)
Array.blit cache 0 cache 1 (n-1); Array.blit cache 0 cache 1 (n-1);
cache.(0) <- Pair(x,y) cache.(0) <- b
(** Try to find [f x] in the cache, otherwise compute it (** Try to find [f x] in the cache, otherwise compute it
and cache the result *) and cache the result *)
@ -131,12 +136,17 @@ module Linear(X : EQ) = struct
(* function that performs the lookup *) (* function that performs the lookup *)
if i = n then begin if i = n then begin
(* cache miss *) (* cache miss *)
let y = f x in try
insert cache x y; let y = f x in
y insert cache (Pair (x, y));
y
with e ->
insert cache (PairRaise (x, e));
raise e
end else match cache.(i) with end else match cache.(i) with
| Pair (x',y) when X.equal x x' -> y | Pair (x',y) when X.equal x x' -> y
| Empty | Pair _ -> search (i+1) | PairRaise (x', e) when X.equal x x' -> raise e
| _ -> search (i+1)
in in
search 0 search 0
@ -148,7 +158,7 @@ end
module Linear2(X : EQ)(Y : EQ) = struct module Linear2(X : EQ)(Y : EQ) = struct
type 'a t = 'a bucket array type 'a t = 'a bucket array
and 'a bucket = Empty | Assoc of key1 * key2 * 'a and 'a bucket = Empty | Assoc of key1 * key2 * 'a | AssocRaise of key1 * key2 * exn
and key1 = X.t and key1 = X.t
and key2 = Y.t and key2 = Y.t
@ -159,12 +169,12 @@ module Linear2(X : EQ)(Y : EQ) = struct
let clear cache = let clear cache =
Array.fill cache 0 (Array.length cache) Empty Array.fill cache 0 (Array.length cache) Empty
(** Insert the binding (x -> y) into the cache *) (** Insert the binding [b] into the cache *)
let insert cache x1 x2 y = let insert cache b =
let n = Array.length cache in let n = Array.length cache in
(* shift other values toward the end *) (* shift other values toward the end *)
Array.blit cache 0 cache 1 (n-1); Array.blit cache 0 cache 1 (n-1);
cache.(0) <- Assoc(x1,x2,y) cache.(0) <- b
(** Try to find [f x] in the cache, otherwise compute it (** Try to find [f x] in the cache, otherwise compute it
and cache the result *) and cache the result *)
@ -174,12 +184,17 @@ module Linear2(X : EQ)(Y : EQ) = struct
(* function that performs the lookup *) (* function that performs the lookup *)
if i = n then begin if i = n then begin
(* cache miss *) (* cache miss *)
let y = f x1 x2 in try
insert cache x1 x2 y; let y = f x1 x2 in
y insert cache (Assoc (x1, x2, y));
y
with e ->
insert cache (AssocRaise (x1, x2, e));
raise e
end else match cache.(i) with end else match cache.(i) with
| Assoc (x1',x2',y) when X.equal x1 x1' && Y.equal x2 x2' -> y | Assoc (x1',x2',y) when X.equal x1 x1' && Y.equal x2 x2' -> y
| Empty | Assoc _ -> search (i+1) | AssocRaise (x1',x2',e) when X.equal x1 x1' && Y.equal x2 x2' -> raise e
| _ -> search (i+1)
in in
search 0 search 0
end end
@ -196,7 +211,7 @@ module Replacing(X : HASH) = struct
The first slot in the array contains the function The first slot in the array contains the function
used to produce the value upon a cache miss. *) used to produce the value upon a cache miss. *)
type 'a t = 'a bucket array type 'a t = 'a bucket array
and 'a bucket = Empty | Assoc of key * 'a and 'a bucket = Empty | Assoc of key * 'a | AssocRaise of key * exn
let create size = let create size =
Array.create size Empty Array.create size Empty
@ -211,10 +226,16 @@ module Replacing(X : HASH) = struct
match c.(i) with match c.(i) with
| Assoc (x', y) when X.equal x x' -> | Assoc (x', y) when X.equal x x' ->
y (* cache hit *) y (* cache hit *)
| Assoc _ | Empty -> (* cache miss *) | AssocRaise (x', e) when X.equal x x' ->
let y = f x in raise e (* cache hit *)
c.(i) <- Assoc (x, y); | _ -> (* cache miss *)
y try
let y = f x in
c.(i) <- Assoc (x, y);
y
with e ->
c.(i) <- AssocRaise (x, e);
raise e
let with_cache_rec cache f x = let with_cache_rec cache f x =
(* make a recursive version of [f] that uses the cache *) (* make a recursive version of [f] that uses the cache *)
@ -230,11 +251,10 @@ module Replacing2(X : HASH)(Y : HASH) = struct
The first slot in the array contains the function The first slot in the array contains the function
used to produce the value upon a cache miss. *) used to produce the value upon a cache miss. *)
type 'a t = 'a bucket array type 'a t = 'a bucket array
and 'a bucket = Empty | Assoc of key1 * key2 * 'a and 'a bucket = Empty | Assoc of key1 * key2 * 'a | AssocRaise of key1 * key2 * exn
and key1 = X.t and key1 = X.t
and key2 = Y.t and key2 = Y.t
let create size = let create size =
Array.create size Empty Array.create size Empty
@ -246,14 +266,22 @@ module Replacing2(X : HASH)(Y : HASH) = struct
match c.(i) with match c.(i) with
| Assoc (x1', x2', y) when X.equal x1 x1' && Y.equal x2 x2' -> | Assoc (x1', x2', y) when X.equal x1 x1' && Y.equal x2 x2' ->
y (* cache hit *) y (* cache hit *)
| Assoc _ | Empty -> (* cache miss *) | AssocRaise (x1', x2', e) when X.equal x1 x1' && Y.equal x2 x2' ->
let y = f x1 x2 in raise e (* cache hit *)
c.(i) <- Assoc (x1, x2, y); | _ -> (* cache miss *)
y try
let y = f x1 x2 in
c.(i) <- Assoc (x1, x2, y);
y
with e ->
c.(i) <- AssocRaise (x1, x2, e);
raise e
end end
(** {2 Hashtables with Least Recently Used eviction policy *) (** {2 Hashtables with Least Recently Used eviction policy *)
(* TODO: handle exceptions *)
module LRU(X : HASH) = struct module LRU(X : HASH) = struct
type key = X.t type key = X.t

View file

@ -49,11 +49,16 @@ module type S = sig
(** Clear content of the cache *) (** Clear content of the cache *)
val with_cache : 'a t -> (key -> 'a) -> key -> 'a val with_cache : 'a t -> (key -> 'a) -> key -> 'a
(** Wrap the function with the cache *) (** Wrap the function with the cache. This means that
[with_cache cache f x] always returns the same value as
[f x], if [f x] returns, or raise the same exception.
However, [f] may not be called if [x] is in the cache. *)
val with_cache_rec : 'a t -> ((key -> 'a) -> key -> 'a) -> key -> 'a val with_cache_rec : 'a t -> ((key -> 'a) -> key -> 'a) -> key -> 'a
(** Partially apply the given function with a cached version of itself. (** Partially apply the given function with a cached version of itself.
It returns the specialized function. *) It returns the specialized function.
[with_cache_rec cache f] applies [f] to a cached version of [f],
called [f'], so that [f' x = f f' x]. *)
end end
(** Signature of a cache for pairs of values *) (** Signature of a cache for pairs of values *)
@ -97,5 +102,6 @@ module Replacing2(X : HASH)(Y : HASH) : S2 with type key1 = X.t and type key2 =
module LRU(X : HASH) : S with type key = X.t module LRU(X : HASH) : S with type key = X.t
(* TODO exception handling in LRU *)
(* TODO LRU2 *) (* TODO LRU2 *)