Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Threads performance issue.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-02-17 (11:28)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] Re: Threads performance issue.
On Tuesday 17 February 2009 10:50:44 Rémi Dewitte wrote:
> Memory mapping of the file could be done using BigArray or do I have to
> write C code ?

You can memory map files very easily entirely from within OCaml.

This was actually covered in the OCaml Journal article about OpenGL 2, which 
used file mapping as an easy way to load texture maps. First, you open the 
file to create a file descriptor:

  try_finally (Unix.openfile file [] 777)

Then you map the file to create a big array:

    (fun desc ->
       let source = Array1.map_file desc int8_signed c_layout false (-1) in

Then you can do something with the big array, like copy it into an ordinary 

       String.init (Array1.dim source) (fun i -> Char.chr source.{i}))

Finally, you close the file:


Note that I have used try_finally and String.init functions from my own stdlib 
but their purpose and use should be obvious.

You probably just want to replace read_char with a function that increments a 
counter and reads from the array, with the whole parser inside the 

Dr Jon Harrop, Flying Frog Consultancy Ltd.