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