diff options
author | Wojciech Meyer <wojciech.meyer@gmail.com> | 2013-01-16 07:36:07 +0000 |
---|---|---|
committer | Wojciech Meyer <wojciech.meyer@gmail.com> | 2013-01-16 07:36:07 +0000 |
commit | 5cb2518dd78ce888c86857e7c1bb7eb444b711dd (patch) | |
tree | 35dfdc982c46a072d089abba1875f526c0187b83 | |
parent | d9fec1107d61a2c8580fbea5757233955c629d6c (diff) |
Implement memoization for expand_module to improve performance
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@13240 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
-rw-r--r-- | ocamlbuild/my_std.ml | 16 | ||||
-rw-r--r-- | ocamlbuild/ocaml_utils.ml | 23 | ||||
-rw-r--r-- | ocamlbuild/signatures.mli | 4 |
3 files changed, 32 insertions, 11 deletions
diff --git a/ocamlbuild/my_std.ml b/ocamlbuild/my_std.ml index aa25ecb13..764341b71 100644 --- a/ocamlbuild/my_std.ml +++ b/ocamlbuild/my_std.ml @@ -403,3 +403,19 @@ let memo f = with Not_found -> let res = f x in (Hashtbl.add cache x res; res) + +let memo2 f = + let cache = Hashtbl.create 103 in + fun x y -> + try Hashtbl.find cache (x,y) + with Not_found -> + let res = f x y in + (Hashtbl.add cache (x,y) res; res) + +let memo3 f = + let cache = Hashtbl.create 103 in + fun x y z -> + try Hashtbl.find cache (x,y,z) + with Not_found -> + let res = f x y z in + (Hashtbl.add cache (x,y,z) res; res) diff --git a/ocamlbuild/ocaml_utils.ml b/ocamlbuild/ocaml_utils.ml index dd74e1048..b35ad679e 100644 --- a/ocamlbuild/ocaml_utils.ml +++ b/ocamlbuild/ocaml_utils.ml @@ -65,17 +65,18 @@ let path_importance path x = end else if ignore_stdlib x then `just_try else `mandatory -let expand_module include_dirs module_name exts = - let dirname = Pathname.dirname module_name in - let basename = Pathname.basename module_name in - let module_name_cap = dirname/(String.capitalize basename) in - let module_name_uncap = dirname/(String.uncapitalize basename) in - List.fold_right begin fun include_dir -> - List.fold_right begin fun ext acc -> - include_dir/(module_name_uncap-.-ext) :: - include_dir/(module_name_cap-.-ext) :: acc - end exts - end include_dirs [] +let expand_module = + memo3 (fun include_dirs module_name exts -> + let dirname = Pathname.dirname module_name in + let basename = Pathname.basename module_name in + let module_name_cap = dirname/(String.capitalize basename) in + let module_name_uncap = dirname/(String.uncapitalize basename) in + List.fold_right begin fun include_dir -> + List.fold_right begin fun ext acc -> + include_dir/(module_name_uncap-.-ext) :: + include_dir/(module_name_cap-.-ext) :: acc + end exts + end include_dirs []) let string_list_of_file file = with_input_file file begin fun ic -> diff --git a/ocamlbuild/signatures.mli b/ocamlbuild/signatures.mli index f1b038857..42590a00d 100644 --- a/ocamlbuild/signatures.mli +++ b/ocamlbuild/signatures.mli @@ -348,6 +348,10 @@ module type MISC = sig val ( @:= ) : 'a list ref -> 'a list -> unit val memo : ('a -> 'b) -> ('a -> 'b) + + val memo2 : ('a -> 'b -> 'c) -> ('a -> 'b -> 'c) + + val memo3 : ('a -> 'b -> 'c -> 'd) -> ('a -> 'b -> 'c -> 'd) end module type OPTIONS = sig |