FFI calls: is it possible to allocate a small memory block on a stack?

Denys Rtveliashvili rtvd at mac.com
Sun Apr 18 05:28:24 EDT 2010

> While alloca is not as cheap as, say, C's alloca, you should find that 
> it is much quicker than C's malloc.  I'm sure there's room for 
> optimisation if it's critical for you.  There may well be low-hanging 
> fruit: take a look at the Core for alloca.

Thank you, Simon.

Indeed, there is a low-hanging fruit.

"alloca"'s type is "Storable a => (Ptr a -> IO b) -> IO b" and it is not
inlined even though the function is small. And calls to functions of
such signature are expensive (I suppose that's because of look-up into
typeclass dictionary). However, when I added an "INLINE" pragma for the
function into Foreign.Marshal.Alloc the time of execution dropped from
40 to 20 nanoseconds. I guess the same effect will take place if other
similar functions get marked with "INLINE".

Is there a reason why we do not want small FFI-related functions with
typeclass arguments be marked with "INLINE" pragma and gain a
performance improvement?
The only reason that comes to my mind is the size of code, but actually
the resulting code looks very small and neat.

With kind regards,
Denys Rtveliashvili
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20100418/6e7d3ad0/attachment.html

More information about the Glasgow-haskell-users mailing list