0.9.3.34: cosmetics
[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 (or caching-p
619                 (gf-requires-emf-keyword-checks gf))
620       ;; This should return T when almost all dispatching is by
621       ;; eql specializers or built-in classes. In other words,
622       ;; return NIL if we might ever need to do more than
623       ;; one (non built-in) typep.
624       ;; Otherwise, it is probably at least as fast to use
625       ;; a caching dfun first, possibly followed by secondary dispatching.
626
627       #||;;; Original found in cmu 17f -- S L O W
628       (< (dispatch-dfun-cost gf) (caching-dfun-cost gf))
629       ||#
630       ;; This uses improved dispatch-dfun-cost below
631       (let ((cdc  (caching-dfun-cost gf))) ; fast
632         (> cdc (dispatch-dfun-cost gf cdc))))))
633
634 (defparameter *non-built-in-typep-cost* 1)
635 (defparameter *structure-typep-cost* 1)
636 (defparameter *built-in-typep-cost* 0)
637
638 ;;; According to comments in the original CMU CL version of PCL,
639 ;;; the cost LIMIT is important to cut off exponential growth for
640 ;;; large numbers of gf methods and argument lists.
641 (defun dispatch-dfun-cost (gf &optional limit)
642   (generate-discrimination-net-internal
643    gf (generic-function-methods gf) nil
644    (lambda (methods known-types)
645      (declare (ignore methods known-types))
646      0)
647    (lambda (position type true-value false-value)
648      (declare (ignore position))
649      (let* ((type-test-cost
650              (if (eq 'class (car type))
651                  (let* ((metaclass (class-of (cadr type)))
652                         (mcpl (class-precedence-list metaclass)))
653                    (cond ((memq *the-class-built-in-class* mcpl)
654                           *built-in-typep-cost*)
655                          ((memq *the-class-structure-class* mcpl)
656                           *structure-typep-cost*)
657                          (t
658                           *non-built-in-typep-cost*)))
659                  0))
660             (max-cost-so-far
661              (+ (max true-value false-value) type-test-cost)))
662        (when (and limit (<= limit max-cost-so-far))
663          (return-from dispatch-dfun-cost max-cost-so-far))
664        max-cost-so-far))
665    #'identity))
666
667 (defparameter *cache-lookup-cost* 1)
668 (defparameter *wrapper-of-cost* 0)
669 (defparameter *secondary-dfun-call-cost* 1)
670
671 (defun caching-dfun-cost (gf)
672   (let* ((arg-info (gf-arg-info gf))
673          (nreq (length (arg-info-metatypes arg-info))))
674     (+ *cache-lookup-cost*
675        (* *wrapper-of-cost* nreq)
676        (if (methods-contain-eql-specializer-p
677             (generic-function-methods gf))
678            *secondary-dfun-call-cost*
679            0))))
680
681 (setq *non-built-in-typep-cost* 100)
682 (setq *structure-typep-cost* 15)
683 (setq *built-in-typep-cost* 5)
684 (setq *cache-lookup-cost* 30)
685 (setq *wrapper-of-cost* 15)
686 (setq *secondary-dfun-call-cost* 30)
687
688 (declaim (inline make-callable))
689 (defun make-callable (gf methods generator method-alist wrappers)
690   (let* ((*applicable-methods* methods)
691          (callable (function-funcall generator method-alist wrappers)))
692     callable))
693
694 (defun make-dispatch-dfun (gf)
695   (values (get-dispatch-function gf) nil (dispatch-dfun-info)))
696
697 (defun get-dispatch-function (gf)
698   (let* ((methods (generic-function-methods gf))
699          (generator (get-secondary-dispatch-function1
700                      gf methods nil nil nil nil nil t)))
701     (make-callable gf methods generator nil nil)))
702
703 (defun make-final-dispatch-dfun (gf)
704   (make-dispatch-dfun gf))
705
706 (defun update-dispatch-dfuns ()
707   (dolist (gf (gfs-of-type '(dispatch initial-dispatch)))
708     (dfun-update gf #'make-dispatch-dfun)))
709
710 (defun fill-dfun-cache (table valuep nkeys limit-fn &optional cache)
711   (let ((cache (or cache (get-cache nkeys valuep limit-fn
712                                     (+ (hash-table-count table) 3)))))
713     (maphash (lambda (classes value)
714                (setq cache (fill-cache cache
715                                        (class-wrapper classes)
716                                        value)))
717              table)
718     cache))
719
720 (defun make-final-ordinary-dfun-internal (generic-function valuep limit-fn
721                                                            classes-list new-class)
722   (let* ((arg-info (gf-arg-info generic-function))
723          (nkeys (arg-info-nkeys arg-info))
724          (new-class (and new-class
725                          (equal (type-of (gf-dfun-info generic-function))
726                                 (cond ((eq valuep t) 'caching)
727                                       ((eq valuep :constant-value) 'constant-value)
728                                       ((null valuep) 'checking)))
729                          new-class))
730          (cache (if new-class
731                     (copy-cache (gf-dfun-cache generic-function))
732                     (get-cache nkeys (not (null valuep)) limit-fn 4))))
733       (make-emf-cache generic-function valuep cache classes-list new-class)))
734 \f
735 (defvar *dfun-miss-gfs-on-stack* ())
736
737 (defmacro dfun-miss ((gf args wrappers invalidp nemf
738                       &optional type index caching-p applicable)
739                      &body body)
740   (unless applicable (setq applicable (gensym)))
741   `(multiple-value-bind (,nemf ,applicable ,wrappers ,invalidp
742                          ,@(when type `(,type ,index)))
743        (cache-miss-values ,gf ,args ',(cond (caching-p 'caching)
744                                             (type 'accessor)
745                                             (t 'checking)))
746     (when (and ,applicable (not (memq ,gf *dfun-miss-gfs-on-stack*)))
747       (let ((*dfun-miss-gfs-on-stack* (cons ,gf *dfun-miss-gfs-on-stack*)))
748         ,@body))
749     ;; Create a FAST-INSTANCE-BOUNDP structure instance for a cached
750     ;; SLOT-BOUNDP so that INVOKE-EMF does the right thing, that is,
751     ;; does not signal a SLOT-UNBOUND error for a boundp test.
752     ,@(if type
753           ;; FIXME: could the NEMF not be a CONS (for :CLASS-allocated
754           ;; slots?)
755           `((if (and (eq ,type 'boundp) (integerp ,nemf))
756                 (invoke-emf (make-fast-instance-boundp :index ,nemf) ,args)
757                 (invoke-emf ,nemf ,args)))
758           `((invoke-emf ,nemf ,args)))))
759
760 ;;; The dynamically adaptive method lookup algorithm is implemented is
761 ;;; implemented as a kind of state machine. The kinds of
762 ;;; discriminating function is the state, the various kinds of reasons
763 ;;; for a cache miss are the state transitions.
764 ;;;
765 ;;; The code which implements the transitions is all in the miss
766 ;;; handlers for each kind of dfun. Those appear here.
767 ;;;
768 ;;; Note that within the states that cache, there are dfun updates
769 ;;; which simply select a new cache or cache field. Those are not
770 ;;; considered as state transitions.
771 (defvar *lazy-dfun-compute-p* t)
772 (defvar *early-p* nil)
773
774 (declaim (type (or null unsigned-byte) *max-emf-precomputation-methods*))
775 (defvar *max-emf-precomputation-methods* nil)
776
777 (defun finalize-specializers (gf)
778   (let ((methods (generic-function-methods gf)))
779     (when (or (null *max-emf-precomputation-methods*)
780               (<= (length methods) *max-emf-precomputation-methods*))
781       (let ((all-finalized t))
782         (dolist (method methods all-finalized)
783           (dolist (specializer (method-specializers method))
784             (when (and (classp specializer)
785                        (not (class-finalized-p specializer)))
786               (if (class-has-a-forward-referenced-superclass-p specializer)
787                   (setq all-finalized nil)
788                   (finalize-inheritance specializer)))))))))
789
790 (defun make-initial-dfun (gf)
791   (let ((initial-dfun
792          #'(lambda (&rest args)
793              (initial-dfun gf args))))
794     (multiple-value-bind (dfun cache info)
795         (cond
796           ((and (eq *boot-state* 'complete)
797                 (not (finalize-specializers gf)))
798            (values initial-dfun nil (initial-dfun-info)))
799           ((and (eq *boot-state* 'complete)
800                 (compute-applicable-methods-emf-std-p gf))
801            (let* ((caching-p (use-caching-dfun-p gf))
802                   ;; KLUDGE: the only effect of this (when
803                   ;; *LAZY-DFUN-COMPUTE-P* is true, as it usually is)
804                   ;; is to signal an error when we try to add methods
805                   ;; with the wrong qualifiers to a generic function.
806                   (classes-list (precompute-effective-methods
807                                  gf caching-p
808                                  (not *lazy-dfun-compute-p*))))
809              (if *lazy-dfun-compute-p*
810                  (cond ((use-dispatch-dfun-p gf caching-p)
811                         (values initial-dfun
812                                 nil
813                                 (initial-dispatch-dfun-info)))
814                        (caching-p
815                         (insure-caching-dfun gf)
816                         (values initial-dfun nil (initial-dfun-info)))
817                        (t
818                         (values initial-dfun nil (initial-dfun-info))))
819                  (make-final-dfun-internal gf classes-list))))
820           (t
821            (let ((arg-info (if (early-gf-p gf)
822                                (early-gf-arg-info gf)
823                                (gf-arg-info gf)))
824                  (type nil))
825              (if (and (gf-precompute-dfun-and-emf-p arg-info)
826                       (setq type (final-accessor-dfun-type gf)))
827                  (if *early-p*
828                      (values (make-early-accessor gf type) nil nil)
829                      (make-final-accessor-dfun gf type))
830                  (values initial-dfun nil (initial-dfun-info))))))
831       (set-dfun gf dfun cache info))))
832
833 (defun make-early-accessor (gf type)
834   (let* ((methods (early-gf-methods gf))
835          (slot-name (early-method-standard-accessor-slot-name (car methods))))
836     (ecase type
837       (reader #'(lambda (instance)
838                   (let* ((class (class-of instance))
839                          (class-name (!bootstrap-get-slot 'class class 'name)))
840                     (!bootstrap-get-slot class-name instance slot-name))))
841       (boundp #'(lambda (instance)
842                   (let* ((class (class-of instance))
843                          (class-name (!bootstrap-get-slot 'class class 'name)))
844                     (not (eq +slot-unbound+
845                              (!bootstrap-get-slot class-name
846                                                   instance slot-name))))))
847       (writer #'(lambda (new-value instance)
848                   (let* ((class (class-of instance))
849                          (class-name (!bootstrap-get-slot 'class class 'name)))
850                     (!bootstrap-set-slot class-name instance slot-name new-value)))))))
851
852 (defun initial-dfun (gf args)
853   (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
854     (cond (invalidp)
855           ((and ntype nindex)
856            (dfun-update
857             gf #'make-one-class-accessor-dfun ntype wrappers nindex))
858           ((use-caching-dfun-p gf)
859            (dfun-update gf #'make-caching-dfun))
860           (t
861            (dfun-update
862             gf #'make-checking-dfun
863             ;; nemf is suitable only for caching, have to do this:
864             (cache-miss-values gf args 'checking))))))
865
866 (defun make-final-dfun (gf &optional classes-list)
867   (multiple-value-bind (dfun cache info)
868       (make-final-dfun-internal gf classes-list)
869     (set-dfun gf dfun cache info)))
870
871 (defvar *new-class* nil)
872
873 (defvar *free-hash-tables* (mapcar #'list '(eq equal eql)))
874
875 (defmacro with-hash-table ((table test) &body forms)
876   `(let* ((.free. (assoc ',test *free-hash-tables*))
877           (,table (if (cdr .free.)
878                       (pop (cdr .free.))
879                       (make-hash-table :test ',test))))
880      (multiple-value-prog1
881          (progn ,@forms)
882        (clrhash ,table)
883        (push ,table (cdr .free.)))))
884
885 (defmacro with-eq-hash-table ((table) &body forms)
886   `(with-hash-table (,table eq) ,@forms))
887
888 (defun final-accessor-dfun-type (gf)
889   (let ((methods (if (early-gf-p gf)
890                      (early-gf-methods gf)
891                      (generic-function-methods gf))))
892     (cond ((every (lambda (method)
893                     (if (consp method)
894                         (eq *the-class-standard-reader-method*
895                             (early-method-class method))
896                         (standard-reader-method-p method)))
897                   methods)
898            'reader)
899           ((every (lambda (method)
900                     (if (consp method)
901                         (eq *the-class-standard-boundp-method*
902                             (early-method-class method))
903                         (standard-boundp-method-p method)))
904                   methods)
905            'boundp)
906           ((every (lambda (method)
907                     (if (consp method)
908                         (eq *the-class-standard-writer-method*
909                             (early-method-class method))
910                         (standard-writer-method-p method)))
911                   methods)
912            'writer))))
913
914 (defun make-final-accessor-dfun (gf type &optional classes-list new-class)
915   (with-eq-hash-table (table)
916     (multiple-value-bind (table all-index first second size no-class-slots-p)
917         (make-accessor-table gf type table)
918       (if table
919           (cond ((= size 1)
920                  (let ((w (class-wrapper first)))
921                    (make-one-class-accessor-dfun gf type w all-index)))
922                 ((and (= size 2) (or (integerp all-index) (consp all-index)))
923                  (let ((w0 (class-wrapper first))
924                        (w1 (class-wrapper second)))
925                    (make-two-class-accessor-dfun gf type w0 w1 all-index)))
926                 ((or (integerp all-index) (consp all-index))
927                  (make-final-one-index-accessor-dfun
928                   gf type all-index table))
929                 (no-class-slots-p
930                  (make-final-n-n-accessor-dfun gf type table))
931                 (t
932                  (make-final-caching-dfun gf classes-list new-class)))
933           (make-final-caching-dfun gf classes-list new-class)))))
934
935 (defun make-final-dfun-internal (gf &optional classes-list)
936   (let ((methods (generic-function-methods gf)) type
937         (new-class *new-class*) (*new-class* nil)
938         specls all-same-p)
939     (cond ((null methods)
940            (values
941             #'(lambda (&rest args)
942                 (apply #'no-applicable-method gf args))
943             nil
944             (no-methods-dfun-info)))
945           ((setq type (final-accessor-dfun-type gf))
946            (make-final-accessor-dfun gf type classes-list new-class))
947           ((and (not (and (every (lambda (specl) (eq specl *the-class-t*))
948                                  (setq specls
949                                        (method-specializers (car methods))))
950                           (setq all-same-p
951                                 (every (lambda (method)
952                                          (and (equal specls
953                                                      (method-specializers
954                                                       method))))
955                                        methods))))
956                 (use-constant-value-dfun-p gf))
957            (make-final-constant-value-dfun gf classes-list new-class))
958           ((use-dispatch-dfun-p gf)
959            (make-final-dispatch-dfun gf))
960           ((and all-same-p (not (use-caching-dfun-p gf)))
961            (let ((emf (get-secondary-dispatch-function gf methods nil)))
962              (make-final-checking-dfun gf emf classes-list new-class)))
963           (t
964            (make-final-caching-dfun gf classes-list new-class)))))
965
966 (defun accessor-miss (gf new object dfun-info)
967   (let* ((ostate (type-of dfun-info))
968          (otype (dfun-info-accessor-type dfun-info))
969          oindex ow0 ow1 cache
970          (args (ecase otype
971                  ;; The congruence rules ensure that this is safe
972                  ;; despite not knowing the new type yet.
973                  ((reader boundp) (list object))
974                  (writer (list new object)))))
975     (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
976
977       ;; The following lexical functions change the state of the
978       ;; dfun to that which is their name. They accept arguments
979       ;; which are the parameters of the new state, and get other
980       ;; information from the lexical variables bound above.
981       (flet ((two-class (index w0 w1)
982                (when (zerop (random 2)) (psetf w0 w1 w1 w0))
983                (dfun-update gf
984                             #'make-two-class-accessor-dfun
985                             ntype
986                             w0
987                             w1
988                             index))
989              (one-index (index &optional cache)
990                (dfun-update gf
991                             #'make-one-index-accessor-dfun
992                             ntype
993                             index
994                             cache))
995              (n-n (&optional cache)
996                (if (consp nindex)
997                    (dfun-update gf #'make-checking-dfun nemf)
998                    (dfun-update gf #'make-n-n-accessor-dfun ntype cache)))
999              (caching () ; because cached accessor emfs are much faster
1000                          ; for accessors
1001                (dfun-update gf #'make-caching-dfun))
1002              (do-fill (update-fn)
1003                (let ((ncache (fill-cache cache wrappers nindex)))
1004                  (unless (eq ncache cache)
1005                    (funcall update-fn ncache)))))
1006
1007         (cond ((null ntype)
1008                (caching))
1009               ((or invalidp
1010                    (null nindex)))
1011               ((not (pcl-instance-p object))
1012                (caching))
1013               ((or (neq ntype otype) (listp wrappers))
1014                (caching))
1015               (t
1016                (ecase ostate
1017                  (one-class
1018                   (setq oindex (dfun-info-index dfun-info))
1019                   (setq ow0 (dfun-info-wrapper0 dfun-info))
1020                   (unless (eq ow0 wrappers)
1021                     (if (eql nindex oindex)
1022                         (two-class nindex ow0 wrappers)
1023                         (n-n))))
1024                  (two-class
1025                   (setq oindex (dfun-info-index dfun-info))
1026                   (setq ow0 (dfun-info-wrapper0 dfun-info))
1027                   (setq ow1 (dfun-info-wrapper1 dfun-info))
1028                   (unless (or (eq ow0 wrappers) (eq ow1 wrappers))
1029                     (if (eql nindex oindex)
1030                         (one-index nindex)
1031                         (n-n))))
1032                  (one-index
1033                   (setq oindex (dfun-info-index dfun-info))
1034                   (setq cache (dfun-info-cache dfun-info))
1035                   (if (eql nindex oindex)
1036                       (do-fill (lambda (ncache)
1037                                  (one-index nindex ncache)))
1038                       (n-n)))
1039                  (n-n
1040                   (setq cache (dfun-info-cache dfun-info))
1041                   (if (consp nindex)
1042                       (caching)
1043                       (do-fill #'n-n))))))))))
1044
1045 (defun checking-miss (generic-function args dfun-info)
1046   (let ((oemf (dfun-info-function dfun-info))
1047         (cache (dfun-info-cache dfun-info)))
1048     (dfun-miss (generic-function args wrappers invalidp nemf)
1049       (cond (invalidp)
1050             ((eq oemf nemf)
1051              (let ((ncache (fill-cache cache wrappers nil)))
1052                (unless (eq ncache cache)
1053                  (dfun-update generic-function #'make-checking-dfun
1054                               nemf ncache))))
1055             (t
1056              (dfun-update generic-function #'make-caching-dfun))))))
1057
1058 (defun caching-miss (generic-function args dfun-info)
1059   (let ((ocache (dfun-info-cache dfun-info)))
1060     (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
1061       (cond (invalidp)
1062             (t
1063              (let ((ncache (fill-cache ocache wrappers emf)))
1064                (unless (eq ncache ocache)
1065                  (dfun-update generic-function
1066                               #'make-caching-dfun ncache))))))))
1067
1068 (defun constant-value-miss (generic-function args dfun-info)
1069   (let ((ocache (dfun-info-cache dfun-info)))
1070     (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
1071       (unless invalidp
1072         (let* ((function
1073                 (typecase emf
1074                   (fast-method-call (fast-method-call-function emf))
1075                   (method-call (method-call-function emf))))
1076                (value (let ((val (method-function-get
1077                                   function :constant-value '.not-found.)))
1078                         (aver (not (eq val '.not-found.)))
1079                         val))
1080                (ncache (fill-cache ocache wrappers value)))
1081           (unless (eq ncache ocache)
1082             (dfun-update generic-function
1083                          #'make-constant-value-dfun ncache)))))))
1084 \f
1085 ;;; Given a generic function and a set of arguments to that generic
1086 ;;; function, return a mess of values.
1087 ;;;
1088 ;;;  <function>   The compiled effective method function for this set of
1089 ;;;            arguments.
1090 ;;;
1091 ;;;  <applicable> Sorted list of applicable methods.
1092 ;;;
1093 ;;;  <wrappers>   Is a single wrapper if the generic function has only
1094 ;;;            one key, that is arg-info-nkeys of the arg-info is 1.
1095 ;;;            Otherwise a list of the wrappers of the specialized
1096 ;;;            arguments to the generic function.
1097 ;;;
1098 ;;;            Note that all these wrappers are valid. This function
1099 ;;;            does invalid wrapper traps when it finds an invalid
1100 ;;;            wrapper and then returns the new, valid wrapper.
1101 ;;;
1102 ;;;  <invalidp>   True if any of the specialized arguments had an invalid
1103 ;;;            wrapper, false otherwise.
1104 ;;;
1105 ;;;  <type>       READER or WRITER when the only method that would be run
1106 ;;;            is a standard reader or writer method. To be specific,
1107 ;;;            the value is READER when the method combination is eq to
1108 ;;;            *standard-method-combination*; there are no applicable
1109 ;;;            :before, :after or :around methods; and the most specific
1110 ;;;            primary method is a standard reader method.
1111 ;;;
1112 ;;;  <index>      If <type> is READER or WRITER, and the slot accessed is
1113 ;;;            an :instance slot, this is the index number of that slot
1114 ;;;            in the object argument.
1115 (defvar *cache-miss-values-stack* ())
1116
1117 (defun cache-miss-values (gf args state)
1118   (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
1119       (get-generic-fun-info gf)
1120     (declare (ignore nreq applyp nkeys))
1121     (with-dfun-wrappers (args metatypes)
1122       (dfun-wrappers invalid-wrapper-p wrappers classes types)
1123       (error-need-at-least-n-args gf (length metatypes))
1124       (multiple-value-bind (emf methods accessor-type index)
1125           (cache-miss-values-internal
1126            gf arg-info wrappers classes types state)
1127         (values emf methods
1128                 dfun-wrappers
1129                 invalid-wrapper-p
1130                 accessor-type index)))))
1131
1132 (defun cache-miss-values-internal (gf arg-info wrappers classes types state)
1133   (if (and classes (equal classes (cdr (assq gf *cache-miss-values-stack*))))
1134       (break-vicious-metacircle gf classes arg-info)
1135       (let ((*cache-miss-values-stack*
1136              (acons gf classes *cache-miss-values-stack*))
1137             (cam-std-p (or (null arg-info)
1138                            (gf-info-c-a-m-emf-std-p arg-info))))
1139         (multiple-value-bind (methods all-applicable-and-sorted-p)
1140             (if cam-std-p
1141                 (compute-applicable-methods-using-types gf types)
1142                 (compute-applicable-methods-using-classes gf classes))
1143
1144   (let* ((for-accessor-p (eq state 'accessor))
1145          (for-cache-p (or (eq state 'caching) (eq state 'accessor)))
1146          (emf (if (or cam-std-p all-applicable-and-sorted-p)
1147                   (let ((generator
1148                          (get-secondary-dispatch-function1
1149                           gf methods types nil (and for-cache-p wrappers)
1150                           all-applicable-and-sorted-p)))
1151                     (make-callable gf methods generator
1152                                    nil (and for-cache-p wrappers)))
1153                   (default-secondary-dispatch-function gf))))
1154     (multiple-value-bind (index accessor-type)
1155         (and for-accessor-p all-applicable-and-sorted-p methods
1156              (accessor-values gf arg-info classes methods))
1157       (values (if (integerp index) index emf)
1158               methods accessor-type index)))))))
1159
1160 ;;; Try to break a vicious circle while computing a cache miss.
1161 ;;; GF is the generic function, CLASSES are the classes of actual
1162 ;;; arguments, and ARG-INFO is the generic functions' arg-info.
1163 ;;;
1164 ;;; A vicious circle can be entered when the computation of the cache
1165 ;;; miss values itself depends on the values being computed.  For
1166 ;;; instance, adding a method which is an instance of a subclass of
1167 ;;; STANDARD-METHOD leads to cache misses for slot accessors of
1168 ;;; STANDARD-METHOD like METHOD-SPECIALIZERS, and METHOD-SPECIALIZERS
1169 ;;; is itself used while we compute cache miss values.
1170 (defun break-vicious-metacircle (gf classes arg-info)
1171   (when (typep gf 'standard-generic-function)
1172     (multiple-value-bind (class slotd accessor-type)
1173         (accesses-standard-class-slot-p gf)
1174       (when class
1175         (let ((method (find-standard-class-accessor-method
1176                        gf class accessor-type))
1177               (index (standard-slot-value/eslotd slotd 'location))
1178               (type (gf-info-simple-accessor-type arg-info)))
1179           (when (and method
1180                      (subtypep (ecase accessor-type
1181                                  ((reader) (car classes))
1182                                  ((writer) (cadr classes)))
1183                                class))
1184             (return-from break-vicious-metacircle
1185               (values index (list method) type index)))))))
1186   (error "~@<vicious metacircle:  The computation of an ~
1187           effective method of ~s for arguments of types ~s uses ~
1188           the effective method being computed.~@:>"
1189          gf classes))
1190
1191 ;;; Return (CLASS SLOTD ACCESSOR-TYPE) if some method of generic
1192 ;;; function GF accesses a slot of some class in *STANDARD-CLASSES*.
1193 ;;; CLASS is the class accessed, SLOTD is the effective slot definition
1194 ;;; object of the slot accessed, and ACCESSOR-TYPE is one of the symbols
1195 ;;; READER or WRITER describing the slot access.
1196 (defun accesses-standard-class-slot-p (gf)
1197   (flet ((standard-class-slot-access (gf class)
1198            (loop with gf-name = (standard-slot-value/gf gf 'name)
1199                  for slotd in (standard-slot-value/class class 'slots)
1200                  ;; FIXME: where does BOUNDP fit in here?  Is it
1201                  ;; relevant?
1202                  as readers = (standard-slot-value/eslotd slotd 'readers)
1203                  as writers = (standard-slot-value/eslotd slotd 'writers)
1204                  if (member gf-name readers :test #'equal)
1205                    return (values slotd 'reader)
1206                  else if (member gf-name writers :test #'equal)
1207                    return (values slotd 'writer))))
1208     (dolist (class-name *standard-classes*)
1209       (let ((class (find-class class-name)))
1210         (multiple-value-bind (slotd accessor-type)
1211             (standard-class-slot-access gf class)
1212           (when slotd
1213             (return (values class slotd accessor-type))))))))
1214
1215 ;;; Find a slot reader/writer method among the methods of generic
1216 ;;; function GF which reads/writes instances of class CLASS.
1217 ;;; TYPE is one of the symbols READER or WRITER.
1218 (defun find-standard-class-accessor-method (gf class type)
1219   (let ((cpl (standard-slot-value/class class 'class-precedence-list))
1220         (found-specializer *the-class-t*)
1221         (found-method nil))
1222     (dolist (method (standard-slot-value/gf gf 'methods) found-method)
1223       (let ((specializers (standard-slot-value/method method 'specializers))
1224             (qualifiers (plist-value method 'qualifiers)))
1225         (when (and (null qualifiers)
1226                    (let ((subcpl (member (ecase type
1227                                            (reader (car specializers))
1228                                            (writer (cadr specializers)))
1229                                          cpl)))
1230                      (and subcpl (member found-specializer subcpl))))
1231           (setf found-specializer (ecase type
1232                                     (reader (car specializers))
1233                                     (writer (cadr specializers))))
1234           (setf found-method method))))))
1235
1236 (defun accessor-values (gf arg-info classes methods)
1237   (declare (ignore gf))
1238   (let* ((accessor-type (gf-info-simple-accessor-type arg-info))
1239          (accessor-class (case accessor-type
1240                            ((reader boundp) (car classes))
1241                            (writer (cadr classes)))))
1242     (accessor-values-internal accessor-type accessor-class methods)))
1243
1244 (defun accessor-values1 (gf accessor-type accessor-class)
1245   (let* ((type `(class-eq ,accessor-class))
1246          (types (ecase accessor-type
1247                   ((reader boundp) `(,type))
1248                   (writer `(t ,type))))
1249          (methods (compute-applicable-methods-using-types gf types)))
1250     (accessor-values-internal accessor-type accessor-class methods)))
1251
1252 (defun accessor-values-internal (accessor-type accessor-class methods)
1253   (dolist (meth methods)
1254     (when (if (consp meth)
1255               (early-method-qualifiers meth)
1256               (method-qualifiers meth))
1257       (return-from accessor-values-internal (values nil nil))))
1258   (let* ((meth (car methods))
1259          (early-p (not (eq *boot-state* 'complete)))
1260          (slot-name (when accessor-class
1261                       (if (consp meth)
1262                           (and (early-method-standard-accessor-p meth)
1263                                (early-method-standard-accessor-slot-name meth))
1264                           (and (member *the-class-std-object*
1265                                        (if early-p
1266                                            (early-class-precedence-list
1267                                             accessor-class)
1268                                            (class-precedence-list
1269                                             accessor-class)))
1270                                (if early-p
1271                                    (not (eq *the-class-standard-method*
1272                                             (early-method-class meth)))
1273                                    (standard-accessor-method-p meth))
1274                                (if early-p
1275                                    (early-accessor-method-slot-name meth)
1276                                    (accessor-method-slot-name meth))))))
1277          (slotd (and accessor-class
1278                      (if early-p
1279                          (dolist (slot (early-class-slotds accessor-class) nil)
1280                            (when (eql slot-name
1281                                       (early-slot-definition-name slot))
1282                              (return slot)))
1283                          (find-slot-definition accessor-class slot-name)))))
1284     (when (and slotd
1285                (or early-p
1286                    (slot-accessor-std-p slotd accessor-type)))
1287       (values (if early-p
1288                   (early-slot-definition-location slotd)
1289                   (slot-definition-location slotd))
1290               accessor-type))))
1291
1292 (defun make-accessor-table (gf type &optional table)
1293   (unless table (setq table (make-hash-table :test 'eq)))
1294   (let ((methods (if (early-gf-p gf)
1295                      (early-gf-methods gf)
1296                      (generic-function-methods gf)))
1297         (all-index nil)
1298         (no-class-slots-p t)
1299         (early-p (not (eq *boot-state* 'complete)))
1300         first second (size 0))
1301     (declare (fixnum size))
1302     ;; class -> {(specl slotd)}
1303     (dolist (method methods)
1304       (let* ((specializers (if (consp method)
1305                                (early-method-specializers method t)
1306                                (method-specializers method)))
1307              (specl (ecase type
1308                       ((reader boundp) (car specializers))
1309                       (writer (cadr specializers))))
1310              (specl-cpl (if early-p
1311                             (early-class-precedence-list specl)
1312                             (and (class-finalized-p specl)
1313                                  (class-precedence-list specl))))
1314              (so-p (member *the-class-std-object* specl-cpl))
1315              (slot-name (if (consp method)
1316                             (and (early-method-standard-accessor-p method)
1317                                  (early-method-standard-accessor-slot-name
1318                                   method))
1319                             (accessor-method-slot-name method))))
1320         (when (or (null specl-cpl)
1321                   (member *the-class-structure-object* specl-cpl))
1322           (return-from make-accessor-table nil))
1323         (maphash (lambda (class slotd)
1324                    (let ((cpl (if early-p
1325                                   (early-class-precedence-list class)
1326                                   (class-precedence-list class))))
1327                      (when (memq specl cpl)
1328                        (unless (and (or so-p
1329                                         (member *the-class-std-object* cpl))
1330                                     (or early-p
1331                                         (slot-accessor-std-p slotd type)))
1332                          (return-from make-accessor-table nil))
1333                        (push (cons specl slotd) (gethash class table)))))
1334                  (gethash slot-name *name->class->slotd-table*))))
1335     (maphash (lambda (class specl+slotd-list)
1336                (dolist (sclass (if early-p
1337                                    (early-class-precedence-list class)
1338                                    (class-precedence-list class))
1339                                (error "This can't happen."))
1340                  (let ((a (assq sclass specl+slotd-list)))
1341                    (when a
1342                      (let* ((slotd (cdr a))
1343                             (index (if early-p
1344                                        (early-slot-definition-location slotd)
1345                                        (slot-definition-location slotd))))
1346                        (unless index (return-from make-accessor-table nil))
1347                        (setf (gethash class table) index)
1348                        (when (consp index) (setq no-class-slots-p nil))
1349                        (setq all-index (if (or (null all-index)
1350                                                (eql all-index index))
1351                                            index t))
1352                        (incf size)
1353                        (cond ((= size 1) (setq first class))
1354                              ((= size 2) (setq second class)))
1355                        (return nil))))))
1356              table)
1357     (values table all-index first second size no-class-slots-p)))
1358
1359 (defun compute-applicable-methods-using-types (generic-function types)
1360   (let ((definite-p t) (possibly-applicable-methods nil))
1361     (dolist (method (if (early-gf-p generic-function)
1362                         (early-gf-methods generic-function)
1363                         (generic-function-methods generic-function)))
1364       (let ((specls (if (consp method)
1365                         (early-method-specializers method t)
1366                         (method-specializers method)))
1367             (types types)
1368             (possibly-applicable-p t) (applicable-p t))
1369         (dolist (specl specls)
1370           (multiple-value-bind (specl-applicable-p specl-possibly-applicable-p)
1371               (specializer-applicable-using-type-p specl (pop types))
1372             (unless specl-applicable-p
1373               (setq applicable-p nil))
1374             (unless specl-possibly-applicable-p
1375               (setq possibly-applicable-p nil)
1376               (return nil))))
1377         (when possibly-applicable-p
1378           (unless applicable-p (setq definite-p nil))
1379           (push method possibly-applicable-methods))))
1380     (let ((precedence (arg-info-precedence (if (early-gf-p generic-function)
1381                                                (early-gf-arg-info
1382                                                 generic-function)
1383                                                (gf-arg-info
1384                                                 generic-function)))))
1385       (values (sort-applicable-methods precedence
1386                                        (nreverse possibly-applicable-methods)
1387                                        types)
1388               definite-p))))
1389
1390 (defun sort-applicable-methods (precedence methods types)
1391   (sort-methods methods
1392                 precedence
1393                 (lambda (class1 class2 index)
1394                   (let* ((class (type-class (nth index types)))
1395                          (cpl (if (eq *boot-state* 'complete)
1396                                   (class-precedence-list class)
1397                                   (early-class-precedence-list class))))
1398                     (if (memq class2 (memq class1 cpl))
1399                         class1 class2)))))
1400
1401 (defun sort-methods (methods precedence compare-classes-function)
1402   (flet ((sorter (method1 method2)
1403            (dolist (index precedence)
1404              (let* ((specl1 (nth index (if (listp method1)
1405                                            (early-method-specializers method1
1406                                                                       t)
1407                                            (method-specializers method1))))
1408                     (specl2 (nth index (if (listp method2)
1409                                            (early-method-specializers method2
1410                                                                       t)
1411                                            (method-specializers method2))))
1412                     (order (order-specializers
1413                              specl1 specl2 index compare-classes-function)))
1414                (when order
1415                  (return-from sorter (eq order specl1)))))))
1416     (stable-sort methods #'sorter)))
1417
1418 (defun order-specializers (specl1 specl2 index compare-classes-function)
1419   (let ((type1 (if (eq *boot-state* 'complete)
1420                    (specializer-type specl1)
1421                    (!bootstrap-get-slot 'specializer specl1 'type)))
1422         (type2 (if (eq *boot-state* 'complete)
1423                    (specializer-type specl2)
1424                    (!bootstrap-get-slot 'specializer specl2 'type))))
1425     (cond ((eq specl1 specl2)
1426            nil)
1427           ((atom type1)
1428            specl2)
1429           ((atom type2)
1430            specl1)
1431           (t
1432            (case (car type1)
1433              (class    (case (car type2)
1434                          (class (funcall compare-classes-function
1435                                          specl1 specl2 index))
1436                          (t specl2)))
1437              (prototype (case (car type2)
1438                          (class (funcall compare-classes-function
1439                                          specl1 specl2 index))
1440                          (t specl2)))
1441              (class-eq (case (car type2)
1442                          (eql specl2)
1443                          (class-eq nil)
1444                          (class type1)))
1445              (eql      (case (car type2)
1446                          (eql nil)
1447                          (t specl1))))))))
1448
1449 (defun map-all-orders (methods precedence function)
1450   (let ((choices nil))
1451     (flet ((compare-classes-function (class1 class2 index)
1452              (declare (ignore index))
1453              (let ((choice nil))
1454                (dolist (c choices nil)
1455                  (when (or (and (eq (first c) class1)
1456                                 (eq (second c) class2))
1457                            (and (eq (first c) class2)
1458                                 (eq (second c) class1)))
1459                    (return (setq choice c))))
1460                (unless choice
1461                  (setq choice
1462                        (if (class-might-precede-p class1 class2)
1463                            (if (class-might-precede-p class2 class1)
1464                                (list class1 class2 nil t)
1465                                (list class1 class2 t))
1466                            (if (class-might-precede-p class2 class1)
1467                                (list class2 class1 t)
1468                                (let ((name1 (class-name class1))
1469                                      (name2 (class-name class2)))
1470                                  (if (and name1
1471                                           name2
1472                                           (symbolp name1)
1473                                           (symbolp name2)
1474                                           (string< (symbol-name name1)
1475                                                    (symbol-name name2)))
1476                                      (list class1 class2 t)
1477                                      (list class2 class1 t))))))
1478                  (push choice choices))
1479                (car choice))))
1480       (loop (funcall function
1481                      (sort-methods methods
1482                                    precedence
1483                                    #'compare-classes-function))
1484             (unless (dolist (c choices nil)
1485                       (unless (third c)
1486                         (rotatef (car c) (cadr c))
1487                         (return (setf (third c) t))))
1488               (return nil))))))
1489
1490 ;;; CMUCL comment: used only in map-all-orders
1491 (defun class-might-precede-p (class1 class2)
1492   (if (not *in-precompute-effective-methods-p*)
1493       (not (member class1 (cdr (class-precedence-list class2))))
1494       (class-can-precede-p class1 class2)))
1495
1496 (defun compute-precedence (lambda-list nreq argument-precedence-order)
1497   (if (null argument-precedence-order)
1498       (let ((list nil))
1499         (dotimes-fixnum (i nreq list) (push (- (1- nreq) i) list)))
1500       (mapcar (lambda (x) (position x lambda-list))
1501               argument-precedence-order)))
1502
1503 (defun cpl-or-nil (class)
1504   (if (eq *boot-state* 'complete)
1505       ;; KLUDGE: why not use (slot-boundp class
1506       ;; 'class-precedence-list)?  Well, unfortunately, CPL-OR-NIL is
1507       ;; used within COMPUTE-APPLICABLE-METHODS, including for
1508       ;; SLOT-BOUNDP-USING-CLASS... and the available mechanism for
1509       ;; breaking such nasty cycles in effective method computation
1510       ;; only works for readers and writers, not boundps.  It might
1511       ;; not be too hard to make it work for BOUNDP accessors, but in
1512       ;; the meantime we use an extra slot for exactly the result of
1513       ;; the SLOT-BOUNDP that we want.  (We cannot use
1514       ;; CLASS-FINALIZED-P, because in the process of class
1515       ;; finalization we need to use the CPL which has been computed
1516       ;; to cache effective methods for slot accessors.) -- CSR,
1517       ;; 2004-09-19.
1518       (when (cpl-available-p class)
1519         (class-precedence-list class))
1520       (early-class-precedence-list class)))
1521
1522 (defun saut-and (specl type)
1523   (let ((applicable nil)
1524         (possibly-applicable t))
1525     (dolist (type (cdr type))
1526       (multiple-value-bind (appl poss-appl)
1527           (specializer-applicable-using-type-p specl type)
1528         (when appl (return (setq applicable t)))
1529         (unless poss-appl (return (setq possibly-applicable nil)))))
1530     (values applicable possibly-applicable)))
1531
1532 (defun saut-not (specl type)
1533   (let ((ntype (cadr type)))
1534     (values nil
1535             (case (car ntype)
1536               (class      (saut-not-class specl ntype))
1537               (class-eq   (saut-not-class-eq specl ntype))
1538               (prototype  (saut-not-prototype specl ntype))
1539               (eql      (saut-not-eql specl ntype))
1540               (t (error "~S cannot handle the second argument ~S"
1541                         'specializer-applicable-using-type-p type))))))
1542
1543 (defun saut-not-class (specl ntype)
1544   (let* ((class (type-class specl))
1545          (cpl (cpl-or-nil class)))
1546     (not (memq (cadr ntype) cpl))))
1547
1548 (defun saut-not-prototype (specl ntype)
1549   (let* ((class (case (car specl)
1550                   (eql       (class-of (cadr specl)))
1551                   (class-eq  (cadr specl))
1552                   (prototype (cadr specl))
1553                   (class     (cadr specl))))
1554          (cpl (cpl-or-nil class)))
1555     (not (memq (cadr ntype) cpl))))
1556
1557 (defun saut-not-class-eq (specl ntype)
1558   (let ((class (case (car specl)
1559                  (eql      (class-of (cadr specl)))
1560                  (class-eq (cadr specl)))))
1561     (not (eq class (cadr ntype)))))
1562
1563 (defun saut-not-eql (specl ntype)
1564   (case (car specl)
1565     (eql (not (eql (cadr specl) (cadr ntype))))
1566     (t   t)))
1567
1568 (defun class-applicable-using-class-p (specl type)
1569   (let ((pred (memq specl (cpl-or-nil type))))
1570     (values pred
1571             (or pred
1572                 (if (not *in-precompute-effective-methods-p*)
1573                     ;; classes might get common subclass
1574                     (superclasses-compatible-p specl type)
1575                     ;; worry only about existing classes
1576                     (classes-have-common-subclass-p specl type))))))
1577
1578 (defun classes-have-common-subclass-p (class1 class2)
1579   (or (eq class1 class2)
1580       (let ((class1-subs (class-direct-subclasses class1)))
1581         (or (memq class2 class1-subs)
1582             (dolist (class1-sub class1-subs nil)
1583               (when (classes-have-common-subclass-p class1-sub class2)
1584                 (return t)))))))
1585
1586 (defun saut-class (specl type)
1587   (case (car specl)
1588     (class (class-applicable-using-class-p (cadr specl) (cadr type)))
1589     (t     (values nil (let ((class (type-class specl)))
1590                          (memq (cadr type)
1591                                (cpl-or-nil class)))))))
1592
1593 (defun saut-class-eq (specl type)
1594   (if (eq (car specl) 'eql)
1595       (values nil (eq (class-of (cadr specl)) (cadr type)))
1596       (let ((pred (case (car specl)
1597                     (class-eq
1598                      (eq (cadr specl) (cadr type)))
1599                     (class
1600                      (or (eq (cadr specl) (cadr type))
1601                          (memq (cadr specl) (cpl-or-nil (cadr type))))))))
1602         (values pred pred))))
1603
1604 (defun saut-prototype (specl type)
1605   (declare (ignore specl type))
1606   (values nil nil)) ; XXX original PCL comment: fix this someday
1607
1608 (defun saut-eql (specl type)
1609   (let ((pred (case (car specl)
1610                 (eql    (eql (cadr specl) (cadr type)))
1611                 (class-eq   (eq (cadr specl) (class-of (cadr type))))
1612                 (class      (memq (cadr specl)
1613                                   (let ((class (class-of (cadr type))))
1614                                     (cpl-or-nil class)))))))
1615     (values pred pred)))
1616
1617 (defun specializer-applicable-using-type-p (specl type)
1618   (setq specl (type-from-specializer specl))
1619   (when (eq specl t)
1620     (return-from specializer-applicable-using-type-p (values t t)))
1621   ;; This is used by C-A-M-U-T and GENERATE-DISCRIMINATION-NET-INTERNAL,
1622   ;; and has only what they need.
1623   (if (or (atom type) (eq (car type) t))
1624       (values nil t)
1625       (case (car type)
1626         (and    (saut-and specl type))
1627         (not    (saut-not specl type))
1628         (class      (saut-class specl type))
1629         (prototype  (saut-prototype specl type))
1630         (class-eq   (saut-class-eq specl type))
1631         (eql    (saut-eql specl type))
1632         (t        (error "~S cannot handle the second argument ~S."
1633                            'specializer-applicable-using-type-p
1634                            type)))))
1635
1636 (defun map-all-classes (function &optional (root t))
1637   (let ((braid-p (or (eq *boot-state* 'braid)
1638                      (eq *boot-state* 'complete))))
1639     (labels ((do-class (class)
1640                (mapc #'do-class
1641                      (if braid-p
1642                          (class-direct-subclasses class)
1643                          (early-class-direct-subclasses class)))
1644                (funcall function class)))
1645       (do-class (if (symbolp root)
1646                     (find-class root)
1647                     root)))))
1648 \f
1649 (defvar *effective-method-cache* (make-hash-table :test 'eq))
1650
1651 (defun flush-effective-method-cache (generic-function)
1652   (dolist (method (generic-function-methods generic-function))
1653     (remhash method *effective-method-cache*)))
1654
1655 (defun get-secondary-dispatch-function (gf methods types
1656                                         &optional method-alist wrappers)
1657   (let ((generator
1658          (get-secondary-dispatch-function1
1659           gf methods types (not (null method-alist)) (not (null wrappers))
1660           (not (methods-contain-eql-specializer-p methods)))))
1661     (make-callable gf methods generator method-alist wrappers)))
1662
1663 (defun get-secondary-dispatch-function1 (gf methods types method-alist-p
1664                                             wrappers-p
1665                                             &optional
1666                                             all-applicable-p
1667                                             (all-sorted-p t)
1668                                             function-p)
1669   (if (null methods)
1670       (if function-p
1671           (lambda (method-alist wrappers)
1672             (declare (ignore method-alist wrappers))
1673             #'(lambda (&rest args)
1674                 (apply #'no-applicable-method gf args)))
1675           (lambda (method-alist wrappers)
1676             (declare (ignore method-alist wrappers))
1677             (lambda (&rest args)
1678               (apply #'no-applicable-method gf args))))
1679       (let* ((key (car methods))
1680              (ht-value (or (gethash key *effective-method-cache*)
1681                            (setf (gethash key *effective-method-cache*)
1682                                  (cons nil nil)))))
1683         (if (and (null (cdr methods)) all-applicable-p ; the most common case
1684                  (null method-alist-p) wrappers-p (not function-p))
1685             (or (car ht-value)
1686                 (setf (car ht-value)
1687                       (get-secondary-dispatch-function2
1688                        gf methods types method-alist-p wrappers-p
1689                        all-applicable-p all-sorted-p function-p)))
1690             (let ((akey (list methods
1691                               (if all-applicable-p 'all-applicable types)
1692                               method-alist-p wrappers-p function-p)))
1693               (or (cdr (assoc akey (cdr ht-value) :test #'equal))
1694                   (let ((value (get-secondary-dispatch-function2
1695                                 gf methods types method-alist-p wrappers-p
1696                                 all-applicable-p all-sorted-p function-p)))
1697                     (push (cons akey value) (cdr ht-value))
1698                     value)))))))
1699
1700 (defun get-secondary-dispatch-function2 (gf methods types method-alist-p
1701                                             wrappers-p all-applicable-p
1702                                             all-sorted-p function-p)
1703   (if (and all-applicable-p all-sorted-p (not function-p))
1704       (if (eq *boot-state* 'complete)
1705           (let* ((combin (generic-function-method-combination gf))
1706                  (effective (compute-effective-method gf combin methods)))
1707             (make-effective-method-function1 gf effective method-alist-p
1708                                              wrappers-p))
1709           (let ((effective (standard-compute-effective-method gf nil methods)))
1710             (make-effective-method-function1 gf effective method-alist-p
1711                                              wrappers-p)))
1712       (let ((net (generate-discrimination-net
1713                   gf methods types all-sorted-p)))
1714         (compute-secondary-dispatch-function1 gf net function-p))))
1715
1716 (defun get-effective-method-function (gf methods
1717                                          &optional method-alist wrappers)
1718   (let ((generator
1719          (get-secondary-dispatch-function1
1720           gf methods nil (not (null method-alist)) (not (null wrappers)) t)))
1721     (make-callable gf methods generator method-alist wrappers)))
1722
1723 (defun get-effective-method-function1 (gf methods &optional (sorted-p t))
1724   (get-secondary-dispatch-function1 gf methods nil nil nil t sorted-p))
1725
1726 (defun methods-contain-eql-specializer-p (methods)
1727   (and (eq *boot-state* 'complete)
1728        (dolist (method methods nil)
1729          (when (dolist (spec (method-specializers method) nil)
1730                  (when (eql-specializer-p spec) (return t)))
1731            (return t)))))
1732 \f
1733 (defun update-dfun (generic-function &optional dfun cache info)
1734   (let* ((early-p (early-gf-p generic-function))
1735          (gf-name (if early-p
1736                       (!early-gf-name generic-function)
1737                       (generic-function-name generic-function))))
1738     (set-dfun generic-function dfun cache info)
1739     (let ((dfun (if early-p
1740                     (or dfun (make-initial-dfun generic-function))
1741                     (compute-discriminating-function generic-function))))
1742       (set-funcallable-instance-function generic-function dfun)
1743       (set-fun-name generic-function gf-name)
1744       dfun)))
1745 \f
1746 (defvar *dfun-count* nil)
1747 (defvar *dfun-list* nil)
1748 (defvar *minimum-cache-size-to-list*)
1749
1750 ;;; These functions aren't used in SBCL, or documented anywhere that
1751 ;;; I'm aware of, but they look like they might be useful for
1752 ;;; debugging or performance tweaking or something, so I've just
1753 ;;; commented them out instead of deleting them. -- WHN 2001-03-28
1754 #|
1755 (defun list-dfun (gf)
1756   (let* ((sym (type-of (gf-dfun-info gf)))
1757          (a (assq sym *dfun-list*)))
1758     (unless a
1759       (push (setq a (list sym)) *dfun-list*))
1760     (push (generic-function-name gf) (cdr a))))
1761
1762 (defun list-all-dfuns ()
1763   (setq *dfun-list* nil)
1764   (map-all-generic-functions #'list-dfun)
1765   *dfun-list*)
1766
1767 (defun list-large-cache (gf)
1768   (let* ((sym (type-of (gf-dfun-info gf)))
1769          (cache (gf-dfun-cache gf)))
1770     (when cache
1771       (let ((size (cache-size cache)))
1772         (when (>= size *minimum-cache-size-to-list*)
1773           (let ((a (assoc size *dfun-list*)))
1774             (unless a
1775               (push (setq a (list size)) *dfun-list*))
1776             (push (let ((name (generic-function-name gf)))
1777                     (if (eq sym 'caching) name (list name sym)))
1778                   (cdr a))))))))
1779
1780 (defun list-large-caches (&optional (*minimum-cache-size-to-list* 130))
1781   (setq *dfun-list* nil)
1782   (map-all-generic-functions #'list-large-cache)
1783   (setq *dfun-list* (sort *dfun-list* #'< :key #'car))
1784   (mapc #'print *dfun-list*)
1785   (values))
1786
1787 (defun count-dfun (gf)
1788   (let* ((sym (type-of (gf-dfun-info gf)))
1789          (cache (gf-dfun-cache gf))
1790          (a (assq sym *dfun-count*)))
1791     (unless a
1792       (push (setq a (list sym 0 nil)) *dfun-count*))
1793     (incf (cadr a))
1794     (when cache
1795       (let* ((size (cache-size cache))
1796              (b (assoc size (third a))))
1797         (unless b
1798           (push (setq b (cons size 0)) (third a)))
1799         (incf (cdr b))))))
1800
1801 (defun count-all-dfuns ()
1802   (setq *dfun-count* (mapcar (lambda (type) (list type 0 nil))
1803                              '(ONE-CLASS TWO-CLASS DEFAULT-METHOD-ONLY
1804                                ONE-INDEX N-N CHECKING CACHING
1805                                DISPATCH)))
1806   (map-all-generic-functions #'count-dfun)
1807   (mapc (lambda (type+count+sizes)
1808           (setf (third type+count+sizes)
1809                 (sort (third type+count+sizes) #'< :key #'car)))
1810         *dfun-count*)
1811   (mapc (lambda (type+count+sizes)
1812           (format t "~&There are ~W dfuns of type ~S."
1813                   (cadr type+count+sizes) (car type+count+sizes))
1814           (format t "~%   ~S~%" (caddr type+count+sizes)))
1815         *dfun-count*)
1816   (values))
1817 |#
1818
1819 (defun gfs-of-type (type)
1820   (unless (consp type) (setq type (list type)))
1821   (let ((gf-list nil))
1822     (map-all-generic-functions (lambda (gf)
1823                                  (when (memq (type-of (gf-dfun-info gf))
1824                                              type)
1825                                    (push gf gf-list))))
1826     gf-list))