Subject: bug#52147: Eager macro-expansion is not performed in `eval-and-compile' functions

From: Stefan Monnier <[email-masked]>
Date: Sat, 15 Jan 2022 10:09:36 -0500

>>> We usually don't stress a lot about the performance of interpreted
>>> (i.e., uncompiled) code.

Indeed.

>> Well we do, a bit,
>>
>>   (info "(elisp) How Programs Do Loading")
>>
>> says that loading a file performs eager macro expansion and that does
>> speed up the resulting code.  `eval', `eval-when-compile' and
>> `eval-and-compile' don't perform eager macro expansion.  I don't know to
>> what degree this is on purpose.  But if a macro is defined later in the
>> file or in another file that has not yet been loaded this may not be
>> possible anyway.
>
> It sounds to me like things are basically working as designed.
>
> Perhaps Stefan has some comments; added to the CCs.

AFAIK the exact time of macro expansion is left underspecified
on purpose.  Whether eager-macroexpansion is used or not should be an
implementation detail.  A piece of code which relies on macro-expansion
being done lazily is a bug, AFAIC.  Usually those bugs become apparent
when byte-compiling the file (where macro-expansion is necessarily
eager), except for those cases that occur during bootstrap.

Both `eval-when-compile` and `eval-and-compile` currently defer to
`eval`, but they should arguably do the macro-expansion eagerly, indeed.

`eval` currently doesn't use eager macro-expansion because I haven't
done the work needed to check/ensure that the resulting potential
slowdown is negligible.  But I think `eval` should also eagerly
macro-expand its argument.

        Stefan