From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: 15294@debbugs.gnu.org
Subject: bug#15294: 24.3.50; js2-mode parser is several times slower in lexical-binding mode
Date: Sat, 14 Sep 2013 10:27:17 -0400 [thread overview]
Message-ID: <jwvwqmjld1r.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <5233E40D.4000102@yandex.ru> (Dmitry Gutov's message of "Sat, 14 Sep 2013 07:20:29 +0300")
>> It seems the slowdown is indeed linked to the way `catch' is handled
>> (indeed, this non-idiomatic ELisp code ends up byte-compiled in a really
>> poor way).
> What's non-idiomatic about this use of `catch'?
The non-idiomatic part is the "one big let on top, with lots of setq
inside". It's clearly C code in Elisp syntax.
> It does not make much of a difference in the interpreted mode.
The interpreted performance is affected by completely different factors.
My guess for the interpreted case is that there are simply "too many"
local variables: the environment is represented by a simple alist, so
variable lookup time is proportional to the number of local variables.
That fine when there are 5 local variables, but is inefficient when you
have 100 (better would be a balanced tree or maybe a hash table).
This said, I'm not terribly concerned about it: if you need it to go
fast, you should byte-compile the code. And I hope we will be able to
get rid of the interpreter in some distantish future.
> Now that we have eager macro-expansion, I was rather happy that interpreted
> js2-mode performance is only like 2x worse than when compiled.
Eager macro-expansion indeed speeds up interpreted code, even though the
intention was rather to get one-step closer to the elimination
of interpretation.
> But 2.6 vs 2.1, it still a noticeable regression. Do you suppose the usage
> of `setq' is the main contributor?
The problem goes as follows:
1- Because of how the `catch' byte-code works, for a (catch TAG BODY)
where BODY refers to some surrounding lexical variables LVARS, the
byte-compiler needs to turn the code into something similar to:
(let ((body-fun (make-closure LVARS () BODY)))
(catch TAG (funcall body-fun)))
2- When a lexical variable is both
a- caught in a closure
b- not immutable
the byte-compiler can't store this variable in the bytecode stack
(since the closure can't refer to the bytecode stack directly, but
instead stores *copies* of the elements it needs), so it needs to
change code like
(let ((lvar VAL1))
...
(setq lvar VAL2)
...(lambda () ..lvar..)...)
into
(let ((lvar (list VAL1)))
...
(setcar lvar VAL2)
...(lambda () ..(car lvar)..)...)
So if you look at js2-get-token, you'll see that the code does not
directly use any closure, but the use of `catch' ends up putting most of
the body into various closures. And since all variables are declared
outside of the catch but used inside, and they're all modified by
`setq', they all end up converted as above, so that every use of such
a variable turns into "get the cons cell from the environment, then
apply car to it".
By moving the let inside the catch, some of those variables end up not
being caught by a closure any more, so they don't need to be converted
to cons cells, hence the reduction from 5s down to 2.6s.
> (*) Would you take a look at it, too? It has quite a few changes in
> js2-get-token' and related functions.
> They also make performing the same change as in your patch more
> difficult, since I'm actually using the value returned by `catch'
> before returning from the function.
That's not a problem. The rule to follow is simply: sink the `let'
bindings closer to their use. You don't need to `let' bind all those
vars together in one big `let': you can split this let into various
`let's which you can then move deeper into the code. In some cases
you'll find that some of those vars don't even need to be `setq'd any
more.
Note that such a "scope-reduction" can also be done in C and in many
cases it's also a good idea to do it in C, tho the impact on performance
is much less significant because C doesn't have closures.
>> the mere existence of a single `setq' on a variable can sometimes
>> slow other chunks of code: in many cases `let' is cheaper than `setq').
> I see. Does this also extend to `setf' and its defstruct-related
> functionality?
It has to do specifically with `setq' (i.e. modification of plain
variables): when `setf' expands to `setq', `setf' is impacted,
otherwise no.
Stefan
next prev parent reply other threads:[~2013-09-14 14:27 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-06 20:59 bug#15294: 24.3.50; js2-mode parser is several times slower in lexical-binding mode Dmitry Gutov
2013-09-06 23:44 ` Xue Fuqiao
2013-09-07 3:15 ` Stefan Monnier
2013-09-08 22:32 ` Stefan Monnier
2013-09-10 2:04 ` Stefan Monnier
2013-09-13 3:40 ` Stefan Monnier
2013-09-13 3:59 ` Drew Adams
2013-09-13 4:37 ` Stefan Monnier
2013-09-13 5:45 ` Drew Adams
2013-09-13 13:01 ` Stefan Monnier
2013-09-14 0:09 ` Lexical let and setq Michael Welsh Duggan
2013-09-14 3:46 ` Stefan Monnier
2013-09-14 11:13 ` Lars Magne Ingebrigtsen
2013-09-14 14:04 ` Pascal J. Bourguignon
2013-09-15 5:11 ` Stefan Monnier
2013-09-14 21:47 ` Richard Stallman
2013-09-15 5:09 ` Stefan Monnier
2013-09-15 16:54 ` Richard Stallman
2013-09-15 17:06 ` Stefan Monnier
2013-09-16 10:47 ` Richard Stallman
2013-09-14 4:20 ` bug#15294: 24.3.50; js2-mode parser is several times slower in lexical-binding mode Dmitry Gutov
2013-09-14 14:27 ` Stefan Monnier [this message]
2013-09-15 0:11 ` Dmitry Gutov
2013-09-15 5:04 ` Stefan Monnier
2013-09-15 16:54 ` Richard Stallman
2013-09-15 0:24 ` Dmitry Gutov
2013-09-15 5:06 ` Stefan Monnier
2013-09-18 23:48 ` Stefan Monnier
2013-09-22 4:56 ` Dmitry Gutov
2013-10-03 5:00 ` Stefan Monnier
2013-10-04 2:38 ` Dmitry Gutov
2013-10-04 13:52 ` Stefan Monnier
2013-10-05 3:27 ` Dmitry Gutov
2014-12-14 12:31 ` Dmitry Gutov
2014-12-14 14:08 ` 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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwvwqmjld1r.fsf-monnier+emacs@gnu.org \
--to=monnier@iro.umontreal.ca \
--cc=15294@debbugs.gnu.org \
--cc=dgutov@yandex.ru \
/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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.