From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Hi-Angel Newsgroups: gmane.emacs.help Subject: Re: Lexical binding doesn't seem to be faster? Date: Fri, 8 Mar 2019 16:33:50 +0300 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="132007"; mail-complaints-to="usenet@blaine.gmane.org" Cc: help-gnu-emacs@gnu.org To: Stefan Monnier Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Mar 08 14:34:42 2019 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h2Fe4-000YDT-VI for geh-help-gnu-emacs@m.gmane.org; Fri, 08 Mar 2019 14:34:41 +0100 Original-Received: from localhost ([127.0.0.1]:43416 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h2Fe3-00040K-RB for geh-help-gnu-emacs@m.gmane.org; Fri, 08 Mar 2019 08:34:39 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:41316) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h2FdW-0003xi-EH for help-gnu-emacs@gnu.org; Fri, 08 Mar 2019 08:34:08 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h2FdU-00059a-Cf for help-gnu-emacs@gnu.org; Fri, 08 Mar 2019 08:34:05 -0500 Original-Received: from mail-it1-x12e.google.com ([2607:f8b0:4864:20::12e]:52625) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h2FdU-00052G-6N for help-gnu-emacs@gnu.org; Fri, 08 Mar 2019 08:34:04 -0500 Original-Received: by mail-it1-x12e.google.com with SMTP id g17so20792386ita.2 for ; Fri, 08 Mar 2019 05:34:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=Tce6LHr4mQnoFgDq9jNHX8ogWaFHPWegEAjbXJYVBcc=; b=BZizo2kX8lp388K+OtwYiw1f0pUrQ+UNZzlz7cki2Ath7W3APoy89sdX507oZXr/gL fvdfGljdM0iLxg4ldvV81UmD1yLWP7qdaWUMoA2gAPHbhKAqSQ9wvrmzuNGsq7yvI9mb lz+YceSeB/Y9u3sOS2aeXXwqjJXucbuVVSRhDMFYady618yzBnawjBuZLw94nSkaEUAG Z1/EKqAsqJcZJDXzQyqWKOTJReXvFy23d1ePoz26LwHTmY4wFkXP2BPLF9hzGCqqWFKw 3uNDqi2sxgSG2jLYZNCTp/ZDKLPDEDn33BbvvPz6eQvAt3odn6LWlXSCMDZfXkkUmuzf 8FyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Tce6LHr4mQnoFgDq9jNHX8ogWaFHPWegEAjbXJYVBcc=; b=UOos/q4P6+YkRSUlPDzw9bklaDy1BxO1iVgMJ8zHHxTEvFuODNKYfWNgXaDb2nQ05/ q6J/RZgMlWgHwPH+gD5DfFmMe5PvjruJiKarmTaLcTAn9UwA1oqKf5kbIu101SnyhihD fTf2HaKiMF6xbzb5l/pNhDnJ3Qs5rB5C9BruehUhEgTpuYkMeds/Tg2LbsDsgULbborx v0iD06ZBY6qbKpxUpeNku0fWFzhbzkYEhc4HDh9n+YEcqO8ZRZ64FD8zE2u2cpD8HQ4h pjA+LLci5hHFDS0zv3hEEzThKhSsgh61bXGlTaohifc+VGFThZfwhxms5ZWUe2ns2+Bb K6LA== X-Gm-Message-State: APjAAAU7TBDtoKWXcPuWIjzNeQDUsC5xyRYCgIaBnbk7Wvi8iDh7skRg 5phouCbY8nQgGuK/g55LGp74bn3AF3OHNiaPFYY= X-Google-Smtp-Source: APXvYqzgO36Nft7oooeLzPEnhVhbnOsbepFM1FLU6i7CGvVseURW+o+qIld7sKD77zlMw5bd5lmN2JGRUtKhR9OALyE= X-Received: by 2002:a24:7854:: with SMTP id p81mr8528900itc.7.1552052042025; Fri, 08 Mar 2019 05:34:02 -0800 (PST) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::12e X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:119587 Archived-At: On Fri, 8 Mar 2019 at 08:39, Stefan Monnier wrot= e: > > > I've stumbled upon a discussion about converting Emacs code to a > > faster lexical-binding and that help needed. > > While the semantics of lexical-binding does offer more opportunities for > optimization, the current Elisp implementation does not make much effort > to take advantage of it, so compiling with lexical-binding may generate > code that goes marginally faster sometimes and marginally slower at > other times simply because the code generated is a bit different, but in > my experience there's rarely much difference either way. > > > The first bad sign happened to be sizes of cc-mode.elc: > > > > ;;; -*- lexical-binding: t -*- | 217581 > > ;;; -*- lexical-binding: nil -*- | 212542 > > > > Lexical-binding bloats byte-code by 5KB. Odd. > > Indeed, in my experience lexical-binding tends to generate slightly > larger .elc files. Not sure why, actually: I never bothered > to investigate. Intuitively, I can't think of a good reason why that > should be the case, so it may be a simple performance bug. > > [ BTW, a performance problem with lexical-binding was found during > Emacs-24 which got fixed in Emacs-25, so I assume you're using > a recent enough version which is not affected by this problem. ] Yeah, it's a =E2=89=88month old version from git, so it's pretty recent. > > So, I took someone's function `with-timer` for benchmarking=C2=B9 > > Any reason why you didn't use `benchmark(-run(-compiled))`? Ah, thanks! Previously I only found `benchmark` function, but running (benchmark 10 (c-font-lock-fontify-region 0 (point-max))) results in error "void-function jit-lock-bounds". However the versions `benchmark-run` and `benchmark-run-compiled` works for= me. > > > ;;; -*- lexical-binding: t -*- | 1.174s, 1.144s, 1.177=C4=85;s, 1= .125s, 1.177s > > ;;; -*- lexical-binding: nil -*- | 1.037s, 1.061s, 1.037s, 1.037s, = 0.991s > > Hmmm... ~13% slowdown isn't terribly bad, but it's indeed > rather disappointing. It'd be nice to try and compare the profiles > in those two cases to try and see if the slowdown is just evenly spread > (which would suck) or is neatly limited to a specific spot (which would > be great). > > 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). > > So, for example, calls to `mapcar` where you pass a (lambda ...) that > refers to variables from the context tend to be a bit more expensive > (tho each iteration may be very slightly faster because the references > to those variables can be slightly cheaper, so the overall impact may > depend on the number of elements in the list). FTR: I re-benchmarked as `(benchmark-run-compiled 10 (c-font-lock-fontify-region 0 (point-max)))`, and also with `emacs -Q` to make sure that none of addons can interfere (I'm using color-identifiers which may add an overhead to fontification). For the same reason I disabled GC (locally I too have it only enabled to run at "idle time"). It's interesting that the difference almost disappeared: nil: (7.463936164 0 0.0) (7.520960622 0 0.0) (7.526411695999999 0 0.0) (7.537842362999999 0 0.0) t: (7.617106151000001 0 0.0) (7.635044875 0 0.0) (7.6383228789999995 0 0.0) (7.598431915 0 0.0) "nil" still seems to be faster, but it may as well be a statistical variati= on. ---- Either way, I'm happy, as you suggested, to look at per-function overhead to see if there's any difference. Do you think it's still worth it? And how do I do it though? Shall I do (profiler-start), and then evaluate the benchmark?