macros v. inline functions

This may be a topic worth considering at "the upcoming Perl 5 Core Hackathon".

It would be useful to have guidelines for when we should prefer to add
inline functions rather than macros.

I suspect we could be using them a lot more than we do, but I don't know
how many platforms still have insufficient capabilities to support them.

Macros suffer from lack of types on arguments, risk of multiple evaluation,
and opacity in debuggers. They also do not give compilers a choice whether
to inline them, so may lead to less efficient code.

Inline functions cannot do everything macros can, but where they can do
what is needed the only drawback I know of is that on a platform that
does not support them you'll get much less efficient code.

Hugo
0
hv
8/12/2019 9:55:31 AM
perl.perl5.porters 47809 articles. 1 followers. Follow

1 Replies
39 Views

Similar Articles

[PageSpeed] 58

On Mon, Aug 12, 2019 at 10:55:31AM +0100, hv@crypt.org wrote:
> This may be a topic worth considering at "the upcoming Perl 5 Core Hackathon".
> 
> It would be useful to have guidelines for when we should prefer to add
> inline functions rather than macros.
> 
> I suspect we could be using them a lot more than we do, but I don't know
> how many platforms still have insufficient capabilities to support them.
> 
> Macros suffer from lack of types on arguments, risk of multiple evaluation,
> and opacity in debuggers. They also do not give compilers a choice whether
> to inline them, so may lead to less efficient code.
> 
> Inline functions cannot do everything macros can, but where they can do
> what is needed the only drawback I know of is that on a platform that
> does not support them you'll get much less efficient code.

I agree.

I expect that given how deep some of our macros are, switching to
inline functions in many cases could result in smaller code with
little performance impact, simply from the compiler not inlining
functions in UNLIKELY() code.

I should probably have used an inline function for SANE_ERRSV.

Tony
0
tony
8/21/2019 1:54:00 AM
Reply: