| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; CMPOPT-SEQUENCE Optimization of SEQUENCE functions |
| 6 |
;;;; |
| 7 |
;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll |
| 8 |
;;;; |
| 9 |
;;;; See file 'LICENSE' for the copyright details. |
| 10 |
|
| 11 |
(in-package "COMPILER") |
| 12 |
|
| 13 |
(defun constant-function-expression (form) |
| 14 |
(and (consp form) |
| 15 |
(member (first form) '(quote function lambda)))) |
| 16 |
|
| 17 |
(defun seq-opt-test-function (test-flag test %test) |
| 18 |
(cond ((null test-flag) |
| 19 |
#'(lambda (v1 v2) `(eql ,v1 ,v2))) |
| 20 |
((eq test-flag :test-not) |
| 21 |
(if (constant-function-expression test) |
| 22 |
#'(lambda (v1 v2) `(not (funcall ,test ,v1 ,v2))) |
| 23 |
#'(lambda (v1 v2) `(not (funcall ,%test ,v1 ,v2))))) |
| 24 |
(t |
| 25 |
(if (constant-function-expression test) |
| 26 |
#'(lambda (v1 v2) `(funcall ,test ,v1 ,v2)) |
| 27 |
#'(lambda (v1 v2) `(funcall ,%test ,v1 ,v2)))))) |
| 28 |
|
| 29 |
(defun seq-opt-key-function (key %key) |
| 30 |
(cond ((null key) |
| 31 |
#'identity) |
| 32 |
((constant-function-expression key) |
| 33 |
#'(lambda (elt) `(funcall ,key ,elt))) |
| 34 |
(t |
| 35 |
#'(lambda (elt) `(funcall (or ,%key #'identity) ,elt))))) |
| 36 |
|
| 37 |
#+(or) |
| 38 |
(define-compiler-macro si::make-seq-iterator (seq &optional (start 0)) |
| 39 |
(ext:with-clean-symbols (%seq %start) |
| 40 |
`(let ((%seq (optional-type-check ,seq sequence)) |
| 41 |
(%start ,start)) |
| 42 |
(cond ((consp %seq) |
| 43 |
(nthcdr %start %seq)) |
| 44 |
((< %start (length %seq)) |
| 45 |
%start) |
| 46 |
(t |
| 47 |
nil))))) |
| 48 |
|
| 49 |
#+(or) |
| 50 |
(define-compiler-macro si::seq-iterator-ref (seq iterator) |
| 51 |
(ext:with-clean-symbols (%seq %iterator) |
| 52 |
`(let* ((%seq ,seq) |
| 53 |
(%iterator ,iterator)) |
| 54 |
(declare (optimize (safety 0))) |
| 55 |
(if (typep %iterator 'fixnum *cmp-env*) |
| 56 |
;; Fixnum iterators are always fine |
| 57 |
(aref %seq %iterator) |
| 58 |
;; Error check in case we may have been passed an improper list |
| 59 |
(si:cons-car (ext:checked-value cons %iterator)))))) |
| 60 |
|
| 61 |
#+(or) |
| 62 |
(define-compiler-macro si::seq-iterator-next (seq iterator) |
| 63 |
(ext:with-clean-symbols (%seq %iterator) |
| 64 |
`(let* ((%seq ,seq) |
| 65 |
(%iterator ,iterator)) |
| 66 |
(declare (optimize (safety 0))) |
| 67 |
(if (typep %iterator 'fixnum *cmp-env*) |
| 68 |
(let ((%iterator (1+ (ext:truly-the fixnum %iterator)))) |
| 69 |
(declare (fixnum %iterator)) |
| 70 |
(and (< %iterator (length (ext:truly-the vector %seq))) |
| 71 |
%iterator)) |
| 72 |
(si:cons-cdr %iterator))))) |
| 73 |
|
| 74 |
(defmacro do-in-seq ((%elt %sequence &key %start %end end output) &body body) |
| 75 |
(ext:with-unique-names (%iterator %counter) |
| 76 |
(let* ((counter (if end |
| 77 |
`(- (or ,%end most-positive-fixnum) ,%start) |
| 78 |
0)) |
| 79 |
(test (if end |
| 80 |
`(and ,%iterator (plusp ,%counter)) |
| 81 |
%iterator))) |
| 82 |
`(let* ((,%iterator (si::make-seq-iterator ,%sequence ,%start)) |
| 83 |
(,%counter ,counter)) |
| 84 |
(declare (:read-only ,%sequence ,%start ,%counter) |
| 85 |
(ignorable ,%counter) |
| 86 |
(fixnum ,%counter)) |
| 87 |
(loop |
| 88 |
(unless ,test (return ,output)) |
| 89 |
(let ((,%elt (si::seq-iterator-ref ,%sequence ,%iterator))) |
| 90 |
,@body) |
| 91 |
(setf ,%iterator (si::seq-iterator-next ,%sequence ,%iterator)) |
| 92 |
,@(when end |
| 93 |
`((decf ,%counter)))))))) |
| 94 |
|
| 95 |
;;; |
| 96 |
;;; MEMBER |
| 97 |
;;; |
| 98 |
|
| 99 |
(defmacro do-in-list ((%elt %sublist %list &rest output) &body body) |
| 100 |
`(do* ((,%sublist ,%list (si:cons-cdr ,%sublist))) |
| 101 |
((null ,%sublist) ,@output) |
| 102 |
(let* ((,%sublist (optional-type-check ,%sublist cons)) |
| 103 |
(,%elt (si:cons-car ,%sublist))) |
| 104 |
,@body))) |
| 105 |
|
| 106 |
(defmacro define-seq-compiler-macro (name lambda-list &body body) |
| 107 |
(let ((whole (second lambda-list))) |
| 108 |
`(define-compiler-macro* ,name ,lambda-list |
| 109 |
(unless (policy-inline-sequence-functions) |
| 110 |
(return-from ,name ,whole)) |
| 111 |
(when (and test test-not) |
| 112 |
(cmpwarn "Cannot specify both :TEST and :TEST-NOT arguments to ~A" ',name) |
| 113 |
(return-from ,name ,whole)) |
| 114 |
(let* ((test-flag (cond (test :test) |
| 115 |
(test-not :test-not) |
| 116 |
(t nil))) |
| 117 |
(key-function (seq-opt-key-function key %key)) |
| 118 |
(test-function (seq-opt-test-function test-flag |
| 119 |
(if test test test-not) |
| 120 |
(if test %test %test-not)))) |
| 121 |
(declare (ignorable test-flag key-function test-function)) |
| 122 |
,@body)))) |
| 123 |
|
| 124 |
(define-seq-compiler-macro member (&whole whole value list &key key test test-not) |
| 125 |
(unless key |
| 126 |
(when (and (or (null test) (constant-function-expression test)) |
| 127 |
(constant-expression-p list)) |
| 128 |
(let ((evaluated-list (cmp-eval list))) |
| 129 |
(when (and (si:proper-list-p evaluated-list) (<= (length evaluated-list) 4)) |
| 130 |
(return-from member |
| 131 |
`(or ,@(loop for l on evaluated-list |
| 132 |
for elt = (first l) |
| 133 |
collect `(and ,(funcall test-function %value `(quote ,elt)) |
| 134 |
',l))))) |
| 135 |
(when (or (consp evaluated-list) (symbolp evaluated-list)) |
| 136 |
(setf list `',evaluated-list)))) |
| 137 |
(when (or (null test-flag) (eq test-flag :test)) |
| 138 |
(when (member test '('EQ #'EQ) :test #'equal) |
| 139 |
(return-from member |
| 140 |
`(ffi:c-inline (,%value ,%list) (:object :object) :object |
| 141 |
"si_memq(#0,#1)" :one-liner t :side-effects nil))) |
| 142 |
(when (or (member test '('EQL #'EQL) :test #'equal) (null test)) |
| 143 |
(return-from member |
| 144 |
`(ffi:c-inline (,%value ,%list) (:object :object) :object |
| 145 |
"ecl_memql(#0,#1)" :one-liner t :side-effects nil))) |
| 146 |
(when (member test '('EQUAL #'EQUAL) :test #'equal) |
| 147 |
(return-from member |
| 148 |
`(ffi:c-inline (,%value ,%list) (:object :object) :object |
| 149 |
"ecl_member(#0,#1)" :one-liner t :side-effects nil))))) |
| 150 |
(ext:with-unique-names (%sublist %elt) |
| 151 |
`(do-in-list (,%elt ,%sublist ,%list) |
| 152 |
(when ,(funcall test-function %value |
| 153 |
(funcall key-function %elt)) |
| 154 |
(return ,%sublist))))) |
| 155 |
|
| 156 |
;;; |
| 157 |
;;; ASSOC |
| 158 |
;;; |
| 159 |
|
| 160 |
(define-seq-compiler-macro assoc (&whole whole value list &key key test test-not) |
| 161 |
(unless key |
| 162 |
(when (or (null test-flag) (eq test-flag :test)) |
| 163 |
(when (member test '('EQ #'EQ) :test #'equal) |
| 164 |
(return-from assoc |
| 165 |
`(ffi:c-inline (,%value ,%list) (:object :object) :object |
| 166 |
"ecl_assq(#0,#1)" :one-liner t :side-effects nil))) |
| 167 |
(when (or (member test '('EQL #'EQL) :test #'equal) (null test)) |
| 168 |
(return-from assoc |
| 169 |
`(ffi:c-inline (,%value ,%list) (:object :object) :object |
| 170 |
"ecl_assql(#0,#1)" :one-liner t :side-effects nil))) |
| 171 |
(when (member test '('EQUAL #'EQUAL) :test #'equal) |
| 172 |
(return-from assoc |
| 173 |
`(ffi:c-inline (,%value ,%list) (:object :object) :object |
| 174 |
"ecl_assoc(#0,#1)" :one-liner t :side-effects nil))) |
| 175 |
(when (member test '('EQUALP #'EQUALP) :test #'equal) |
| 176 |
(return-from assoc |
| 177 |
`(ffi:c-inline (,%value ,%list) (:object :object) :object |
| 178 |
"ecl_assqlp(#0,#1)" :one-liner t :side-effects nil))))) |
| 179 |
(ext:with-unique-names (%sublist %elt %car) |
| 180 |
`(do-in-list (,%elt ,%sublist ,%list) |
| 181 |
(when ,%elt |
| 182 |
(let ((,%car (si:cons-car (optional-type-check ,%elt cons)))) |
| 183 |
(when ,(funcall test-function %value |
| 184 |
(funcall key-function %car)) |
| 185 |
(return ,%elt))))))) |
| 186 |
|
| 187 |
;;; |
| 188 |
;;; FIND |
| 189 |
;;; |
| 190 |
|
| 191 |
(define-seq-compiler-macro find (&whole whole value sequence &key key test test-not (start 0) end from-end) |
| 192 |
(when from-end |
| 193 |
(return-from find whole)) |
| 194 |
(ext:with-unique-names (%elt) |
| 195 |
`(do-in-seq (,%elt ,%sequence :%start ,%start :%end ,%end :end ,end) |
| 196 |
(when ,(funcall test-function %value |
| 197 |
(funcall key-function %elt)) |
| 198 |
(return ,%elt))))) |