0.8.0.29:
[sbcl/lichteblau.git] / src / pcl / dfun.lisp
blob066585cefec30871b0974631d8328c80d893c1b6
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
4 ;;;; This software is derived from software originally released by Xerox
5 ;;;; Corporation. Copyright and release statements follow. Later modifications
6 ;;;; to the software are in the public domain and are provided with
7 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
8 ;;;; information.
10 ;;;; copyright information from original PCL sources:
11 ;;;;
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
14 ;;;;
15 ;;;; Use and copying of this software and preparation of derivative works based
16 ;;;; upon this software are permitted. Any distribution of this software or
17 ;;;; derivative works must comply with all applicable United States export
18 ;;;; control laws.
19 ;;;;
20 ;;;; This software is made available AS IS, and Xerox Corporation makes no
21 ;;;; warranty about the software, its performance or its conformity to any
22 ;;;; specification.
24 (in-package "SB-PCL")
28 This implementation of method lookup was redone in early August of 89.
30 It has the following properties:
32 - Its modularity makes it easy to modify the actual caching algorithm.
33 The caching algorithm is almost completely separated into the files
34 cache.lisp and dlap.lisp. This file just contains the various uses
35 of it. There will be more tuning as we get more results from Luis'
36 measurements of caching behavior.
38 - The metacircularity issues have been dealt with properly. All of
39 PCL now grounds out properly. Moreover, it is now possible to have
40 metaobject classes which are themselves not instances of standard
41 metaobject classes.
43 ** Modularity of the code **
45 The actual caching algorithm is isolated in a modest number of functions.
46 The code which generates cache lookup code is all found in cache.lisp and
47 dlap.lisp. Certain non-wrapper-caching special cases are in this file.
49 ** Handling the metacircularity **
51 In CLOS, method lookup is the potential source of infinite metacircular
52 regress. The metaobject protocol specification gives us wide flexibility
53 in how to address this problem. PCL uses a technique which handles the
54 problem not only for the metacircular language described in Chapter 3, but
55 also for the PCL protocol which includes additional generic functions
56 which control more aspects of the CLOS implementation.
58 The source of the metacircular regress can be seen in a number of ways.
59 One is that the specified method lookup protocol must, as part of doing
60 the method lookup (or at least the cache miss case), itself call generic
61 functions. It is easy to see that if the method lookup for a generic
62 function ends up calling that same generic function there can be trouble.
64 Fortunately, there is an easy solution at hand. The solution is based on
65 the restriction that portable code cannot change the class of a specified
66 metaobject. This restriction implies that for specified generic functions,
67 the method lookup protocol they follow is fixed.
69 More precisely, for such specified generic functions, most generic functions
70 that are called during their own method lookup will not run portable methods.
71 This allows the implementation to usurp the actual generic function call in
72 this case. In short, method lookup of a standard generic function, in the
73 case where the only applicable methods are themselves standard doesn't
74 have to do any method lookup to implement itself.
76 And so, we are saved.
78 Except see also BREAK-VICIOUS-METACIRCLE. -- CSR, 2003-05-28
82 ;;; an alist in which each entry is of the form
83 ;;; (<generator> . (<subentry> ...)).
84 ;;; Each subentry is of the form
85 ;;; (<args> <constructor> <system>).
86 (defvar *dfun-constructors* ())
88 ;;; If this is NIL, then the whole mechanism for caching dfun constructors is
89 ;;; turned off. The only time that makes sense is when debugging LAP code.
90 (defvar *enable-dfun-constructor-caching* t)
92 (defun show-dfun-constructors ()
93 (format t "~&DFUN constructor caching is ~A."
94 (if *enable-dfun-constructor-caching*
95 "enabled" "disabled"))
96 (dolist (generator-entry *dfun-constructors*)
97 (dolist (args-entry (cdr generator-entry))
98 (format t "~&~S ~S"
99 (cons (car generator-entry) (caar args-entry))
100 (caddr args-entry)))))
102 (defvar *raise-metatypes-to-class-p* t)
104 (defun get-dfun-constructor (generator &rest args)
105 (when (and *raise-metatypes-to-class-p*
106 (member generator '(emit-checking emit-caching
107 emit-in-checking-cache-p emit-constant-value)))
108 (setq args (cons (mapcar (lambda (mt)
109 (if (eq mt t)
111 'class))
112 (car args))
113 (cdr args))))
114 (let* ((generator-entry (assq generator *dfun-constructors*))
115 (args-entry (assoc args (cdr generator-entry) :test #'equal)))
116 (if (null *enable-dfun-constructor-caching*)
117 (apply (fdefinition generator) args)
118 (or (cadr args-entry)
119 (multiple-value-bind (new not-best-p)
120 (apply (symbol-function generator) args)
121 (let ((entry (list (copy-list args) new (unless not-best-p 'pcl)
122 not-best-p)))
123 (if generator-entry
124 (push entry (cdr generator-entry))
125 (push (list generator entry)
126 *dfun-constructors*)))
127 (values new not-best-p))))))
129 (defun load-precompiled-dfun-constructor (generator args system constructor)
130 (let* ((generator-entry (assq generator *dfun-constructors*))
131 (args-entry (assoc args (cdr generator-entry) :test #'equal)))
132 (if args-entry
133 (when (fourth args-entry)
134 (let* ((dfun-type (case generator
135 (emit-checking 'checking)
136 (emit-caching 'caching)
137 (emit-constant-value 'constant-value)
138 (emit-default-only 'default-method-only)))
139 (metatypes (car args))
140 (gfs (when dfun-type (gfs-of-type dfun-type))))
141 (dolist (gf gfs)
142 (when (and (equal metatypes
143 (arg-info-metatypes (gf-arg-info gf)))
144 (let ((gf-name (generic-function-name gf)))
145 (and (not (eq gf-name 'slot-value-using-class))
146 (not (equal gf-name
147 '(setf slot-value-using-class)))
148 (not (eq gf-name 'slot-boundp-using-class)))))
149 (update-dfun gf)))
150 (setf (second args-entry) constructor)
151 (setf (third args-entry) system)
152 (setf (fourth args-entry) nil)))
153 (let ((entry (list args constructor system nil)))
154 (if generator-entry
155 (push entry (cdr generator-entry))
156 (push (list generator entry) *dfun-constructors*))))))
158 (defmacro precompile-dfun-constructors (&optional system)
159 (let ((*precompiling-lap* t))
160 `(progn
161 ,@(let (collect)
162 (dolist (generator-entry *dfun-constructors*)
163 (dolist (args-entry (cdr generator-entry))
164 (when (or (null (caddr args-entry))
165 (eq (caddr args-entry) system))
166 (when system (setf (caddr args-entry) system))
167 (push `(load-precompiled-dfun-constructor
168 ',(car generator-entry)
169 ',(car args-entry)
170 ',system
171 ,(apply (fdefinition (car generator-entry))
172 (car args-entry)))
173 collect))))
174 (nreverse collect)))))
176 ;;; Standardized class slot access: when trying to break vicious
177 ;;; metacircles, we need a way to get at the values of slots of some
178 ;;; standard classes without going through the whole meta machinery,
179 ;;; because that would likely enter the vicious circle again. The
180 ;;; following are helper functions that short-circuit the generic
181 ;;; lookup machinery.
183 (defvar *standard-classes*
184 '(standard-method standard-generic-function standard-class
185 standard-effective-slot-definition))
187 (defvar *standard-slot-locations* (make-hash-table :test 'equal))
189 (defun compute-standard-slot-locations ()
190 (clrhash *standard-slot-locations*)
191 (dolist (class-name *standard-classes*)
192 (let ((class (find-class class-name)))
193 (dolist (slot (class-slots class))
194 (setf (gethash (cons class (slot-definition-name slot))
195 *standard-slot-locations*)
196 (slot-definition-location slot))))))
198 ;;; FIXME: harmonize the names between COMPUTE-STANDARD-SLOT-LOCATIONS
199 ;;; and MAYBE-UPDATE-STANDARD-CLASS-LOCATIONS.
200 (defun maybe-update-standard-class-locations (class)
201 (when (and (eq *boot-state* 'complete)
202 (memq (class-name class) *standard-classes*))
203 (compute-standard-slot-locations)))
205 (defun standard-slot-value (object slot-name class)
206 (let ((location (gethash (cons class slot-name) *standard-slot-locations*)))
207 (if location
208 (let ((value (if (funcallable-instance-p object)
209 (funcallable-standard-instance-access object location)
210 (standard-instance-access object location))))
211 (when (eq +slot-unbound+ value)
212 (error "~@<slot ~s of class ~s is unbound in object ~s~@:>"
213 slot-name class object))
214 value)
215 (error "~@<cannot get standard value of slot ~s of class ~s ~
216 in object ~s~@:>"
217 slot-name class object))))
219 (defun standard-slot-value/gf (gf slot-name)
220 (standard-slot-value gf slot-name *the-class-standard-generic-function*))
222 (defun standard-slot-value/method (method slot-name)
223 (standard-slot-value method slot-name *the-class-standard-method*))
225 (defun standard-slot-value/eslotd (slotd slot-name)
226 (standard-slot-value slotd slot-name
227 *the-class-standard-effective-slot-definition*))
229 (defun standard-slot-value/class (class slot-name)
230 (standard-slot-value class slot-name *the-class-standard-class*))
232 ;;; When all the methods of a generic function are automatically
233 ;;; generated reader or writer methods a number of special
234 ;;; optimizations are possible. These are important because of the
235 ;;; large number of generic functions of this type.
237 ;;; There are a number of cases:
239 ;;; ONE-CLASS-ACCESSOR
240 ;;; In this case, the accessor generic function has only been
241 ;;; called with one class of argument. There is no cache vector,
242 ;;; the wrapper of the one class, and the slot index are stored
243 ;;; directly as closure variables of the discriminating function.
244 ;;; This case can convert to either of the next kind.
246 ;;; TWO-CLASS-ACCESSOR
247 ;;; Like above, but two classes. This is common enough to do
248 ;;; specially. There is no cache vector. The two classes are
249 ;;; stored a separate closure variables.
251 ;;; ONE-INDEX-ACCESSOR
252 ;;; In this case, the accessor generic function has seen more than
253 ;;; one class of argument, but the index of the slot is the same
254 ;;; for all the classes that have been seen. A cache vector is
255 ;;; used to store the wrappers that have been seen, the slot index
256 ;;; is stored directly as a closure variable of the discriminating
257 ;;; function. This case can convert to the next kind.
259 ;;; N-N-ACCESSOR
260 ;;; This is the most general case. In this case, the accessor
261 ;;; generic function has seen more than one class of argument and
262 ;;; more than one slot index. A cache vector stores the wrappers
263 ;;; and corresponding slot indexes. Because each cache line is
264 ;;; more than one element long, a cache lock count is used.
265 (defstruct (dfun-info (:constructor nil)
266 (:copier nil))
267 (cache nil))
269 (defstruct (no-methods (:constructor no-methods-dfun-info ())
270 (:include dfun-info)
271 (:copier nil)))
273 (defstruct (initial (:constructor initial-dfun-info ())
274 (:include dfun-info)
275 (:copier nil)))
277 (defstruct (initial-dispatch (:constructor initial-dispatch-dfun-info ())
278 (:include dfun-info)
279 (:copier nil)))
281 (defstruct (dispatch (:constructor dispatch-dfun-info ())
282 (:include dfun-info)
283 (:copier nil)))
285 (defstruct (default-method-only (:constructor default-method-only-dfun-info ())
286 (:include dfun-info)
287 (:copier nil)))
289 ;without caching:
290 ; dispatch one-class two-class default-method-only
292 ;with caching:
293 ; one-index n-n checking caching
295 ;accessor:
296 ; one-class two-class one-index n-n
297 (defstruct (accessor-dfun-info (:constructor nil)
298 (:include dfun-info)
299 (:copier nil))
300 accessor-type) ; (member reader writer)
302 (defmacro dfun-info-accessor-type (di)
303 `(accessor-dfun-info-accessor-type ,di))
305 (defstruct (one-index-dfun-info (:constructor nil)
306 (:include accessor-dfun-info)
307 (:copier nil))
308 index)
310 (defmacro dfun-info-index (di)
311 `(one-index-dfun-info-index ,di))
313 (defstruct (n-n (:constructor n-n-dfun-info (accessor-type cache))
314 (:include accessor-dfun-info)
315 (:copier nil)))
317 (defstruct (one-class (:constructor one-class-dfun-info
318 (accessor-type index wrapper0))
319 (:include one-index-dfun-info)
320 (:copier nil))
321 wrapper0)
323 (defmacro dfun-info-wrapper0 (di)
324 `(one-class-wrapper0 ,di))
326 (defstruct (two-class (:constructor two-class-dfun-info
327 (accessor-type index wrapper0 wrapper1))
328 (:include one-class)
329 (:copier nil))
330 wrapper1)
332 (defmacro dfun-info-wrapper1 (di)
333 `(two-class-wrapper1 ,di))
335 (defstruct (one-index (:constructor one-index-dfun-info
336 (accessor-type index cache))
337 (:include one-index-dfun-info)
338 (:copier nil)))
340 (defstruct (checking (:constructor checking-dfun-info (function cache))
341 (:include dfun-info)
342 (:copier nil))
343 function)
345 (defmacro dfun-info-function (di)
346 `(checking-function ,di))
348 (defstruct (caching (:constructor caching-dfun-info (cache))
349 (:include dfun-info)
350 (:copier nil)))
352 (defstruct (constant-value (:constructor constant-value-dfun-info (cache))
353 (:include dfun-info)
354 (:copier nil)))
356 (defmacro dfun-update (generic-function function &rest args)
357 `(multiple-value-bind (dfun cache info)
358 (funcall ,function ,generic-function ,@args)
359 (update-dfun ,generic-function dfun cache info)))
361 (defun accessor-miss-function (gf dfun-info)
362 (ecase (dfun-info-accessor-type dfun-info)
363 ((reader boundp)
364 (lambda (arg)
365 (accessor-miss gf nil arg dfun-info)))
366 (writer
367 (lambda (new arg)
368 (accessor-miss gf new arg dfun-info)))))
370 #-sb-fluid (declaim (sb-ext:freeze-type dfun-info))
372 (defun make-one-class-accessor-dfun (gf type wrapper index)
373 (let ((emit (ecase type
374 (reader 'emit-one-class-reader)
375 (boundp 'emit-one-class-boundp)
376 (writer 'emit-one-class-writer)))
377 (dfun-info (one-class-dfun-info type index wrapper)))
378 (values
379 (funcall (get-dfun-constructor emit (consp index))
380 wrapper index
381 (accessor-miss-function gf dfun-info))
383 dfun-info)))
385 (defun make-two-class-accessor-dfun (gf type w0 w1 index)
386 (let ((emit (ecase type
387 (reader 'emit-two-class-reader)
388 (boundp 'emit-two-class-boundp)
389 (writer 'emit-two-class-writer)))
390 (dfun-info (two-class-dfun-info type index w0 w1)))
391 (values
392 (funcall (get-dfun-constructor emit (consp index))
393 w0 w1 index
394 (accessor-miss-function gf dfun-info))
396 dfun-info)))
398 ;;; std accessors same index dfun
399 (defun make-one-index-accessor-dfun (gf type index &optional cache)
400 (let* ((emit (ecase type
401 (reader 'emit-one-index-readers)
402 (boundp 'emit-one-index-boundps)
403 (writer 'emit-one-index-writers)))
404 (cache (or cache (get-cache 1 nil #'one-index-limit-fn 4)))
405 (dfun-info (one-index-dfun-info type index cache)))
406 (declare (type cache cache))
407 (values
408 (funcall (get-dfun-constructor emit (consp index))
409 cache
410 index
411 (accessor-miss-function gf dfun-info))
412 cache
413 dfun-info)))
415 (defun make-final-one-index-accessor-dfun (gf type index table)
416 (let ((cache (fill-dfun-cache table nil 1 #'one-index-limit-fn)))
417 (make-one-index-accessor-dfun gf type index cache)))
419 (defun one-index-limit-fn (nlines)
420 (default-limit-fn nlines))
422 (defun make-n-n-accessor-dfun (gf type &optional cache)
423 (let* ((emit (ecase type
424 (reader 'emit-n-n-readers)
425 (boundp 'emit-n-n-boundps)
426 (writer 'emit-n-n-writers)))
427 (cache (or cache (get-cache 1 t #'n-n-accessors-limit-fn 2)))
428 (dfun-info (n-n-dfun-info type cache)))
429 (declare (type cache cache))
430 (values
431 (funcall (get-dfun-constructor emit)
432 cache
433 (accessor-miss-function gf dfun-info))
434 cache
435 dfun-info)))
437 (defun make-final-n-n-accessor-dfun (gf type table)
438 (let ((cache (fill-dfun-cache table t 1 #'n-n-accessors-limit-fn)))
439 (make-n-n-accessor-dfun gf type cache)))
441 (defun n-n-accessors-limit-fn (nlines)
442 (default-limit-fn nlines))
444 (defun make-checking-dfun (generic-function function &optional cache)
445 (unless cache
446 (when (use-caching-dfun-p generic-function)
447 (return-from make-checking-dfun (make-caching-dfun generic-function)))
448 (when (use-dispatch-dfun-p generic-function)
449 (return-from make-checking-dfun (make-dispatch-dfun generic-function))))
450 (multiple-value-bind (nreq applyp metatypes nkeys)
451 (get-generic-fun-info generic-function)
452 (declare (ignore nreq))
453 (if (every (lambda (mt) (eq mt t)) metatypes)
454 (let ((dfun-info (default-method-only-dfun-info)))
455 (values
456 (funcall (get-dfun-constructor 'emit-default-only metatypes applyp)
457 function)
459 dfun-info))
460 (let* ((cache (or cache (get-cache nkeys nil #'checking-limit-fn 2)))
461 (dfun-info (checking-dfun-info function cache)))
462 (values
463 (funcall (get-dfun-constructor 'emit-checking metatypes applyp)
464 cache
465 function
466 (lambda (&rest args)
467 (checking-miss generic-function args dfun-info)))
468 cache
469 dfun-info)))))
471 (defun make-final-checking-dfun (generic-function function
472 classes-list new-class)
473 (let ((metatypes (arg-info-metatypes (gf-arg-info generic-function))))
474 (if (every (lambda (mt) (eq mt t)) metatypes)
475 (values (lambda (&rest args)
476 (invoke-emf function args))
477 nil (default-method-only-dfun-info))
478 (let ((cache (make-final-ordinary-dfun-internal
479 generic-function nil #'checking-limit-fn
480 classes-list new-class)))
481 (make-checking-dfun generic-function function cache)))))
483 (defun use-default-method-only-dfun-p (generic-function)
484 (multiple-value-bind (nreq applyp metatypes nkeys)
485 (get-generic-fun-info generic-function)
486 (declare (ignore nreq applyp nkeys))
487 (every (lambda (mt) (eq mt t)) metatypes)))
489 (defun use-caching-dfun-p (generic-function)
490 (some (lambda (method)
491 (let ((fmf (if (listp method)
492 (third method)
493 (method-fast-function method))))
494 (method-function-get fmf :slot-name-lists)))
495 ;; KLUDGE: As of sbcl-0.6.4, it's very important for
496 ;; efficiency to know the type of the sequence argument to
497 ;; quantifiers (SOME/NOTANY/etc.) at compile time, but
498 ;; the compiler isn't smart enough to understand the :TYPE
499 ;; slot option for DEFCLASS, so we just tell
500 ;; it the type by hand here.
501 (the list
502 (if (early-gf-p generic-function)
503 (early-gf-methods generic-function)
504 (generic-function-methods generic-function)))))
506 (defun checking-limit-fn (nlines)
507 (default-limit-fn nlines))
509 (defun make-caching-dfun (generic-function &optional cache)
510 (unless cache
511 (when (use-constant-value-dfun-p generic-function)
512 (return-from make-caching-dfun
513 (make-constant-value-dfun generic-function)))
514 (when (use-dispatch-dfun-p generic-function)
515 (return-from make-caching-dfun
516 (make-dispatch-dfun generic-function))))
517 (multiple-value-bind (nreq applyp metatypes nkeys)
518 (get-generic-fun-info generic-function)
519 (declare (ignore nreq))
520 (let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
521 (dfun-info (caching-dfun-info cache)))
522 (values
523 (funcall (get-dfun-constructor 'emit-caching metatypes applyp)
524 cache
525 (lambda (&rest args)
526 (caching-miss generic-function args dfun-info)))
527 cache
528 dfun-info))))
530 (defun make-final-caching-dfun (generic-function classes-list new-class)
531 (let ((cache (make-final-ordinary-dfun-internal
532 generic-function t #'caching-limit-fn
533 classes-list new-class)))
534 (make-caching-dfun generic-function cache)))
536 (defun caching-limit-fn (nlines)
537 (default-limit-fn nlines))
539 (defun insure-caching-dfun (gf)
540 (multiple-value-bind (nreq applyp metatypes nkeys)
541 (get-generic-fun-info gf)
542 (declare (ignore nreq nkeys))
543 (when (and metatypes
544 (not (null (car metatypes)))
545 (dolist (mt metatypes nil)
546 (unless (eq mt t) (return t))))
547 (get-dfun-constructor 'emit-caching metatypes applyp))))
549 (defun use-constant-value-dfun-p (gf &optional boolean-values-p)
550 (multiple-value-bind (nreq applyp metatypes nkeys)
551 (get-generic-fun-info gf)
552 (declare (ignore nreq metatypes nkeys))
553 (let* ((early-p (early-gf-p gf))
554 (methods (if early-p
555 (early-gf-methods gf)
556 (generic-function-methods gf)))
557 (default '(unknown)))
558 (and (null applyp)
559 (or (not (eq *boot-state* 'complete))
560 (compute-applicable-methods-emf-std-p gf))
561 (notany (lambda (method)
562 (or (and (eq *boot-state* 'complete)
563 (some #'eql-specializer-p
564 (method-specializers method)))
565 (let ((value (method-function-get
566 (if early-p
567 (or (third method) (second method))
568 (or (method-fast-function method)
569 (method-function method)))
570 :constant-value default)))
571 (if boolean-values-p
572 (not (or (eq value t) (eq value nil)))
573 (eq value default)))))
574 methods)))))
576 (defun make-constant-value-dfun (generic-function &optional cache)
577 (multiple-value-bind (nreq applyp metatypes nkeys)
578 (get-generic-fun-info generic-function)
579 (declare (ignore nreq applyp))
580 (let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
581 (dfun-info (constant-value-dfun-info cache)))
582 (values
583 (funcall (get-dfun-constructor 'emit-constant-value metatypes)
584 cache
585 (lambda (&rest args)
586 (constant-value-miss generic-function args dfun-info)))
587 cache
588 dfun-info))))
590 (defun make-final-constant-value-dfun (generic-function classes-list new-class)
591 (let ((cache (make-final-ordinary-dfun-internal
592 generic-function :constant-value #'caching-limit-fn
593 classes-list new-class)))
594 (make-constant-value-dfun generic-function cache)))
596 (defun use-dispatch-dfun-p (gf &optional (caching-p (use-caching-dfun-p gf)))
597 (when (eq *boot-state* 'complete)
598 (unless caching-p
599 ;; This should return T when almost all dispatching is by
600 ;; eql specializers or built-in classes. In other words,
601 ;; return NIL if we might ever need to do more than
602 ;; one (non built-in) typep.
603 ;; Otherwise, it is probably at least as fast to use
604 ;; a caching dfun first, possibly followed by secondary dispatching.
606 #||;;; Original found in cmu 17f -- S L O W
607 (< (dispatch-dfun-cost gf) (caching-dfun-cost gf))
609 ;; This uses improved dispatch-dfun-cost below
610 (let ((cdc (caching-dfun-cost gf))) ; fast
611 (> cdc (dispatch-dfun-cost gf cdc))))))
613 (defparameter *non-built-in-typep-cost* 1)
614 (defparameter *structure-typep-cost* 1)
615 (defparameter *built-in-typep-cost* 0)
617 ;;; According to comments in the original CMU CL version of PCL,
618 ;;; the cost LIMIT is important to cut off exponential growth for
619 ;;; large numbers of gf methods and argument lists.
620 (defun dispatch-dfun-cost (gf &optional limit)
621 (generate-discrimination-net-internal
622 gf (generic-function-methods gf) nil
623 (lambda (methods known-types)
624 (declare (ignore methods known-types))
626 (lambda (position type true-value false-value)
627 (declare (ignore position))
628 (let* ((type-test-cost
629 (if (eq 'class (car type))
630 (let* ((metaclass (class-of (cadr type)))
631 (mcpl (class-precedence-list metaclass)))
632 (cond ((memq *the-class-built-in-class* mcpl)
633 *built-in-typep-cost*)
634 ((memq *the-class-structure-class* mcpl)
635 *structure-typep-cost*)
637 *non-built-in-typep-cost*)))
639 (max-cost-so-far
640 (+ (max true-value false-value) type-test-cost)))
641 (when (and limit (<= limit max-cost-so-far))
642 (return-from dispatch-dfun-cost max-cost-so-far))
643 max-cost-so-far))
644 #'identity))
646 (defparameter *cache-lookup-cost* 1)
647 (defparameter *wrapper-of-cost* 0)
648 (defparameter *secondary-dfun-call-cost* 1)
650 (defun caching-dfun-cost (gf)
651 (let* ((arg-info (gf-arg-info gf))
652 (nreq (length (arg-info-metatypes arg-info))))
653 (+ *cache-lookup-cost*
654 (* *wrapper-of-cost* nreq)
655 (if (methods-contain-eql-specializer-p
656 (generic-function-methods gf))
657 *secondary-dfun-call-cost*
658 0))))
660 (setq *non-built-in-typep-cost* 100)
661 (setq *structure-typep-cost* 15)
662 (setq *built-in-typep-cost* 5)
663 (setq *cache-lookup-cost* 30)
664 (setq *wrapper-of-cost* 15)
665 (setq *secondary-dfun-call-cost* 30)
667 (defun make-dispatch-dfun (gf)
668 (values (get-dispatch-function gf) nil (dispatch-dfun-info)))
670 (defun get-dispatch-function (gf)
671 (let ((methods (generic-function-methods gf)))
672 (function-funcall (get-secondary-dispatch-function1 gf methods nil nil nil
673 nil nil t)
674 nil nil)))
676 (defun make-final-dispatch-dfun (gf)
677 (make-dispatch-dfun gf))
679 (defun update-dispatch-dfuns ()
680 (dolist (gf (gfs-of-type '(dispatch initial-dispatch)))
681 (dfun-update gf #'make-dispatch-dfun)))
683 (defun fill-dfun-cache (table valuep nkeys limit-fn &optional cache)
684 (let ((cache (or cache (get-cache nkeys valuep limit-fn
685 (+ (hash-table-count table) 3)))))
686 (maphash (lambda (classes value)
687 (setq cache (fill-cache cache
688 (class-wrapper classes)
689 value)))
690 table)
691 cache))
693 (defun make-final-ordinary-dfun-internal (generic-function valuep limit-fn
694 classes-list new-class)
695 (let* ((arg-info (gf-arg-info generic-function))
696 (nkeys (arg-info-nkeys arg-info))
697 (new-class (and new-class
698 (equal (type-of (gf-dfun-info generic-function))
699 (cond ((eq valuep t) 'caching)
700 ((eq valuep :constant-value) 'constant-value)
701 ((null valuep) 'checking)))
702 new-class))
703 (cache (if new-class
704 (copy-cache (gf-dfun-cache generic-function))
705 (get-cache nkeys (not (null valuep)) limit-fn 4))))
706 (make-emf-cache generic-function valuep cache classes-list new-class)))
708 (defvar *dfun-miss-gfs-on-stack* ())
710 (defmacro dfun-miss ((gf args wrappers invalidp nemf
711 &optional type index caching-p applicable)
712 &body body)
713 (unless applicable (setq applicable (gensym)))
714 `(multiple-value-bind (,nemf ,applicable ,wrappers ,invalidp
715 ,@(when type `(,type ,index)))
716 (cache-miss-values ,gf ,args ',(cond (caching-p 'caching)
717 (type 'accessor)
718 (t 'checking)))
719 (when (and ,applicable (not (memq ,gf *dfun-miss-gfs-on-stack*)))
720 (let ((*dfun-miss-gfs-on-stack* (cons ,gf *dfun-miss-gfs-on-stack*)))
721 ,@body))
722 ;; Create a FAST-INSTANCE-BOUNDP structure instance for a cached
723 ;; SLOT-BOUNDP so that INVOKE-EMF does the right thing, that is,
724 ;; does not signal a SLOT-UNBOUND error for a boundp test.
725 ,@(if type
726 ;; FIXME: could the NEMF not be a CONS (for :CLASS-allocated
727 ;; slots?)
728 `((if (and (eq ,type 'boundp) (integerp ,nemf))
729 (invoke-emf (make-fast-instance-boundp :index ,nemf) ,args)
730 (invoke-emf ,nemf ,args)))
731 `((invoke-emf ,nemf ,args)))))
733 ;;; The dynamically adaptive method lookup algorithm is implemented is
734 ;;; implemented as a kind of state machine. The kinds of
735 ;;; discriminating function is the state, the various kinds of reasons
736 ;;; for a cache miss are the state transitions.
738 ;;; The code which implements the transitions is all in the miss
739 ;;; handlers for each kind of dfun. Those appear here.
741 ;;; Note that within the states that cache, there are dfun updates
742 ;;; which simply select a new cache or cache field. Those are not
743 ;;; considered as state transitions.
744 (defvar *lazy-dfun-compute-p* t)
745 (defvar *early-p* nil)
747 (defun finalize-specializers (gf)
748 (let ((all-finalized t))
749 (dolist (method (generic-function-methods gf))
750 (dolist (specializer (method-specializers method))
751 (when (and (classp specializer)
752 (not (class-finalized-p specializer)))
753 (if (class-has-a-forward-referenced-superclass-p specializer)
754 (setq all-finalized nil)
755 (finalize-inheritance specializer)))))
756 all-finalized))
758 (defun make-initial-dfun (gf)
759 (let ((initial-dfun
760 #'(instance-lambda (&rest args)
761 (initial-dfun gf args))))
762 (multiple-value-bind (dfun cache info)
763 (cond
764 ((and (eq *boot-state* 'complete)
765 (not (finalize-specializers gf)))
766 (values initial-dfun nil (initial-dfun-info)))
767 ((and (eq *boot-state* 'complete)
768 (compute-applicable-methods-emf-std-p gf))
769 (let* ((caching-p (use-caching-dfun-p gf))
770 ;; KLUDGE: the only effect of this (when
771 ;; *LAZY-DFUN-COMPUTE-P* is true, as it usually is)
772 ;; is to signal an error when we try to add methods
773 ;; with the wrong qualifiers to a generic function.
774 (classes-list (precompute-effective-methods
775 gf caching-p
776 (not *lazy-dfun-compute-p*))))
777 (if *lazy-dfun-compute-p*
778 (cond ((use-dispatch-dfun-p gf caching-p)
779 (values initial-dfun
781 (initial-dispatch-dfun-info)))
782 (caching-p
783 (insure-caching-dfun gf)
784 (values initial-dfun nil (initial-dfun-info)))
786 (values initial-dfun nil (initial-dfun-info))))
787 (make-final-dfun-internal gf classes-list))))
789 (let ((arg-info (if (early-gf-p gf)
790 (early-gf-arg-info gf)
791 (gf-arg-info gf)))
792 (type nil))
793 (if (and (gf-precompute-dfun-and-emf-p arg-info)
794 (setq type (final-accessor-dfun-type gf)))
795 (if *early-p*
796 (values (make-early-accessor gf type) nil nil)
797 (make-final-accessor-dfun gf type))
798 (values initial-dfun nil (initial-dfun-info))))))
799 (set-dfun gf dfun cache info))))
801 (defun make-early-accessor (gf type)
802 (let* ((methods (early-gf-methods gf))
803 (slot-name (early-method-standard-accessor-slot-name (car methods))))
804 (ecase type
805 (reader #'(instance-lambda (instance)
806 (let* ((class (class-of instance))
807 (class-name (!bootstrap-get-slot 'class class 'name)))
808 (!bootstrap-get-slot class-name instance slot-name))))
809 (boundp #'(instance-lambda (instance)
810 (let* ((class (class-of instance))
811 (class-name (!bootstrap-get-slot 'class class 'name)))
812 (not (eq +slot-unbound+
813 (!bootstrap-get-slot class-name
814 instance slot-name))))))
815 (writer #'(instance-lambda (new-value instance)
816 (let* ((class (class-of instance))
817 (class-name (!bootstrap-get-slot 'class class 'name)))
818 (!bootstrap-set-slot class-name instance slot-name new-value)))))))
820 (defun initial-dfun (gf args)
821 (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
822 (cond (invalidp)
823 ((and ntype nindex)
824 (dfun-update
825 gf #'make-one-class-accessor-dfun ntype wrappers nindex))
826 ((use-caching-dfun-p gf)
827 (dfun-update gf #'make-caching-dfun))
829 (dfun-update
830 gf #'make-checking-dfun
831 ;; nemf is suitable only for caching, have to do this:
832 (cache-miss-values gf args 'checking))))))
834 (defun make-final-dfun (gf &optional classes-list)
835 (multiple-value-bind (dfun cache info)
836 (make-final-dfun-internal gf classes-list)
837 (set-dfun gf dfun cache info)))
839 (defvar *new-class* nil)
841 (defvar *free-hash-tables* (mapcar #'list '(eq equal eql)))
843 (defmacro with-hash-table ((table test) &body forms)
844 `(let* ((.free. (assoc ',test *free-hash-tables*))
845 (,table (if (cdr .free.)
846 (pop (cdr .free.))
847 (make-hash-table :test ',test))))
848 (multiple-value-prog1
849 (progn ,@forms)
850 (clrhash ,table)
851 (push ,table (cdr .free.)))))
853 (defmacro with-eq-hash-table ((table) &body forms)
854 `(with-hash-table (,table eq) ,@forms))
856 (defun final-accessor-dfun-type (gf)
857 (let ((methods (if (early-gf-p gf)
858 (early-gf-methods gf)
859 (generic-function-methods gf))))
860 (cond ((every (lambda (method)
861 (if (consp method)
862 (eq *the-class-standard-reader-method*
863 (early-method-class method))
864 (standard-reader-method-p method)))
865 methods)
866 'reader)
867 ((every (lambda (method)
868 (if (consp method)
869 (eq *the-class-standard-boundp-method*
870 (early-method-class method))
871 (standard-boundp-method-p method)))
872 methods)
873 'boundp)
874 ((every (lambda (method)
875 (if (consp method)
876 (eq *the-class-standard-writer-method*
877 (early-method-class method))
878 (standard-writer-method-p method)))
879 methods)
880 'writer))))
882 (defun make-final-accessor-dfun (gf type &optional classes-list new-class)
883 (with-eq-hash-table (table)
884 (multiple-value-bind (table all-index first second size no-class-slots-p)
885 (make-accessor-table gf type table)
886 (if table
887 (cond ((= size 1)
888 (let ((w (class-wrapper first)))
889 (make-one-class-accessor-dfun gf type w all-index)))
890 ((and (= size 2) (or (integerp all-index) (consp all-index)))
891 (let ((w0 (class-wrapper first))
892 (w1 (class-wrapper second)))
893 (make-two-class-accessor-dfun gf type w0 w1 all-index)))
894 ((or (integerp all-index) (consp all-index))
895 (make-final-one-index-accessor-dfun
896 gf type all-index table))
897 (no-class-slots-p
898 (make-final-n-n-accessor-dfun gf type table))
900 (make-final-caching-dfun gf classes-list new-class)))
901 (make-final-caching-dfun gf classes-list new-class)))))
903 (defun make-final-dfun-internal (gf &optional classes-list)
904 (let ((methods (generic-function-methods gf)) type
905 (new-class *new-class*) (*new-class* nil)
906 specls all-same-p)
907 (cond ((null methods)
908 (values
909 #'(instance-lambda (&rest args)
910 (apply #'no-applicable-method gf args))
912 (no-methods-dfun-info)))
913 ((setq type (final-accessor-dfun-type gf))
914 (make-final-accessor-dfun gf type classes-list new-class))
915 ((and (not (and (every (lambda (specl) (eq specl *the-class-t*))
916 (setq specls
917 (method-specializers (car methods))))
918 (setq all-same-p
919 (every (lambda (method)
920 (and (equal specls
921 (method-specializers
922 method))))
923 methods))))
924 (use-constant-value-dfun-p gf))
925 (make-final-constant-value-dfun gf classes-list new-class))
926 ((use-dispatch-dfun-p gf)
927 (make-final-dispatch-dfun gf))
928 ((and all-same-p (not (use-caching-dfun-p gf)))
929 (let ((emf (get-secondary-dispatch-function gf methods nil)))
930 (make-final-checking-dfun gf emf classes-list new-class)))
932 (make-final-caching-dfun gf classes-list new-class)))))
934 (defun accessor-miss (gf new object dfun-info)
935 (let* ((ostate (type-of dfun-info))
936 (otype (dfun-info-accessor-type dfun-info))
937 oindex ow0 ow1 cache
938 (args (ecase otype
939 ;; The congruence rules ensure that this is safe
940 ;; despite not knowing the new type yet.
941 ((reader boundp) (list object))
942 (writer (list new object)))))
943 (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
945 ;; The following lexical functions change the state of the
946 ;; dfun to that which is their name. They accept arguments
947 ;; which are the parameters of the new state, and get other
948 ;; information from the lexical variables bound above.
949 (flet ((two-class (index w0 w1)
950 (when (zerop (random 2)) (psetf w0 w1 w1 w0))
951 (dfun-update gf
952 #'make-two-class-accessor-dfun
953 ntype
956 index))
957 (one-index (index &optional cache)
958 (dfun-update gf
959 #'make-one-index-accessor-dfun
960 ntype
961 index
962 cache))
963 (n-n (&optional cache)
964 (if (consp nindex)
965 (dfun-update gf #'make-checking-dfun nemf)
966 (dfun-update gf #'make-n-n-accessor-dfun ntype cache)))
967 (caching () ; because cached accessor emfs are much faster
968 ; for accessors
969 (dfun-update gf #'make-caching-dfun))
970 (do-fill (update-fn)
971 (let ((ncache (fill-cache cache wrappers nindex)))
972 (unless (eq ncache cache)
973 (funcall update-fn ncache)))))
975 (cond ((null ntype)
976 (caching))
977 ((or invalidp
978 (null nindex)))
979 ((not (pcl-instance-p object))
980 (caching))
981 ((or (neq ntype otype) (listp wrappers))
982 (caching))
984 (ecase ostate
985 (one-class
986 (setq oindex (dfun-info-index dfun-info))
987 (setq ow0 (dfun-info-wrapper0 dfun-info))
988 (unless (eq ow0 wrappers)
989 (if (eql nindex oindex)
990 (two-class nindex ow0 wrappers)
991 (n-n))))
992 (two-class
993 (setq oindex (dfun-info-index dfun-info))
994 (setq ow0 (dfun-info-wrapper0 dfun-info))
995 (setq ow1 (dfun-info-wrapper1 dfun-info))
996 (unless (or (eq ow0 wrappers) (eq ow1 wrappers))
997 (if (eql nindex oindex)
998 (one-index nindex)
999 (n-n))))
1000 (one-index
1001 (setq oindex (dfun-info-index dfun-info))
1002 (setq cache (dfun-info-cache dfun-info))
1003 (if (eql nindex oindex)
1004 (do-fill (lambda (ncache)
1005 (one-index nindex ncache)))
1006 (n-n)))
1007 (n-n
1008 (setq cache (dfun-info-cache dfun-info))
1009 (if (consp nindex)
1010 (caching)
1011 (do-fill #'n-n))))))))))
1013 (defun checking-miss (generic-function args dfun-info)
1014 (let ((oemf (dfun-info-function dfun-info))
1015 (cache (dfun-info-cache dfun-info)))
1016 (dfun-miss (generic-function args wrappers invalidp nemf)
1017 (cond (invalidp)
1018 ((eq oemf nemf)
1019 (let ((ncache (fill-cache cache wrappers nil)))
1020 (unless (eq ncache cache)
1021 (dfun-update generic-function #'make-checking-dfun
1022 nemf ncache))))
1024 (dfun-update generic-function #'make-caching-dfun))))))
1026 (defun caching-miss (generic-function args dfun-info)
1027 (let ((ocache (dfun-info-cache dfun-info)))
1028 (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
1029 (cond (invalidp)
1031 (let ((ncache (fill-cache ocache wrappers emf)))
1032 (unless (eq ncache ocache)
1033 (dfun-update generic-function
1034 #'make-caching-dfun ncache))))))))
1036 (defun constant-value-miss (generic-function args dfun-info)
1037 (let ((ocache (dfun-info-cache dfun-info)))
1038 (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
1039 (cond (invalidp)
1041 (let* ((function (typecase emf
1042 (fast-method-call (fast-method-call-function
1043 emf))
1044 (method-call (method-call-function emf))))
1045 (value (method-function-get function :constant-value))
1046 (ncache (fill-cache ocache wrappers value)))
1047 (unless (eq ncache ocache)
1048 (dfun-update generic-function
1049 #'make-constant-value-dfun ncache))))))))
1051 ;;; Given a generic function and a set of arguments to that generic
1052 ;;; function, return a mess of values.
1054 ;;; <function> The compiled effective method function for this set of
1055 ;;; arguments.
1057 ;;; <applicable> Sorted list of applicable methods.
1059 ;;; <wrappers> Is a single wrapper if the generic function has only
1060 ;;; one key, that is arg-info-nkeys of the arg-info is 1.
1061 ;;; Otherwise a list of the wrappers of the specialized
1062 ;;; arguments to the generic function.
1064 ;;; Note that all these wrappers are valid. This function
1065 ;;; does invalid wrapper traps when it finds an invalid
1066 ;;; wrapper and then returns the new, valid wrapper.
1068 ;;; <invalidp> True if any of the specialized arguments had an invalid
1069 ;;; wrapper, false otherwise.
1071 ;;; <type> READER or WRITER when the only method that would be run
1072 ;;; is a standard reader or writer method. To be specific,
1073 ;;; the value is READER when the method combination is eq to
1074 ;;; *standard-method-combination*; there are no applicable
1075 ;;; :before, :after or :around methods; and the most specific
1076 ;;; primary method is a standard reader method.
1078 ;;; <index> If <type> is READER or WRITER, and the slot accessed is
1079 ;;; an :instance slot, this is the index number of that slot
1080 ;;; in the object argument.
1081 (defvar *cache-miss-values-stack* ())
1083 (defun cache-miss-values (gf args state)
1084 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
1085 (get-generic-fun-info gf)
1086 (declare (ignore nreq applyp nkeys))
1087 (with-dfun-wrappers (args metatypes)
1088 (dfun-wrappers invalid-wrapper-p wrappers classes types)
1089 (error-need-at-least-n-args gf (length metatypes))
1090 (multiple-value-bind (emf methods accessor-type index)
1091 (cache-miss-values-internal
1092 gf arg-info wrappers classes types state)
1093 (values emf methods
1094 dfun-wrappers
1095 invalid-wrapper-p
1096 accessor-type index)))))
1098 (defun cache-miss-values-internal (gf arg-info wrappers classes types state)
1099 (if (and classes (equal classes (cdr (assq gf *cache-miss-values-stack*))))
1100 (break-vicious-metacircle gf classes arg-info)
1101 (let ((*cache-miss-values-stack*
1102 (acons gf classes *cache-miss-values-stack*))
1103 (cam-std-p (or (null arg-info)
1104 (gf-info-c-a-m-emf-std-p arg-info))))
1105 (multiple-value-bind (methods all-applicable-and-sorted-p)
1106 (if cam-std-p
1107 (compute-applicable-methods-using-types gf types)
1108 (compute-applicable-methods-using-classes gf classes))
1110 (let* ((for-accessor-p (eq state 'accessor))
1111 (for-cache-p (or (eq state 'caching) (eq state 'accessor)))
1112 (emf (if (or cam-std-p all-applicable-and-sorted-p)
1113 (function-funcall (get-secondary-dispatch-function1
1114 gf methods types nil (and for-cache-p
1115 wrappers)
1116 all-applicable-and-sorted-p)
1117 nil (and for-cache-p wrappers))
1118 (default-secondary-dispatch-function gf))))
1119 (multiple-value-bind (index accessor-type)
1120 (and for-accessor-p all-applicable-and-sorted-p methods
1121 (accessor-values gf arg-info classes methods))
1122 (values (if (integerp index) index emf)
1123 methods accessor-type index)))))))
1125 ;;; Try to break a vicious circle while computing a cache miss.
1126 ;;; GF is the generic function, CLASSES are the classes of actual
1127 ;;; arguments, and ARG-INFO is the generic functions' arg-info.
1129 ;;; A vicious circle can be entered when the computation of the cache
1130 ;;; miss values itself depends on the values being computed. For
1131 ;;; instance, adding a method which is an instance of a subclass of
1132 ;;; STANDARD-METHOD leads to cache misses for slot accessors of
1133 ;;; STANDARD-METHOD like METHOD-SPECIALIZERS, and METHOD-SPECIALIZERS
1134 ;;; is itself used while we compute cache miss values.
1135 (defun break-vicious-metacircle (gf classes arg-info)
1136 (when (typep gf 'standard-generic-function)
1137 (multiple-value-bind (class slotd accessor-type)
1138 (accesses-standard-class-slot-p gf)
1139 (when class
1140 (let ((method (find-standard-class-accessor-method
1141 gf class accessor-type))
1142 (index (standard-slot-value/eslotd slotd 'location))
1143 (type (gf-info-simple-accessor-type arg-info)))
1144 (when (and method
1145 (subtypep (ecase accessor-type
1146 ((reader) (car classes))
1147 ((writer) (cadr classes)))
1148 class))
1149 (return-from break-vicious-metacircle
1150 (values index (list method) type index)))))))
1151 (error "~@<vicious metacircle: The computation of an ~
1152 effective method of ~s for arguments of types ~s uses ~
1153 the effective method being computed.~@:>"
1154 gf classes))
1156 ;;; Return (CLASS SLOTD ACCESSOR-TYPE) if some method of generic
1157 ;;; function GF accesses a slot of some class in *STANDARD-CLASSES*.
1158 ;;; CLASS is the class accessed, SLOTD is the effective slot definition
1159 ;;; object of the slot accessed, and ACCESSOR-TYPE is one of the symbols
1160 ;;; READER or WRITER describing the slot access.
1161 (defun accesses-standard-class-slot-p (gf)
1162 (flet ((standard-class-slot-access (gf class)
1163 (loop with gf-name = (standard-slot-value/gf gf 'name)
1164 for slotd in (standard-slot-value/class class 'slots)
1165 ;; FIXME: where does BOUNDP fit in here? Is it
1166 ;; relevant?
1167 as readers = (standard-slot-value/eslotd slotd 'readers)
1168 as writers = (standard-slot-value/eslotd slotd 'writers)
1169 if (member gf-name readers :test #'equal)
1170 return (values slotd 'reader)
1171 else if (member gf-name writers :test #'equal)
1172 return (values slotd 'writer))))
1173 (dolist (class-name *standard-classes*)
1174 (let ((class (find-class class-name)))
1175 (multiple-value-bind (slotd accessor-type)
1176 (standard-class-slot-access gf class)
1177 (when slotd
1178 (return (values class slotd accessor-type))))))))
1180 ;;; Find a slot reader/writer method among the methods of generic
1181 ;;; function GF which reads/writes instances of class CLASS.
1182 ;;; TYPE is one of the symbols READER or WRITER.
1183 (defun find-standard-class-accessor-method (gf class type)
1184 (dolist (method (standard-slot-value/gf gf 'methods))
1185 (let ((specializers (standard-slot-value/method method 'specializers))
1186 (qualifiers (plist-value method 'qualifiers)))
1187 (when (and (null qualifiers)
1188 (eq (ecase type
1189 (reader (car specializers))
1190 (writer (cadr specializers)))
1191 class))
1192 (return method)))))
1194 (defun accessor-values (gf arg-info classes methods)
1195 (declare (ignore gf))
1196 (let* ((accessor-type (gf-info-simple-accessor-type arg-info))
1197 (accessor-class (case accessor-type
1198 ((reader boundp) (car classes))
1199 (writer (cadr classes)))))
1200 (accessor-values-internal accessor-type accessor-class methods)))
1202 (defun accessor-values1 (gf accessor-type accessor-class)
1203 (let* ((type `(class-eq ,accessor-class))
1204 (types (ecase accessor-type
1205 ((reader boundp) `(,type))
1206 (writer `(t ,type))))
1207 (methods (compute-applicable-methods-using-types gf types)))
1208 (accessor-values-internal accessor-type accessor-class methods)))
1210 (defun accessor-values-internal (accessor-type accessor-class methods)
1211 (dolist (meth methods)
1212 (when (if (consp meth)
1213 (early-method-qualifiers meth)
1214 (method-qualifiers meth))
1215 (return-from accessor-values-internal (values nil nil))))
1216 (let* ((meth (car methods))
1217 (early-p (not (eq *boot-state* 'complete)))
1218 (slot-name (when accessor-class
1219 (if (consp meth)
1220 (and (early-method-standard-accessor-p meth)
1221 (early-method-standard-accessor-slot-name meth))
1222 (and (member *the-class-std-object*
1223 (if early-p
1224 (early-class-precedence-list
1225 accessor-class)
1226 (class-precedence-list
1227 accessor-class)))
1228 (if early-p
1229 (not (eq *the-class-standard-method*
1230 (early-method-class meth)))
1231 (standard-accessor-method-p meth))
1232 (if early-p
1233 (early-accessor-method-slot-name meth)
1234 (accessor-method-slot-name meth))))))
1235 (slotd (and accessor-class
1236 (if early-p
1237 (dolist (slot (early-class-slotds accessor-class) nil)
1238 (when (eql slot-name
1239 (early-slot-definition-name slot))
1240 (return slot)))
1241 (find-slot-definition accessor-class slot-name)))))
1242 (when (and slotd
1243 (or early-p
1244 (slot-accessor-std-p slotd accessor-type)))
1245 (values (if early-p
1246 (early-slot-definition-location slotd)
1247 (slot-definition-location slotd))
1248 accessor-type))))
1250 (defun make-accessor-table (gf type &optional table)
1251 (unless table (setq table (make-hash-table :test 'eq)))
1252 (let ((methods (if (early-gf-p gf)
1253 (early-gf-methods gf)
1254 (generic-function-methods gf)))
1255 (all-index nil)
1256 (no-class-slots-p t)
1257 (early-p (not (eq *boot-state* 'complete)))
1258 first second (size 0))
1259 (declare (fixnum size))
1260 ;; class -> {(specl slotd)}
1261 (dolist (method methods)
1262 (let* ((specializers (if (consp method)
1263 (early-method-specializers method t)
1264 (method-specializers method)))
1265 (specl (ecase type
1266 ((reader boundp) (car specializers))
1267 (writer (cadr specializers))))
1268 (specl-cpl (if early-p
1269 (early-class-precedence-list specl)
1270 (and (class-finalized-p specl)
1271 (class-precedence-list specl))))
1272 (so-p (member *the-class-std-object* specl-cpl))
1273 (slot-name (if (consp method)
1274 (and (early-method-standard-accessor-p method)
1275 (early-method-standard-accessor-slot-name
1276 method))
1277 (accessor-method-slot-name method))))
1278 (when (or (null specl-cpl)
1279 (member *the-class-structure-object* specl-cpl))
1280 (return-from make-accessor-table nil))
1281 (maphash (lambda (class slotd)
1282 (let ((cpl (if early-p
1283 (early-class-precedence-list class)
1284 (class-precedence-list class))))
1285 (when (memq specl cpl)
1286 (unless (and (or so-p
1287 (member *the-class-std-object* cpl))
1288 (or early-p
1289 (slot-accessor-std-p slotd type)))
1290 (return-from make-accessor-table nil))
1291 (push (cons specl slotd) (gethash class table)))))
1292 (gethash slot-name *name->class->slotd-table*))))
1293 (maphash (lambda (class specl+slotd-list)
1294 (dolist (sclass (if early-p
1295 (early-class-precedence-list class)
1296 (class-precedence-list class))
1297 (error "This can't happen."))
1298 (let ((a (assq sclass specl+slotd-list)))
1299 (when a
1300 (let* ((slotd (cdr a))
1301 (index (if early-p
1302 (early-slot-definition-location slotd)
1303 (slot-definition-location slotd))))
1304 (unless index (return-from make-accessor-table nil))
1305 (setf (gethash class table) index)
1306 (when (consp index) (setq no-class-slots-p nil))
1307 (setq all-index (if (or (null all-index)
1308 (eql all-index index))
1309 index t))
1310 (incf size)
1311 (cond ((= size 1) (setq first class))
1312 ((= size 2) (setq second class)))
1313 (return nil))))))
1314 table)
1315 (values table all-index first second size no-class-slots-p)))
1317 (defun compute-applicable-methods-using-types (generic-function types)
1318 (let ((definite-p t) (possibly-applicable-methods nil))
1319 (dolist (method (if (early-gf-p generic-function)
1320 (early-gf-methods generic-function)
1321 (generic-function-methods generic-function)))
1322 (let ((specls (if (consp method)
1323 (early-method-specializers method t)
1324 (method-specializers method)))
1325 (types types)
1326 (possibly-applicable-p t) (applicable-p t))
1327 (dolist (specl specls)
1328 (multiple-value-bind (specl-applicable-p specl-possibly-applicable-p)
1329 (specializer-applicable-using-type-p specl (pop types))
1330 (unless specl-applicable-p
1331 (setq applicable-p nil))
1332 (unless specl-possibly-applicable-p
1333 (setq possibly-applicable-p nil)
1334 (return nil))))
1335 (when possibly-applicable-p
1336 (unless applicable-p (setq definite-p nil))
1337 (push method possibly-applicable-methods))))
1338 (let ((precedence (arg-info-precedence (if (early-gf-p generic-function)
1339 (early-gf-arg-info
1340 generic-function)
1341 (gf-arg-info
1342 generic-function)))))
1343 (values (sort-applicable-methods precedence
1344 (nreverse possibly-applicable-methods)
1345 types)
1346 definite-p))))
1348 (defun sort-applicable-methods (precedence methods types)
1349 (sort-methods methods
1350 precedence
1351 (lambda (class1 class2 index)
1352 (let* ((class (type-class (nth index types)))
1353 (cpl (if (eq *boot-state* 'complete)
1354 (class-precedence-list class)
1355 (early-class-precedence-list class))))
1356 (if (memq class2 (memq class1 cpl))
1357 class1 class2)))))
1359 (defun sort-methods (methods precedence compare-classes-function)
1360 (flet ((sorter (method1 method2)
1361 (dolist (index precedence)
1362 (let* ((specl1 (nth index (if (listp method1)
1363 (early-method-specializers method1
1365 (method-specializers method1))))
1366 (specl2 (nth index (if (listp method2)
1367 (early-method-specializers method2
1369 (method-specializers method2))))
1370 (order (order-specializers
1371 specl1 specl2 index compare-classes-function)))
1372 (when order
1373 (return-from sorter (eq order specl1)))))))
1374 (stable-sort methods #'sorter)))
1376 (defun order-specializers (specl1 specl2 index compare-classes-function)
1377 (let ((type1 (if (eq *boot-state* 'complete)
1378 (specializer-type specl1)
1379 (!bootstrap-get-slot 'specializer specl1 'type)))
1380 (type2 (if (eq *boot-state* 'complete)
1381 (specializer-type specl2)
1382 (!bootstrap-get-slot 'specializer specl2 'type))))
1383 (cond ((eq specl1 specl2)
1384 nil)
1385 ((atom type1)
1386 specl2)
1387 ((atom type2)
1388 specl1)
1390 (case (car type1)
1391 (class (case (car type2)
1392 (class (funcall compare-classes-function
1393 specl1 specl2 index))
1394 (t specl2)))
1395 (prototype (case (car type2)
1396 (class (funcall compare-classes-function
1397 specl1 specl2 index))
1398 (t specl2)))
1399 (class-eq (case (car type2)
1400 (eql specl2)
1401 (class-eq nil)
1402 (class type1)))
1403 (eql (case (car type2)
1404 (eql nil)
1405 (t specl1))))))))
1407 (defun map-all-orders (methods precedence function)
1408 (let ((choices nil))
1409 (flet ((compare-classes-function (class1 class2 index)
1410 (declare (ignore index))
1411 (let ((choice nil))
1412 (dolist (c choices nil)
1413 (when (or (and (eq (first c) class1)
1414 (eq (second c) class2))
1415 (and (eq (first c) class2)
1416 (eq (second c) class1)))
1417 (return (setq choice c))))
1418 (unless choice
1419 (setq choice
1420 (if (class-might-precede-p class1 class2)
1421 (if (class-might-precede-p class2 class1)
1422 (list class1 class2 nil t)
1423 (list class1 class2 t))
1424 (if (class-might-precede-p class2 class1)
1425 (list class2 class1 t)
1426 (let ((name1 (class-name class1))
1427 (name2 (class-name class2)))
1428 (if (and name1
1429 name2
1430 (symbolp name1)
1431 (symbolp name2)
1432 (string< (symbol-name name1)
1433 (symbol-name name2)))
1434 (list class1 class2 t)
1435 (list class2 class1 t))))))
1436 (push choice choices))
1437 (car choice))))
1438 (loop (funcall function
1439 (sort-methods methods
1440 precedence
1441 #'compare-classes-function))
1442 (unless (dolist (c choices nil)
1443 (unless (third c)
1444 (rotatef (car c) (cadr c))
1445 (return (setf (third c) t))))
1446 (return nil))))))
1448 (defvar *in-precompute-effective-methods-p* nil)
1450 ;used only in map-all-orders
1451 (defun class-might-precede-p (class1 class2)
1452 (if (not *in-precompute-effective-methods-p*)
1453 (not (member class1 (cdr (class-precedence-list class2))))
1454 (class-can-precede-p class1 class2)))
1456 (defun compute-precedence (lambda-list nreq argument-precedence-order)
1457 (if (null argument-precedence-order)
1458 (let ((list nil))
1459 (dotimes-fixnum (i nreq list) (push (- (1- nreq) i) list)))
1460 (mapcar (lambda (x) (position x lambda-list))
1461 argument-precedence-order)))
1463 (defun cpl-or-nil (class)
1464 (if (eq *boot-state* 'complete)
1465 (when (class-finalized-p class)
1466 (class-precedence-list class))
1467 (early-class-precedence-list class)))
1469 (defun saut-and (specl type)
1470 (let ((applicable nil)
1471 (possibly-applicable t))
1472 (dolist (type (cdr type))
1473 (multiple-value-bind (appl poss-appl)
1474 (specializer-applicable-using-type-p specl type)
1475 (when appl (return (setq applicable t)))
1476 (unless poss-appl (return (setq possibly-applicable nil)))))
1477 (values applicable possibly-applicable)))
1479 (defun saut-not (specl type)
1480 (let ((ntype (cadr type)))
1481 (values nil
1482 (case (car ntype)
1483 (class (saut-not-class specl ntype))
1484 (class-eq (saut-not-class-eq specl ntype))
1485 (prototype (saut-not-prototype specl ntype))
1486 (eql (saut-not-eql specl ntype))
1487 (t (error "~S cannot handle the second argument ~S"
1488 'specializer-applicable-using-type-p type))))))
1490 (defun saut-not-class (specl ntype)
1491 (let* ((class (type-class specl))
1492 (cpl (cpl-or-nil class)))
1493 (not (memq (cadr ntype) cpl))))
1495 (defun saut-not-prototype (specl ntype)
1496 (let* ((class (case (car specl)
1497 (eql (class-of (cadr specl)))
1498 (class-eq (cadr specl))
1499 (prototype (cadr specl))
1500 (class (cadr specl))))
1501 (cpl (cpl-or-nil class)))
1502 (not (memq (cadr ntype) cpl))))
1504 (defun saut-not-class-eq (specl ntype)
1505 (let ((class (case (car specl)
1506 (eql (class-of (cadr specl)))
1507 (class-eq (cadr specl)))))
1508 (not (eq class (cadr ntype)))))
1510 (defun saut-not-eql (specl ntype)
1511 (case (car specl)
1512 (eql (not (eql (cadr specl) (cadr ntype))))
1513 (t t)))
1515 (defun class-applicable-using-class-p (specl type)
1516 (let ((pred (memq specl (cpl-or-nil type))))
1517 (values pred
1518 (or pred
1519 (if (not *in-precompute-effective-methods-p*)
1520 ;; classes might get common subclass
1521 (superclasses-compatible-p specl type)
1522 ;; worry only about existing classes
1523 (classes-have-common-subclass-p specl type))))))
1525 (defun classes-have-common-subclass-p (class1 class2)
1526 (or (eq class1 class2)
1527 (let ((class1-subs (class-direct-subclasses class1)))
1528 (or (memq class2 class1-subs)
1529 (dolist (class1-sub class1-subs nil)
1530 (when (classes-have-common-subclass-p class1-sub class2)
1531 (return t)))))))
1533 (defun saut-class (specl type)
1534 (case (car specl)
1535 (class (class-applicable-using-class-p (cadr specl) (cadr type)))
1536 (t (values nil (let ((class (type-class specl)))
1537 (memq (cadr type)
1538 (cpl-or-nil class)))))))
1540 (defun saut-class-eq (specl type)
1541 (if (eq (car specl) 'eql)
1542 (values nil (eq (class-of (cadr specl)) (cadr type)))
1543 (let ((pred (case (car specl)
1544 (class-eq
1545 (eq (cadr specl) (cadr type)))
1546 (class
1547 (or (eq (cadr specl) (cadr type))
1548 (memq (cadr specl) (cpl-or-nil (cadr type))))))))
1549 (values pred pred))))
1551 (defun saut-prototype (specl type)
1552 (declare (ignore specl type))
1553 (values nil nil)) ; XXX original PCL comment: fix this someday
1555 (defun saut-eql (specl type)
1556 (let ((pred (case (car specl)
1557 (eql (eql (cadr specl) (cadr type)))
1558 (class-eq (eq (cadr specl) (class-of (cadr type))))
1559 (class (memq (cadr specl)
1560 (let ((class (class-of (cadr type))))
1561 (cpl-or-nil class)))))))
1562 (values pred pred)))
1564 (defun specializer-applicable-using-type-p (specl type)
1565 (setq specl (type-from-specializer specl))
1566 (when (eq specl t)
1567 (return-from specializer-applicable-using-type-p (values t t)))
1568 ;; This is used by C-A-M-U-T and GENERATE-DISCRIMINATION-NET-INTERNAL,
1569 ;; and has only what they need.
1570 (if (or (atom type) (eq (car type) t))
1571 (values nil t)
1572 (case (car type)
1573 (and (saut-and specl type))
1574 (not (saut-not specl type))
1575 (class (saut-class specl type))
1576 (prototype (saut-prototype specl type))
1577 (class-eq (saut-class-eq specl type))
1578 (eql (saut-eql specl type))
1579 (t (error "~S cannot handle the second argument ~S."
1580 'specializer-applicable-using-type-p
1581 type)))))
1583 (defun map-all-classes (function &optional (root t))
1584 (let ((braid-p (or (eq *boot-state* 'braid)
1585 (eq *boot-state* 'complete))))
1586 (labels ((do-class (class)
1587 (mapc #'do-class
1588 (if braid-p
1589 (class-direct-subclasses class)
1590 (early-class-direct-subclasses class)))
1591 (funcall function class)))
1592 (do-class (if (symbolp root)
1593 (find-class root)
1594 root)))))
1596 ;;; NOTE: We are assuming a restriction on user code that the method
1597 ;;; combination must not change once it is connected to the
1598 ;;; generic function.
1600 ;;; This has to be legal, because otherwise any kind of method
1601 ;;; lookup caching couldn't work. See this by saying that this
1602 ;;; cache, is just a backing cache for the fast cache. If that
1603 ;;; cache is legal, this one must be too.
1605 ;;; Don't clear this table!
1606 (defvar *effective-method-table* (make-hash-table :test 'eq))
1608 (defun get-secondary-dispatch-function (gf methods types &optional
1609 method-alist wrappers)
1610 (function-funcall (get-secondary-dispatch-function1
1611 gf methods types
1612 (not (null method-alist))
1613 (not (null wrappers))
1614 (not (methods-contain-eql-specializer-p methods)))
1615 method-alist wrappers))
1617 (defun get-secondary-dispatch-function1 (gf methods types method-alist-p
1618 wrappers-p
1619 &optional
1620 all-applicable-p
1621 (all-sorted-p t)
1622 function-p)
1623 (if (null methods)
1624 (if function-p
1625 (lambda (method-alist wrappers)
1626 (declare (ignore method-alist wrappers))
1627 #'(instance-lambda (&rest args)
1628 (apply #'no-applicable-method gf args)))
1629 (lambda (method-alist wrappers)
1630 (declare (ignore method-alist wrappers))
1631 (lambda (&rest args)
1632 (apply #'no-applicable-method gf args))))
1633 (let* ((key (car methods))
1634 (ht-value (or (gethash key *effective-method-table*)
1635 (setf (gethash key *effective-method-table*)
1636 (cons nil nil)))))
1637 (if (and (null (cdr methods)) all-applicable-p ; the most common case
1638 (null method-alist-p) wrappers-p (not function-p))
1639 (or (car ht-value)
1640 (setf (car ht-value)
1641 (get-secondary-dispatch-function2
1642 gf methods types method-alist-p wrappers-p
1643 all-applicable-p all-sorted-p function-p)))
1644 (let ((akey (list methods
1645 (if all-applicable-p 'all-applicable types)
1646 method-alist-p wrappers-p function-p)))
1647 (or (cdr (assoc akey (cdr ht-value) :test #'equal))
1648 (let ((value (get-secondary-dispatch-function2
1649 gf methods types method-alist-p wrappers-p
1650 all-applicable-p all-sorted-p function-p)))
1651 (push (cons akey value) (cdr ht-value))
1652 value)))))))
1654 (defun get-secondary-dispatch-function2 (gf methods types method-alist-p
1655 wrappers-p all-applicable-p
1656 all-sorted-p function-p)
1657 (if (and all-applicable-p all-sorted-p (not function-p))
1658 (if (eq *boot-state* 'complete)
1659 (let* ((combin (generic-function-method-combination gf))
1660 (effective (compute-effective-method gf combin methods)))
1661 (make-effective-method-function1 gf effective method-alist-p
1662 wrappers-p))
1663 (let ((effective (standard-compute-effective-method gf nil methods)))
1664 (make-effective-method-function1 gf effective method-alist-p
1665 wrappers-p)))
1666 (let ((net (generate-discrimination-net
1667 gf methods types all-sorted-p)))
1668 (compute-secondary-dispatch-function1 gf net function-p))))
1670 (defun get-effective-method-function (gf methods
1671 &optional method-alist wrappers)
1672 (function-funcall (get-secondary-dispatch-function1 gf methods nil
1673 (not (null method-alist))
1674 (not (null wrappers))
1676 method-alist wrappers))
1678 (defun get-effective-method-function1 (gf methods &optional (sorted-p t))
1679 (get-secondary-dispatch-function1 gf methods nil nil nil t sorted-p))
1681 (defun methods-contain-eql-specializer-p (methods)
1682 (and (eq *boot-state* 'complete)
1683 (dolist (method methods nil)
1684 (when (dolist (spec (method-specializers method) nil)
1685 (when (eql-specializer-p spec) (return t)))
1686 (return t)))))
1688 (defun update-dfun (generic-function &optional dfun cache info)
1689 (let* ((early-p (early-gf-p generic-function))
1690 (gf-name (if early-p
1691 (!early-gf-name generic-function)
1692 (generic-function-name generic-function))))
1693 (set-dfun generic-function dfun cache info)
1694 (let ((dfun (if early-p
1695 (or dfun (make-initial-dfun generic-function))
1696 (compute-discriminating-function generic-function))))
1697 (set-funcallable-instance-function generic-function dfun)
1698 (set-fun-name generic-function gf-name)
1699 dfun)))
1701 (defvar *dfun-count* nil)
1702 (defvar *dfun-list* nil)
1703 (defvar *minimum-cache-size-to-list*)
1705 ;;; These functions aren't used in SBCL, or documented anywhere that
1706 ;;; I'm aware of, but they look like they might be useful for
1707 ;;; debugging or performance tweaking or something, so I've just
1708 ;;; commented them out instead of deleting them. -- WHN 2001-03-28
1710 (defun list-dfun (gf)
1711 (let* ((sym (type-of (gf-dfun-info gf)))
1712 (a (assq sym *dfun-list*)))
1713 (unless a
1714 (push (setq a (list sym)) *dfun-list*))
1715 (push (generic-function-name gf) (cdr a))))
1717 (defun list-all-dfuns ()
1718 (setq *dfun-list* nil)
1719 (map-all-generic-functions #'list-dfun)
1720 *dfun-list*)
1722 (defun list-large-cache (gf)
1723 (let* ((sym (type-of (gf-dfun-info gf)))
1724 (cache (gf-dfun-cache gf)))
1725 (when cache
1726 (let ((size (cache-size cache)))
1727 (when (>= size *minimum-cache-size-to-list*)
1728 (let ((a (assoc size *dfun-list*)))
1729 (unless a
1730 (push (setq a (list size)) *dfun-list*))
1731 (push (let ((name (generic-function-name gf)))
1732 (if (eq sym 'caching) name (list name sym)))
1733 (cdr a))))))))
1735 (defun list-large-caches (&optional (*minimum-cache-size-to-list* 130))
1736 (setq *dfun-list* nil)
1737 (map-all-generic-functions #'list-large-cache)
1738 (setq *dfun-list* (sort *dfun-list* #'< :key #'car))
1739 (mapc #'print *dfun-list*)
1740 (values))
1742 (defun count-dfun (gf)
1743 (let* ((sym (type-of (gf-dfun-info gf)))
1744 (cache (gf-dfun-cache gf))
1745 (a (assq sym *dfun-count*)))
1746 (unless a
1747 (push (setq a (list sym 0 nil)) *dfun-count*))
1748 (incf (cadr a))
1749 (when cache
1750 (let* ((size (cache-size cache))
1751 (b (assoc size (third a))))
1752 (unless b
1753 (push (setq b (cons size 0)) (third a)))
1754 (incf (cdr b))))))
1756 (defun count-all-dfuns ()
1757 (setq *dfun-count* (mapcar (lambda (type) (list type 0 nil))
1758 '(ONE-CLASS TWO-CLASS DEFAULT-METHOD-ONLY
1759 ONE-INDEX N-N CHECKING CACHING
1760 DISPATCH)))
1761 (map-all-generic-functions #'count-dfun)
1762 (mapc (lambda (type+count+sizes)
1763 (setf (third type+count+sizes)
1764 (sort (third type+count+sizes) #'< :key #'car)))
1765 *dfun-count*)
1766 (mapc (lambda (type+count+sizes)
1767 (format t "~&There are ~W dfuns of type ~S."
1768 (cadr type+count+sizes) (car type+count+sizes))
1769 (format t "~% ~S~%" (caddr type+count+sizes)))
1770 *dfun-count*)
1771 (values))
1774 (defun gfs-of-type (type)
1775 (unless (consp type) (setq type (list type)))
1776 (let ((gf-list nil))
1777 (map-all-generic-functions (lambda (gf)
1778 (when (memq (type-of (gf-dfun-info gf))
1779 type)
1780 (push gf gf-list))))
1781 gf-list))