Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Haskell inline functions passed as an argument?

Let's say I pass a small function f to map. Can Haskell inline f with map to produce a small imperative loop? If so, how does Haskell keep track of what function f really is? Can the same be done with Arrow combinators?

like image 301
yong Avatar asked Aug 29 '14 10:08

yong


People also ask

What are the disadvantages of inline?

5) Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed. 6) Inline functions might cause thrashing because inlining might increase size of the binary executable file. Thrashing in memory causes performance of computer to degrade.

What should we not use while making a function inline?

We should not use functions that are I/O bound as inline functions. When large code is used in some function, then we should avoid the inline. When recursion is used, inline function may not work properly.

Is inline deprecated?

Inline classes are a subset of value-based classes. They don't have an identity and can only hold values. The inline modifier for inline classes is deprecated.

What does ++ do Haskell?

The ++ operator is the list concatenation operator which takes two lists as operands and "combines" them into a single list.


1 Answers

If f is passed in as an argument, then no, probably not. If f is the name of a top-level function or a local function, then probably yes.

foobar f = ... map f ...
-- Probably not inlined.

foobar = ... map (\ x -> ...) ...
-- Probably inlined.

That said, I gather that most of the performance difference between inline and out of line comes not from the actual inlining itself, but rather from any additional subsequent optimisations this might allow.

The only way to be "sure" about these things is to actually write the code, actually compile it, and have a look at the Core that gets generated. And the only way to know if it makes a difference (positive or negative) is to actually benchmark the thing.

like image 132
MathematicalOrchid Avatar answered Sep 28 '22 03:09

MathematicalOrchid