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