
However, if you have any suggestions about how to make a FAST global counter I would be very glad to hear it. From profiling it seems like this code is a little expensive (also it is called quite frequently).
You could try the FastMutInt module from GHC (ghc/compiler/utils/FastMutInt.hs) to speed things up. Unfortunately unsafePerformIO has some unavoidable overhead: it can't be inlined because we don't want the compiler to see its definition.
What happens if you use the FFI to call a C function like int getCount() { static int x; return x++; } and mark the function pure (outside the IO monad) and noinline? (Probably all the calls get commoned up and it only gets called once; but it might be worth a try). You mentioned that you're trying to get a new counter value for every function application; maybe something like an FFI call to int getCount(void *f, void *a) { static int x; return x++; } where you have getCount :: (a -> b) -> a -> Int; then you pass the function and its argument to getCount. This should prevent any unwanted common subexpression elimination. Carl Witty