Ticket #73: cache-normalized-types.patch
File cache-normalized-types.patch, 11.2 KB (added by , 12 years ago) |
---|
-
boot.lisp
174 174 (load-system-file "proclaim") 175 175 (load-system-file "arrays") 176 176 (load-system-file "compiler-macro") 177 178 (%make-type-cache) 179 177 180 (load-system-file "subtypep") 178 181 (load-system-file "typep") 179 182 (load-system-file "signal") -
deftype.lisp
57 57 (push thing new-lambda-list)) 58 58 (setf lambda-list (nreverse new-lambda-list)))) 59 59 `(progn 60 (%clear-type-cache) 60 61 (setf (get ',name 'deftype-definition) 61 62 #'(lambda ,lambda-list (block ,name ,@body))) 62 63 ',name)) -
early-defuns.lisp
63 63 :datum arg 64 64 :expected-type type))) 65 65 66 (defun normalize-type (type)66 (defun %normalize-type (type) 67 67 (cond ((symbolp type) 68 68 (case type 69 69 (BIT 70 (return-from normalize-type '(integer 0 1)))70 (return-from %normalize-type '(integer 0 1))) 71 71 (CONS 72 (return-from normalize-type '(cons t t)))72 (return-from %normalize-type '(cons t t))) 73 73 (FIXNUM 74 (return-from normalize-type74 (return-from %normalize-type 75 75 '(integer #.most-negative-fixnum #.most-positive-fixnum))) 76 76 (SIGNED-BYTE 77 (return-from normalize-type 'integer))77 (return-from %normalize-type 'integer)) 78 78 (UNSIGNED-BYTE 79 (return-from normalize-type '(integer 0 *)))79 (return-from %normalize-type '(integer 0 *))) 80 80 (BASE-CHAR 81 (return-from normalize-type 'character))81 (return-from %normalize-type 'character)) 82 82 (SHORT-FLOAT 83 (return-from normalize-type 'single-float))83 (return-from %normalize-type 'single-float)) 84 84 (LONG-FLOAT 85 (return-from normalize-type 'double-float))85 (return-from %normalize-type 'double-float)) 86 86 (COMPLEX 87 (return-from normalize-type '(complex *)))87 (return-from %normalize-type '(complex *))) 88 88 (ARRAY 89 (return-from normalize-type '(array * *)))89 (return-from %normalize-type '(array * *))) 90 90 (SIMPLE-ARRAY 91 (return-from normalize-type '(simple-array * *)))91 (return-from %normalize-type '(simple-array * *))) 92 92 (VECTOR 93 (return-from normalize-type '(array * (*))))93 (return-from %normalize-type '(array * (*)))) 94 94 (SIMPLE-VECTOR 95 (return-from normalize-type '(simple-array t (*))))95 (return-from %normalize-type '(simple-array t (*)))) 96 96 (BIT-VECTOR 97 (return-from normalize-type '(bit-vector *)))97 (return-from %normalize-type '(bit-vector *))) 98 98 (SIMPLE-BIT-VECTOR 99 (return-from normalize-type '(simple-bit-vector *)))99 (return-from %normalize-type '(simple-bit-vector *))) 100 100 (BASE-STRING 101 (return-from normalize-type '(array base-char (*))))101 (return-from %normalize-type '(array base-char (*)))) 102 102 (SIMPLE-BASE-STRING 103 (return-from normalize-type '(simple-array base-char (*))))103 (return-from %normalize-type '(simple-array base-char (*)))) 104 104 (STRING 105 (return-from normalize-type '(string *)))105 (return-from %normalize-type '(string *))) 106 106 (SIMPLE-STRING 107 (return-from normalize-type '(simple-string *)))107 (return-from %normalize-type '(simple-string *))) 108 108 ((nil) 109 (return-from normalize-type nil))109 (return-from %normalize-type nil)) 110 110 (t 111 111 (unless (get type 'deftype-definition) 112 (return-from normalize-type type)))))112 (return-from %normalize-type type))))) 113 113 ((classp type) 114 (return-from normalize-type114 (return-from %normalize-type 115 115 (if (eq (%class-name type) 'fixnum) 116 116 '(integer #.most-negative-fixnum #.most-positive-fixnum) 117 117 type))) … … 119 119 (memq (%car type) '(and or not eql member satisfies mod values))) 120 120 (cond ((or (equal type '(and fixnum unsigned-byte)) 121 121 (equal type '(and unsigned-byte fixnum))) 122 (return-from normalize-type '(integer 0 #.most-positive-fixnum)))122 (return-from %normalize-type '(integer 0 #.most-positive-fixnum))) 123 123 (t 124 (return-from normalize-type type)))))124 (return-from %normalize-type type))))) 125 125 ;; Fall through... 126 126 (let (tp i) 127 127 (loop … … 133 133 (return))) 134 134 (case tp 135 135 (INTEGER 136 (return-from normalize-type (if i (cons tp i) tp)))136 (return-from %normalize-type (if i (cons tp i) tp))) 137 137 (CONS 138 138 (let* ((len (length i)) 139 139 (car-typespec (if (> len 0) (car i) t)) 140 140 (cdr-typespec (if (> len 1) (cadr i) t))) 141 141 (unless (and car-typespec cdr-typespec) 142 (return-from normalize-type nil))142 (return-from %normalize-type nil)) 143 143 (when (eq car-typespec '*) 144 144 (setf car-typespec t)) 145 145 (when (eq cdr-typespec '*) 146 146 (setf cdr-typespec t)) 147 (return-from normalize-type (cons tp (list car-typespec cdr-typespec)))))147 (return-from %normalize-type (cons tp (list car-typespec cdr-typespec))))) 148 148 (SIGNED-BYTE 149 149 (if (or (null i) (eq (car i) '*)) 150 (return-from normalize-type 'integer)151 (return-from normalize-type150 (return-from %normalize-type 'integer) 151 (return-from %normalize-type 152 152 (list 'integer 153 153 (- (expt 2 (1- (car i)))) 154 154 (1- (expt 2 (1- (car i)))))))) 155 155 (UNSIGNED-BYTE 156 156 (if (or (null i) (eq (car i) '*)) 157 (return-from normalize-type '(integer 0 *)))158 (return-from normalize-type (list 'integer 0 (1- (expt 2 (car i))))))157 (return-from %normalize-type '(integer 0 *))) 158 (return-from %normalize-type (list 'integer 0 (1- (expt 2 (car i)))))) 159 159 ((ARRAY SIMPLE-ARRAY) 160 160 (unless i 161 (return-from normalize-type (list tp '* '*)))161 (return-from %normalize-type (list tp '* '*))) 162 162 (when (= (length i) 1) 163 163 (setf i (append i '(*)))) 164 (setf (car i) ( normalize-type (car i)))165 (return-from normalize-type (cons tp i)))164 (setf (car i) (%normalize-type (car i))) 165 (return-from %normalize-type (cons tp i))) 166 166 (VECTOR 167 167 (case (length i) 168 168 (0 169 (return-from normalize-type '(array * (*))))169 (return-from %normalize-type '(array * (*)))) 170 170 (1 171 (setf (car i) ( normalize-type (car i)))172 (return-from normalize-type (list 'array (car i) '(*))))171 (setf (car i) (%normalize-type (car i))) 172 (return-from %normalize-type (list 'array (car i) '(*)))) 173 173 (2 174 (setf (car i) ( normalize-type (car i)))175 (return-from normalize-type (list 'array (car i) (list (cadr i)))))174 (setf (car i) (%normalize-type (car i))) 175 (return-from %normalize-type (list 'array (car i) (list (cadr i))))) 176 176 (t 177 177 (error "Invalid type specifier ~S." type)))) 178 178 (SIMPLE-VECTOR 179 179 (case (length i) 180 180 (0 181 (return-from normalize-type '(simple-array t (*))))181 (return-from %normalize-type '(simple-array t (*)))) 182 182 (1 183 (return-from normalize-type (list 'simple-array t (list (car i)))))183 (return-from %normalize-type (list 'simple-array t (list (car i))))) 184 184 (t 185 185 (error "Invalid type specifier ~S." type)))) 186 186 (BIT-VECTOR 187 187 (case (length i) 188 188 (0 189 (return-from normalize-type '(bit-vector *)))189 (return-from %normalize-type '(bit-vector *))) 190 190 (1 191 (return-from normalize-type (list 'bit-vector (car i))))191 (return-from %normalize-type (list 'bit-vector (car i)))) 192 192 (t 193 193 (error "Invalid type specifier ~S." type)))) 194 194 (SIMPLE-BIT-VECTOR 195 195 (case (length i) 196 196 (0 197 (return-from normalize-type '(simple-bit-vector *)))197 (return-from %normalize-type '(simple-bit-vector *))) 198 198 (1 199 (return-from normalize-type (list 'simple-bit-vector (car i))))199 (return-from %normalize-type (list 'simple-bit-vector (car i)))) 200 200 (t 201 201 (error "Invalid type specifier ~S." type)))) 202 202 (BASE-STRING 203 203 (if i 204 (return-from normalize-type (list 'array 'base-char (list (car i))))205 (return-from normalize-type '(array base-char (*)))))204 (return-from %normalize-type (list 'array 'base-char (list (car i)))) 205 (return-from %normalize-type '(array base-char (*))))) 206 206 (SIMPLE-BASE-STRING 207 207 (if i 208 (return-from normalize-type (list 'simple-array 'base-char (list (car i))))209 (return-from normalize-type '(simple-array base-char (*)))))208 (return-from %normalize-type (list 'simple-array 'base-char (list (car i)))) 209 (return-from %normalize-type '(simple-array base-char (*))))) 210 210 (SHORT-FLOAT 211 211 (setf tp 'single-float)) 212 212 (LONG-FLOAT 213 213 (setf tp 'double-float)) 214 214 (COMPLEX 215 215 (cond ((null i) 216 (return-from normalize-type '(complex *)))216 (return-from %normalize-type '(complex *))) 217 217 ((eq (car i) 'short-float) 218 (return-from normalize-type '(complex single-float)))218 (return-from %normalize-type '(complex single-float))) 219 219 ((eq (car i) 'long-float) 220 (return-from normalize-type '(complex double-float))))))220 (return-from %normalize-type '(complex double-float)))))) 221 221 (if i (cons tp i) tp))) 222 222 223 (let (type-cache 224 (calls 0) 225 (hits 0)) 226 (defun %type-cache-stats () 227 (values (hash-table-count type-cache) 228 calls hits (ignore-errors (/ hits calls)))) 229 230 (defun %make-type-cache () 231 (unless type-cache 232 (setf type-cache 233 (make-hash-table :test #'equal 234 :rehash-threshold 0.5 235 :rehash-size 3.0)))) 236 237 (defun %clear-type-cache () 238 (when type-cache 239 (clrhash type-cache))) 240 241 (defun normalize-type (type) 242 (when type-cache 243 (incf calls) 244 (let ((normalized-type (gethash type type-cache))) 245 (when normalized-type 246 (incf hits)) 247 (return-from normalize-type 248 (or normalized-type 249 (setf (gethash type type-cache) 250 (%normalize-type type)))))) 251 (%normalize-type type))) 252 223 253 (defun caaaar (list) (car (car (car (car list))))) 224 254 (defun caaadr (list) (car (car (car (cdr list))))) 225 255 (defun caaddr (list) (car (car (cdr (cdr list)))))