unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Paul Pogonyshev <pogonyshev@gmx.net>
Subject: byte-code optimizations
Date: Sat, 18 Sep 2004 11:52:15 -0200	[thread overview]
Message-ID: <200409181152.15364.pogonyshev@gmx.net> (raw)

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:

	(lambda (x)
	  (cons (caar x) (cddr x)))

With current Emacs byte-compiler we get

	M-: (disassemble (lambda (x) (cons (caar x) (cddr x))))

byte code:
  args: (x)
0	varref	  x
1	dup	  
2	varbind	  x
3	car	  
4	car	  
5	unbind	  1
6	varref	  x
7	dup	  
8	varbind	  x
9	cdr	  
10	cdr	  
11	unbind	  1
12	cons	  
13	return	  

With the patch (see the very end of this message), this, however,
reduces simply to

byte code:
  args: (x)
0	varref	  x
1	car	  
2	car	  
3	varref	  x
4	cdr	  
5	cdr	  
6	cons	  
7	return	  

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	22 Mar 2004 13:21:08 -0200	1.75
+++ byte-opt.el	18 Sep 2004 11:04:59 -0200	
@@ -1993,6 +1993,22 @@ If FOR-EFFECT is non-nil, the return val
 		 (byte-compile-log-lap
 		  "  %s [dup/%s]...\t-->\t%s dup..." lap0 lap0 lap0)))
 	    ;;
+	    ;; dup varbind-X [car/cdr ...] unbind-1 --> [car/cdr ...]
+	    ;;
+	    ((and (eq 'byte-dup     (car lap0))
+		  (eq 'byte-varbind (car lap1)))
+	     (setq tmp (cdr rest))
+	     (while (memq (caar (setq tmp (cdr tmp))) '(byte-car byte-cdr)))
+	     (when (and (eq 'byte-unbind (caar tmp)) (= 1 (cdar tmp)))
+	       ;; Throw away  dup varbind-X
+	       (setcar rest (nth 2 rest))
+	       (setcdr rest (nthcdr 3 rest))
+	       ;; Throw away unbind-1
+	       (setcar tmp (nth 1 tmp))
+	       (setcdr tmp (nthcdr 2 tmp))
+	       (byte-compile-log-lap
+		"  dup %s [car/cdr ...] unbind-1\t-->\t[car/cdr...]" lap1)))
+	    ;;
 	    ;; unbind-N unbind-M  -->  unbind-(N+M)
 	    ;;
 	    ((and (eq 'byte-unbind (car lap0))

             reply	other threads:[~2004-09-18 13:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-09-18 13:52 Paul Pogonyshev [this message]
2004-09-18 20:26 ` byte-code optimizations Stefan
2004-09-19 11:00   ` Richard Stallman
2004-09-19 16:15   ` Paul Pogonyshev
2004-09-21 18:31     ` Richard Stallman
2004-09-22  0:42       ` Paul Pogonyshev
2004-09-21 21:05         ` Stefan Monnier
2004-09-21 21:15           ` Miles Bader
2004-09-22  3:31             ` Paul Pogonyshev
2004-09-21 22:57               ` Miles Bader
2004-09-22  3:25           ` Paul Pogonyshev
2004-09-22 18:20         ` Richard Stallman
2004-09-23  0:33           ` Paul Pogonyshev
2004-09-18 22:55 ` Richard Stallman

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200409181152.15364.pogonyshev@gmx.net \
    --to=pogonyshev@gmx.net \
    /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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).