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