aboutsummaryrefslogtreecommitdiff
path: root/lib/cArray.ml
diff options
context:
space:
mode:
authorppedrot2013-11-04 12:17:57 +0000
committerppedrot2013-11-04 12:17:57 +0000
commita45a7f0411f19c10f7a50a02b84c1820c5907bb0 (patch)
tree05254f5c8a7a34cc454a2b4bb160ba6bc18e1196 /lib/cArray.ml
parentb021f265da7666fa93d34ae3e42fa8bcf28f63ac (diff)
Adding closure-preventing functions in CArray. These functions are all
higher-order functions like map and iter, and they are modified so that they take one additional argument, thus saving a cloure allocation. Compare the following. Array.iter: ('a -> unit) -> 'a array -> unit Array.Fun1.iter: ('r -> 'a -> unit) -> 'r -> 'a array -> unit Basically, Array.Fun1.iter f x v = Array.iter (f x) v, though it does not allocate a closure. For now only the most critical functions are recoded. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/coq/trunk@17053 85f007b7-540e-0410-9357-904b9bb8a0f7
Diffstat (limited to 'lib/cArray.ml')
-rw-r--r--lib/cArray.ml46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/cArray.ml b/lib/cArray.ml
index 5bbc813d55..7b008b8db2 100644
--- a/lib/cArray.ml
+++ b/lib/cArray.ml
@@ -454,3 +454,49 @@ let rev_to_list a =
let filter_with filter v =
Array.of_list (CList.filter_with filter (Array.to_list v))
+module Fun1 =
+struct
+
+ let map f arg v = match v with
+ | [| |] -> [| |]
+ | _ ->
+ let len = Array.length v in
+ let x0 = Array.unsafe_get v 0 in
+ let ans = Array.make len (f arg x0) in
+ for i = 1 to pred len do
+ let x = Array.unsafe_get v i in
+ Array.unsafe_set ans i (f arg x)
+ done;
+ ans
+
+ exception Local of int * Obj.t
+
+ let smartmap f arg v =
+ let len = Array.length v in
+ try
+ for i = 0 to len - 1 do
+ let x = uget v i in
+ let y = f arg x in
+ if x != y then (* pointer (in)equality *)
+ raise (Local (i, Obj.repr y))
+ done;
+ v
+ with Local (i, x) ->
+ (** FIXME: use Array.unsafe_blit from OCAML 4.00 *)
+ let ans : 'a array = Array.make len (uget v 0) in
+ let () = Array.blit v 0 ans 0 i in
+ let () = Array.unsafe_set ans i (Obj.obj x) in
+ for j = succ i to pred len do
+ let y = f arg (uget v j) in
+ Array.unsafe_set ans j y
+ done;
+ ans
+
+ let iter f arg v =
+ let len = Array.length v in
+ for i = 0 to pred len do
+ let x = uget v i in
+ f arg x
+ done
+
+end