diff options
| author | Daniel Colascione | 2014-04-21 20:51:12 -0700 |
|---|---|---|
| committer | Daniel Colascione | 2014-04-21 20:51:12 -0700 |
| commit | 66fda7948f7d0b94194d53edcfbf27bec596c019 (patch) | |
| tree | 5e24a63483e983e426a44bfa6b5629b28e3be6f4 | |
| parent | d6f14ca729315dd94cdb24da47e1569519e3a4dd (diff) | |
| download | emacs-66fda7948f7d0b94194d53edcfbf27bec596c019.tar.gz emacs-66fda7948f7d0b94194d53edcfbf27bec596c019.zip | |
Optimize cl-struct-slot-value; fix test
2014-04-22 Daniel Colascione <dancol@dancol.org>
* emacs-lisp/cl-macs.el
(cl-struct-sequence-type,cl-struct-slot-info): Declare pure.
(cl-struct-slot-value): Conditionally use aref or nth so that the
compiler produces optimal code.
2014-04-22 Daniel Colascione <dancol@dancol.org>
* automated/cl-lib.el (cl-lib-struct-accessors): Fix test to
account for removal of `cl-struct-set-slot-value'.
| -rw-r--r-- | lisp/ChangeLog | 7 | ||||
| -rw-r--r-- | lisp/emacs-lisp/cl-macs.el | 9 | ||||
| -rw-r--r-- | test/ChangeLog | 5 | ||||
| -rw-r--r-- | test/automated/cl-lib.el | 2 |
4 files changed, 21 insertions, 2 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 5d96e867072..c4b1c051210 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | 2014-04-22 Daniel Colascione <dancol@dancol.org> | ||
| 2 | |||
| 3 | * emacs-lisp/cl-macs.el | ||
| 4 | (cl-struct-sequence-type,cl-struct-slot-info): Declare pure. | ||
| 5 | (cl-struct-slot-value): Conditionally use aref or nth so that the | ||
| 6 | compiler produces optimal code. | ||
| 7 | |||
| 1 | 2014-04-22 Stefan Monnier <monnier@iro.umontreal.ca> | 8 | 2014-04-22 Stefan Monnier <monnier@iro.umontreal.ca> |
| 2 | 9 | ||
| 3 | * emacs-lisp/cl-macs.el (cl-struct-slot-offset): Mark as pure. | 10 | * emacs-lisp/cl-macs.el (cl-struct-slot-offset): Mark as pure. |
diff --git a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el index a15918d262f..47a89d0880b 100644 --- a/lisp/emacs-lisp/cl-macs.el +++ b/lisp/emacs-lisp/cl-macs.el | |||
| @@ -2600,6 +2600,7 @@ STRUCT-TYPE is a symbol naming a struct type. Return 'vector or | |||
| 2600 | 'list, or nil if STRUCT-TYPE is not a struct type. " | 2600 | 'list, or nil if STRUCT-TYPE is not a struct type. " |
| 2601 | (car (get struct-type 'cl-struct-type))) | 2601 | (car (get struct-type 'cl-struct-type))) |
| 2602 | (put 'cl-struct-sequence-type 'side-effect-free t) | 2602 | (put 'cl-struct-sequence-type 'side-effect-free t) |
| 2603 | (put 'cl-struct-sequence-type 'pure t) | ||
| 2603 | 2604 | ||
| 2604 | (defun cl-struct-slot-info (struct-type) | 2605 | (defun cl-struct-slot-info (struct-type) |
| 2605 | "Return a list of slot names of struct STRUCT-TYPE. | 2606 | "Return a list of slot names of struct STRUCT-TYPE. |
| @@ -2609,6 +2610,7 @@ slot name symbol and OPTS is a list of slot options given to | |||
| 2609 | slots skipped by :initial-offset may appear in the list." | 2610 | slots skipped by :initial-offset may appear in the list." |
| 2610 | (get struct-type 'cl-struct-slots)) | 2611 | (get struct-type 'cl-struct-slots)) |
| 2611 | (put 'cl-struct-slot-info 'side-effect-free t) | 2612 | (put 'cl-struct-slot-info 'side-effect-free t) |
| 2613 | (put 'cl-struct-slot-info 'pure t) | ||
| 2612 | 2614 | ||
| 2613 | (defun cl-struct-slot-offset (struct-type slot-name) | 2615 | (defun cl-struct-slot-offset (struct-type slot-name) |
| 2614 | "Return the offset of slot SLOT-NAME in STRUCT-TYPE. | 2616 | "Return the offset of slot SLOT-NAME in STRUCT-TYPE. |
| @@ -2942,7 +2944,12 @@ The type name can then be used in `cl-typecase', `cl-check-type', etc." | |||
| 2942 | STRUCT and SLOT-NAME are symbols. INST is a structure instance." | 2944 | STRUCT and SLOT-NAME are symbols. INST is a structure instance." |
| 2943 | (unless (cl-typep inst struct-type) | 2945 | (unless (cl-typep inst struct-type) |
| 2944 | (signal 'wrong-type-argument (list struct-type inst))) | 2946 | (signal 'wrong-type-argument (list struct-type inst))) |
| 2945 | (elt inst (cl-struct-slot-offset struct-type slot-name))) | 2947 | ;; We could use `elt', but since the byte compiler will resolve the |
| 2948 | ;; branch below at compile time, it's more efficient to use the | ||
| 2949 | ;; type-specific accessor. | ||
| 2950 | (if (eq (cl-struct-sequence-type struct-type) 'vector) | ||
| 2951 | (aref inst (cl-struct-slot-offset struct-type slot-name)) | ||
| 2952 | (nth (cl-struct-slot-offset struct-type slot-name) inst))) | ||
| 2946 | (put 'cl-struct-slot-value 'side-effect-free t) | 2953 | (put 'cl-struct-slot-value 'side-effect-free t) |
| 2947 | 2954 | ||
| 2948 | (run-hooks 'cl-macs-load-hook) | 2955 | (run-hooks 'cl-macs-load-hook) |
diff --git a/test/ChangeLog b/test/ChangeLog index 4003a24bc6b..1163402fd19 100644 --- a/test/ChangeLog +++ b/test/ChangeLog | |||
| @@ -1,3 +1,8 @@ | |||
| 1 | 2014-04-22 Daniel Colascione <dancol@dancol.org> | ||
| 2 | |||
| 3 | * automated/cl-lib.el (cl-lib-struct-accessors): Fix test to | ||
| 4 | account for removal of `cl-struct-set-slot-value'. | ||
| 5 | |||
| 1 | 2014-04-21 Daniel Colascione <dancol@dancol.org> | 6 | 2014-04-21 Daniel Colascione <dancol@dancol.org> |
| 2 | 7 | ||
| 3 | * automated/bytecomp-tests.el (test-byte-comp-compile-and-load): | 8 | * automated/bytecomp-tests.el (test-byte-comp-compile-and-load): |
diff --git a/test/automated/cl-lib.el b/test/automated/cl-lib.el index 8bf1482a30a..89bc3cea392 100644 --- a/test/automated/cl-lib.el +++ b/test/automated/cl-lib.el | |||
| @@ -206,7 +206,7 @@ | |||
| 206 | (let ((x (make-mystruct :abc 1 :def 2))) | 206 | (let ((x (make-mystruct :abc 1 :def 2))) |
| 207 | (should (eql (cl-struct-slot-value 'mystruct 'abc x) 1)) | 207 | (should (eql (cl-struct-slot-value 'mystruct 'abc x) 1)) |
| 208 | (should (eql (cl-struct-slot-value 'mystruct 'def x) 2)) | 208 | (should (eql (cl-struct-slot-value 'mystruct 'def x) 2)) |
| 209 | (cl-struct-set-slot-value 'mystruct 'def x -1) | 209 | (setf (cl-struct-slot-value 'mystruct 'def x) -1) |
| 210 | (should (eql (cl-struct-slot-value 'mystruct 'def x) -1)) | 210 | (should (eql (cl-struct-slot-value 'mystruct 'def x) -1)) |
| 211 | (should (eql (cl-struct-slot-offset 'mystruct 'abc) 1)) | 211 | (should (eql (cl-struct-slot-offset 'mystruct 'abc) 1)) |
| 212 | (should-error (cl-struct-slot-offset 'mystruct 'marypoppins)) | 212 | (should-error (cl-struct-slot-offset 'mystruct 'marypoppins)) |