unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: rms@gnu.org
Cc: Dan Nicolaescu <dann@ics.uci.edu>, emacs-devel@gnu.org
Subject: Re: byte compiling defcustom
Date: Fri, 16 Nov 2007 10:25:13 -0500	[thread overview]
Message-ID: <jwvlk8yw8ih.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <E1IssoT-0003OT-GZ@fencepost.gnu.org> (Richard Stallman's message of "Thu, 15 Nov 2007 23:28:17 -0500")

>     (defvar foo     (if (featurep 'xemacs) 'bar 'baz) "doc")
>     (defcustom foo1 (if (featurep 'xemacs) 'bar 'baz) "doc")


>     Are compiled to:

>     (defvar foo 'baz (#$ . 543))
>     (custom-declare-variable 'foo1 '(if (featurep 'xemacs) 'bar 'baz) '(#$ . 581))

> The initial value expression for defcustom is saved and gets
> recalculated later in some cases.  So it cannot in general be
> optimized.

He probably meant "compiled" as much as "optimized".
The same holds of keyword arguments to custom-declare-variable, by the way.

> This particular simplification could be done even in defcustom, since
> (featurep 'xemacs) is effectively a constant.  But why bother
> to implement that optimization?  It works fine as it is now.

The patch below implements the desired feature: it byte-compiles the
default-value-expression (first part of the hunk) as well as any
keyword arguments (second part of the hunk).  Notice that the second
part actually simplifies the code.

To me the main benefit is not so much efficiency as better warnings.


        Stefan


PS: While fiddling with it, I discovered that (defvar foo 1 "haha") is
byte compiled into itself (more or less), whereas (defvar foo 1)
gets "open coded" as
(byte-code "..." [current-load-list foo default-boundp set-default 1] 3)
so it won't call Fdefvar and will hence circumvent the check added to
defvar to detect when defvar is used within a `let' binding:

	{ /* Check if there is really a global binding rather than just a let
	     binding that shadows the global unboundness of the var.  */
	  struct specbinding *pdl = specpdl_ptr;
	  while (--pdl >= specpdl)
	    {
	      if (EQ (pdl->symbol, sym) && !pdl->func
		  && EQ (pdl->old_value, Qunbound))
		{
		  message_with_string ("Warning: defvar ignored because %s is let-bound",
				       SYMBOL_NAME (sym), 1);
		  break;
		}
	    }
	}

Maybe those defvars shouldn't be open-coded?


--- orig/lisp/emacs-lisp/bytecomp.el
+++ mod/lisp/emacs-lisp/bytecomp.el
@@ -2309,18 +2309,16 @@
   ;;   (byte-compile-nogroup-warn form))
   (when (byte-compile-warning-enabled-p 'free-vars)
     (push (nth 1 (nth 1 form)) byte-compile-special-variables))
+  (when (eq (car-safe (nth 2 form)) 'quote)
+    ;; (nth 2 form) is meant to evaluate to an expression, so if we have the
+    ;; final value already, we can byte-compile it.
+    (setcar (cdr (nth 2 form))
+            (byte-compile-top-level (cadr (nth 2 form)) nil 'file)))
   (let ((tail (nthcdr 4 form)))
     (while tail
-      ;; If there are any (function (lambda ...)) expressions, compile
-      ;; those functions.
-      (if (and (consp (car tail))
-	       (eq (car (car tail)) 'function)
-	       (consp (nth 1 (car tail))))
-	  (setcar tail (byte-compile-lambda (nth 1 (car tail))))
-	;; Likewise for a bare lambda.
-	(if (and (consp (car tail))
-		 (eq (car (car tail)) 'lambda))
-	    (setcar tail (byte-compile-lambda (car tail)))))
+      (unless (keywordp (car tail))      ;No point optimizing keywords.
+        ;; Compile the keyword arguments.
+        (setcar tail (byte-compile-top-level (car tail) nil 'file)))
       (setq tail (cdr tail))))
   form)

  reply	other threads:[~2007-11-16 15:25 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-15  4:54 byte compiling defcustom Dan Nicolaescu
2007-11-16  4:28 ` Richard Stallman
2007-11-16 15:25   ` Stefan Monnier [this message]
2007-11-17  4:53     ` Richard Stallman
2007-11-18  4:07       ` Stefan Monnier
2007-11-17  4:53     ` Richard Stallman
2007-11-17  5:40     ` Dan Nicolaescu
2007-11-17 16:47     ` Luc Teirlinck
2007-11-17 19:06       ` Dan Nicolaescu
2007-11-17 19:51         ` Luc Teirlinck
2007-11-17 20:05         ` Luc Teirlinck
2007-11-18 13:00         ` Richard Stallman
2007-11-18 18:24           ` disappearing custom menu (was: Re: byte compiling defcustom) Dan Nicolaescu
2007-11-18 18:42             ` Drew Adams
2007-11-18 18:47             ` disappearing custom menu Lennart Borgman (gmail)
2007-11-18 19:20             ` martin rudalics
2007-11-18 19:33               ` Dan Nicolaescu
2007-11-18 19:44                 ` martin rudalics
2007-11-18 23:41                   ` Dan Nicolaescu
2007-11-19  7:58                     ` martin rudalics
2007-11-19 12:25                 ` Richard Stallman
2007-11-19 13:08                   ` martin rudalics
2007-11-19 15:28                   ` Drew Adams
2007-11-18 19:57               ` Drew Adams
2007-11-18 22:23                 ` martin rudalics
2007-11-17 19:55       ` byte compiling defcustom Luc Teirlinck
2007-11-17 20:32       ` Luc Teirlinck
2007-11-17 20:41         ` Luc Teirlinck
2007-11-18  4:13           ` Luc Teirlinck
2007-11-18  4:32             ` Stefan Monnier
2007-11-18  5:11               ` Luc Teirlinck
2007-11-18 18:04             ` Dan Nicolaescu
2007-11-18 18:41               ` Luc Teirlinck
2007-11-18 23:29                 ` Dan Nicolaescu
2007-11-19  7:55                   ` Stefan Monnier
2007-11-19 19:03                     ` Richard Stallman
2007-11-19 20:44                       ` Luc Teirlinck
2007-11-20 12:12                         ` Richard Stallman
2007-11-19 20:48                       ` Luc Teirlinck
2007-11-19 19:03                   ` Richard Stallman
2007-11-19 12:25               ` Richard Stallman
2007-11-19 15:48                 ` Dan Nicolaescu
2007-11-20  3:59                   ` Richard Stallman
2007-11-17 23:31       ` 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=jwvlk8yw8ih.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=dann@ics.uci.edu \
    --cc=emacs-devel@gnu.org \
    --cc=rms@gnu.org \
    /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).