Skip to content

Commit

Permalink
refactor: generalize [Fs_cache]
Browse files Browse the repository at this point in the history
Allow the path type to vary

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>

ps-id: 883fa61f-c6d1-41fd-8102-dee06ae24a88
  • Loading branch information
rgrinberg committed Aug 25, 2022
1 parent 7ce9e8e commit ca53c15
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/dune_engine/fs_cache.ml
Expand Up @@ -4,26 +4,26 @@ open Import

(* CR-someday amokhov: Implement garbage collection. *)

type 'a t =
type ('a, 'path) t =
{ name : string (* For debugging *)
; sample : Path.t -> 'a
; cache : 'a Path.Table.t
; sample : 'path -> 'a
; cache : ('path, 'a) Table.t
; equal : 'a -> 'a -> bool (* Used to implement cutoff *)
; update_hook : Path.t -> unit (* Run this hook before updating an entry. *)
; update_hook : 'path -> unit (* Run this hook before updating an entry. *)
}

let create ?(update_hook = fun _path -> ()) name ~sample ~equal =
{ name; sample; equal; cache = Path.Table.create 128; update_hook }
{ name; sample; equal; cache = Table.create (module Path) 128; update_hook }
let read { sample; cache; _ } path =
match Path.Table.find cache path with
match Table.find cache path with
| Some cached_result -> cached_result
| None ->
let result = sample path in
Path.Table.add_exn cache path result;
Table.add_exn cache path result;
result
let evict { cache; _ } path = Path.Table.remove cache path
let evict { cache; _ } path = Table.remove cache path
module Update_result = struct
type t =
Expand All @@ -45,15 +45,15 @@ module Update_result = struct
end
let update { sample; cache; equal; update_hook; _ } path =
match Path.Table.find cache path with
match Table.find cache path with
| None -> Update_result.Skipped
| Some old_result -> (
update_hook path;
let new_result = sample path in
match equal old_result new_result with
| true -> Updated { changed = false }
| false ->
Path.Table.set cache path new_result;
Table.set cache path new_result;
Updated { changed = true })
module Reduced_stats = struct
Expand Down
17 changes: 9 additions & 8 deletions src/dune_engine/fs_cache.mli
Expand Up @@ -5,15 +5,16 @@ open Import
Currently we do not expose a way to construct such cached operations; see
the [Untracked] module for a few predefined ones. *)
type 'a t
type ('a, 'path) t

(** If the cache contains the result of applying an operation to a path, return
it. Otherwise, perform the operation, store the result in the cache, and
then return it. *)
val read : 'a t -> Path.t -> 'a
val read : ('a, 'p) t -> 'p -> 'a

(** Evict an entry from the cache. *)
val evict : 'a t -> Path.t -> unit

val evict : ('a, 'p) t -> 'p -> unit

(** Result of updating a cache entry. *)
module Update_result : sig
Expand All @@ -30,7 +31,7 @@ module Update_result : sig
end

(** Perform an operation and update the result stored in the cache. *)
val update : 'a t -> Path.t -> Update_result.t
val update : ('a, 'p) t -> 'p -> Update_result.t

(** This module caches only a subset of fields of [Unix.stats] because other
fields are currently unused.
Expand Down Expand Up @@ -67,14 +68,14 @@ end
See [fs_memo.ml] for tracked versions of these operations. *)
module Untracked : sig
val path_stat : (Reduced_stats.t, Unix_error.Detailed.t) result t
val path_stat : ((Reduced_stats.t, Unix_error.Detailed.t) result, Path.t) t

val file_digest : Cached_digest.Digest_result.t t
val file_digest : (Cached_digest.Digest_result.t, Path.t) t

val dir_contents : (Dir_contents.t, Unix_error.Detailed.t) result t
val dir_contents : ((Dir_contents.t, Unix_error.Detailed.t) result, Path.t) t
end

module Debug : sig
(** The name of a cached operation. *)
val name : 'a t -> string
val name : (_, _) t -> string
end

0 comments on commit ca53c15

Please sign in to comment.