all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [RFC] bytecomp: simple source-level optimization
@ 2014-06-23  6:43 Dmitry Antipov
  2014-06-23  8:29 ` Leo Liu
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Dmitry Antipov @ 2014-06-23  6:43 UTC (permalink / raw)
  To: Emacs development discussions

[-- 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)


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2014-06-25  1:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-23  6:43 [RFC] bytecomp: simple source-level optimization Dmitry Antipov
2014-06-23  8:29 ` 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

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.