unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Brent Pappas <pappasbrent@Knights.ucf.edu>
Cc: emacs-devel@gnu.org
Subject: Re: Thoughts on replacing macros with static inline functions
Date: Tue, 15 Nov 2022 14:45:19 +0200	[thread overview]
Message-ID: <83r0y4idy8.fsf@gnu.org> (raw)
In-Reply-To: <BN7PR07MB41955BD8B1509EFE774D3961F6059@BN7PR07MB4195.namprd07.prod.outlook.com> (message from Brent Pappas on Mon, 14 Nov 2022 18:05:15 +0000)

> 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	other threads:[~2022-11-15 12:45 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-14 18:05 Thoughts on replacing macros with static inline functions Brent Pappas
2022-11-15 12:45 ` Eli Zaretskii [this message]
2022-11-18  5:05   ` Richard Stallman
2022-11-18  6:19     ` xenodasein--- via Emacs development discussions.
2022-11-18  6:27       ` Po Lu
2022-11-18  6:32         ` xenodasein--- via Emacs development discussions.
2022-11-18  7:29           ` Po Lu
2022-11-18  8:34         ` Eli Zaretskii
2022-11-18 11:31           ` xenodasein--- via Emacs development discussions.
2022-11-18 11:43             ` Po Lu
2022-11-18 12:00             ` Eli Zaretskii
2022-11-18  6:59       ` tomas
2022-11-18  7:12         ` xenodasein--- via Emacs development discussions.
2022-11-18 14:53       ` Stefan Monnier
2022-11-18 16:16         ` Dr. Arne Babenhauserheide
2022-11-15 13:49 ` Po Lu
2022-11-15 15:54   ` xenodasein--- via Emacs development discussions.
2022-11-15 14:10 ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=83r0y4idy8.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=pappasbrent@Knights.ucf.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).