comparison lisp/bytecomp.el @ 5366:f00192e1cd49

Examining the result of #'length: `eql', not `=', it's better style & cheaper 2011-03-08 Aidan Kehoe <kehoea@parhasard.net> * buff-menu.el (list-buffers-noselect): * byte-optimize.el (byte-optimize-identity): * byte-optimize.el (byte-optimize-if): * byte-optimize.el (byte-optimize-nth): * byte-optimize.el (byte-optimize-nthcdr): * bytecomp.el (byte-compile-warn-wrong-args): * bytecomp.el (byte-compile-two-args-19->20): * bytecomp.el (byte-compile-list): * bytecomp.el (byte-compile-beginning-of-line): * bytecomp.el (byte-compile-set): * bytecomp.el (byte-compile-set-default): * bytecomp.el (byte-compile-values): * bytecomp.el (byte-compile-values-list): * bytecomp.el (byte-compile-integerp): * bytecomp.el (byte-compile-multiple-value-list-internal): * bytecomp.el (byte-compile-throw): * cl-macs.el (cl-do-arglist): * cl-macs.el (cl-parse-loop-clause): * cl-macs.el (multiple-value-bind): * cl-macs.el (multiple-value-setq): * cl-macs.el (get-setf-method): * cmdloop.el (command-error): * cmdloop.el (y-or-n-p-minibuf): * cmdloop.el (yes-or-no-p-minibuf): * coding.el (unencodable-char-position): * cus-edit.el (custom-face-prompt): * cus-edit.el (custom-buffer-create-internal): * cus-edit.el (widget-face-action): * cus-edit.el (custom-group-value-create): * descr-text.el (describe-char-unicode-data): * dialog-gtk.el (popup-builtin-question-dialog): * dragdrop.el (experimental-dragdrop-drop-log-function): * dragdrop.el (experimental-dragdrop-drop-mime-default): * easymenu.el (easy-menu-add): * easymenu.el (easy-menu-remove): * faces.el (read-face-name): * faces.el (set-face-stipple): * files.el (file-name-non-special): * font.el (font-combine-fonts): * font.el (font-set-face-font): * font.el (font-parse-rgb-components): * font.el (font-rgb-color-p): * font.el (font-color-rgb-components): * gnuserv.el (gnuserv-edit-files): * help.el (key-or-menu-binding): * help.el (function-documentation-1): * help.el (function-documentation): * info.el (info): * isearch-mode.el (isearch-exit): * isearch-mode.el (isearch-edit-string): * isearch-mode.el (isearch-*-char): * isearch-mode.el (isearch-complete1): * ldap.el (ldap-encode-country-string): * ldap.el (ldap-decode-string): * minibuf.el (read-file-name-internal-1): * minibuf.el (read-non-nil-coding-system): * minibuf.el (get-user-response): * mouse.el (drag-window-divider): * mule/ccl.el: * mule/ccl.el (ccl-compile-if): * mule/ccl.el (ccl-compile-break): * mule/ccl.el (ccl-compile-repeat): * mule/ccl.el (ccl-compile-write-repeat): * mule/ccl.el (ccl-compile-call): * mule/ccl.el (ccl-compile-end): * mule/ccl.el (ccl-compile-read-multibyte-character): * mule/ccl.el (ccl-compile-write-multibyte-character): * mule/ccl.el (ccl-compile-translate-character): * mule/ccl.el (ccl-compile-mule-to-unicode): * mule/ccl.el (ccl-compile-unicode-to-mule): * mule/ccl.el (ccl-compile-lookup-integer): * mule/ccl.el (ccl-compile-lookup-character): * mule/ccl.el (ccl-compile-map-multiple): * mule/ccl.el (ccl-compile-map-single): * mule/devan-util.el (devanagari-compose-to-one-glyph): * mule/devan-util.el (devanagari-composition-component): * mule/mule-cmds.el (finish-set-language-environment): * mule/viet-util.el: * mule/viet-util.el (viet-encode-viscii-char): * multicast.el (open-multicast-group): * newcomment.el (comment-quote-nested): * newcomment.el (comment-region): * newcomment.el (comment-dwim): * regexp-opt.el (regexp-opt-group): * replace.el (map-query-replace-regexp): * specifier.el (derive-device-type-from-tag-set): * subr.el (skip-chars-quote): * test-harness.el (test-harness-from-buffer): * test-harness.el (batch-test-emacs): * wid-edit.el (widget-choice-action): * wid-edit.el (widget-symbol-prompt-internal): * wid-edit.el (widget-color-action): * window-xemacs.el (push-window-configuration): * window-xemacs.el (pop-window-configuration): * window.el (quit-window): * x-compose.el (electric-diacritic): It's better style, and cheaper (often one assembler instruction vs. a C funcall in the byte code), to use `eql' instead of `=' when it's clear what numerical type a given result will be. Change much of our code to do this, with the help of a byte-compiler change (not comitted) that looked for calls to #'length (which always returns an integer) in its args.
author Aidan Kehoe <kehoea@parhasard.net>
date Tue, 08 Mar 2011 23:41:52 +0000
parents 31475de17064
children d967d96ca043
comparison
equal deleted inserted replaced
5365:dbae25a8949d 5366:f00192e1cd49
3218 3218
3219 (defun byte-compile-warn-wrong-args (form n) 3219 (defun byte-compile-warn-wrong-args (form n)
3220 (when (memq 'subr-callargs byte-compile-warnings) 3220 (when (memq 'subr-callargs byte-compile-warnings)
3221 (byte-compile-warn "%s called with %d arg%s, but requires %s" 3221 (byte-compile-warn "%s called with %d arg%s, but requires %s"
3222 (car form) (length (cdr form)) 3222 (car form) (length (cdr form))
3223 (if (= 1 (length (cdr form))) "" "s") n))) 3223 (if (eql 1 (length (cdr form))) "" "s") n)))
3224 3224
3225 (defun byte-compile-subr-wrong-args (form n) 3225 (defun byte-compile-subr-wrong-args (form n)
3226 (byte-compile-warn-wrong-args form n) 3226 (byte-compile-warn-wrong-args form n)
3227 ;; get run-time wrong-number-of-args error. 3227 ;; get run-time wrong-number-of-args error.
3228 (byte-compile-normal-call form)) 3228 (byte-compile-normal-call form))
3343 (t (byte-compile-subr-wrong-args form "1-3")))) 3343 (t (byte-compile-subr-wrong-args form "1-3"))))
3344 3344
3345 ;; XEmacs: used for functions that have a different opcode in v19 than v20. 3345 ;; XEmacs: used for functions that have a different opcode in v19 than v20.
3346 ;; this includes `eq', `equal', and other old-ified functions. 3346 ;; this includes `eq', `equal', and other old-ified functions.
3347 (defun byte-compile-two-args-19->20 (form) 3347 (defun byte-compile-two-args-19->20 (form)
3348 (if (not (= (length form) 3)) 3348 (if (not (eql (length form) 3))
3349 (byte-compile-subr-wrong-args form 2) 3349 (byte-compile-subr-wrong-args form 2)
3350 (byte-compile-form (car (cdr form))) ;; Push the arguments 3350 (byte-compile-form (car (cdr form))) ;; Push the arguments
3351 (byte-compile-form (nth 2 form)) 3351 (byte-compile-form (nth 2 form))
3352 (if (byte-compile-version-cond byte-compile-emacs19-compatibility) 3352 (if (byte-compile-version-cond byte-compile-emacs19-compatibility)
3353 (byte-compile-out (get (car form) 'byte-opcode19) 0) 3353 (byte-compile-out (get (car form) 'byte-opcode19) 0)
3444 3444
3445 (defun byte-compile-list (form) 3445 (defun byte-compile-list (form)
3446 (let* ((args (cdr form)) 3446 (let* ((args (cdr form))
3447 (nargs (length args))) 3447 (nargs (length args)))
3448 (cond 3448 (cond
3449 ((= nargs 0) 3449 ((eql nargs 0)
3450 (byte-compile-constant nil)) 3450 (byte-compile-constant nil))
3451 ((< nargs 5) 3451 ((< nargs 5)
3452 (mapc 'byte-compile-form args) 3452 (mapc 'byte-compile-form args)
3453 (byte-compile-out 3453 (byte-compile-out
3454 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- nargs)) 3454 (aref [byte-list1 byte-list2 byte-list3 byte-list4] (1- nargs))
3694 3694
3695 (defun byte-compile-beginning-of-line (form) 3695 (defun byte-compile-beginning-of-line (form)
3696 (let ((len (length form))) 3696 (let ((len (length form)))
3697 (cond ((> len 3) 3697 (cond ((> len 3)
3698 (byte-compile-subr-wrong-args form "0-2")) 3698 (byte-compile-subr-wrong-args form "0-2"))
3699 ((or (= len 3) (not (byte-compile-constp (nth 1 form)))) 3699 ((or (eql len 3) (not (byte-compile-constp (nth 1 form))))
3700 (byte-compile-normal-call form)) 3700 (byte-compile-normal-call form))
3701 (t 3701 (t
3702 (byte-compile-form 3702 (byte-compile-form
3703 (list 'forward-line 3703 (list 'forward-line
3704 (if (integerp (setq form (or (eval (nth 1 form)) 1))) 3704 (if (integerp (setq form (or (eval (nth 1 form)) 1)))
3764 3764
3765 (defun byte-compile-set-default (form) 3765 (defun byte-compile-set-default (form)
3766 (let* ((args (cdr form)) 3766 (let* ((args (cdr form))
3767 (nargs (length args)) 3767 (nargs (length args))
3768 (var (car args))) 3768 (var (car args)))
3769 (when (and (= (safe-length var) 2) 3769 (when (and (eql (safe-length var) 2) (eq (car var) 'quote))
3770 (eq (car var) 'quote))
3771 (let ((sym (nth 1 var))) 3770 (let ((sym (nth 1 var)))
3772 (cond 3771 (cond
3773 ((not (symbolp sym)) 3772 ((not (symbolp sym))
3774 (byte-compile-warn "Attempt to set-globally non-symbol %s" sym)) 3773 (byte-compile-warn "Attempt to set-globally non-symbol %s" sym))
3775 ((byte-compile-constant-symbol-p sym) 3774 ((byte-compile-constant-symbol-p sym)
3784 ((not (memq 'free-vars byte-compile-warnings))) ; warnings suppressed? 3783 ((not (memq 'free-vars byte-compile-warnings))) ; warnings suppressed?
3785 ((memq sym byte-compile-free-assignments)) ; already warned about sym 3784 ((memq sym byte-compile-free-assignments)) ; already warned about sym
3786 (t 3785 (t
3787 (byte-compile-warn "assignment to free variable %s" sym) 3786 (byte-compile-warn "assignment to free variable %s" sym)
3788 (push sym byte-compile-free-assignments))))) 3787 (push sym byte-compile-free-assignments)))))
3789 (if (= nargs 2) 3788 (if (eql nargs 2)
3790 ;; now emit a normal call to set-default 3789 ;; now emit a normal call to set-default
3791 (byte-compile-normal-call form) 3790 (byte-compile-normal-call form)
3792 (byte-compile-subr-wrong-args form 2)))) 3791 (byte-compile-subr-wrong-args form 2))))
3793 3792
3794 3793
3921 (setq form (cdr form)) 3920 (setq form (cdr form))
3922 (byte-compile-form-do-effect (pop form)) 3921 (byte-compile-form-do-effect (pop form))
3923 (byte-compile-body form t)) 3922 (byte-compile-body form t))
3924 3923
3925 (defun byte-compile-values (form) 3924 (defun byte-compile-values (form)
3926 (if (= 2 (length form)) 3925 (if (eql 2 (length form))
3927 (if (byte-compile-constp (second form)) 3926 (if (byte-compile-constp (second form))
3928 (byte-compile-form-do-effect (second form)) 3927 (byte-compile-form-do-effect (second form))
3929 ;; #'or compiles to bytecode, #'values doesn't: 3928 ;; #'or compiles to bytecode, #'values doesn't:
3930 (byte-compile-form-do-effect `(or ,(second form) nil))) 3929 (byte-compile-form-do-effect `(or ,(second form) nil)))
3931 (byte-compile-normal-call form))) 3930 (byte-compile-normal-call form)))
3932 3931
3933 (defun byte-compile-values-list (form) 3932 (defun byte-compile-values-list (form)
3934 (if (and (= 2 (length form)) 3933 (if (and (eql 2 (length form))
3935 (or (null (second form)) 3934 (or (null (second form))
3936 (and (consp (second form)) 3935 (and (consp (second form))
3937 (eq (car (second form)) 3936 (eq (car (second form))
3938 'quote) 3937 'quote)
3939 (not (symbolp (car-safe (cdr (second form)))))))) 3938 (not (symbolp (car-safe (cdr (second form))))))))
4108 ;; (except that earlier 21.5 with bignum support will confuse Bfixnump and 4107 ;; (except that earlier 21.5 with bignum support will confuse Bfixnump and
4109 ;; Bintegerp; which it did in dealing with byte-compiled code from 21.4 4108 ;; Bintegerp; which it did in dealing with byte-compiled code from 21.4
4110 ;; anyway). 4109 ;; anyway).
4111 4110
4112 (defun byte-compile-integerp (form) 4111 (defun byte-compile-integerp (form)
4113 (if (/= 2 (length form)) 4112 (if (not (eql (length form) 2))
4114 (byte-compile-subr-wrong-args form 1) 4113 (byte-compile-subr-wrong-args form 1)
4115 (let ((donetag (byte-compile-make-tag)) 4114 (let ((donetag (byte-compile-make-tag))
4116 (wintag (byte-compile-make-tag)) 4115 (wintag (byte-compile-make-tag))
4117 (failtag (byte-compile-make-tag))) 4116 (failtag (byte-compile-make-tag)))
4118 (byte-compile-constant 'integerp) 4117 (byte-compile-constant 'integerp)
4336 (pushnew '(subrp (symbol-function 'multiple-value-call)) 4335 (pushnew '(subrp (symbol-function 'multiple-value-call))
4337 byte-compile-checks-on-load 4336 byte-compile-checks-on-load
4338 :test #'equal))) 4337 :test #'equal)))
4339 4338
4340 (defun byte-compile-multiple-value-list-internal (form) 4339 (defun byte-compile-multiple-value-list-internal (form)
4341 (if (/= 4 (length form)) 4340 (if (not (eql 4 (length form)))
4342 (progn 4341 (progn
4343 (byte-compile-warn-wrong-args form 3) 4342 (byte-compile-warn-wrong-args form 3)
4344 (byte-compile-normal-call 4343 (byte-compile-normal-call
4345 `(signal 'wrong-number-of-arguments '(,(car form) 4344 `(signal 'wrong-number-of-arguments '(,(car form)
4346 ,(length (cdr form)))))) 4345 ,(length (cdr form))))))
4358 ;; the form does not have two args, it tries to #'funcall it expecting a 4357 ;; the form does not have two args, it tries to #'funcall it expecting a
4359 ;; runtime wrong-number-of-arguments error. Now that #'throw is a special 4358 ;; runtime wrong-number-of-arguments error. Now that #'throw is a special
4360 ;; form, it provokes an invalid-function error instead (or at least it 4359 ;; form, it provokes an invalid-function error instead (or at least it
4361 ;; should; there's a kludge around for the moment in eval.c that avoids 4360 ;; should; there's a kludge around for the moment in eval.c that avoids
4362 ;; that, but this file should not assume that that will always be there). 4361 ;; that, but this file should not assume that that will always be there).
4363 (if (/= 2 (length (cdr form))) 4362 (if (not (eql 2 (length (cdr form))))
4364 (progn 4363 (progn
4365 (byte-compile-warn-wrong-args form 2) 4364 (byte-compile-warn-wrong-args form 2)
4366 (byte-compile-normal-call 4365 (byte-compile-normal-call
4367 `(signal 'wrong-number-of-arguments '(,(car form) 4366 `(signal 'wrong-number-of-arguments '(,(car form)
4368 ,(length (cdr form)))))) 4367 ,(length (cdr form))))))