1 ;;;; This software is part of the SBCL system. See the README file for
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
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
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
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
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
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.
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))
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)
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)
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)))
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))))
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))
147 '(setf slot-value-using-class)))
148 (not (eq gf-name 'slot-boundp-using-class)))))
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)))
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))
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)
171 ,(apply (fdefinition (car generator-entry))
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*)))
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))
215 (error "~@<cannot get standard value of slot ~S of class ~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.
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)
269 (defstruct (no-methods (:constructor no-methods-dfun-info ())
273 (defstruct (initial (:constructor initial-dfun-info ())
277 (defstruct (initial-dispatch (:constructor initial-dispatch-dfun-info ())
281 (defstruct (dispatch (:constructor dispatch-dfun-info ())
285 (defstruct (default-method-only (:constructor default-method-only-dfun-info ())
290 ; dispatch one-class two-class default-method-only
293 ; one-index n-n checking caching
296 ; one-class two-class one-index n-n
297 (defstruct (accessor-dfun-info (:constructor 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)
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)
317 (defstruct (one-class (:constructor one-class-dfun-info
318 (accessor-type index wrapper0))
319 (:include one-index-dfun-info)
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))
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)
340 (defstruct (checking (:constructor checking-dfun-info (function cache))
345 (defmacro dfun-info-function (di)
346 `(checking-function ,di))
348 (defstruct (caching (:constructor caching-dfun-info (cache))
352 (defstruct (constant-value (:constructor constant-value-dfun-info (cache))
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)
365 (accessor-miss gf nil arg dfun-info)))
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)))
379 (funcall (get-dfun-constructor emit (consp index))
381 (accessor-miss-function gf 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)))
392 (funcall (get-dfun-constructor emit (consp index))
394 (accessor-miss-function gf 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))
408 (funcall (get-dfun-constructor emit (consp index))
411 (accessor-miss-function gf 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))
431 (funcall (get-dfun-constructor emit)
433 (accessor-miss-function gf 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)
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)))
456 (funcall (get-dfun-constructor 'emit-default-only metatypes applyp)
460 (let* ((cache (or cache (get-cache nkeys nil #'checking-limit-fn 2)))
461 (dfun-info (checking-dfun-info function cache)))
463 (funcall (get-dfun-constructor 'emit-checking metatypes applyp)
467 (checking-miss generic-function args dfun-info)))
471 (defun make-final-checking-dfun (generic-function function
472 classes-list new-class)
473 (multiple-value-bind (nreq applyp metatypes nkeys)
474 (get-generic-fun-info generic-function)
475 (declare (ignore nreq applyp nkeys))
476 (if (every (lambda (mt) (eq mt t)) metatypes)
477 (values (lambda (&rest args)
478 (invoke-emf function args))
479 nil (default-method-only-dfun-info))
480 (let ((cache (make-final-ordinary-dfun-internal
481 generic-function nil #'checking-limit-fn
482 classes-list new-class)))
483 (make-checking-dfun generic-function function cache)))))
485 (defun use-default-method-only-dfun-p (generic-function)
486 (multiple-value-bind (nreq applyp metatypes nkeys)
487 (get-generic-fun-info generic-function)
488 (declare (ignore nreq applyp nkeys))
489 (every (lambda (mt) (eq mt t)) metatypes)))
491 (defun use-caching-dfun-p (generic-function)
492 (some (lambda (method) (method-plist-value method :slot-name-lists))
493 ;; KLUDGE: As of sbcl-0.6.4, it's very important for
494 ;; efficiency to know the type of the sequence argument to
495 ;; quantifiers (SOME/NOTANY/etc.) at compile time, but
496 ;; the compiler isn't smart enough to understand the :TYPE
497 ;; slot option for DEFCLASS, so we just tell
498 ;; it the type by hand here.
500 (if (early-gf-p generic-function)
501 (early-gf-methods generic-function)
502 (generic-function-methods generic-function)))))
504 (defun checking-limit-fn (nlines)
505 (default-limit-fn nlines))
507 (defun make-caching-dfun (generic-function &optional cache)
509 (when (use-constant-value-dfun-p generic-function)
510 (return-from make-caching-dfun
511 (make-constant-value-dfun generic-function)))
512 (when (use-dispatch-dfun-p generic-function)
513 (return-from make-caching-dfun
514 (make-dispatch-dfun generic-function))))
515 (multiple-value-bind (nreq applyp metatypes nkeys)
516 (get-generic-fun-info generic-function)
517 (declare (ignore nreq))
518 (let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
519 (dfun-info (caching-dfun-info cache)))
521 (funcall (get-dfun-constructor 'emit-caching metatypes applyp)
524 (caching-miss generic-function args dfun-info)))
528 (defun make-final-caching-dfun (generic-function classes-list new-class)
529 (let ((cache (make-final-ordinary-dfun-internal
530 generic-function t #'caching-limit-fn
531 classes-list new-class)))
532 (make-caching-dfun generic-function cache)))
534 (defun caching-limit-fn (nlines)
535 (default-limit-fn nlines))
537 (defun insure-caching-dfun (gf)
538 (multiple-value-bind (nreq applyp metatypes nkeys)
539 (get-generic-fun-info gf)
540 (declare (ignore nreq nkeys))
542 (not (null (car metatypes)))
543 (dolist (mt metatypes nil)
544 (unless (eq mt t) (return t))))
545 (get-dfun-constructor 'emit-caching metatypes applyp))))
547 (defun use-constant-value-dfun-p (gf &optional boolean-values-p)
548 (multiple-value-bind (nreq applyp metatypes nkeys)
549 (get-generic-fun-info gf)
550 (declare (ignore nreq metatypes nkeys))
551 (let* ((early-p (early-gf-p gf))
553 (early-gf-methods gf)
554 (generic-function-methods gf)))
555 (default '(unknown)))
557 (or (not (eq *boot-state* 'complete))
558 ;; If COMPUTE-APPLICABLE-METHODS is specialized, we
559 ;; can't use this, of course, because we can't tell
560 ;; which methods will be considered applicable.
562 ;; Also, don't use this dfun method if the generic
563 ;; function has a non-standard method combination,
564 ;; because if it has, it's not sure that method
565 ;; functions are used directly as effective methods,
566 ;; which CONSTANT-VALUE-MISS depends on. The
567 ;; pre-defined method combinations like LIST are
569 (and (compute-applicable-methods-emf-std-p gf)
570 (eq (generic-function-method-combination gf)
571 *standard-method-combination*)))
572 ;; Check that no method is eql-specialized, and that all
573 ;; methods return a constant value. If BOOLEAN-VALUES-P,
574 ;; check that all return T or NIL. Also, check that no
575 ;; method has qualifiers, to make sure that emfs are really
576 ;; method functions; see above.
577 (dolist (method methods t)
578 (when (eq *boot-state* 'complete)
579 (when (or (some #'eql-specializer-p
580 (safe-method-specializers method))
581 (safe-method-qualifiers method))
583 (let ((value (method-plist-value method :constant-value default)))
584 (when (or (eq value default)
585 (and boolean-values-p
586 (not (member value '(t nil)))))
589 (defun make-constant-value-dfun (generic-function &optional cache)
590 (multiple-value-bind (nreq applyp metatypes nkeys)
591 (get-generic-fun-info generic-function)
592 (declare (ignore nreq applyp))
593 (let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
594 (dfun-info (constant-value-dfun-info cache)))
596 (funcall (get-dfun-constructor 'emit-constant-value metatypes)
599 (constant-value-miss generic-function args dfun-info)))
603 (defun make-final-constant-value-dfun (generic-function classes-list new-class)
604 (let ((cache (make-final-ordinary-dfun-internal
605 generic-function :constant-value #'caching-limit-fn
606 classes-list new-class)))
607 (make-constant-value-dfun generic-function cache)))
609 (defun use-dispatch-dfun-p (gf &optional (caching-p (use-caching-dfun-p gf)))
610 (when (eq *boot-state* 'complete)
611 (unless (or caching-p
612 (gf-requires-emf-keyword-checks gf))
613 ;; This should return T when almost all dispatching is by
614 ;; eql specializers or built-in classes. In other words,
615 ;; return NIL if we might ever need to do more than
616 ;; one (non built-in) typep.
617 ;; Otherwise, it is probably at least as fast to use
618 ;; a caching dfun first, possibly followed by secondary dispatching.
620 #||;;; Original found in cmu 17f -- S L O W
621 (< (dispatch-dfun-cost gf) (caching-dfun-cost gf))
623 ;; This uses improved dispatch-dfun-cost below
624 (let ((cdc (caching-dfun-cost gf))) ; fast
625 (> cdc (dispatch-dfun-cost gf cdc))))))
627 (defparameter *non-built-in-typep-cost* 100)
628 (defparameter *structure-typep-cost* 15)
629 (defparameter *built-in-typep-cost* 5)
631 ;;; According to comments in the original CMU CL version of PCL,
632 ;;; the cost LIMIT is important to cut off exponential growth for
633 ;;; large numbers of gf methods and argument lists.
634 (defun dispatch-dfun-cost (gf &optional limit)
635 (generate-discrimination-net-internal
636 gf (generic-function-methods gf) nil
637 (lambda (methods known-types)
638 (declare (ignore methods known-types))
640 (lambda (position type true-value false-value)
641 (declare (ignore position))
642 (let* ((type-test-cost
643 (if (eq 'class (car type))
644 (let* ((metaclass (class-of (cadr type)))
645 (mcpl (class-precedence-list metaclass)))
646 (cond ((memq *the-class-built-in-class* mcpl)
647 *built-in-typep-cost*)
648 ((memq *the-class-structure-class* mcpl)
649 *structure-typep-cost*)
651 *non-built-in-typep-cost*)))
654 (+ (max true-value false-value) type-test-cost)))
655 (when (and limit (<= limit max-cost-so-far))
656 (return-from dispatch-dfun-cost max-cost-so-far))
660 (defparameter *cache-lookup-cost* 30)
661 (defparameter *wrapper-of-cost* 15)
662 (defparameter *secondary-dfun-call-cost* 30)
664 (defun caching-dfun-cost (gf)
665 (let ((nreq (get-generic-fun-info gf)))
666 (+ *cache-lookup-cost*
667 (* *wrapper-of-cost* nreq)
668 (if (methods-contain-eql-specializer-p
669 (generic-function-methods gf))
670 *secondary-dfun-call-cost*
673 (declaim (inline make-callable))
674 (defun make-callable (gf methods generator method-alist wrappers)
675 (let* ((*applicable-methods* methods)
676 (callable (function-funcall generator method-alist wrappers)))
679 (defun make-dispatch-dfun (gf)
680 (values (get-dispatch-function gf) nil (dispatch-dfun-info)))
682 (defun get-dispatch-function (gf)
683 (let* ((methods (generic-function-methods gf))
684 (generator (get-secondary-dispatch-function1
685 gf methods nil nil nil nil nil t)))
686 (make-callable gf methods generator nil nil)))
688 (defun make-final-dispatch-dfun (gf)
689 (make-dispatch-dfun gf))
691 (defun update-dispatch-dfuns ()
692 (dolist (gf (gfs-of-type '(dispatch initial-dispatch)))
693 (dfun-update gf #'make-dispatch-dfun)))
695 (defun fill-dfun-cache (table valuep nkeys limit-fn &optional cache)
696 (let ((cache (or cache (get-cache nkeys valuep limit-fn
697 (+ (hash-table-count table) 3)))))
698 (maphash (lambda (classes value)
699 (setq cache (fill-cache cache
700 (class-wrapper classes)
705 (defun make-final-ordinary-dfun-internal (generic-function valuep limit-fn
706 classes-list new-class)
707 (let* ((arg-info (gf-arg-info generic-function))
708 (nkeys (arg-info-nkeys arg-info))
709 (new-class (and new-class
710 (equal (type-of (gf-dfun-info generic-function))
711 (cond ((eq valuep t) 'caching)
712 ((eq valuep :constant-value) 'constant-value)
713 ((null valuep) 'checking)))
716 (copy-cache (gf-dfun-cache generic-function))
717 (get-cache nkeys (not (null valuep)) limit-fn 4))))
718 (make-emf-cache generic-function valuep cache classes-list new-class)))
720 (defvar *dfun-miss-gfs-on-stack* ())
722 (defmacro dfun-miss ((gf args wrappers invalidp nemf
723 &optional type index caching-p applicable)
725 (unless applicable (setq applicable (gensym)))
726 `(multiple-value-bind (,nemf ,applicable ,wrappers ,invalidp
727 ,@(when type `(,type ,index)))
728 (cache-miss-values ,gf ,args ',(cond (caching-p 'caching)
731 (when (and ,applicable (not (memq ,gf *dfun-miss-gfs-on-stack*)))
732 (let ((*dfun-miss-gfs-on-stack* (cons ,gf *dfun-miss-gfs-on-stack*)))
734 ;; Create a FAST-INSTANCE-BOUNDP structure instance for a cached
735 ;; SLOT-BOUNDP so that INVOKE-EMF does the right thing, that is,
736 ;; does not signal a SLOT-UNBOUND error for a boundp test.
738 ;; FIXME: could the NEMF not be a CONS (for :CLASS-allocated
740 `((if (and (eq ,type 'boundp) (integerp ,nemf))
741 (invoke-emf (make-fast-instance-boundp :index ,nemf) ,args)
742 (invoke-emf ,nemf ,args)))
743 `((invoke-emf ,nemf ,args)))))
745 ;;; The dynamically adaptive method lookup algorithm is implemented is
746 ;;; implemented as a kind of state machine. The kinds of
747 ;;; discriminating function is the state, the various kinds of reasons
748 ;;; for a cache miss are the state transitions.
750 ;;; The code which implements the transitions is all in the miss
751 ;;; handlers for each kind of dfun. Those appear here.
753 ;;; Note that within the states that cache, there are dfun updates
754 ;;; which simply select a new cache or cache field. Those are not
755 ;;; considered as state transitions.
756 (defvar *lazy-dfun-compute-p* t)
757 (defvar *early-p* nil)
759 ;;; This variable is used for controlling the load-time effective
760 ;;; method precomputation: precomputation will only be done for emfs
761 ;;; with fewer than methods than this value. This value has
762 ;;; traditionally been NIL on SBCL (meaning that precomputation will
763 ;;; always be done) but that makes method loading O(n^2). Use a small
764 ;;; value for now, to flush out any possible problems that doing a
765 ;;; limited amount of precomputation might cause. If none appear, we
766 ;;; might change it to a larger value later. -- JES, 2006-12-01
767 (declaim (type (or null unsigned-byte) *max-emf-precomputation-methods*))
768 (defvar *max-emf-precomputation-methods* 1)
770 (defun finalize-specializers (gf)
771 (let ((methods (generic-function-methods gf)))
772 (when (or (null *max-emf-precomputation-methods*)
773 (<= (length methods) *max-emf-precomputation-methods*))
774 (let ((all-finalized t))
775 (dolist (method methods all-finalized)
776 (dolist (specializer (method-specializers method))
777 (when (and (classp specializer)
778 (not (class-finalized-p specializer)))
779 (if (class-has-a-forward-referenced-superclass-p specializer)
780 (setq all-finalized nil)
781 (finalize-inheritance specializer)))))))))
783 (defun make-initial-dfun (gf)
785 #'(lambda (&rest args)
786 (initial-dfun gf args))))
787 (multiple-value-bind (dfun cache info)
789 ((and (eq *boot-state* 'complete)
790 (not (finalize-specializers gf)))
791 (values initial-dfun nil (initial-dfun-info)))
792 ((and (eq *boot-state* 'complete)
793 (compute-applicable-methods-emf-std-p gf))
794 (let* ((caching-p (use-caching-dfun-p gf))
795 ;; KLUDGE: the only effect of this (when
796 ;; *LAZY-DFUN-COMPUTE-P* is true, as it usually is)
797 ;; is to signal an error when we try to add methods
798 ;; with the wrong qualifiers to a generic function.
799 (classes-list (precompute-effective-methods
801 (not *lazy-dfun-compute-p*))))
802 (if *lazy-dfun-compute-p*
803 (cond ((use-dispatch-dfun-p gf caching-p)
806 (initial-dispatch-dfun-info)))
808 (insure-caching-dfun gf)
809 (values initial-dfun nil (initial-dfun-info)))
811 (values initial-dfun nil (initial-dfun-info))))
812 (make-final-dfun-internal gf classes-list))))
814 (let ((arg-info (if (early-gf-p gf)
815 (early-gf-arg-info gf)
818 (if (and (gf-precompute-dfun-and-emf-p arg-info)
819 (setq type (final-accessor-dfun-type gf)))
821 (values (make-early-accessor gf type) nil nil)
822 (make-final-accessor-dfun gf type))
823 (values initial-dfun nil (initial-dfun-info))))))
824 (set-dfun gf dfun cache info))))
826 (defun make-early-accessor (gf type)
827 (let* ((methods (early-gf-methods gf))
828 (slot-name (early-method-standard-accessor-slot-name (car methods))))
830 (reader #'(lambda (instance)
831 (let* ((class (class-of instance))
832 (class-name (!bootstrap-get-slot 'class class 'name)))
833 (!bootstrap-get-slot class-name instance slot-name))))
834 (boundp #'(lambda (instance)
835 (let* ((class (class-of instance))
836 (class-name (!bootstrap-get-slot 'class class 'name)))
837 (not (eq +slot-unbound+
838 (!bootstrap-get-slot class-name
839 instance slot-name))))))
840 (writer #'(lambda (new-value instance)
841 (let* ((class (class-of instance))
842 (class-name (!bootstrap-get-slot 'class class 'name)))
843 (!bootstrap-set-slot class-name instance slot-name new-value)))))))
845 (defun initial-dfun (gf args)
846 (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
850 gf #'make-one-class-accessor-dfun ntype wrappers nindex))
851 ((use-caching-dfun-p gf)
852 (dfun-update gf #'make-caching-dfun))
855 gf #'make-checking-dfun
856 ;; nemf is suitable only for caching, have to do this:
857 (cache-miss-values gf args 'checking))))))
859 (defun make-final-dfun (gf &optional classes-list)
860 (multiple-value-bind (dfun cache info)
861 (make-final-dfun-internal gf classes-list)
862 (set-dfun gf dfun cache info)))
864 (defvar *new-class* nil)
866 (defun final-accessor-dfun-type (gf)
867 (let ((methods (if (early-gf-p gf)
868 (early-gf-methods gf)
869 (generic-function-methods gf))))
870 (cond ((every (lambda (method)
872 (let ((class (early-method-class method)))
873 (or (eq class *the-class-standard-reader-method*)
874 (eq class *the-class-global-reader-method*)))
875 (or (standard-reader-method-p method)
876 (global-reader-method-p method))))
879 ((every (lambda (method)
881 (let ((class (early-method-class method)))
882 (or (eq class *the-class-standard-boundp-method*)
883 (eq class *the-class-global-boundp-method*)))
884 (or (standard-boundp-method-p method)
885 (global-boundp-method-p method))))
888 ((every (lambda (method)
890 (let ((class (early-method-class method)))
891 (or (eq class *the-class-standard-writer-method*)
892 (eq class *the-class-global-writer-method*)))
894 (or (standard-writer-method-p method)
895 (global-writer-method-p method))
897 (slot-definition-class
898 (accessor-method-slot-definition method)))))))
902 (defun make-final-accessor-dfun (gf type &optional classes-list new-class)
903 (let ((table (make-hash-table :test #'eq)))
904 (multiple-value-bind (table all-index first second size no-class-slots-p)
905 (make-accessor-table gf type table)
908 (let ((w (class-wrapper first)))
909 (make-one-class-accessor-dfun gf type w all-index)))
910 ((and (= size 2) (or (integerp all-index) (consp all-index)))
911 (let ((w0 (class-wrapper first))
912 (w1 (class-wrapper second)))
913 (make-two-class-accessor-dfun gf type w0 w1 all-index)))
914 ((or (integerp all-index) (consp all-index))
915 (make-final-one-index-accessor-dfun
916 gf type all-index table))
918 (make-final-n-n-accessor-dfun gf type table))
920 (make-final-caching-dfun gf classes-list new-class)))
921 (make-final-caching-dfun gf classes-list new-class)))))
923 (defun make-final-dfun-internal (gf &optional classes-list)
924 (let ((methods (generic-function-methods gf)) type
925 (new-class *new-class*) (*new-class* nil)
927 (cond ((null methods)
929 #'(lambda (&rest args)
930 (apply #'no-applicable-method gf args))
932 (no-methods-dfun-info)))
933 ((setq type (final-accessor-dfun-type gf))
934 (make-final-accessor-dfun gf type classes-list new-class))
935 ((and (not (and (every (lambda (specl) (eq specl *the-class-t*))
937 (method-specializers (car methods))))
939 (every (lambda (method)
944 (use-constant-value-dfun-p gf))
945 (make-final-constant-value-dfun gf classes-list new-class))
946 ((use-dispatch-dfun-p gf)
947 (make-final-dispatch-dfun gf))
948 ((and all-same-p (not (use-caching-dfun-p gf)))
949 (let ((emf (get-secondary-dispatch-function gf methods nil)))
950 (make-final-checking-dfun gf emf classes-list new-class)))
952 (make-final-caching-dfun gf classes-list new-class)))))
955 (defun accessor-miss (gf new object dfun-info)
956 (let* ((ostate (type-of dfun-info))
957 (otype (dfun-info-accessor-type dfun-info))
960 ((reader boundp) (list object))
961 (writer (list new object)))))
962 (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
963 ;; The following lexical functions change the state of the
964 ;; dfun to that which is their name. They accept arguments
965 ;; which are the parameters of the new state, and get other
966 ;; information from the lexical variables bound above.
967 (flet ((two-class (index w0 w1)
968 (when (zerop (random 2 *pcl-misc-random-state*))
971 #'make-two-class-accessor-dfun
976 (one-index (index &optional cache)
978 #'make-one-index-accessor-dfun
982 (n-n (&optional cache)
984 (dfun-update gf #'make-checking-dfun nemf)
985 (dfun-update gf #'make-n-n-accessor-dfun ntype cache)))
986 (caching () ; because cached accessor emfs are much faster
988 (dfun-update gf #'make-caching-dfun))
990 (let ((ncache (fill-cache cache wrappers nindex)))
991 (unless (eq ncache cache)
992 (funcall update-fn ncache)))))
998 ((not (pcl-instance-p object))
1000 ((or (neq ntype otype) (listp wrappers))
1005 (setq oindex (dfun-info-index dfun-info))
1006 (setq ow0 (dfun-info-wrapper0 dfun-info))
1007 (unless (eq ow0 wrappers)
1008 (if (eql nindex oindex)
1009 (two-class nindex ow0 wrappers)
1012 (setq oindex (dfun-info-index dfun-info))
1013 (setq ow0 (dfun-info-wrapper0 dfun-info))
1014 (setq ow1 (dfun-info-wrapper1 dfun-info))
1015 (unless (or (eq ow0 wrappers) (eq ow1 wrappers))
1016 (if (eql nindex oindex)
1020 (setq oindex (dfun-info-index dfun-info))
1021 (setq cache (dfun-info-cache dfun-info))
1022 (if (eql nindex oindex)
1023 (do-fill (lambda (ncache)
1024 (one-index nindex ncache)))
1027 (setq cache (dfun-info-cache dfun-info))
1030 (do-fill #'n-n))))))))))
1032 (defun checking-miss (generic-function args dfun-info)
1033 (let ((oemf (dfun-info-function dfun-info))
1034 (cache (dfun-info-cache dfun-info)))
1035 (dfun-miss (generic-function args wrappers invalidp nemf)
1038 (let ((ncache (fill-cache cache wrappers nil)))
1039 (unless (eq ncache cache)
1040 (dfun-update generic-function #'make-checking-dfun
1043 (dfun-update generic-function #'make-caching-dfun))))))
1045 (defun caching-miss (generic-function args dfun-info)
1046 (let ((ocache (dfun-info-cache dfun-info)))
1047 (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
1050 (let ((ncache (fill-cache ocache wrappers emf)))
1051 (unless (eq ncache ocache)
1052 (dfun-update generic-function
1053 #'make-caching-dfun ncache))))))))
1055 (defun constant-value-miss (generic-function args dfun-info)
1056 (let ((ocache (dfun-info-cache dfun-info)))
1057 (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
1061 (constant-fast-method-call
1062 (constant-fast-method-call-value emf))
1063 (constant-method-call (constant-method-call-value emf))
1064 (t (bug "~S with non-constant EMF ~S"
1065 'constant-value-miss emf))))
1066 (ncache (fill-cache ocache wrappers value)))
1067 (unless (eq ncache ocache)
1068 (dfun-update generic-function
1069 #'make-constant-value-dfun ncache)))))))
1071 ;;; Given a generic function and a set of arguments to that generic
1072 ;;; function, return a mess of values.
1074 ;;; <function> The compiled effective method function for this set of
1077 ;;; <applicable> Sorted list of applicable methods.
1079 ;;; <wrappers> Is a single wrapper if the generic function has only
1080 ;;; one key, that is arg-info-nkeys of the arg-info is 1.
1081 ;;; Otherwise a list of the wrappers of the specialized
1082 ;;; arguments to the generic function.
1084 ;;; Note that all these wrappers are valid. This function
1085 ;;; does invalid wrapper traps when it finds an invalid
1086 ;;; wrapper and then returns the new, valid wrapper.
1088 ;;; <invalidp> True if any of the specialized arguments had an invalid
1089 ;;; wrapper, false otherwise.
1091 ;;; <type> READER or WRITER when the only method that would be run
1092 ;;; is a standard reader or writer method. To be specific,
1093 ;;; the value is READER when the method combination is eq to
1094 ;;; *standard-method-combination*; there are no applicable
1095 ;;; :before, :after or :around methods; and the most specific
1096 ;;; primary method is a standard reader method.
1098 ;;; <index> If <type> is READER or WRITER, and the slot accessed is
1099 ;;; an :instance slot, this is the index number of that slot
1100 ;;; in the object argument.
1101 (defvar *cache-miss-values-stack* ())
1103 (defun cache-miss-values (gf args state)
1104 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
1105 (get-generic-fun-info gf)
1106 (declare (ignore nreq applyp nkeys))
1107 (with-dfun-wrappers (args metatypes)
1108 (dfun-wrappers invalid-wrapper-p wrappers classes types)
1109 (error-need-at-least-n-args gf (length metatypes))
1110 (multiple-value-bind (emf methods accessor-type index)
1111 (cache-miss-values-internal
1112 gf arg-info wrappers classes types state)
1116 accessor-type index)))))
1118 (defun cache-miss-values-internal (gf arg-info wrappers classes types state)
1119 (if (and classes (equal classes (cdr (assq gf *cache-miss-values-stack*))))
1120 (break-vicious-metacircle gf classes arg-info)
1121 (let ((*cache-miss-values-stack*
1122 (acons gf classes *cache-miss-values-stack*))
1123 (cam-std-p (or (null arg-info)
1124 (gf-info-c-a-m-emf-std-p arg-info))))
1125 (multiple-value-bind (methods all-applicable-and-sorted-p)
1127 (compute-applicable-methods-using-types gf types)
1128 (compute-applicable-methods-using-classes gf classes))
1130 (let* ((for-accessor-p (eq state 'accessor))
1131 (for-cache-p (or (eq state 'caching) (eq state 'accessor)))
1132 (emf (if (or cam-std-p all-applicable-and-sorted-p)
1134 (get-secondary-dispatch-function1
1135 gf methods types nil (and for-cache-p wrappers)
1136 all-applicable-and-sorted-p)))
1137 (make-callable gf methods generator
1138 nil (and for-cache-p wrappers)))
1139 (default-secondary-dispatch-function gf))))
1140 (multiple-value-bind (index accessor-type)
1141 (and for-accessor-p all-applicable-and-sorted-p methods
1142 (accessor-values gf arg-info classes methods))
1143 (values (if (integerp index) index emf)
1144 methods accessor-type index)))))))
1146 ;;; Try to break a vicious circle while computing a cache miss.
1147 ;;; GF is the generic function, CLASSES are the classes of actual
1148 ;;; arguments, and ARG-INFO is the generic functions' arg-info.
1150 ;;; A vicious circle can be entered when the computation of the cache
1151 ;;; miss values itself depends on the values being computed. For
1152 ;;; instance, adding a method which is an instance of a subclass of
1153 ;;; STANDARD-METHOD leads to cache misses for slot accessors of
1154 ;;; STANDARD-METHOD like METHOD-SPECIALIZERS, and METHOD-SPECIALIZERS
1155 ;;; is itself used while we compute cache miss values.
1156 (defun break-vicious-metacircle (gf classes arg-info)
1157 (when (typep gf 'standard-generic-function)
1158 (multiple-value-bind (class slotd accessor-type)
1159 (accesses-standard-class-slot-p gf)
1161 (let ((method (find-standard-class-accessor-method
1162 gf class accessor-type))
1163 (index (standard-slot-value/eslotd slotd 'location))
1164 (type (gf-info-simple-accessor-type arg-info)))
1166 (subtypep (ecase accessor-type
1167 ((reader) (car classes))
1168 ((writer) (cadr classes)))
1170 (return-from break-vicious-metacircle
1171 (values index (list method) type index)))))))
1172 (error "~@<vicious metacircle: The computation of an ~
1173 effective method of ~s for arguments of types ~s uses ~
1174 the effective method being computed.~@:>"
1177 ;;; Return (CLASS SLOTD ACCESSOR-TYPE) if some method of generic
1178 ;;; function GF accesses a slot of some class in *STANDARD-CLASSES*.
1179 ;;; CLASS is the class accessed, SLOTD is the effective slot definition
1180 ;;; object of the slot accessed, and ACCESSOR-TYPE is one of the symbols
1181 ;;; READER or WRITER describing the slot access.
1182 (defun accesses-standard-class-slot-p (gf)
1183 (flet ((standard-class-slot-access (gf class)
1184 (loop with gf-name = (standard-slot-value/gf gf 'name)
1185 for slotd in (standard-slot-value/class class 'slots)
1186 ;; FIXME: where does BOUNDP fit in here? Is it
1188 as readers = (standard-slot-value/eslotd slotd 'readers)
1189 as writers = (standard-slot-value/eslotd slotd 'writers)
1190 if (member gf-name readers :test #'equal)
1191 return (values slotd 'reader)
1192 else if (member gf-name writers :test #'equal)
1193 return (values slotd 'writer))))
1194 (dolist (class-name *standard-classes*)
1195 (let ((class (find-class class-name)))
1196 (multiple-value-bind (slotd accessor-type)
1197 (standard-class-slot-access gf class)
1199 (return (values class slotd accessor-type))))))))
1201 ;;; Find a slot reader/writer method among the methods of generic
1202 ;;; function GF which reads/writes instances of class CLASS.
1203 ;;; TYPE is one of the symbols READER or WRITER.
1204 (defun find-standard-class-accessor-method (gf class type)
1205 (let ((cpl (standard-slot-value/class class '%class-precedence-list))
1206 (found-specializer *the-class-t*)
1208 (dolist (method (standard-slot-value/gf gf 'methods) found-method)
1209 (let ((specializers (standard-slot-value/method method 'specializers))
1210 (qualifiers (standard-slot-value/method method 'qualifiers)))
1211 (when (and (null qualifiers)
1212 (let ((subcpl (member (ecase type
1213 (reader (car specializers))
1214 (writer (cadr specializers)))
1216 (and subcpl (member found-specializer subcpl))))
1217 (setf found-specializer (ecase type
1218 (reader (car specializers))
1219 (writer (cadr specializers))))
1220 (setf found-method method))))))
1222 (defun accessor-values (gf arg-info classes methods)
1223 (declare (ignore gf))
1224 (let* ((accessor-type (gf-info-simple-accessor-type arg-info))
1225 (accessor-class (case accessor-type
1226 ((reader boundp) (car classes))
1227 (writer (cadr classes)))))
1228 (accessor-values-internal accessor-type accessor-class methods)))
1230 (defun accessor-values1 (gf accessor-type accessor-class)
1231 (let* ((type `(class-eq ,accessor-class))
1232 (types (ecase accessor-type
1233 ((reader boundp) `(,type))
1234 (writer `(t ,type))))
1235 (methods (compute-applicable-methods-using-types gf types)))
1236 (accessor-values-internal accessor-type accessor-class methods)))
1238 (defun accessor-values-internal (accessor-type accessor-class methods)
1239 (dolist (meth methods)
1240 (when (if (consp meth)
1241 (early-method-qualifiers meth)
1242 (safe-method-qualifiers meth))
1243 (return-from accessor-values-internal (values nil nil))))
1244 (let* ((meth (car methods))
1245 (early-p (not (eq *boot-state* 'complete)))
1246 (slot-name (when accessor-class
1248 (and (early-method-standard-accessor-p meth)
1249 (early-method-standard-accessor-slot-name meth))
1250 (and (member *the-class-standard-object*
1252 (early-class-precedence-list
1254 (class-precedence-list
1257 (not (eq *the-class-standard-method*
1258 (early-method-class meth)))
1259 (accessor-method-p meth))
1261 (early-accessor-method-slot-name meth)
1262 (accessor-method-slot-name meth))))))
1263 (slotd (and accessor-class
1265 (dolist (slot (early-class-slotds accessor-class) nil)
1266 (when (eql slot-name
1267 (early-slot-definition-name slot))
1269 (find-slot-definition accessor-class slot-name)))))
1272 (slot-accessor-std-p slotd accessor-type))
1274 (not (safe-p accessor-class))))
1276 (early-slot-definition-location slotd)
1277 (slot-definition-location slotd))
1280 (defun make-accessor-table (gf type &optional table)
1281 (unless table (setq table (make-hash-table :test 'eq)))
1282 (let ((methods (if (early-gf-p gf)
1283 (early-gf-methods gf)
1284 (generic-function-methods gf)))
1286 (no-class-slots-p t)
1287 (early-p (not (eq *boot-state* 'complete)))
1288 first second (size 0))
1289 (declare (fixnum size))
1290 ;; class -> {(specl slotd)}
1291 (dolist (method methods)
1292 (let* ((specializers (if (consp method)
1293 (early-method-specializers method t)
1294 (method-specializers method)))
1296 ((reader boundp) (car specializers))
1297 (writer (cadr specializers))))
1298 (specl-cpl (if early-p
1299 (early-class-precedence-list specl)
1300 (and (class-finalized-p specl)
1301 (class-precedence-list specl))))
1302 (so-p (member *the-class-standard-object* specl-cpl))
1303 (slot-name (if (consp method)
1304 (and (early-method-standard-accessor-p method)
1305 (early-method-standard-accessor-slot-name
1307 (accessor-method-slot-name method))))
1308 (when (or (null specl-cpl)
1310 (member *the-class-structure-object* specl-cpl))
1311 (return-from make-accessor-table nil))
1312 ;; Collect all the slot-definitions for SLOT-NAME from SPECL and
1313 ;; all of its subclasses. If either SPECL or one of the subclasses
1314 ;; is not a standard-class, bail out.
1315 (labels ((aux (class)
1316 ;; FIND-SLOT-DEFINITION might not be defined yet
1317 (let ((slotd (find-if (lambda (x)
1318 (eq (sb-pcl::slot-definition-name x)
1320 (sb-pcl::class-slots class))))
1323 (slot-accessor-std-p slotd type))
1324 (return-from make-accessor-table nil))
1325 (push (cons specl slotd) (gethash class table))))
1326 (dolist (subclass (sb-pcl::class-direct-subclasses class))
1329 (maphash (lambda (class specl+slotd-list)
1330 (dolist (sclass (if early-p
1331 (early-class-precedence-list class)
1332 (class-precedence-list class))
1333 (error "This can't happen."))
1334 (let ((a (assq sclass specl+slotd-list)))
1336 (let* ((slotd (cdr a))
1338 (early-slot-definition-location slotd)
1339 (slot-definition-location slotd))))
1340 (unless index (return-from make-accessor-table nil))
1341 (setf (gethash class table) index)
1342 (when (consp index) (setq no-class-slots-p nil))
1343 (setq all-index (if (or (null all-index)
1344 (eql all-index index))
1347 (cond ((= size 1) (setq first class))
1348 ((= size 2) (setq second class)))
1351 (values table all-index first second size no-class-slots-p)))
1353 (defun compute-applicable-methods-using-types (generic-function types)
1354 (let ((definite-p t) (possibly-applicable-methods nil))
1355 (dolist (method (if (early-gf-p generic-function)
1356 (early-gf-methods generic-function)
1357 (safe-generic-function-methods generic-function)))
1358 (let ((specls (if (consp method)
1359 (early-method-specializers method t)
1360 (safe-method-specializers method)))
1362 (possibly-applicable-p t) (applicable-p t))
1363 (dolist (specl specls)
1364 (multiple-value-bind (specl-applicable-p specl-possibly-applicable-p)
1365 (specializer-applicable-using-type-p specl (pop types))
1366 (unless specl-applicable-p
1367 (setq applicable-p nil))
1368 (unless specl-possibly-applicable-p
1369 (setq possibly-applicable-p nil)
1371 (when possibly-applicable-p
1372 (unless applicable-p (setq definite-p nil))
1373 (push method possibly-applicable-methods))))
1374 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
1375 (get-generic-fun-info generic-function)
1376 (declare (ignore nreq applyp metatypes nkeys))
1377 (let* ((precedence (arg-info-precedence arg-info)))
1378 (values (sort-applicable-methods precedence
1379 (nreverse possibly-applicable-methods)
1383 (defun sort-applicable-methods (precedence methods types)
1384 (sort-methods methods
1386 (lambda (class1 class2 index)
1387 (let* ((class (type-class (nth index types)))
1388 (cpl (if (eq *boot-state* 'complete)
1389 (class-precedence-list class)
1390 (early-class-precedence-list class))))
1391 (if (memq class2 (memq class1 cpl))
1394 (defun sort-methods (methods precedence compare-classes-function)
1395 (flet ((sorter (method1 method2)
1396 (dolist (index precedence)
1397 (let* ((specl1 (nth index (if (listp method1)
1398 (early-method-specializers method1
1400 (method-specializers method1))))
1401 (specl2 (nth index (if (listp method2)
1402 (early-method-specializers method2
1404 (method-specializers method2))))
1405 (order (order-specializers
1406 specl1 specl2 index compare-classes-function)))
1408 (return-from sorter (eq order specl1)))))))
1409 (stable-sort methods #'sorter)))
1411 (defun order-specializers (specl1 specl2 index compare-classes-function)
1412 (let ((type1 (if (eq *boot-state* 'complete)
1413 (specializer-type specl1)
1414 (!bootstrap-get-slot 'specializer specl1 '%type)))
1415 (type2 (if (eq *boot-state* 'complete)
1416 (specializer-type specl2)
1417 (!bootstrap-get-slot 'specializer specl2 '%type))))
1418 (cond ((eq specl1 specl2)
1426 (class (case (car type2)
1427 (class (funcall compare-classes-function
1428 specl1 specl2 index))
1430 (prototype (case (car type2)
1431 (class (funcall compare-classes-function
1432 specl1 specl2 index))
1434 (class-eq (case (car type2)
1438 (eql (case (car type2)
1442 (defun map-all-orders (methods precedence function)
1443 (let ((choices nil))
1444 (flet ((compare-classes-function (class1 class2 index)
1445 (declare (ignore index))
1447 (dolist (c choices nil)
1448 (when (or (and (eq (first c) class1)
1449 (eq (second c) class2))
1450 (and (eq (first c) class2)
1451 (eq (second c) class1)))
1452 (return (setq choice c))))
1455 (if (class-might-precede-p class1 class2)
1456 (if (class-might-precede-p class2 class1)
1457 (list class1 class2 nil t)
1458 (list class1 class2 t))
1459 (if (class-might-precede-p class2 class1)
1460 (list class2 class1 t)
1461 (let ((name1 (class-name class1))
1462 (name2 (class-name class2)))
1467 (string< (symbol-name name1)
1468 (symbol-name name2)))
1469 (list class1 class2 t)
1470 (list class2 class1 t))))))
1471 (push choice choices))
1473 (loop (funcall function
1474 (sort-methods methods
1476 #'compare-classes-function))
1477 (unless (dolist (c choices nil)
1479 (rotatef (car c) (cadr c))
1480 (return (setf (third c) t))))
1483 ;;; CMUCL comment: used only in map-all-orders
1484 (defun class-might-precede-p (class1 class2)
1485 (if (not *in-precompute-effective-methods-p*)
1486 (not (member class1 (cdr (class-precedence-list class2))))
1487 (class-can-precede-p class1 class2)))
1489 (defun compute-precedence (lambda-list nreq argument-precedence-order)
1490 (if (null argument-precedence-order)
1492 (dotimes-fixnum (i nreq list) (push (- (1- nreq) i) list)))
1493 (mapcar (lambda (x) (position x lambda-list))
1494 argument-precedence-order)))
1496 (defun cpl-or-nil (class)
1497 (if (eq *boot-state* 'complete)
1499 ;; KLUDGE: why not use (slot-boundp class
1500 ;; 'class-precedence-list)? Well, unfortunately, CPL-OR-NIL is
1501 ;; used within COMPUTE-APPLICABLE-METHODS, including for
1502 ;; SLOT-BOUNDP-USING-CLASS... and the available mechanism for
1503 ;; breaking such nasty cycles in effective method computation
1504 ;; only works for readers and writers, not boundps. It might
1505 ;; not be too hard to make it work for BOUNDP accessors, but in
1506 ;; the meantime we use an extra slot for exactly the result of
1507 ;; the SLOT-BOUNDP that we want. (We cannot use
1508 ;; CLASS-FINALIZED-P, because in the process of class
1509 ;; finalization we need to use the CPL which has been computed
1510 ;; to cache effective methods for slot accessors.) -- CSR,
1513 (when (cpl-available-p class)
1514 (return-from cpl-or-nil (class-precedence-list class)))
1516 ;; if we can finalize an unfinalized class, then do so
1517 (when (and (not (class-finalized-p class))
1518 (not (class-has-a-forward-referenced-superclass-p class)))
1519 (finalize-inheritance class)
1520 (class-precedence-list class)))
1522 (early-class-precedence-list class)))
1524 (defun saut-and (specl type)
1525 (let ((applicable nil)
1526 (possibly-applicable t))
1527 (dolist (type (cdr type))
1528 (multiple-value-bind (appl poss-appl)
1529 (specializer-applicable-using-type-p specl type)
1530 (when appl (return (setq applicable t)))
1531 (unless poss-appl (return (setq possibly-applicable nil)))))
1532 (values applicable possibly-applicable)))
1534 (defun saut-not (specl type)
1535 (let ((ntype (cadr type)))
1538 (class (saut-not-class specl ntype))
1539 (class-eq (saut-not-class-eq specl ntype))
1540 (prototype (saut-not-prototype specl ntype))
1541 (eql (saut-not-eql specl ntype))
1542 (t (error "~S cannot handle the second argument ~S"
1543 'specializer-applicable-using-type-p type))))))
1545 (defun saut-not-class (specl ntype)
1546 (let* ((class (type-class specl))
1547 (cpl (cpl-or-nil class)))
1548 (not (memq (cadr ntype) cpl))))
1550 (defun saut-not-prototype (specl ntype)
1551 (let* ((class (case (car specl)
1552 (eql (class-of (cadr specl)))
1553 (class-eq (cadr specl))
1554 (prototype (cadr specl))
1555 (class (cadr specl))))
1556 (cpl (cpl-or-nil class)))
1557 (not (memq (cadr ntype) cpl))))
1559 (defun saut-not-class-eq (specl ntype)
1560 (let ((class (case (car specl)
1561 (eql (class-of (cadr specl)))
1562 (class-eq (cadr specl)))))
1563 (not (eq class (cadr ntype)))))
1565 (defun saut-not-eql (specl ntype)
1567 (eql (not (eql (cadr specl) (cadr ntype))))
1570 (defun class-applicable-using-class-p (specl type)
1571 (let ((pred (memq specl (cpl-or-nil type))))
1574 (if (not *in-precompute-effective-methods-p*)
1575 ;; classes might get common subclass
1576 (superclasses-compatible-p specl type)
1577 ;; worry only about existing classes
1578 (classes-have-common-subclass-p specl type))))))
1580 (defun classes-have-common-subclass-p (class1 class2)
1581 (or (eq class1 class2)
1582 (let ((class1-subs (class-direct-subclasses class1)))
1583 (or (memq class2 class1-subs)
1584 (dolist (class1-sub class1-subs nil)
1585 (when (classes-have-common-subclass-p class1-sub class2)
1588 (defun saut-class (specl type)
1590 (class (class-applicable-using-class-p (cadr specl) (cadr type)))
1591 (t (values nil (let ((class (type-class specl)))
1593 (cpl-or-nil class)))))))
1595 (defun saut-class-eq (specl type)
1596 (if (eq (car specl) 'eql)
1597 (values nil (eq (class-of (cadr specl)) (cadr type)))
1598 (let ((pred (case (car specl)
1600 (eq (cadr specl) (cadr type)))
1602 (or (eq (cadr specl) (cadr type))
1603 (memq (cadr specl) (cpl-or-nil (cadr type))))))))
1604 (values pred pred))))
1606 (defun saut-prototype (specl type)
1607 (declare (ignore specl type))
1608 (values nil nil)) ; XXX original PCL comment: fix this someday
1610 (defun saut-eql (specl type)
1611 (let ((pred (case (car specl)
1612 (eql (eql (cadr specl) (cadr type)))
1613 (class-eq (eq (cadr specl) (class-of (cadr type))))
1614 (class (memq (cadr specl)
1615 (let ((class (class-of (cadr type))))
1616 (cpl-or-nil class)))))))
1617 (values pred pred)))
1619 (defun specializer-applicable-using-type-p (specl type)
1620 (setq specl (type-from-specializer specl))
1622 (return-from specializer-applicable-using-type-p (values t t)))
1623 ;; This is used by C-A-M-U-T and GENERATE-DISCRIMINATION-NET-INTERNAL,
1624 ;; and has only what they need.
1625 (if (or (atom type) (eq (car type) t))
1628 (and (saut-and specl type))
1629 (not (saut-not specl type))
1630 (class (saut-class specl type))
1631 (prototype (saut-prototype specl type))
1632 (class-eq (saut-class-eq specl type))
1633 (eql (saut-eql specl type))
1634 (t (error "~S cannot handle the second argument ~S."
1635 'specializer-applicable-using-type-p
1638 (defun map-all-classes (fun &optional (root t))
1639 (let ((all-classes (make-hash-table :test 'eq))
1640 (braid-p (or (eq *boot-state* 'braid)
1641 (eq *boot-state* 'complete))))
1642 (labels ((do-class (class)
1643 (unless (gethash class all-classes)
1644 (setf (gethash class all-classes) t)
1648 (class-direct-subclasses class)
1649 (early-class-direct-subclasses class))))))
1650 (do-class (if (symbolp root)
1655 ;;; FIXME: Needs a lock.
1656 (defvar *effective-method-cache* (make-hash-table :test 'eq))
1658 (defun flush-effective-method-cache (generic-function)
1659 (dolist (method (generic-function-methods generic-function))
1660 (remhash method *effective-method-cache*)))
1662 (defun get-secondary-dispatch-function (gf methods types
1663 &optional method-alist wrappers)
1665 (get-secondary-dispatch-function1
1666 gf methods types (not (null method-alist)) (not (null wrappers))
1667 (not (methods-contain-eql-specializer-p methods)))))
1668 (make-callable gf methods generator method-alist wrappers)))
1670 (defun get-secondary-dispatch-function1 (gf methods types method-alist-p
1678 (lambda (method-alist wrappers)
1679 (declare (ignore method-alist wrappers))
1680 #'(lambda (&rest args)
1681 (apply #'no-applicable-method gf args)))
1682 (lambda (method-alist wrappers)
1683 (declare (ignore method-alist wrappers))
1684 (lambda (&rest args)
1685 (apply #'no-applicable-method gf args))))
1686 (let* ((key (car methods))
1687 (ht-value (or (gethash key *effective-method-cache*)
1688 (setf (gethash key *effective-method-cache*)
1690 (if (and (null (cdr methods)) all-applicable-p ; the most common case
1691 (null method-alist-p) wrappers-p (not function-p))
1693 (setf (car ht-value)
1694 (get-secondary-dispatch-function2
1695 gf methods types method-alist-p wrappers-p
1696 all-applicable-p all-sorted-p function-p)))
1697 (let ((akey (list methods
1698 (if all-applicable-p 'all-applicable types)
1699 method-alist-p wrappers-p function-p)))
1700 (or (cdr (assoc akey (cdr ht-value) :test #'equal))
1701 (let ((value (get-secondary-dispatch-function2
1702 gf methods types method-alist-p wrappers-p
1703 all-applicable-p all-sorted-p function-p)))
1704 (push (cons akey value) (cdr ht-value))
1707 (defun get-secondary-dispatch-function2 (gf methods types method-alist-p
1708 wrappers-p all-applicable-p
1709 all-sorted-p function-p)
1710 (if (and all-applicable-p all-sorted-p (not function-p))
1711 (if (eq *boot-state* 'complete)
1712 (let* ((combin (generic-function-method-combination gf))
1713 (effective (compute-effective-method gf combin methods)))
1714 (make-effective-method-function1 gf effective method-alist-p
1716 (let ((effective (standard-compute-effective-method gf nil methods)))
1717 (make-effective-method-function1 gf effective method-alist-p
1719 (let ((net (generate-discrimination-net
1720 gf methods types all-sorted-p)))
1721 (compute-secondary-dispatch-function1 gf net function-p))))
1723 (defun get-effective-method-function (gf methods
1724 &optional method-alist wrappers)
1726 (get-secondary-dispatch-function1
1727 gf methods nil (not (null method-alist)) (not (null wrappers)) t)))
1728 (make-callable gf methods generator method-alist wrappers)))
1730 (defun get-effective-method-function1 (gf methods &optional (sorted-p t))
1731 (get-secondary-dispatch-function1 gf methods nil nil nil t sorted-p))
1733 (defun methods-contain-eql-specializer-p (methods)
1734 (and (eq *boot-state* 'complete)
1735 (dolist (method methods nil)
1736 (when (dolist (spec (method-specializers method) nil)
1737 (when (eql-specializer-p spec) (return t)))
1740 (defun update-dfun (generic-function &optional dfun cache info)
1741 (let* ((early-p (early-gf-p generic-function)))
1742 (set-dfun generic-function dfun cache info)
1743 (let ((dfun (if early-p
1744 (or dfun (make-initial-dfun generic-function))
1745 (compute-discriminating-function generic-function))))
1746 (set-funcallable-instance-function generic-function dfun)
1747 (let ((gf-name (if early-p
1748 (!early-gf-name generic-function)
1749 (generic-function-name generic-function))))
1750 (set-fun-name generic-function gf-name)
1753 (defvar *dfun-count* nil)
1754 (defvar *dfun-list* nil)
1755 (defvar *minimum-cache-size-to-list*)
1757 ;;; These functions aren't used in SBCL, or documented anywhere that
1758 ;;; I'm aware of, but they look like they might be useful for
1759 ;;; debugging or performance tweaking or something, so I've just
1760 ;;; commented them out instead of deleting them. -- WHN 2001-03-28
1762 (defun list-dfun (gf)
1763 (let* ((sym (type-of (gf-dfun-info gf)))
1764 (a (assq sym *dfun-list*)))
1766 (push (setq a (list sym)) *dfun-list*))
1767 (push (generic-function-name gf) (cdr a))))
1769 (defun list-all-dfuns ()
1770 (setq *dfun-list* nil)
1771 (map-all-generic-functions #'list-dfun)
1774 (defun list-large-cache (gf)
1775 (let* ((sym (type-of (gf-dfun-info gf)))
1776 (cache (gf-dfun-cache gf)))
1778 (let ((size (cache-size cache)))
1779 (when (>= size *minimum-cache-size-to-list*)
1780 (let ((a (assoc size *dfun-list*)))
1782 (push (setq a (list size)) *dfun-list*))
1783 (push (let ((name (generic-function-name gf)))
1784 (if (eq sym 'caching) name (list name sym)))
1787 (defun list-large-caches (&optional (*minimum-cache-size-to-list* 130))
1788 (setq *dfun-list* nil)
1789 (map-all-generic-functions #'list-large-cache)
1790 (setq *dfun-list* (sort *dfun-list* #'< :key #'car))
1791 (mapc #'print *dfun-list*)
1794 (defun count-dfun (gf)
1795 (let* ((sym (type-of (gf-dfun-info gf)))
1796 (cache (gf-dfun-cache gf))
1797 (a (assq sym *dfun-count*)))
1799 (push (setq a (list sym 0 nil)) *dfun-count*))
1802 (let* ((size (cache-size cache))
1803 (b (assoc size (third a))))
1805 (push (setq b (cons size 0)) (third a)))
1808 (defun count-all-dfuns ()
1809 (setq *dfun-count* (mapcar (lambda (type) (list type 0 nil))
1810 '(ONE-CLASS TWO-CLASS DEFAULT-METHOD-ONLY
1811 ONE-INDEX N-N CHECKING CACHING
1813 (map-all-generic-functions #'count-dfun)
1814 (mapc (lambda (type+count+sizes)
1815 (setf (third type+count+sizes)
1816 (sort (third type+count+sizes) #'< :key #'car)))
1818 (mapc (lambda (type+count+sizes)
1819 (format t "~&There are ~W dfuns of type ~S."
1820 (cadr type+count+sizes) (car type+count+sizes))
1821 (format t "~% ~S~%" (caddr type+count+sizes)))
1826 (defun gfs-of-type (type)
1827 (unless (consp type) (setq type (list type)))
1828 (let ((gf-list nil))
1829 (map-all-generic-functions (lambda (gf)
1830 (when (memq (type-of (gf-dfun-info gf))
1832 (push gf gf-list))))