From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Paul Pogonyshev Newsgroups: gmane.emacs.devel Subject: byte-code optimizations Date: Sat, 18 Sep 2004 11:52:15 -0200 Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Message-ID: <200409181152.15364.pogonyshev@gmx.net> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: sea.gmane.org 1095497477 15072 80.91.229.6 (18 Sep 2004 08:51:17 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Sat, 18 Sep 2004 08:51:17 +0000 (UTC) Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Sep 18 10:51:04 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1C8avw-0003MM-00 for ; Sat, 18 Sep 2004 10:51:04 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1C8b1h-00019k-KS for ged-emacs-devel@m.gmane.org; Sat, 18 Sep 2004 04:57:01 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1C8b1b-00019f-0G for emacs-devel@gnu.org; Sat, 18 Sep 2004 04:56:55 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1C8b1Z-00019T-Bj for emacs-devel@gnu.org; Sat, 18 Sep 2004 04:56:54 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1C8b1Z-00019Q-3H for emacs-devel@gnu.org; Sat, 18 Sep 2004 04:56:53 -0400 Original-Received: from [213.165.64.20] (helo=mail.gmx.net) by monty-python.gnu.org with smtp (Exim 4.34) id 1C8avW-0005xW-13 for emacs-devel@gnu.org; Sat, 18 Sep 2004 04:50:38 -0400 Original-Received: (qmail 3397 invoked by uid 65534); 18 Sep 2004 08:50:23 -0000 Original-Received: from unknown (EHLO localhost.localdomain) (195.50.12.118) by mail.gmx.net (mp018) with SMTP; 18 Sep 2004 10:50:23 +0200 X-Authenticated: #16844820 Original-To: emacs-devel@gnu.org User-Agent: KMail/1.4.3 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:27219 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:27219 Hi. Are you interested in byte-code optimizations? I think I found a rather good one, specifically targeted at `c[ad][ad]r' substs. It can be generalized further, though. Consider this hypothetical function: =09(lambda (x) =09 (cons (caar x) (cddr x))) With current Emacs byte-compiler we get =09M-: (disassemble (lambda (x) (cons (caar x) (cddr x)))) byte code: args: (x) 0=09varref=09 x 1=09dup=09 =20 2=09varbind=09 x 3=09car=09 =20 4=09car=09 =20 5=09unbind=09 1 6=09varref=09 x 7=09dup=09 =20 8=09varbind=09 x 9=09cdr=09 =20 10=09cdr=09 =20 11=09unbind=09 1 12=09cons=09 =20 13=09return=09 =20 With the patch (see the very end of this message), this, however, reduces simply to byte code: args: (x) 0=09varref=09 x 1=09car=09 =20 2=09car=09 =20 3=09varref=09 x 4=09cdr=09 =20 5=09cdr=09 =20 6=09cons=09 =20 7=09return=09 =20 In other words, it squeezes the unnecessary binding out of each `c[ad][ad]r'. Three commands per each substitution. This is a lightweight (and special-cases-only) implementation of a TODO item in `byte-opt.el'. If you are interested, I can polish the patch and generalize it a bit. Currently, there is one unresolved problem with the patch. In `byte-opt.el' top comment it is mentioned that ``However certain variables should never have their bindings optimized away, because they affect everything.'' (i.e. `debug-on-error'). I doubt this is particularly important for bindings followed by car/cdr sequences, but it is certainly better not to left open pits. There is also an obvious way to improve: in addition to `byte-c[ad]r' certain other byte commands can be allowed in sequences, i.e. `byte-not'. Comments and suggestions are welcome. Paul --- byte-opt.el=0922 Mar 2004 13:21:08 -0200=091.75 +++ byte-opt.el=0918 Sep 2004 11:04:59 -0200=09 @@ -1993,6 +1993,22 @@ If FOR-EFFECT is non-nil, the return val =09=09 (byte-compile-log-lap =09=09 " %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0))) =09 ;; +=09 ;; dup varbind-X [car/cdr ...] unbind-1 --> [car/cdr ...] +=09 ;; +=09 ((and (eq 'byte-dup (car lap0)) +=09=09 (eq 'byte-varbind (car lap1))) +=09 (setq tmp (cdr rest)) +=09 (while (memq (caar (setq tmp (cdr tmp))) '(byte-car byte-cdr))) +=09 (when (and (eq 'byte-unbind (caar tmp)) (=3D 1 (cdar tmp))) +=09 ;; Throw away dup varbind-X +=09 (setcar rest (nth 2 rest)) +=09 (setcdr rest (nthcdr 3 rest)) +=09 ;; Throw away unbind-1 +=09 (setcar tmp (nth 1 tmp)) +=09 (setcdr tmp (nthcdr 2 tmp)) +=09 (byte-compile-log-lap +=09=09" dup %s [car/cdr ...] unbind-1\t-->\t[car/cdr...]" lap1))) +=09 ;; =09 ;; unbind-N unbind-M --> unbind-(N+M) =09 ;; =09 ((and (eq 'byte-unbind (car lap0))