Skip to content

Commit

Permalink
cache find_file in class path results to speedup results especially w…
Browse files Browse the repository at this point in the history
…hen a lot of class paths are involved
  • Loading branch information
ncannasse committed Mar 17, 2014
1 parent 864fbde commit 75f316a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
41 changes: 30 additions & 11 deletions common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ type context = {
mutable print : string -> unit;
mutable get_macros : unit -> context option;
mutable run_command : string -> int;
file_lookup_cache : (string,string option) Hashtbl.t;
(* output *)
mutable file : string;
mutable flash_version : float;
Expand Down Expand Up @@ -687,6 +688,7 @@ let create v args =
tstring = m;
tarray = (fun _ -> assert false);
};
file_lookup_cache = Hashtbl.create 0;
memory_marker = memory_marker;
}

Expand All @@ -695,7 +697,12 @@ let log com str =

let clone com =
let t = com.basic in
{ com with basic = { t with tvoid = t.tvoid }; main_class = None; features = Hashtbl.create 0; }
{ com with
basic = { t with tvoid = t.tvoid };
main_class = None;
features = Hashtbl.create 0;
file_lookup_cache = Hashtbl.create 0;
}

let file_time file =
try (Unix.stat file).Unix.st_mtime with _ -> 0.
Expand Down Expand Up @@ -862,16 +869,28 @@ let add_final_filter ctx f =
ctx.final_filters <- f :: ctx.final_filters

let find_file ctx f =
let rec loop = function
| [] -> raise Not_found
| p :: l ->
let file = p ^ f in
if Sys.file_exists file then
file
else
loop l
in
loop ctx.class_path
try
(match Hashtbl.find ctx.file_lookup_cache f with
| None -> raise Exit
| Some f -> f)
with Exit ->
raise Not_found
| Not_found ->
let rec loop = function
| [] -> raise Not_found
| p :: l ->
let file = p ^ f in
if Sys.file_exists file then
file
else
loop l
in
let r = (try Some (loop ctx.class_path) with Not_found -> None) in
Hashtbl.add ctx.file_lookup_cache f r;
(match r with
| None -> raise Not_found
| Some f -> f)


let get_full_path f = try Extc.get_full_path f with _ -> f

Expand Down
1 change: 1 addition & 0 deletions interp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2469,6 +2469,7 @@ let macro_lib =
| VString cp ->
let com = ccom() in
com.class_path <- (Common.normalize_path cp) :: com.class_path;
Hashtbl.clear com.file_lookup_cache;
VNull
| _ ->
error()
Expand Down

0 comments on commit 75f316a

Please sign in to comment.