openFile and threads

Peter Thiemann thiemann@informatik.uni-freiburg.de
09 Jan 2003 12:50:32 -0800


Folks,

here is the piece of code that takes most of the time in a program I
have:

      f6 = {-# SCC "f6" #-}\gumd ->
	    let fileName = usermetadir ++ gumd in
                catch (do h <- {-# SCC "f6.1" #-} openFile fileName ReadMode
		          str <- {-# SCC "f6.2" #-} hGetLine h
		          _ <- {-# SCC "f6.2a" #-} hClose h
		          return $ {-# SCC "f6.3" #-} words str)
		      (const $ return [])

Profiling yields this output:
                                                      individual    inherited
COST CENTRE              MODULE      no.    entries  %time %alloc   %time %alloc
      f6                 MailStore  346         577   0.0    3.5    87.5   85.1
       f6.3              MailStore  351           0   0.0    9.9     0.0    9.9
       f6.2a             MailStore  350           0   0.0    0.7     0.0    0.7
       f6.2              MailStore  349           0   0.0    7.5     0.0    7.5
       f6.1              MailStore  347           0  87.5   63.5    87.5   63.5

If I read this correctly, openFile performs 63.5% of all allocations
and takes 87.5% of the runtime.

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?

Any help appreciated.

-Peter