openFile and threads

Simon Marlow
Fri, 10 Jan 2003 10:51:56 -0000

> Now I'm wondering about ways to cut that down:
> 1. How can I avoid the allocations inside of openFile?
> 2. Would it help to call f6 in different threads? That is, does a
>    thread yield when it calls an IO function?

openFile is quite expensive because it normally allocates a buffer,
which I suspect accounts for most of the allocation you're seeing.  It
also sets up a finalizer, allocates the Handle, and does a couple of
extra system calls.

If the openFile isn't on the critical path, then it can help to do it in
another thread, but only if the openFile is blocking (not usually the
case if you're opening a normal file).

You might consider bypassing the Handle interface and going to the bare
metal using the Posix library, which will cut down on the overhead in

The best solution is to avoid the open altogether if possible (eg. with
caching), but I don't know about your particular application so this
might not be a possibility.