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: Fri, 19 May 2023 09:07:27 -0400
User-agent: Gnus/5.13 (Gnus v5.13)

> I'm trying to provide myself a tool for generic programming that
> dispatches at compile time.

I still don't understand what you mean by that.
Do you mean that the programmers write

    (my-foo a b c)

and during macroexpansion this is turned into

    (my-foo-for-thurbies a b c)

when we somehow figure out that `a` is a "thurby"?

> I've been writing too much repetitive
> code, but I'm not a fan of ad hoc macros either.  Basically, I'm
> trying to provide a C#-ish system for writing generic code
> parameterized by interface requirements.

[ "generic code" and "interface requirements" are ... generic terms with
wildly different meanings in different communities, and I'm not familiar
enough with the C# world to guess what you mean by that.  ]

>> > (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.
>
> That's the issue - if you use inline-letvals, then the code can't make
> use of constant values provided as operands.

Are you sure?  I think the real problem is that `inline-const-p` is not
a function but a macro, so you can pass it as a first-class function.
E.g.:

    (define-inline my-plus (&rest args)
      (inline-letevals args
        (if (seq-every-p (lambda (x) (inline-const-p x)) args)
            (apply #'+ (mapcar (lambda (x) (inline-const-val x)) args))
          (inline-quote (+ . ,args)))))

seems to do (more or less) what your code illustrated.

>> 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.
>
> I agree.
>
> I posted a patch to emacs-devel for define-inline-pure-subr that
> adapts the technique of define-inline.

I'm still not sure why you're not using a `compiler-macro` which seems
to be exactly what you're after.


        Stefan




reply via email to

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