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)
493 (let ((fmf (if (listp method)
495 (safe-method-fast-function method))))
496 (method-function-get fmf :slot-name-lists)))
497 ;; KLUDGE: As of sbcl-0.6.4, it's very important for
498 ;; efficiency to know the type of the sequence argument to
499 ;; quantifiers (SOME/NOTANY/etc.) at compile time, but
500 ;; the compiler isn't smart enough to understand the :TYPE
501 ;; slot option for DEFCLASS, so we just tell
502 ;; it the type by hand here.
504 (if (early-gf-p generic-function)
505 (early-gf-methods generic-function)
506 (generic-function-methods generic-function)))))
508 (defun checking-limit-fn (nlines)
509 (default-limit-fn nlines))
511 (defun make-caching-dfun (generic-function &optional cache)
513 (when (use-constant-value-dfun-p generic-function)
514 (return-from make-caching-dfun
515 (make-constant-value-dfun generic-function)))
516 (when (use-dispatch-dfun-p generic-function)
517 (return-from make-caching-dfun
518 (make-dispatch-dfun generic-function))))
519 (multiple-value-bind (nreq applyp metatypes nkeys)
520 (get-generic-fun-info generic-function)
521 (declare (ignore nreq))
522 (let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
523 (dfun-info (caching-dfun-info cache)))
525 (funcall (get-dfun-constructor 'emit-caching metatypes applyp)
528 (caching-miss generic-function args dfun-info)))
532 (defun make-final-caching-dfun (generic-function classes-list new-class)
533 (let ((cache (make-final-ordinary-dfun-internal
534 generic-function t #'caching-limit-fn
535 classes-list new-class)))
536 (make-caching-dfun generic-function cache)))
538 (defun caching-limit-fn (nlines)
539 (default-limit-fn nlines))
541 (defun insure-caching-dfun (gf)
542 (multiple-value-bind (nreq applyp metatypes nkeys)
543 (get-generic-fun-info gf)
544 (declare (ignore nreq nkeys))
546 (not (null (car metatypes)))
547 (dolist (mt metatypes nil)
548 (unless (eq mt t) (return t))))
549 (get-dfun-constructor 'emit-caching metatypes applyp))))
551 (defun use-constant-value-dfun-p (gf &optional boolean-values-p)
552 (multiple-value-bind (nreq applyp metatypes nkeys)
553 (get-generic-fun-info gf)
554 (declare (ignore nreq metatypes nkeys))
555 (let* ((early-p (early-gf-p gf))
557 (early-gf-methods gf)
558 (generic-function-methods gf)))
559 (default '(unknown)))
561 (or (not (eq *boot-state* 'complete))
562 ;; If COMPUTE-APPLICABLE-METHODS is specialized, we
563 ;; can't use this, of course, because we can't tell
564 ;; which methods will be considered applicable.
566 ;; Also, don't use this dfun method if the generic
567 ;; function has a non-standard method combination,
568 ;; because if it has, it's not sure that method
569 ;; functions are used directly as effective methods,
570 ;; which CONSTANT-VALUE-MISS depends on. The
571 ;; pre-defined method combinations like LIST are
573 (and (compute-applicable-methods-emf-std-p gf)
574 (eq (generic-function-method-combination gf)
575 *standard-method-combination*)))
576 ;; Check that no method is eql-specialized, and that all
577 ;; methods return a constant value. If BOOLEAN-VALUES-P,
578 ;; check that all return T or NIL. Also, check that no
579 ;; method has qualifiers, to make sure that emfs are really
580 ;; method functions; see above.
581 (dolist (method methods t)
582 (when (eq *boot-state* 'complete)
583 (when (or (some #'eql-specializer-p
584 (safe-method-specializers method))
585 (safe-method-qualifiers method))
587 (let ((value (method-function-get
589 (or (third method) (second method))
590 (or (safe-method-fast-function method)
591 (safe-method-function method)))
592 :constant-value default)))
593 (when (or (eq value default)
594 (and boolean-values-p
595 (not (member value '(t nil)))))
598 (defun make-constant-value-dfun (generic-function &optional cache)
599 (multiple-value-bind (nreq applyp metatypes nkeys)
600 (get-generic-fun-info generic-function)
601 (declare (ignore nreq applyp))
602 (let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
603 (dfun-info (constant-value-dfun-info cache)))
605 (funcall (get-dfun-constructor 'emit-constant-value metatypes)
608 (constant-value-miss generic-function args dfun-info)))
612 (defun make-final-constant-value-dfun (generic-function classes-list new-class)
613 (let ((cache (make-final-ordinary-dfun-internal
614 generic-function :constant-value #'caching-limit-fn
615 classes-list new-class)))
616 (make-constant-value-dfun generic-function cache)))
618 (defun use-dispatch-dfun-p (gf &optional (caching-p (use-caching-dfun-p gf)))
619 (when (eq *boot-state* 'complete)
620 (unless (or caching-p
621 (gf-requires-emf-keyword-checks gf))
622 ;; This should return T when almost all dispatching is by
623 ;; eql specializers or built-in classes. In other words,
624 ;; return NIL if we might ever need to do more than
625 ;; one (non built-in) typep.
626 ;; Otherwise, it is probably at least as fast to use
627 ;; a caching dfun first, possibly followed by secondary dispatching.
629 #||;;; Original found in cmu 17f -- S L O W
630 (< (dispatch-dfun-cost gf) (caching-dfun-cost gf))
632 ;; This uses improved dispatch-dfun-cost below
633 (let ((cdc (caching-dfun-cost gf))) ; fast
634 (> cdc (dispatch-dfun-cost gf cdc))))))
636 (defparameter *non-built-in-typep-cost* 1)
637 (defparameter *structure-typep-cost* 1)
638 (defparameter *built-in-typep-cost* 0)
640 ;;; According to comments in the original CMU CL version of PCL,
641 ;;; the cost LIMIT is important to cut off exponential growth for
642 ;;; large numbers of gf methods and argument lists.
643 (defun dispatch-dfun-cost (gf &optional limit)
644 (generate-discrimination-net-internal
645 gf (generic-function-methods gf) nil
646 (lambda (methods known-types)
647 (declare (ignore methods known-types))
649 (lambda (position type true-value false-value)
650 (declare (ignore position))
651 (let* ((type-test-cost
652 (if (eq 'class (car type))
653 (let* ((metaclass (class-of (cadr type)))
654 (mcpl (class-precedence-list metaclass)))
655 (cond ((memq *the-class-built-in-class* mcpl)
656 *built-in-typep-cost*)
657 ((memq *the-class-structure-class* mcpl)
658 *structure-typep-cost*)
660 *non-built-in-typep-cost*)))
663 (+ (max true-value false-value) type-test-cost)))
664 (when (and limit (<= limit max-cost-so-far))
665 (return-from dispatch-dfun-cost max-cost-so-far))
669 (defparameter *cache-lookup-cost* 1)
670 (defparameter *wrapper-of-cost* 0)
671 (defparameter *secondary-dfun-call-cost* 1)
673 (defun caching-dfun-cost (gf)
674 (let ((nreq (get-generic-fun-info gf)))
675 (+ *cache-lookup-cost*
676 (* *wrapper-of-cost* nreq)
677 (if (methods-contain-eql-specializer-p
678 (generic-function-methods gf))
679 *secondary-dfun-call-cost*
682 (setq *non-built-in-typep-cost* 100)
683 (setq *structure-typep-cost* 15)
684 (setq *built-in-typep-cost* 5)
685 (setq *cache-lookup-cost* 30)
686 (setq *wrapper-of-cost* 15)
687 (setq *secondary-dfun-call-cost* 30)
689 (declaim (inline make-callable))
690 (defun make-callable (gf methods generator method-alist wrappers)
691 (let* ((*applicable-methods* methods)
692 (callable (function-funcall generator method-alist wrappers)))
695 (defun make-dispatch-dfun (gf)
696 (values (get-dispatch-function gf) nil (dispatch-dfun-info)))
698 (defun get-dispatch-function (gf)
699 (let* ((methods (generic-function-methods gf))
700 (generator (get-secondary-dispatch-function1
701 gf methods nil nil nil nil nil t)))
702 (make-callable gf methods generator nil nil)))
704 (defun make-final-dispatch-dfun (gf)
705 (make-dispatch-dfun gf))
707 (defun update-dispatch-dfuns ()
708 (dolist (gf (gfs-of-type '(dispatch initial-dispatch)))
709 (dfun-update gf #'make-dispatch-dfun)))
711 (defun fill-dfun-cache (table valuep nkeys limit-fn &optional cache)
712 (let ((cache (or cache (get-cache nkeys valuep limit-fn
713 (+ (hash-table-count table) 3)))))
714 (maphash (lambda (classes value)
715 (setq cache (fill-cache cache
716 (class-wrapper classes)
721 (defun make-final-ordinary-dfun-internal (generic-function valuep limit-fn
722 classes-list new-class)
723 (let* ((arg-info (gf-arg-info generic-function))
724 (nkeys (arg-info-nkeys arg-info))
725 (new-class (and new-class
726 (equal (type-of (gf-dfun-info generic-function))
727 (cond ((eq valuep t) 'caching)
728 ((eq valuep :constant-value) 'constant-value)
729 ((null valuep) 'checking)))
732 (copy-cache (gf-dfun-cache generic-function))
733 (get-cache nkeys (not (null valuep)) limit-fn 4))))
734 (make-emf-cache generic-function valuep cache classes-list new-class)))
736 (defvar *dfun-miss-gfs-on-stack* ())
738 (defmacro dfun-miss ((gf args wrappers invalidp nemf
739 &optional type index caching-p applicable)
741 (unless applicable (setq applicable (gensym)))
742 `(multiple-value-bind (,nemf ,applicable ,wrappers ,invalidp
743 ,@(when type `(,type ,index)))
744 (cache-miss-values ,gf ,args ',(cond (caching-p 'caching)
747 (when (and ,applicable (not (memq ,gf *dfun-miss-gfs-on-stack*)))
748 (let ((*dfun-miss-gfs-on-stack* (cons ,gf *dfun-miss-gfs-on-stack*)))
750 ;; Create a FAST-INSTANCE-BOUNDP structure instance for a cached
751 ;; SLOT-BOUNDP so that INVOKE-EMF does the right thing, that is,
752 ;; does not signal a SLOT-UNBOUND error for a boundp test.
754 ;; FIXME: could the NEMF not be a CONS (for :CLASS-allocated
756 `((if (and (eq ,type 'boundp) (integerp ,nemf))
757 (invoke-emf (make-fast-instance-boundp :index ,nemf) ,args)
758 (invoke-emf ,nemf ,args)))
759 `((invoke-emf ,nemf ,args)))))
761 ;;; The dynamically adaptive method lookup algorithm is implemented is
762 ;;; implemented as a kind of state machine. The kinds of
763 ;;; discriminating function is the state, the various kinds of reasons
764 ;;; for a cache miss are the state transitions.
766 ;;; The code which implements the transitions is all in the miss
767 ;;; handlers for each kind of dfun. Those appear here.
769 ;;; Note that within the states that cache, there are dfun updates
770 ;;; which simply select a new cache or cache field. Those are not
771 ;;; considered as state transitions.
772 (defvar *lazy-dfun-compute-p* t)
773 (defvar *early-p* nil)
775 (declaim (type (or null unsigned-byte) *max-emf-precomputation-methods*))
776 (defvar *max-emf-precomputation-methods* nil)
778 (defun finalize-specializers (gf)
779 (let ((methods (generic-function-methods gf)))
780 (when (or (null *max-emf-precomputation-methods*)
781 (<= (length methods) *max-emf-precomputation-methods*))
782 (let ((all-finalized t))
783 (dolist (method methods all-finalized)
784 (dolist (specializer (method-specializers method))
785 (when (and (classp specializer)
786 (not (class-finalized-p specializer)))
787 (if (class-has-a-forward-referenced-superclass-p specializer)
788 (setq all-finalized nil)
789 (finalize-inheritance specializer)))))))))
791 (defun make-initial-dfun (gf)
793 #'(lambda (&rest args)
794 (initial-dfun gf args))))
795 (multiple-value-bind (dfun cache info)
797 ((and (eq *boot-state* 'complete)
798 (not (finalize-specializers gf)))
799 (values initial-dfun nil (initial-dfun-info)))
800 ((and (eq *boot-state* 'complete)
801 (compute-applicable-methods-emf-std-p gf))
802 (let* ((caching-p (use-caching-dfun-p gf))
803 ;; KLUDGE: the only effect of this (when
804 ;; *LAZY-DFUN-COMPUTE-P* is true, as it usually is)
805 ;; is to signal an error when we try to add methods
806 ;; with the wrong qualifiers to a generic function.
807 (classes-list (precompute-effective-methods
809 (not *lazy-dfun-compute-p*))))
810 (if *lazy-dfun-compute-p*
811 (cond ((use-dispatch-dfun-p gf caching-p)
814 (initial-dispatch-dfun-info)))
816 (insure-caching-dfun gf)
817 (values initial-dfun nil (initial-dfun-info)))
819 (values initial-dfun nil (initial-dfun-info))))
820 (make-final-dfun-internal gf classes-list))))
822 (let ((arg-info (if (early-gf-p gf)
823 (early-gf-arg-info gf)
826 (if (and (gf-precompute-dfun-and-emf-p arg-info)
827 (setq type (final-accessor-dfun-type gf)))
829 (values (make-early-accessor gf type) nil nil)
830 (make-final-accessor-dfun gf type))
831 (values initial-dfun nil (initial-dfun-info))))))
832 (set-dfun gf dfun cache info))))
834 (defun make-early-accessor (gf type)
835 (let* ((methods (early-gf-methods gf))
836 (slot-name (early-method-standard-accessor-slot-name (car methods))))
838 (reader #'(lambda (instance)
839 (let* ((class (class-of instance))
840 (class-name (!bootstrap-get-slot 'class class 'name)))
841 (!bootstrap-get-slot class-name instance slot-name))))
842 (boundp #'(lambda (instance)
843 (let* ((class (class-of instance))
844 (class-name (!bootstrap-get-slot 'class class 'name)))
845 (not (eq +slot-unbound+
846 (!bootstrap-get-slot class-name
847 instance slot-name))))))
848 (writer #'(lambda (new-value instance)
849 (let* ((class (class-of instance))
850 (class-name (!bootstrap-get-slot 'class class 'name)))
851 (!bootstrap-set-slot class-name instance slot-name new-value)))))))
853 (defun initial-dfun (gf args)
854 (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
858 gf #'make-one-class-accessor-dfun ntype wrappers nindex))
859 ((use-caching-dfun-p gf)
860 (dfun-update gf #'make-caching-dfun))
863 gf #'make-checking-dfun
864 ;; nemf is suitable only for caching, have to do this:
865 (cache-miss-values gf args 'checking))))))
867 (defun make-final-dfun (gf &optional classes-list)
868 (multiple-value-bind (dfun cache info)
869 (make-final-dfun-internal gf classes-list)
870 (set-dfun gf dfun cache info)))
872 (defvar *new-class* nil)
874 (defvar *free-hash-tables* (mapcar #'list '(eq equal eql)))
876 (defmacro with-hash-table ((table test) &body forms)
877 `(let* ((.free. (assoc ',test *free-hash-tables*))
878 (,table (if (cdr .free.)
880 (make-hash-table :test ',test))))
881 (multiple-value-prog1
884 (push ,table (cdr .free.)))))
886 (defmacro with-eq-hash-table ((table) &body forms)
887 `(with-hash-table (,table eq) ,@forms))
889 (defun final-accessor-dfun-type (gf)
890 (let ((methods (if (early-gf-p gf)
891 (early-gf-methods gf)
892 (generic-function-methods gf))))
893 (cond ((every (lambda (method)
895 (eq *the-class-standard-reader-method*
896 (early-method-class method))
897 (standard-reader-method-p method)))
900 ((every (lambda (method)
902 (eq *the-class-standard-boundp-method*
903 (early-method-class method))
904 (standard-boundp-method-p method)))
907 ((every (lambda (method)
909 (eq *the-class-standard-writer-method*
910 (early-method-class method))
911 (standard-writer-method-p method)))
915 (defun make-final-accessor-dfun (gf type &optional classes-list new-class)
916 (with-eq-hash-table (table)
917 (multiple-value-bind (table all-index first second size no-class-slots-p)
918 (make-accessor-table gf type table)
921 (let ((w (class-wrapper first)))
922 (make-one-class-accessor-dfun gf type w all-index)))
923 ((and (= size 2) (or (integerp all-index) (consp all-index)))
924 (let ((w0 (class-wrapper first))
925 (w1 (class-wrapper second)))
926 (make-two-class-accessor-dfun gf type w0 w1 all-index)))
927 ((or (integerp all-index) (consp all-index))
928 (make-final-one-index-accessor-dfun
929 gf type all-index table))
931 (make-final-n-n-accessor-dfun gf type table))
933 (make-final-caching-dfun gf classes-list new-class)))
934 (make-final-caching-dfun gf classes-list new-class)))))
936 (defun make-final-dfun-internal (gf &optional classes-list)
937 (let ((methods (generic-function-methods gf)) type
938 (new-class *new-class*) (*new-class* nil)
940 (cond ((null methods)
942 #'(lambda (&rest args)
943 (apply #'no-applicable-method gf args))
945 (no-methods-dfun-info)))
946 ((setq type (final-accessor-dfun-type gf))
947 (make-final-accessor-dfun gf type classes-list new-class))
948 ((and (not (and (every (lambda (specl) (eq specl *the-class-t*))
950 (method-specializers (car methods))))
952 (every (lambda (method)
957 (use-constant-value-dfun-p gf))
958 (make-final-constant-value-dfun gf classes-list new-class))
959 ((use-dispatch-dfun-p gf)
960 (make-final-dispatch-dfun gf))
961 ((and all-same-p (not (use-caching-dfun-p gf)))
962 (let ((emf (get-secondary-dispatch-function gf methods nil)))
963 (make-final-checking-dfun gf emf classes-list new-class)))
965 (make-final-caching-dfun gf classes-list new-class)))))
967 (defvar *accessor-miss-history* nil)
969 (defun accessor-miss (gf new object dfun-info)
970 (let ((wrapper (wrapper-of object))
971 (previous-miss (assq gf *accessor-miss-history*)))
972 (when (eq wrapper (cdr previous-miss))
973 (error "~@<Vicious metacircle: The computation of a ~
974 dfun of ~s for argument ~s uses the dfun being ~
977 (let* ((*accessor-miss-history* (acons gf wrapper *accessor-miss-history*))
978 (ostate (type-of dfun-info))
979 (otype (dfun-info-accessor-type dfun-info))
982 ((reader boundp) (list object))
983 (writer (list new object)))))
984 (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
985 ;; The following lexical functions change the state of the
986 ;; dfun to that which is their name. They accept arguments
987 ;; which are the parameters of the new state, and get other
988 ;; information from the lexical variables bound above.
989 (flet ((two-class (index w0 w1)
990 (when (zerop (random 2)) (psetf w0 w1 w1 w0))
992 #'make-two-class-accessor-dfun
997 (one-index (index &optional cache)
999 #'make-one-index-accessor-dfun
1003 (n-n (&optional cache)
1005 (dfun-update gf #'make-checking-dfun nemf)
1006 (dfun-update gf #'make-n-n-accessor-dfun ntype cache)))
1007 (caching () ; because cached accessor emfs are much faster
1009 (dfun-update gf #'make-caching-dfun))
1010 (do-fill (update-fn)
1011 (let ((ncache (fill-cache cache wrappers nindex)))
1012 (unless (eq ncache cache)
1013 (funcall update-fn ncache)))))
1019 ((not (pcl-instance-p object))
1021 ((or (neq ntype otype) (listp wrappers))
1026 (setq oindex (dfun-info-index dfun-info))
1027 (setq ow0 (dfun-info-wrapper0 dfun-info))
1028 (unless (eq ow0 wrappers)
1029 (if (eql nindex oindex)
1030 (two-class nindex ow0 wrappers)
1033 (setq oindex (dfun-info-index dfun-info))
1034 (setq ow0 (dfun-info-wrapper0 dfun-info))
1035 (setq ow1 (dfun-info-wrapper1 dfun-info))
1036 (unless (or (eq ow0 wrappers) (eq ow1 wrappers))
1037 (if (eql nindex oindex)
1041 (setq oindex (dfun-info-index dfun-info))
1042 (setq cache (dfun-info-cache dfun-info))
1043 (if (eql nindex oindex)
1044 (do-fill (lambda (ncache)
1045 (one-index nindex ncache)))
1048 (setq cache (dfun-info-cache dfun-info))
1051 (do-fill #'n-n)))))))))))
1053 (defun checking-miss (generic-function args dfun-info)
1054 (let ((oemf (dfun-info-function dfun-info))
1055 (cache (dfun-info-cache dfun-info)))
1056 (dfun-miss (generic-function args wrappers invalidp nemf)
1059 (let ((ncache (fill-cache cache wrappers nil)))
1060 (unless (eq ncache cache)
1061 (dfun-update generic-function #'make-checking-dfun
1064 (dfun-update generic-function #'make-caching-dfun))))))
1066 (defun caching-miss (generic-function args dfun-info)
1067 (let ((ocache (dfun-info-cache dfun-info)))
1068 (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
1071 (let ((ncache (fill-cache ocache wrappers emf)))
1072 (unless (eq ncache ocache)
1073 (dfun-update generic-function
1074 #'make-caching-dfun ncache))))))))
1076 (defun constant-value-miss (generic-function args dfun-info)
1077 (let ((ocache (dfun-info-cache dfun-info)))
1078 (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
1082 (fast-method-call (fast-method-call-function emf))
1083 (method-call (method-call-function emf))))
1084 (value (let ((val (method-function-get
1085 function :constant-value '.not-found.)))
1086 (aver (not (eq val '.not-found.)))
1088 (ncache (fill-cache ocache wrappers value)))
1089 (unless (eq ncache ocache)
1090 (dfun-update generic-function
1091 #'make-constant-value-dfun ncache)))))))
1093 ;;; Given a generic function and a set of arguments to that generic
1094 ;;; function, return a mess of values.
1096 ;;; <function> The compiled effective method function for this set of
1099 ;;; <applicable> Sorted list of applicable methods.
1101 ;;; <wrappers> Is a single wrapper if the generic function has only
1102 ;;; one key, that is arg-info-nkeys of the arg-info is 1.
1103 ;;; Otherwise a list of the wrappers of the specialized
1104 ;;; arguments to the generic function.
1106 ;;; Note that all these wrappers are valid. This function
1107 ;;; does invalid wrapper traps when it finds an invalid
1108 ;;; wrapper and then returns the new, valid wrapper.
1110 ;;; <invalidp> True if any of the specialized arguments had an invalid
1111 ;;; wrapper, false otherwise.
1113 ;;; <type> READER or WRITER when the only method that would be run
1114 ;;; is a standard reader or writer method. To be specific,
1115 ;;; the value is READER when the method combination is eq to
1116 ;;; *standard-method-combination*; there are no applicable
1117 ;;; :before, :after or :around methods; and the most specific
1118 ;;; primary method is a standard reader method.
1120 ;;; <index> If <type> is READER or WRITER, and the slot accessed is
1121 ;;; an :instance slot, this is the index number of that slot
1122 ;;; in the object argument.
1123 (defvar *cache-miss-values-stack* ())
1125 (defun cache-miss-values (gf args state)
1126 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
1127 (get-generic-fun-info gf)
1128 (declare (ignore nreq applyp nkeys))
1129 (with-dfun-wrappers (args metatypes)
1130 (dfun-wrappers invalid-wrapper-p wrappers classes types)
1131 (error-need-at-least-n-args gf (length metatypes))
1132 (multiple-value-bind (emf methods accessor-type index)
1133 (cache-miss-values-internal
1134 gf arg-info wrappers classes types state)
1138 accessor-type index)))))
1140 (defun cache-miss-values-internal (gf arg-info wrappers classes types state)
1141 (if (and classes (equal classes (cdr (assq gf *cache-miss-values-stack*))))
1142 (break-vicious-metacircle gf classes arg-info)
1143 (let ((*cache-miss-values-stack*
1144 (acons gf classes *cache-miss-values-stack*))
1145 (cam-std-p (or (null arg-info)
1146 (gf-info-c-a-m-emf-std-p arg-info))))
1147 (multiple-value-bind (methods all-applicable-and-sorted-p)
1149 (compute-applicable-methods-using-types gf types)
1150 (compute-applicable-methods-using-classes gf classes))
1152 (let* ((for-accessor-p (eq state 'accessor))
1153 (for-cache-p (or (eq state 'caching) (eq state 'accessor)))
1154 (emf (if (or cam-std-p all-applicable-and-sorted-p)
1156 (get-secondary-dispatch-function1
1157 gf methods types nil (and for-cache-p wrappers)
1158 all-applicable-and-sorted-p)))
1159 (make-callable gf methods generator
1160 nil (and for-cache-p wrappers)))
1161 (default-secondary-dispatch-function gf))))
1162 (multiple-value-bind (index accessor-type)
1163 (and for-accessor-p all-applicable-and-sorted-p methods
1164 (accessor-values gf arg-info classes methods))
1165 (values (if (integerp index) index emf)
1166 methods accessor-type index)))))))
1168 ;;; Try to break a vicious circle while computing a cache miss.
1169 ;;; GF is the generic function, CLASSES are the classes of actual
1170 ;;; arguments, and ARG-INFO is the generic functions' arg-info.
1172 ;;; A vicious circle can be entered when the computation of the cache
1173 ;;; miss values itself depends on the values being computed. For
1174 ;;; instance, adding a method which is an instance of a subclass of
1175 ;;; STANDARD-METHOD leads to cache misses for slot accessors of
1176 ;;; STANDARD-METHOD like METHOD-SPECIALIZERS, and METHOD-SPECIALIZERS
1177 ;;; is itself used while we compute cache miss values.
1178 (defun break-vicious-metacircle (gf classes arg-info)
1179 (when (typep gf 'standard-generic-function)
1180 (multiple-value-bind (class slotd accessor-type)
1181 (accesses-standard-class-slot-p gf)
1183 (let ((method (find-standard-class-accessor-method
1184 gf class accessor-type))
1185 (index (standard-slot-value/eslotd slotd 'location))
1186 (type (gf-info-simple-accessor-type arg-info)))
1188 (subtypep (ecase accessor-type
1189 ((reader) (car classes))
1190 ((writer) (cadr classes)))
1192 (return-from break-vicious-metacircle
1193 (values index (list method) type index)))))))
1194 (error "~@<vicious metacircle: The computation of an ~
1195 effective method of ~s for arguments of types ~s uses ~
1196 the effective method being computed.~@:>"
1199 ;;; Return (CLASS SLOTD ACCESSOR-TYPE) if some method of generic
1200 ;;; function GF accesses a slot of some class in *STANDARD-CLASSES*.
1201 ;;; CLASS is the class accessed, SLOTD is the effective slot definition
1202 ;;; object of the slot accessed, and ACCESSOR-TYPE is one of the symbols
1203 ;;; READER or WRITER describing the slot access.
1204 (defun accesses-standard-class-slot-p (gf)
1205 (flet ((standard-class-slot-access (gf class)
1206 (loop with gf-name = (standard-slot-value/gf gf 'name)
1207 for slotd in (standard-slot-value/class class 'slots)
1208 ;; FIXME: where does BOUNDP fit in here? Is it
1210 as readers = (standard-slot-value/eslotd slotd 'readers)
1211 as writers = (standard-slot-value/eslotd slotd 'writers)
1212 if (member gf-name readers :test #'equal)
1213 return (values slotd 'reader)
1214 else if (member gf-name writers :test #'equal)
1215 return (values slotd 'writer))))
1216 (dolist (class-name *standard-classes*)
1217 (let ((class (find-class class-name)))
1218 (multiple-value-bind (slotd accessor-type)
1219 (standard-class-slot-access gf class)
1221 (return (values class slotd accessor-type))))))))
1223 ;;; Find a slot reader/writer method among the methods of generic
1224 ;;; function GF which reads/writes instances of class CLASS.
1225 ;;; TYPE is one of the symbols READER or WRITER.
1226 (defun find-standard-class-accessor-method (gf class type)
1227 (let ((cpl (standard-slot-value/class class '%class-precedence-list))
1228 (found-specializer *the-class-t*)
1230 (dolist (method (standard-slot-value/gf gf 'methods) found-method)
1231 (let ((specializers (standard-slot-value/method method 'specializers))
1232 (qualifiers (plist-value method 'qualifiers)))
1233 (when (and (null qualifiers)
1234 (let ((subcpl (member (ecase type
1235 (reader (car specializers))
1236 (writer (cadr specializers)))
1238 (and subcpl (member found-specializer subcpl))))
1239 (setf found-specializer (ecase type
1240 (reader (car specializers))
1241 (writer (cadr specializers))))
1242 (setf found-method method))))))
1244 (defun accessor-values (gf arg-info classes methods)
1245 (declare (ignore gf))
1246 (let* ((accessor-type (gf-info-simple-accessor-type arg-info))
1247 (accessor-class (case accessor-type
1248 ((reader boundp) (car classes))
1249 (writer (cadr classes)))))
1250 (accessor-values-internal accessor-type accessor-class methods)))
1252 (defun accessor-values1 (gf accessor-type accessor-class)
1253 (let* ((type `(class-eq ,accessor-class))
1254 (types (ecase accessor-type
1255 ((reader boundp) `(,type))
1256 (writer `(t ,type))))
1257 (methods (compute-applicable-methods-using-types gf types)))
1258 (accessor-values-internal accessor-type accessor-class methods)))
1260 (defun accessor-values-internal (accessor-type accessor-class methods)
1261 (dolist (meth methods)
1262 (when (if (consp meth)
1263 (early-method-qualifiers meth)
1264 (method-qualifiers meth))
1265 (return-from accessor-values-internal (values nil nil))))
1266 (let* ((meth (car methods))
1267 (early-p (not (eq *boot-state* 'complete)))
1268 (slot-name (when accessor-class
1270 (and (early-method-standard-accessor-p meth)
1271 (early-method-standard-accessor-slot-name meth))
1272 (and (member *the-class-standard-object*
1274 (early-class-precedence-list
1276 (class-precedence-list
1279 (not (eq *the-class-standard-method*
1280 (early-method-class meth)))
1281 (standard-accessor-method-p meth))
1283 (early-accessor-method-slot-name meth)
1284 (accessor-method-slot-name meth))))))
1285 (slotd (and accessor-class
1287 (dolist (slot (early-class-slotds accessor-class) nil)
1288 (when (eql slot-name
1289 (early-slot-definition-name slot))
1291 (find-slot-definition accessor-class slot-name)))))
1294 (slot-accessor-std-p slotd accessor-type)))
1296 (early-slot-definition-location slotd)
1297 (slot-definition-location slotd))
1300 (defun make-accessor-table (gf type &optional table)
1301 (unless table (setq table (make-hash-table :test 'eq)))
1302 (let ((methods (if (early-gf-p gf)
1303 (early-gf-methods gf)
1304 (generic-function-methods gf)))
1306 (no-class-slots-p t)
1307 (early-p (not (eq *boot-state* 'complete)))
1308 first second (size 0))
1309 (declare (fixnum size))
1310 ;; class -> {(specl slotd)}
1311 (dolist (method methods)
1312 (let* ((specializers (if (consp method)
1313 (early-method-specializers method t)
1314 (method-specializers method)))
1316 ((reader boundp) (car specializers))
1317 (writer (cadr specializers))))
1318 (specl-cpl (if early-p
1319 (early-class-precedence-list specl)
1320 (and (class-finalized-p specl)
1321 (class-precedence-list specl))))
1322 (so-p (member *the-class-standard-object* specl-cpl))
1323 (slot-name (if (consp method)
1324 (and (early-method-standard-accessor-p method)
1325 (early-method-standard-accessor-slot-name
1327 (accessor-method-slot-name method))))
1328 (when (or (null specl-cpl)
1329 (member *the-class-structure-object* specl-cpl))
1330 (return-from make-accessor-table nil))
1331 (maphash (lambda (class slotd)
1332 (let ((cpl (if early-p
1333 (early-class-precedence-list class)
1334 (class-precedence-list class))))
1335 (when (memq specl cpl)
1336 (unless (and (or so-p
1337 (member *the-class-standard-object*
1340 (slot-accessor-std-p slotd type)))
1341 (return-from make-accessor-table nil))
1342 (push (cons specl slotd) (gethash class table)))))
1343 (gethash slot-name *name->class->slotd-table*))))
1344 (maphash (lambda (class specl+slotd-list)
1345 (dolist (sclass (if early-p
1346 (early-class-precedence-list class)
1347 (class-precedence-list class))
1348 (error "This can't happen."))
1349 (let ((a (assq sclass specl+slotd-list)))
1351 (let* ((slotd (cdr a))
1353 (early-slot-definition-location slotd)
1354 (slot-definition-location slotd))))
1355 (unless index (return-from make-accessor-table nil))
1356 (setf (gethash class table) index)
1357 (when (consp index) (setq no-class-slots-p nil))
1358 (setq all-index (if (or (null all-index)
1359 (eql all-index index))
1362 (cond ((= size 1) (setq first class))
1363 ((= size 2) (setq second class)))
1366 (values table all-index first second size no-class-slots-p)))
1368 (defun compute-applicable-methods-using-types (generic-function types)
1369 (let ((definite-p t) (possibly-applicable-methods nil))
1370 (dolist (method (if (early-gf-p generic-function)
1371 (early-gf-methods generic-function)
1372 (safe-generic-function-methods generic-function)))
1373 (let ((specls (if (consp method)
1374 (early-method-specializers method t)
1375 (safe-method-specializers method)))
1377 (possibly-applicable-p t) (applicable-p t))
1378 (dolist (specl specls)
1379 (multiple-value-bind (specl-applicable-p specl-possibly-applicable-p)
1380 (specializer-applicable-using-type-p specl (pop types))
1381 (unless specl-applicable-p
1382 (setq applicable-p nil))
1383 (unless specl-possibly-applicable-p
1384 (setq possibly-applicable-p nil)
1386 (when possibly-applicable-p
1387 (unless applicable-p (setq definite-p nil))
1388 (push method possibly-applicable-methods))))
1389 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
1390 (get-generic-fun-info generic-function)
1391 (declare (ignore nreq applyp metatypes nkeys))
1392 (let* ((precedence (arg-info-precedence arg-info)))
1393 (values (sort-applicable-methods precedence
1394 (nreverse possibly-applicable-methods)
1398 (defun sort-applicable-methods (precedence methods types)
1399 (sort-methods methods
1401 (lambda (class1 class2 index)
1402 (let* ((class (type-class (nth index types)))
1403 (cpl (if (eq *boot-state* 'complete)
1404 (class-precedence-list class)
1405 (early-class-precedence-list class))))
1406 (if (memq class2 (memq class1 cpl))
1409 (defun sort-methods (methods precedence compare-classes-function)
1410 (flet ((sorter (method1 method2)
1411 (dolist (index precedence)
1412 (let* ((specl1 (nth index (if (listp method1)
1413 (early-method-specializers method1
1415 (method-specializers method1))))
1416 (specl2 (nth index (if (listp method2)
1417 (early-method-specializers method2
1419 (method-specializers method2))))
1420 (order (order-specializers
1421 specl1 specl2 index compare-classes-function)))
1423 (return-from sorter (eq order specl1)))))))
1424 (stable-sort methods #'sorter)))
1426 (defun order-specializers (specl1 specl2 index compare-classes-function)
1427 (let ((type1 (if (eq *boot-state* 'complete)
1428 (specializer-type specl1)
1429 (!bootstrap-get-slot 'specializer specl1 '%type)))
1430 (type2 (if (eq *boot-state* 'complete)
1431 (specializer-type specl2)
1432 (!bootstrap-get-slot 'specializer specl2 '%type))))
1433 (cond ((eq specl1 specl2)
1441 (class (case (car type2)
1442 (class (funcall compare-classes-function
1443 specl1 specl2 index))
1445 (prototype (case (car type2)
1446 (class (funcall compare-classes-function
1447 specl1 specl2 index))
1449 (class-eq (case (car type2)
1453 (eql (case (car type2)
1457 (defun map-all-orders (methods precedence function)
1458 (let ((choices nil))
1459 (flet ((compare-classes-function (class1 class2 index)
1460 (declare (ignore index))
1462 (dolist (c choices nil)
1463 (when (or (and (eq (first c) class1)
1464 (eq (second c) class2))
1465 (and (eq (first c) class2)
1466 (eq (second c) class1)))
1467 (return (setq choice c))))
1470 (if (class-might-precede-p class1 class2)
1471 (if (class-might-precede-p class2 class1)
1472 (list class1 class2 nil t)
1473 (list class1 class2 t))
1474 (if (class-might-precede-p class2 class1)
1475 (list class2 class1 t)
1476 (let ((name1 (class-name class1))
1477 (name2 (class-name class2)))
1482 (string< (symbol-name name1)
1483 (symbol-name name2)))
1484 (list class1 class2 t)
1485 (list class2 class1 t))))))
1486 (push choice choices))
1488 (loop (funcall function
1489 (sort-methods methods
1491 #'compare-classes-function))
1492 (unless (dolist (c choices nil)
1494 (rotatef (car c) (cadr c))
1495 (return (setf (third c) t))))
1498 ;;; CMUCL comment: used only in map-all-orders
1499 (defun class-might-precede-p (class1 class2)
1500 (if (not *in-precompute-effective-methods-p*)
1501 (not (member class1 (cdr (class-precedence-list class2))))
1502 (class-can-precede-p class1 class2)))
1504 (defun compute-precedence (lambda-list nreq argument-precedence-order)
1505 (if (null argument-precedence-order)
1507 (dotimes-fixnum (i nreq list) (push (- (1- nreq) i) list)))
1508 (mapcar (lambda (x) (position x lambda-list))
1509 argument-precedence-order)))
1511 (defun cpl-or-nil (class)
1512 (if (eq *boot-state* 'complete)
1513 ;; KLUDGE: why not use (slot-boundp class
1514 ;; 'class-precedence-list)? Well, unfortunately, CPL-OR-NIL is
1515 ;; used within COMPUTE-APPLICABLE-METHODS, including for
1516 ;; SLOT-BOUNDP-USING-CLASS... and the available mechanism for
1517 ;; breaking such nasty cycles in effective method computation
1518 ;; only works for readers and writers, not boundps. It might
1519 ;; not be too hard to make it work for BOUNDP accessors, but in
1520 ;; the meantime we use an extra slot for exactly the result of
1521 ;; the SLOT-BOUNDP that we want. (We cannot use
1522 ;; CLASS-FINALIZED-P, because in the process of class
1523 ;; finalization we need to use the CPL which has been computed
1524 ;; to cache effective methods for slot accessors.) -- CSR,
1526 (when (cpl-available-p class)
1527 (class-precedence-list class))
1528 (early-class-precedence-list class)))
1530 (defun saut-and (specl type)
1531 (let ((applicable nil)
1532 (possibly-applicable t))
1533 (dolist (type (cdr type))
1534 (multiple-value-bind (appl poss-appl)
1535 (specializer-applicable-using-type-p specl type)
1536 (when appl (return (setq applicable t)))
1537 (unless poss-appl (return (setq possibly-applicable nil)))))
1538 (values applicable possibly-applicable)))
1540 (defun saut-not (specl type)
1541 (let ((ntype (cadr type)))
1544 (class (saut-not-class specl ntype))
1545 (class-eq (saut-not-class-eq specl ntype))
1546 (prototype (saut-not-prototype specl ntype))
1547 (eql (saut-not-eql specl ntype))
1548 (t (error "~S cannot handle the second argument ~S"
1549 'specializer-applicable-using-type-p type))))))
1551 (defun saut-not-class (specl ntype)
1552 (let* ((class (type-class specl))
1553 (cpl (cpl-or-nil class)))
1554 (not (memq (cadr ntype) cpl))))
1556 (defun saut-not-prototype (specl ntype)
1557 (let* ((class (case (car specl)
1558 (eql (class-of (cadr specl)))
1559 (class-eq (cadr specl))
1560 (prototype (cadr specl))
1561 (class (cadr specl))))
1562 (cpl (cpl-or-nil class)))
1563 (not (memq (cadr ntype) cpl))))
1565 (defun saut-not-class-eq (specl ntype)
1566 (let ((class (case (car specl)
1567 (eql (class-of (cadr specl)))
1568 (class-eq (cadr specl)))))
1569 (not (eq class (cadr ntype)))))
1571 (defun saut-not-eql (specl ntype)
1573 (eql (not (eql (cadr specl) (cadr ntype))))
1576 (defun class-applicable-using-class-p (specl type)
1577 (let ((pred (memq specl (cpl-or-nil type))))
1580 (if (not *in-precompute-effective-methods-p*)
1581 ;; classes might get common subclass
1582 (superclasses-compatible-p specl type)
1583 ;; worry only about existing classes
1584 (classes-have-common-subclass-p specl type))))))
1586 (defun classes-have-common-subclass-p (class1 class2)
1587 (or (eq class1 class2)
1588 (let ((class1-subs (class-direct-subclasses class1)))
1589 (or (memq class2 class1-subs)
1590 (dolist (class1-sub class1-subs nil)
1591 (when (classes-have-common-subclass-p class1-sub class2)
1594 (defun saut-class (specl type)
1596 (class (class-applicable-using-class-p (cadr specl) (cadr type)))
1597 (t (values nil (let ((class (type-class specl)))
1599 (cpl-or-nil class)))))))
1601 (defun saut-class-eq (specl type)
1602 (if (eq (car specl) 'eql)
1603 (values nil (eq (class-of (cadr specl)) (cadr type)))
1604 (let ((pred (case (car specl)
1606 (eq (cadr specl) (cadr type)))
1608 (or (eq (cadr specl) (cadr type))
1609 (memq (cadr specl) (cpl-or-nil (cadr type))))))))
1610 (values pred pred))))
1612 (defun saut-prototype (specl type)
1613 (declare (ignore specl type))
1614 (values nil nil)) ; XXX original PCL comment: fix this someday
1616 (defun saut-eql (specl type)
1617 (let ((pred (case (car specl)
1618 (eql (eql (cadr specl) (cadr type)))
1619 (class-eq (eq (cadr specl) (class-of (cadr type))))
1620 (class (memq (cadr specl)
1621 (let ((class (class-of (cadr type))))
1622 (cpl-or-nil class)))))))
1623 (values pred pred)))
1625 (defun specializer-applicable-using-type-p (specl type)
1626 (setq specl (type-from-specializer specl))
1628 (return-from specializer-applicable-using-type-p (values t t)))
1629 ;; This is used by C-A-M-U-T and GENERATE-DISCRIMINATION-NET-INTERNAL,
1630 ;; and has only what they need.
1631 (if (or (atom type) (eq (car type) t))
1634 (and (saut-and specl type))
1635 (not (saut-not specl type))
1636 (class (saut-class specl type))
1637 (prototype (saut-prototype specl type))
1638 (class-eq (saut-class-eq specl type))
1639 (eql (saut-eql specl type))
1640 (t (error "~S cannot handle the second argument ~S."
1641 'specializer-applicable-using-type-p
1644 (defun map-all-classes (fun &optional (root t))
1645 (let ((all-classes (make-hash-table :test 'eq))
1646 (braid-p (or (eq *boot-state* 'braid)
1647 (eq *boot-state* 'complete))))
1648 (labels ((do-class (class)
1649 (unless (gethash class all-classes)
1650 (setf (gethash class all-classes) t)
1654 (class-direct-subclasses class)
1655 (early-class-direct-subclasses class))))))
1656 (do-class (if (symbolp root)
1661 (defvar *effective-method-cache* (make-hash-table :test 'eq))
1663 (defun flush-effective-method-cache (generic-function)
1664 (dolist (method (generic-function-methods generic-function))
1665 (remhash method *effective-method-cache*)))
1667 (defun get-secondary-dispatch-function (gf methods types
1668 &optional method-alist wrappers)
1670 (get-secondary-dispatch-function1
1671 gf methods types (not (null method-alist)) (not (null wrappers))
1672 (not (methods-contain-eql-specializer-p methods)))))
1673 (make-callable gf methods generator method-alist wrappers)))
1675 (defun get-secondary-dispatch-function1 (gf methods types method-alist-p
1683 (lambda (method-alist wrappers)
1684 (declare (ignore method-alist wrappers))
1685 #'(lambda (&rest args)
1686 (apply #'no-applicable-method gf args)))
1687 (lambda (method-alist wrappers)
1688 (declare (ignore method-alist wrappers))
1689 (lambda (&rest args)
1690 (apply #'no-applicable-method gf args))))
1691 (let* ((key (car methods))
1692 (ht-value (or (gethash key *effective-method-cache*)
1693 (setf (gethash key *effective-method-cache*)
1695 (if (and (null (cdr methods)) all-applicable-p ; the most common case
1696 (null method-alist-p) wrappers-p (not function-p))
1698 (setf (car ht-value)
1699 (get-secondary-dispatch-function2
1700 gf methods types method-alist-p wrappers-p
1701 all-applicable-p all-sorted-p function-p)))
1702 (let ((akey (list methods
1703 (if all-applicable-p 'all-applicable types)
1704 method-alist-p wrappers-p function-p)))
1705 (or (cdr (assoc akey (cdr ht-value) :test #'equal))
1706 (let ((value (get-secondary-dispatch-function2
1707 gf methods types method-alist-p wrappers-p
1708 all-applicable-p all-sorted-p function-p)))
1709 (push (cons akey value) (cdr ht-value))
1712 (defun get-secondary-dispatch-function2 (gf methods types method-alist-p
1713 wrappers-p all-applicable-p
1714 all-sorted-p function-p)
1715 (if (and all-applicable-p all-sorted-p (not function-p))
1716 (if (eq *boot-state* 'complete)
1717 (let* ((combin (generic-function-method-combination gf))
1718 (effective (compute-effective-method gf combin methods)))
1719 (make-effective-method-function1 gf effective method-alist-p
1721 (let ((effective (standard-compute-effective-method gf nil methods)))
1722 (make-effective-method-function1 gf effective method-alist-p
1724 (let ((net (generate-discrimination-net
1725 gf methods types all-sorted-p)))
1726 (compute-secondary-dispatch-function1 gf net function-p))))
1728 (defun get-effective-method-function (gf methods
1729 &optional method-alist wrappers)
1731 (get-secondary-dispatch-function1
1732 gf methods nil (not (null method-alist)) (not (null wrappers)) t)))
1733 (make-callable gf methods generator method-alist wrappers)))
1735 (defun get-effective-method-function1 (gf methods &optional (sorted-p t))
1736 (get-secondary-dispatch-function1 gf methods nil nil nil t sorted-p))
1738 (defun methods-contain-eql-specializer-p (methods)
1739 (and (eq *boot-state* 'complete)
1740 (dolist (method methods nil)
1741 (when (dolist (spec (method-specializers method) nil)
1742 (when (eql-specializer-p spec) (return t)))
1745 (defun update-dfun (generic-function &optional dfun cache info)
1746 (let* ((early-p (early-gf-p generic-function)))
1747 (set-dfun generic-function dfun cache info)
1748 (let ((dfun (if early-p
1749 (or dfun (make-initial-dfun generic-function))
1750 (compute-discriminating-function generic-function))))
1751 (set-funcallable-instance-function generic-function dfun)
1752 (let ((gf-name (if early-p
1753 (!early-gf-name generic-function)
1754 (generic-function-name generic-function))))
1755 (set-fun-name generic-function gf-name)
1758 (defvar *dfun-count* nil)
1759 (defvar *dfun-list* nil)
1760 (defvar *minimum-cache-size-to-list*)
1762 ;;; These functions aren't used in SBCL, or documented anywhere that
1763 ;;; I'm aware of, but they look like they might be useful for
1764 ;;; debugging or performance tweaking or something, so I've just
1765 ;;; commented them out instead of deleting them. -- WHN 2001-03-28
1767 (defun list-dfun (gf)
1768 (let* ((sym (type-of (gf-dfun-info gf)))
1769 (a (assq sym *dfun-list*)))
1771 (push (setq a (list sym)) *dfun-list*))
1772 (push (generic-function-name gf) (cdr a))))
1774 (defun list-all-dfuns ()
1775 (setq *dfun-list* nil)
1776 (map-all-generic-functions #'list-dfun)
1779 (defun list-large-cache (gf)
1780 (let* ((sym (type-of (gf-dfun-info gf)))
1781 (cache (gf-dfun-cache gf)))
1783 (let ((size (cache-size cache)))
1784 (when (>= size *minimum-cache-size-to-list*)
1785 (let ((a (assoc size *dfun-list*)))
1787 (push (setq a (list size)) *dfun-list*))
1788 (push (let ((name (generic-function-name gf)))
1789 (if (eq sym 'caching) name (list name sym)))
1792 (defun list-large-caches (&optional (*minimum-cache-size-to-list* 130))
1793 (setq *dfun-list* nil)
1794 (map-all-generic-functions #'list-large-cache)
1795 (setq *dfun-list* (sort *dfun-list* #'< :key #'car))
1796 (mapc #'print *dfun-list*)
1799 (defun count-dfun (gf)
1800 (let* ((sym (type-of (gf-dfun-info gf)))
1801 (cache (gf-dfun-cache gf))
1802 (a (assq sym *dfun-count*)))
1804 (push (setq a (list sym 0 nil)) *dfun-count*))
1807 (let* ((size (cache-size cache))
1808 (b (assoc size (third a))))
1810 (push (setq b (cons size 0)) (third a)))
1813 (defun count-all-dfuns ()
1814 (setq *dfun-count* (mapcar (lambda (type) (list type 0 nil))
1815 '(ONE-CLASS TWO-CLASS DEFAULT-METHOD-ONLY
1816 ONE-INDEX N-N CHECKING CACHING
1818 (map-all-generic-functions #'count-dfun)
1819 (mapc (lambda (type+count+sizes)
1820 (setf (third type+count+sizes)
1821 (sort (third type+count+sizes) #'< :key #'car)))
1823 (mapc (lambda (type+count+sizes)
1824 (format t "~&There are ~W dfuns of type ~S."
1825 (cadr type+count+sizes) (car type+count+sizes))
1826 (format t "~% ~S~%" (caddr type+count+sizes)))
1831 (defun gfs-of-type (type)
1832 (unless (consp type) (setq type (list type)))
1833 (let ((gf-list nil))
1834 (map-all-generic-functions (lambda (gf)
1835 (when (memq (type-of (gf-dfun-info gf))
1837 (push gf gf-list))))