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

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

Re: Lexical binding doesn't seem to be faster?


From: Stefan Monnier
Subject: Re: Lexical binding doesn't seem to be faster?
Date: Fri, 08 Mar 2019 08:53:32 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

>> FWIW, the usual case where lexical-binding leads to bigger&slower code
>> is when you pass a lambda-expression to another function and that
>> lambda-expression has free variables (in the lexical-binding case, it
>> requires building a closure which is rather costly currently, whereas in
>> the dynamic-binding case it relies on the dynamic-scoping instead).
> Let's see if I got that right: in those cases, the code would (most
> probably!) behave differently depending on the binding regime (lexical
> vs dynamic), so it would be doing quite different things in each case?

Actually, in most such cases the code behaves identically in the end,
but it gets there in a different way (so yes, in some cases it does
behave differently).

E.g.

    (let ((buf (current-buffer)))
      [...]
      (with-temp-buffer
        (mapcar (lambda (x)
                  (with-current-buffer buf ...))
                ...))
      [...])
      
The end result will most likely be the same regardless which binding
style is used, but the way to find the value of `buf` from within the
lambda is different in the two cases.

The difference becomes apparent if you do

    (advice-add 'mapcar :around
                (lambda (&rest orig-call)
                  (let ((buf 42)) (apply orig-call))))
                  
since in this case dynamic-binding will cause your `with-current-buffer`
to try and use buffer 42 and signal an error (because of the name
conflict, aka "variable capture"), whereas lexical-binding
will be unaffected.


        Stefan




reply via email to

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