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