Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: Size of arrays
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <Pierre.Weis@i...>
Subject: Re: Size of arrays
> I've noticed that OCaml imposes a very small upper limit in the size of
> arrays: 8Mb or so, I think. Can this limit be modified somehow?
[...]

No there is no way to modify this limit: it is wired into the data
structures representation algorithm. However, this limit is machine
dependant, and very high on a 64bits machine
(18014398509481983).

Anyway, it is not difficult to design an ADT that gives you large
arrays on a 32 bits machine:

(* Module Bvect of big vectors (length up-to max_int).
   Access and assigment 2 times slowlier than regular arrays.
   Iteration is as efficient as Array.iter.  *)
(* bvect.mli *)
type 'a bvect;;
val make : int -> 'a -> 'a bvect;;
val length : 'a bvect -> int;;
val item : 'a bvect -> int -> 'a;;
val assign : 'a bvect -> int -> 'a -> unit;;
....

(* bvect.ml *)
type 'a bvect = 'a array * 'a array;;

let make len init =
 if len <= Sys.max_array_length then (Array.make len init, [| |])
 else (Array.make Sys.max_array_length init,
       Array.make (len - Sys.max_array_length) init);;

let length (v1, v2) = Array.length v1 + Array.length v2;;

let item (v1, v2) i =
 if i < Sys.max_array_length then v1.(i)
 else v2.(i - Sys.max_array_length);;

let assign (v1, v2) i item =
 if i < Sys.max_array_length then v1.(i) <- item
 else v2.(i - Sys.max_array_length) <- item;;

let iter f (v1, v2) = Array.iter f v1; Array.iter f v2;;

(* Omitted, sub_vect, map_vect, ..., as in module Array. *)

Hope this helps,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/