help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: inline function expansion


From: Stefan Monnier
Subject: Re: inline function expansion
Date: Thu, 18 May 2023 14:29:18 -0400
User-agent: Gnus/5.13 (Gnus v5.13)

[ Sorry, this also feel through.  ]

>> Isn't the idea of inlining that the behaviour/effect of invoking a
>> function shouldn't change, just that the resulting code might be more
>> efficient?

Indeed, but `define-inline` is a tool that lets you define the inliner
at the same time as the non-inlined definition, and it's up to the user
of `define-inline` to make sure the inliner gives the right result (tho
`define-inline` tries to make it easier to DTRT).

> I'd like to define inline- variants of pure subrs, e.g. arithmetic
> operators, type-predicates, that evaluate during macroexpansion rather
> than involving the compiler's optimization phase.

`define-inline` is definitely not meant for that use-case, since it's
designed to have a single definition that does both:
- define the inliner.
- define the non-inlined version of the code.

In your case, the non-inlined version is written elsewhere :-(

Instead, you want to use a "compiler macro".

Moving optimizations to the macroexpansion phase can have many benefits,
indeed (e.g. it lets those optimization affect the subsequent closure
conversion), but also downsides (you may get more warnings about chunks
of code over which you have no/little control, like "unused variable"
warnings in places where the vars is "used" by code that's optimized
away).

> I think it will be easier to ensure compile-time generic methods
> (e.g. a macro that simply calls a generic method) will (or at least
> can) dispatch during macro-expansion, without involving the
> byte-compiler's optimization phase.

I don't understand this, sorry.

> (define-inline inline-+ (&rest args)
>    (if (seq-every-p #'inline-const-p args)
>        (apply (eval-when-compile (symbol-function '+)) args)
>     (inline-quote (,(eval-when-compile (symbol-function '+)) . ,args))))

IIRC you definitely need an `inline-letvals` somewhere here.
But also this will define the non-inlined version as something along the
lines of:

    (defun inline-+ (&rest args)
       (if (seq-every-p ... args)
           (apply '+ args)
        (apply (symbol-function '+) args)))

which is much too slow IMO.


        Stefan




reply via email to

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