Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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:

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

Finally, you close the file:

    Unix.close

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 
try_finally.

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e