I have a module which collects and exports a bunch of small functions such as:
fromEither :: (MonadError e m) => Either e a -> m a
fromEither = either throwError return
or
withError :: MonadError e m => (e -> e) -> m a -> m a
withError = flip catchError (throwError . f)
It's a good idea to have them inlined, as after specializing to a particular instance of MonadError
it's likely a lot of code will be optimized away.
The GHC's documentation says:
GHC (with
-O
, as always) tries to inline (or “unfold”) functions/values that are “small enough,” thus avoiding the call overhead and possibly exposing other more-wonderful optimisations. Normally, if GHC decides a function is “too expensive” to inline, it will not do so, nor will it export that unfolding for other modules to use.
Does it mean that such functions are most likely to be treated as if they had an INLINE
pragma (i.e. their non-optimised RHS recorded in the interface file)? Or do I have to add INLINE
myself? Does adding it change anything (assuming GHC decides they're "small enough")?
I don't mind if GHC decides not to inline some of my functions, if it feels they're too expensive, but in general I'd like to have them inlined without polluting my source code with adding INLINE
everywhere.