emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Thoughts on replacing macros with static inline functions


From: Eli Zaretskii
Subject: Re: Thoughts on replacing macros with static inline functions
Date: Tue, 15 Nov 2022 14:45:19 +0200

> From: Brent Pappas <pappasbrent@Knights.ucf.edu>
> Date: Mon, 14 Nov 2022 18:05:15 +0000
> msip_labels: 
> 
> I noticed that Emacs code sometimes uses macros where a static inline function
> would appear to work equally as well.
> For instance, consider the macro PAD defined in src/xsettings.c
> 
> #define PAD(nr)    (((nr) + 3) & ~3)
> 
> I imagine this could be turned into a function like so:
> 
> static inline int PAD(int nr)    { return (((nr) + 3) & ~3); }

This is only equivalent in an optimized build, where modern compilers
usually inline such functions.  Emacs developers (yours truly
included) frequently run unoptimized builds because optimized code can
be tricky to debug.  Using functions in an unoptimized build can slow
down Emacs, especially if the macro you want to replace is used in
inner loops that are "hot spots" in Emacs.

> The reason why one would want to replace macros with functions is because
> functions are often easier to reason about than macros.
> The GNU C Preprocessor manual even has a list of pitfalls one can fall into
> when programming with macros.
> So it may be worthwhile to turn such macros into functions to prevent
> developers from accidentally falling into one of these pitfalls.
> 
> How interested would the Emacs community be in porting macros to functions?

I'd prefer not to do this en masse.  Certain specific cases could be
considered on a case by case basis.  But even here, changes like this
tend to make the code less familiar for people who have many years of
hacking Emacs under their belts, so the reason for the conversion
would have to be very convincing for me to agree.

Thanks.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]