From: Dmitry Antipov <dmantipov@yandex.ru>
To: Emacs development discussions <emacs-devel@gnu.org>
Subject: [RFC] bytecomp: simple source-level optimization
Date: Mon, 23 Jun 2014 10:43:55 +0400 [thread overview]
Message-ID: <53A7CCAB.7020807@yandex.ru> (raw)
[-- Attachment #1: Type: text/plain, Size: 604 bytes --]
Source forms like:
(if (= (point) (point-min)) ...)
or:
(if (eq (point-max) (point)) ...)
are widely used, but compiled as is, which is suboptimal.
The first one can be optimized to (if (bobp) ...) and the
second to (if (eobp) ...), respectively.
Example:
(defun foo (x y)
(if (= (point) (point-min)) x y))
Suboptimal:
0 point
1 point-min
2 eqlsign
3 goto-if-nil 1
6 varref x
7 return
8:1 varref y
9 return
Better:
0 bobp
1 goto-if-nil 1
4 varref x
5 return
6:1 varref y
7 return
Thoughts?
Dmitry
[-- Attachment #2: byte_optimize_point_location.patch --]
[-- Type: text/x-patch, Size: 1340 bytes --]
=== modified file 'lisp/emacs-lisp/byte-opt.el'
--- lisp/emacs-lisp/byte-opt.el 2014-06-02 00:18:22 +0000
+++ lisp/emacs-lisp/byte-opt.el 2014-06-23 06:30:10 +0000
@@ -1057,11 +1057,27 @@
(if (nth 1 form)
form))
+(defun byte-optimize-point-location (form)
+ ;; For OP in '=', 'eq' and 'equal':
+ ;; (OP (point) (point-min)) and (OP (point-min) (point)) => (bobp)
+ ;; (OP (point) (point-max)) and (OP (point-max) (point)) => (eobp)
+ (let ((op0 (nth 1 form)) (op1 (nth 2 form)))
+ (cond ((or (and (equal op0 '(point)) (equal op1 '(point-min)))
+ (and (equal op0 '(point-min)) (equal op1 '(point))))
+ '(bobp))
+ ((or (and (equal op0 '(point)) (equal op1 '(point-max)))
+ (and (equal op0 '(point-max)) (equal op1 '(point))))
+ '(eobp))
+ (t form))))
+
(put 'and 'byte-optimizer 'byte-optimize-and)
(put 'or 'byte-optimizer 'byte-optimize-or)
(put 'cond 'byte-optimizer 'byte-optimize-cond)
(put 'if 'byte-optimizer 'byte-optimize-if)
(put 'while 'byte-optimizer 'byte-optimize-while)
+(put '= 'byte-optimizer 'byte-optimize-point-location)
+(put 'eq 'byte-optimizer 'byte-optimize-point-location)
+(put 'equal 'byte-optimizer 'byte-optimize-point-location)
;; byte-compile-negation-optimizer lives in bytecomp.el
(put '/= 'byte-optimizer 'byte-compile-negation-optimizer)
next reply other threads:[~2014-06-23 6:43 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-23 6:43 Dmitry Antipov [this message]
2014-06-23 8:29 ` [RFC] bytecomp: simple source-level optimization Leo Liu
2014-06-23 11:05 ` Dmitry Antipov
2014-06-23 12:26 ` Leo Liu
2014-06-23 9:09 ` Andreas Röhler
2014-06-23 12:44 ` Nicolas Richard
2014-06-23 14:01 ` Andreas Röhler
2014-06-23 14:19 ` Stefan Monnier
2014-06-23 9:56 ` David Kastrup
2014-06-23 11:40 ` Dmitry Antipov
2014-06-23 11:44 ` Dmitry Antipov
2014-06-25 1:08 ` Stephen J. Turnbull
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=53A7CCAB.7020807@yandex.ru \
--to=dmantipov@yandex.ru \
--cc=emacs-devel@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).