1 ;;;; This software is part of the SBCL system. See the README file for
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
10 ;;;; copyright information from original PCL sources:
12 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
13 ;;;; All rights reserved.
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
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
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
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.
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 (define-load-time-global *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
))
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)
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
)
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
)))
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
))))
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
))
147 '(setf slot-value-using-class
)))
148 (not (eq gf-name
'slot-boundp-using-class
))
149 (not (eq gf-name
'slot-makunbound-using-class
)))))
151 (setf (second args-entry
) constructor
)
152 (setf (third args-entry
) system
)
153 (setf (fourth args-entry
) nil
)))
154 (let ((entry (list args constructor system nil
)))
156 (push entry
(cdr generator-entry
))
157 (push (list generator entry
) *dfun-constructors
*))))))
159 (defmacro precompile-dfun-constructors
(&optional system
)
160 (let ((*precompiling-lap
* t
))
163 (dolist (generator-entry *dfun-constructors
*)
164 (dolist (args-entry (cdr generator-entry
))
165 (when (or (null (caddr args-entry
))
166 (eq (caddr args-entry
) system
))
167 (when system
(setf (caddr args-entry
) system
))
168 (push `(load-precompiled-dfun-constructor
169 ',(car generator-entry
)
172 ,(apply (fdefinition (car generator-entry
))
175 (nreverse collect
)))))
177 ;;; Standardized class slot access: when trying to break vicious
178 ;;; metacircles, we need a way to get at the values of slots of some
179 ;;; standard classes without going through the whole meta machinery,
180 ;;; because that would likely enter the vicious circle again. The
181 ;;; following are helper functions that short-circuit the generic
182 ;;; lookup machinery.
184 (defconstant-eqx +standard-classes
+
185 ;; KLUDGE: order matters! finding effective slot definitions
186 ;; involves calling slot-definition-name, and we need to do that to
187 ;; break metacycles, so STANDARD-EFFECTIVE-SLOT-DEFINITION must
188 ;; precede STANDARD-DIRECT-SLOT-DEFINITION in this list, at least
189 ;; until ACCESSES-STANDARD-CLASS-SLOT-P is generalized
190 '(standard-method standard-generic-function standard-class
191 standard-effective-slot-definition standard-direct-slot-definition
)
194 (define-load-time-global *standard-slot-locations
* (make-hash-table :test
'equal
))
196 (defun compute-standard-slot-locations ()
197 (let ((new (make-hash-table :test
'equal
)))
198 (dolist (class-name +standard-classes
+)
199 (let ((class (find-class class-name
)))
200 (dolist (slot (class-slots class
))
201 (setf (gethash (cons class
(slot-definition-name slot
)) new
)
202 (slot-definition-location slot
)))))
203 (setf *standard-slot-locations
* new
)))
205 (defun standard-slot-value (object slot-name class
)
206 (declare (notinline standard-instance-access
207 funcallable-standard-instance-access
))
208 (let ((location (gethash (cons class slot-name
) *standard-slot-locations
*)))
210 (let ((value (if (funcallable-instance-p object
)
211 (funcallable-standard-instance-access object location
)
212 (standard-instance-access object location
))))
213 (when (unbound-marker-p value
)
214 (error "~@<slot ~S of class ~S is unbound in object ~S~@:>"
215 slot-name class object
))
217 (error "~@<cannot get standard value of slot ~S of class ~S ~
219 slot-name class object
))))
221 (defun standard-slot-value/gf
(gf slot-name
)
222 (standard-slot-value gf slot-name
*the-class-standard-generic-function
*))
224 (defun standard-slot-value/method
(method slot-name
)
225 (standard-slot-value method slot-name
*the-class-standard-method
*))
227 (defun standard-slot-value/eslotd
(slotd slot-name
)
228 (standard-slot-value slotd slot-name
229 *the-class-standard-effective-slot-definition
*))
231 (defun standard-slot-value/dslotd
(slotd slot-name
)
232 (standard-slot-value slotd slot-name
233 *the-class-standard-direct-slot-definition
*))
235 (defun standard-slot-value/class
(class slot-name
)
236 (standard-slot-value class slot-name
*the-class-standard-class
*))
238 ;;; When all the methods of a generic function are automatically
239 ;;; generated reader or writer methods a number of special
240 ;;; optimizations are possible. These are important because of the
241 ;;; large number of generic functions of this type.
243 ;;; There are a number of cases:
245 ;;; ONE-CLASS-ACCESSOR
246 ;;; In this case, the accessor generic function has only been
247 ;;; called with one class of argument. There is no cache vector,
248 ;;; the wrapper of the one class, and the slot index are stored
249 ;;; directly as closure variables of the discriminating function.
250 ;;; This case can convert to either of the next kind.
252 ;;; TWO-CLASS-ACCESSOR
253 ;;; Like above, but two classes. This is common enough to do
254 ;;; specially. There is no cache vector. The two classes are
255 ;;; stored a separate closure variables.
257 ;;; ONE-INDEX-ACCESSOR
258 ;;; In this case, the accessor generic function has seen more than
259 ;;; one class of argument, but the index of the slot is the same
260 ;;; for all the classes that have been seen. A cache vector is
261 ;;; used to store the wrappers that have been seen, the slot index
262 ;;; is stored directly as a closure variable of the discriminating
263 ;;; function. This case can convert to the next kind.
266 ;;; This is the most general case. In this case, the accessor
267 ;;; generic function has seen more than one class of argument and
268 ;;; more than one slot index. A cache vector stores the wrappers
269 ;;; and corresponding slot indexes.
271 (defstruct (dfun-info (:constructor nil
)
275 (defstruct (no-methods (:constructor no-methods-dfun-info
())
279 (defstruct (initial (:constructor initial-dfun-info
())
283 (defstruct (dispatch (:constructor dispatch-dfun-info
())
287 (defstruct (default-method-only (:constructor default-method-only-dfun-info
())
292 ; dispatch one-class two-class default-method-only
295 ; one-index n-n checking caching
298 ; one-class two-class one-index n-n
299 (defstruct (accessor-dfun-info (:constructor nil
)
302 accessor-type
) ; (member reader writer)
304 (defmacro dfun-info-accessor-type
(di)
305 `(accessor-dfun-info-accessor-type ,di
))
307 (defstruct (one-index-dfun-info (:constructor nil
)
308 (:include accessor-dfun-info
)
312 (defmacro dfun-info-index
(di)
313 `(one-index-dfun-info-index ,di
))
315 (defstruct (n-n (:constructor n-n-dfun-info
(accessor-type cache
))
316 (:include accessor-dfun-info
)
319 (defstruct (one-class (:constructor one-class-dfun-info
320 (accessor-type index wrapper0
))
321 (:include one-index-dfun-info
)
325 (defmacro dfun-info-wrapper0
(di)
326 `(one-class-wrapper0 ,di
))
328 (defstruct (two-class (:constructor two-class-dfun-info
329 (accessor-type index wrapper0 wrapper1
))
334 (defmacro dfun-info-wrapper1
(di)
335 `(two-class-wrapper1 ,di
))
337 (defstruct (one-index (:constructor one-index-dfun-info
338 (accessor-type index cache
))
339 (:include one-index-dfun-info
)
342 (defstruct (checking (:constructor checking-dfun-info
(function cache
))
347 (defmacro dfun-info-function
(di)
348 `(checking-function ,di
))
350 (defstruct (caching (:constructor caching-dfun-info
(cache))
354 (defstruct (constant-value (:constructor constant-value-dfun-info
(cache))
358 (defmacro dfun-update
(generic-function function
&rest args
)
359 `(multiple-value-bind (dfun cache info
)
360 (funcall ,function
,generic-function
,@args
)
361 (update-dfun ,generic-function dfun cache info
)))
363 (defun accessor-miss-function (gf dfun-info
)
364 (ecase (dfun-info-accessor-type dfun-info
)
365 ((reader boundp makunbound
)
367 (accessor-miss gf nil arg dfun-info
)))
370 (accessor-miss gf new arg dfun-info
)))))
372 (declaim (sb-ext:freeze-type dfun-info
))
374 (defun make-one-class-accessor-dfun (gf type wrapper index
)
375 (let ((emit (ecase type
376 (reader 'emit-one-class-reader
)
377 (boundp 'emit-one-class-boundp
)
378 (makunbound 'emit-one-class-makunbound
)
379 (writer 'emit-one-class-writer
)))
380 (dfun-info (one-class-dfun-info type index wrapper
)))
382 (funcall (get-dfun-constructor emit
(consp index
))
384 (accessor-miss-function gf dfun-info
))
388 (defun make-two-class-accessor-dfun (gf type w0 w1 index
)
389 (let ((emit (ecase type
390 (reader 'emit-two-class-reader
)
391 (boundp 'emit-two-class-boundp
)
392 (makunbound 'emit-two-class-makunbound
)
393 (writer 'emit-two-class-writer
)))
394 (dfun-info (two-class-dfun-info type index w0 w1
)))
396 (funcall (get-dfun-constructor emit
(consp index
))
398 (accessor-miss-function gf dfun-info
))
402 ;;; std accessors same index dfun
403 (defun make-one-index-accessor-dfun (gf type index
&optional cache
)
404 (let* ((emit (ecase type
405 (reader 'emit-one-index-readers
)
406 (boundp 'emit-one-index-boundps
)
407 (makunbound 'emit-one-index-makunbounds
)
408 (writer 'emit-one-index-writers
)))
409 (cache (or cache
(make-cache :key-count
1 :value nil
:size
4)))
410 (dfun-info (one-index-dfun-info type index cache
)))
411 (declare (type cache cache
))
413 (funcall (get-dfun-constructor emit
(consp index
))
416 (accessor-miss-function gf dfun-info
))
420 (defun make-n-n-accessor-dfun (gf type
&optional cache
)
421 (let* ((emit (ecase type
422 (reader 'emit-n-n-readers
)
423 (boundp 'emit-n-n-boundps
)
424 (makunbound 'emit-n-n-makunbounds
)
425 (writer 'emit-n-n-writers
)))
426 (cache (or cache
(make-cache :key-count
1 :value t
:size
2)))
427 (dfun-info (n-n-dfun-info type cache
)))
428 (declare (type cache cache
))
430 (funcall (get-dfun-constructor emit
)
432 (accessor-miss-function gf dfun-info
))
436 (defun make-checking-dfun (generic-function function
&optional cache
)
437 (unless (or cache
(use-default-method-only-dfun-p generic-function
))
438 (when (use-caching-dfun-p generic-function
)
439 (return-from make-checking-dfun
(make-caching-dfun generic-function
)))
440 (when (use-dispatch-dfun-p generic-function
)
441 (return-from make-checking-dfun
(make-dispatch-dfun generic-function
))))
442 (multiple-value-bind (nreq applyp metatypes nkeys
)
443 (get-generic-fun-info generic-function
)
444 (declare (ignore nreq
))
445 (if (every (lambda (mt) (eq mt t
)) metatypes
)
446 (let ((dfun-info (default-method-only-dfun-info)))
448 (let* ((constructor (get-dfun-constructor 'emit-default-only metatypes applyp
))
449 (fun (funcall constructor function
))
450 (name (generic-function-name generic-function
)))
451 (set-fun-name fun
`(default-only ,name
))
455 (let* ((cache (or cache
(make-cache :key-count nkeys
:value nil
:size
2)))
456 (dfun-info (checking-dfun-info function cache
)))
458 (funcall (get-dfun-constructor 'emit-checking metatypes applyp
)
462 (checking-miss generic-function args dfun-info
)))
466 (defun make-final-checking-dfun (generic-function function classes-list new-class
)
467 (multiple-value-bind (nreq applyp metatypes nkeys
)
468 (get-generic-fun-info generic-function
)
469 (declare (ignore nreq applyp nkeys
))
470 (if (every (lambda (mt) (eq mt t
)) metatypes
)
471 (values (lambda (&rest args
)
472 (invoke-emf function args
))
473 nil
(default-method-only-dfun-info))
474 (let ((cache (make-final-ordinary-dfun-cache
475 generic-function nil classes-list new-class
)))
476 (make-checking-dfun generic-function function cache
)))))
478 (defun use-default-method-only-dfun-p (generic-function)
479 (multiple-value-bind (nreq applyp metatypes nkeys
)
480 (get-generic-fun-info generic-function
)
481 (declare (ignore nreq applyp nkeys
))
482 (every (lambda (mt) (eq mt t
)) metatypes
)))
484 (defun use-caching-dfun-p (generic-function)
485 (some (lambda (method) (method-plist-value method
:slot-name-lists
))
486 ;; KLUDGE: As of sbcl-0.6.4, it's very important for
487 ;; efficiency to know the type of the sequence argument to
488 ;; quantifiers (SOME/NOTANY/etc.) at compile time, but
489 ;; the compiler isn't smart enough to understand the :TYPE
490 ;; slot option for DEFCLASS, so we just tell
491 ;; it the type by hand here.
493 (if (early-gf-p generic-function
)
494 (early-gf-methods generic-function
)
495 (generic-function-methods generic-function
)))))
497 (defun make-caching-dfun (generic-function &optional cache
)
499 (when (use-constant-value-dfun-p generic-function
)
500 (return-from make-caching-dfun
501 (make-constant-value-dfun generic-function
)))
502 (when (use-dispatch-dfun-p generic-function
)
503 (return-from make-caching-dfun
504 (make-dispatch-dfun generic-function
))))
505 (multiple-value-bind (nreq applyp metatypes nkeys
)
506 (get-generic-fun-info generic-function
)
507 (declare (ignore nreq
))
508 (let* ((cache (or cache
(make-cache :key-count nkeys
:value t
:size
2)))
509 (dfun-info (caching-dfun-info cache
)))
511 (funcall (get-dfun-constructor 'emit-caching metatypes applyp
)
514 (caching-miss generic-function args dfun-info
)))
518 (defun make-final-caching-dfun (generic-function classes-list new-class
)
519 (let ((cache (make-final-ordinary-dfun-cache
520 generic-function t classes-list new-class
)))
521 (make-caching-dfun generic-function cache
)))
523 (defun insure-caching-dfun (gf)
524 (multiple-value-bind (nreq applyp metatypes nkeys
)
525 (get-generic-fun-info gf
)
526 (declare (ignore nreq nkeys
))
528 (not (null (car metatypes
)))
529 (dolist (mt metatypes nil
)
530 (unless (eq mt t
) (return t
))))
531 (get-dfun-constructor 'emit-caching metatypes applyp
))))
533 (defun use-constant-value-dfun-p (gf &optional boolean-values-p
)
534 (multiple-value-bind (nreq applyp metatypes nkeys
)
535 (get-generic-fun-info gf
)
536 (declare (ignore nreq metatypes nkeys
))
537 (let* ((early-p (early-gf-p gf
))
539 (early-gf-methods gf
)
540 (generic-function-methods gf
)))
541 (default '(unknown)))
543 (or (not (eq **boot-state
** 'complete
))
544 ;; If COMPUTE-APPLICABLE-METHODS is specialized, we
545 ;; can't use this, of course, because we can't tell
546 ;; which methods will be considered applicable.
548 ;; Also, don't use this dfun method if the generic
549 ;; function has a non-standard method combination,
550 ;; because if it has, it's not sure that method
551 ;; functions are used directly as effective methods,
552 ;; which CONSTANT-VALUE-MISS depends on. The
553 ;; pre-defined method combinations like LIST are
555 (and (compute-applicable-methods-emf-std-p gf
)
556 (eq (generic-function-method-combination gf
)
557 *standard-method-combination
*)))
558 ;; Check that no method is eql-specialized, and that all
559 ;; methods return a constant value. If BOOLEAN-VALUES-P,
560 ;; check that all return T or NIL. Also, check that no
561 ;; method has qualifiers, to make sure that emfs are really
562 ;; method functions; see above.
563 (dolist (method methods t
)
564 (when (eq **boot-state
** 'complete
)
565 (when (or (some #'eql-specializer-p
566 (safe-method-specializers method
))
567 (safe-method-qualifiers method
))
569 (let ((value (method-plist-value method
:constant-value default
)))
570 (when (or (eq value default
)
571 (and boolean-values-p
572 (not (member value
'(t nil
)))))
575 (defun make-constant-value-dfun (generic-function &optional cache
)
576 (multiple-value-bind (nreq applyp metatypes nkeys
)
577 (get-generic-fun-info generic-function
)
578 (declare (ignore nreq applyp
))
579 (let* ((cache (or cache
(make-cache :key-count nkeys
:value t
:size
2)))
580 (dfun-info (constant-value-dfun-info cache
)))
581 (declare (type cache cache
))
583 (funcall (get-dfun-constructor 'emit-constant-value metatypes
)
586 (constant-value-miss generic-function args dfun-info
)))
590 (defun make-final-constant-value-dfun (generic-function classes-list new-class
)
591 (let ((cache (make-final-ordinary-dfun-cache
592 generic-function
:constant-value classes-list new-class
)))
593 (make-constant-value-dfun generic-function cache
)))
595 (defun gf-has-method-with-nonstandard-specializer-p (gf)
596 (let ((methods (generic-function-methods gf
)))
597 (dolist (method methods nil
)
598 (unless (every (lambda (s) (standard-specializer-p s
))
599 (method-specializers method
))
602 (defun use-dispatch-dfun-p (gf &optional
(caching-p (use-caching-dfun-p gf
)))
603 (when (eq **boot-state
** 'complete
)
604 (unless (or caching-p
605 (gf-requires-emf-keyword-checks gf
)
606 ;; DISPATCH-DFUN-COST will error if it encounters a
607 ;; method with a non-standard specializer.
608 (gf-has-method-with-nonstandard-specializer-p gf
))
609 ;; This should return T when almost all dispatching is by
610 ;; eql specializers or built-in classes. In other words,
611 ;; return NIL if we might ever need to do more than
612 ;; one (non built-in) typep.
613 ;; Otherwise, it is probably at least as fast to use
614 ;; a caching dfun first, possibly followed by secondary dispatching.
616 #||;
;; Original found in cmu 17f -- S L O W
617 (< (dispatch-dfun-cost gf
) (caching-dfun-cost gf
))
619 ;; This uses improved dispatch-dfun-cost below
620 (let ((cdc (caching-dfun-cost gf
))) ; fast
621 (> cdc
(dispatch-dfun-cost gf cdc
))))))
623 (defparameter *non-system-typep-cost
* 100)
624 (defparameter *structure-typep-cost
* 15)
625 (defparameter *system-typep-cost
* 5)
627 ;;; According to comments in the original CMU CL version of PCL,
628 ;;; the cost LIMIT is important to cut off exponential growth for
629 ;;; large numbers of gf methods and argument lists.
630 (defun dispatch-dfun-cost (gf &optional limit
)
631 (generate-discrimination-net-internal
632 gf
(generic-function-methods gf
) nil
633 (lambda (methods known-types
)
634 (declare (ignore methods known-types
))
636 (lambda (position type true-value false-value
)
637 (declare (ignore position
))
638 (let* ((type-test-cost
639 (if (eq 'class
(car type
))
640 (let* ((metaclass (class-of (cadr type
)))
641 (mcpl (class-precedence-list metaclass
)))
642 (cond ((memq *the-class-system-class
* mcpl
)
644 ((memq *the-class-structure-class
* mcpl
)
645 *structure-typep-cost
*)
646 (t *non-system-typep-cost
*)))
649 (+ (max true-value false-value
) type-test-cost
)))
650 (when (and limit
(<= limit max-cost-so-far
))
651 (return-from dispatch-dfun-cost max-cost-so-far
))
655 (defparameter *cache-lookup-cost
* 30)
656 (defparameter *wrapper-of-cost
* 15)
657 (defparameter *secondary-dfun-call-cost
* 30)
659 (defun caching-dfun-cost (gf)
660 (let ((nreq (get-generic-fun-info gf
)))
661 (+ *cache-lookup-cost
*
662 (* *wrapper-of-cost
* nreq
)
663 (if (methods-contain-eql-specializer-p
664 (generic-function-methods gf
))
665 *secondary-dfun-call-cost
*
668 (declaim (inline make-callable
))
669 (defun make-callable (generator method-alist wrappers
)
670 (funcall (the function generator
) method-alist wrappers
))
672 (defun make-dispatch-dfun (gf)
673 (values (get-dispatch-function gf
) nil
(dispatch-dfun-info)))
675 (defun get-dispatch-function (gf)
676 (let* ((methods (generic-function-methods gf
))
677 (generator (get-secondary-dispatch-function1
678 gf methods nil nil nil nil nil t
)))
679 (make-callable generator nil nil
)))
681 (defun make-final-dispatch-dfun (gf)
682 (make-dispatch-dfun gf
))
684 (defun update-dispatch-dfuns ()
685 (dolist (gf (gfs-of-type '(dispatch)))
686 (dfun-update gf
#'make-dispatch-dfun
)))
688 (defun make-final-ordinary-dfun-cache
689 (generic-function valuep classes-list new-class
)
690 (let* ((arg-info (gf-arg-info generic-function
))
691 (nkeys (arg-info-nkeys arg-info
))
692 (new-class (and new-class
693 (equal (type-of (gf-dfun-info generic-function
))
694 (cond ((eq valuep t
) 'caching
)
695 ((eq valuep
:constant-value
) 'constant-value
)
696 ((null valuep
) 'checking
)))
699 (copy-cache (gf-dfun-cache generic-function
))
700 (make-cache :key-count nkeys
:value
(not (null valuep
))
702 (make-emf-cache generic-function valuep cache classes-list new-class
)))
704 (defvar *dfun-miss-gfs-on-stack
* ())
706 (defmacro dfun-miss
((gf args wrappers invalidp nemf
707 &optional type index caching-p applicable
)
709 (unless applicable
(setq applicable
(gensym)))
710 `(multiple-value-bind (,nemf
,applicable
,wrappers
,invalidp
711 ,@(when type
`(,type
,index
)))
712 (cache-miss-values ,gf
,args
',(cond (caching-p 'caching
)
715 (when (and ,applicable
(not (memq ,gf
*dfun-miss-gfs-on-stack
*)))
716 (let ((*dfun-miss-gfs-on-stack
* (cons ,gf
*dfun-miss-gfs-on-stack
*)))
719 ;; Munge the EMF so that INVOKE-EMF can do the right thing:
720 ;; BOUNDP and MAKUNBOUND get a structure, WRITER the logical
721 ;; not of the index, so that READER can use the raw index.
723 ;; FIXME: could the NEMF not be a CONS (for :CLASS-allocated
725 `(if (integerp ,nemf
)
728 (invoke-emf (make-fast-instance-boundp :index
(lognot ,nemf
)) ,args
))
729 (boundp (invoke-emf (make-fast-instance-boundp :index
,nemf
) ,args
))
730 (reader (invoke-emf ,nemf
,args
))
731 (writer (invoke-emf (lognot ,nemf
) ,args
)))
732 (invoke-emf ,nemf
,args
))
733 `(invoke-emf ,nemf
,args
))))
735 ;;; The dynamically adaptive method lookup algorithm is implemented is
736 ;;; implemented as a kind of state machine. The kinds of
737 ;;; discriminating function is the state, the various kinds of reasons
738 ;;; for a cache miss are the state transitions.
740 ;;; The code which implements the transitions is all in the miss
741 ;;; handlers for each kind of dfun. Those appear here.
743 ;;; Note that within the states that cache, there are dfun updates
744 ;;; which simply select a new cache or cache field. Those are not
745 ;;; considered as state transitions.
746 (defvar *early-p
* nil
)
748 (defun make-initial-dfun (gf)
749 (let ((initial-dfun #'(lambda (&rest args
) (initial-dfun gf args
))))
750 (multiple-value-bind (dfun cache info
)
751 (if (eq **boot-state
** 'complete
)
752 (values initial-dfun nil
(initial-dfun-info))
753 (let ((arg-info (if (early-gf-p gf
)
754 (early-gf-arg-info gf
)
757 (if (and (gf-precompute-dfun-and-emf-p arg-info
)
758 (setq type
(final-accessor-dfun-type gf
)))
760 (values (make-early-accessor gf type
) nil nil
)
761 (make-final-accessor-dfun gf type
))
762 (values initial-dfun nil
(initial-dfun-info)))))
763 (set-dfun gf dfun cache info
))))
765 (defun make-early-accessor (gf type
)
766 (let* ((methods (early-gf-methods gf
))
767 (slot-name (early-method-standard-accessor-slot-name (car methods
))))
769 (reader #'(lambda (instance)
770 (let* ((class (class-of instance
))
771 (class-name (!bootstrap-get-slot
'class class
'name
)))
772 (!bootstrap-get-slot class-name instance slot-name
))))
773 (boundp #'(lambda (instance)
774 (let* ((class (class-of instance
))
775 (class-name (!bootstrap-get-slot
'class class
'name
)))
776 (not (unbound-marker-p
777 (!bootstrap-get-slot class-name instance slot-name
))))))
778 (writer #'(lambda (new-value instance
)
779 (let* ((class (class-of instance
))
780 (class-name (!bootstrap-get-slot
'class class
'name
)))
781 (!bootstrap-set-slot class-name instance slot-name new-value
))))
782 (makunbound #'(lambda (instance)
783 (let* ((class (class-of instance
))
784 (class-name (!bootstrap-get-slot
'class class
'name
)))
785 (!bootstrap-set-slot class-name instance slot-name
+slot-unbound
+)
788 (defun initial-dfun (gf args
)
789 (dfun-miss (gf args wrappers invalidp nemf ntype nindex
)
793 gf
#'make-one-class-accessor-dfun ntype wrappers nindex
))
794 ((use-caching-dfun-p gf
)
795 (dfun-update gf
#'make-caching-dfun
))
797 (dfun-update gf
#'make-checking-dfun
798 ;; nemf is suitable only for caching, have to do this:
799 (cache-miss-values gf args
'checking
))))))
801 (defun make-final-dfun (gf &optional classes-list
)
802 (multiple-value-bind (dfun cache info
)
803 (make-final-dfun-internal gf classes-list
)
804 (set-dfun gf dfun cache info
)))
806 ;;; FIXME: What is this?
807 (defvar *new-class
* nil
)
809 (defun final-accessor-dfun-type (gf)
810 (let ((methods (if (early-gf-p gf
)
811 (early-gf-methods gf
)
812 (generic-function-methods gf
))))
813 (cond ((every (lambda (method)
815 (let ((class (early-method-class method
)))
816 (or (eq class
*the-class-standard-reader-method
*)
817 (eq class
*the-class-global-reader-method
*)))
818 (or (standard-reader-method-p method
)
819 (global-reader-method-p method
))))
822 ((every (lambda (method)
824 (let ((class (early-method-class method
)))
825 (or (eq class
*the-class-standard-writer-method
*)
826 (eq class
*the-class-global-writer-method
*)))
828 (or (standard-writer-method-p method
)
829 (global-writer-method-p method
))
831 (slot-definition-class
832 (accessor-method-slot-definition method
)))))))
835 ((every (lambda (method)
837 (let ((class (early-method-class method
)))
838 (eq class
*the-class-global-boundp-method
*))
839 (global-boundp-method-p method
)))
842 ((every (lambda (method)
844 (let ((class (early-method-class method
)))
845 (eq class
*the-class-global-makunbound-method
*))
846 (global-makunbound-method-p method
)))
850 (defun make-final-accessor-dfun (gf type
&optional classes-list new-class
)
851 (let ((table (make-hash-table :test
#'eq
)))
852 (multiple-value-bind (table all-index first second size no-class-slots-p
)
853 (make-accessor-table gf type table
)
856 (let ((w (class-wrapper first
)))
857 (make-one-class-accessor-dfun gf type w all-index
)))
858 ((and (= size
2) (or (integerp all-index
) (consp all-index
)))
859 (let ((w0 (class-wrapper first
))
860 (w1 (class-wrapper second
)))
861 (make-two-class-accessor-dfun gf type w0 w1 all-index
)))
862 ((or (integerp all-index
) (consp all-index
))
863 (let ((cache (hash-table-to-cache table
:value nil
:key-count
1)))
864 (make-one-index-accessor-dfun gf type all-index cache
)))
866 (let ((cache (hash-table-to-cache table
:value t
:key-count
1)))
867 (make-n-n-accessor-dfun gf type cache
)))
869 (make-final-caching-dfun gf classes-list new-class
)))
870 (make-final-caching-dfun gf classes-list new-class
)))))
872 (defun make-final-dfun-internal (gf &optional classes-list
)
873 (let ((methods (generic-function-methods gf
)) type
874 (new-class *new-class
*) (*new-class
* nil
)
876 (cond ((null methods
)
878 #'(lambda (&rest args
)
879 (call-no-applicable-method gf args
))
881 (no-methods-dfun-info)))
882 ((setq type
(final-accessor-dfun-type gf
))
883 (make-final-accessor-dfun gf type classes-list new-class
))
884 ((and (not (and (every (lambda (specl) (eq specl
*the-class-t
*))
886 (method-specializers (car methods
))))
888 (every (lambda (method)
893 (use-constant-value-dfun-p gf
))
894 (make-final-constant-value-dfun gf classes-list new-class
))
895 ((use-dispatch-dfun-p gf
)
896 (make-final-dispatch-dfun gf
))
897 ((and all-same-p
(not (use-caching-dfun-p gf
)))
898 (let ((emf (get-secondary-dispatch-function gf methods nil
)))
899 (make-final-checking-dfun gf emf classes-list new-class
)))
901 (make-final-caching-dfun gf classes-list new-class
)))))
903 (defun accessor-miss (gf new object dfun-info
)
904 (let* ((ostate (type-of dfun-info
))
905 (otype (dfun-info-accessor-type dfun-info
))
908 ((reader boundp makunbound
) (list object
))
909 (writer (list new object
)))))
910 (dfun-miss (gf args wrappers invalidp nemf ntype nindex
)
911 ;; The following lexical functions change the state of the
912 ;; dfun to that which is their name. They accept arguments
913 ;; which are the parameters of the new state, and get other
914 ;; information from the lexical variables bound above.
915 (flet ((two-class (index w0 w1
)
916 (when (zerop (random 2 (load-time-value *pcl-misc-random-state
*)))
919 #'make-two-class-accessor-dfun
924 (one-index (index &optional cache
)
926 #'make-one-index-accessor-dfun
930 (n-n (&optional cache
)
932 (dfun-update gf
#'make-checking-dfun nemf
)
933 (dfun-update gf
#'make-n-n-accessor-dfun ntype cache
)))
934 (caching () ; because cached accessor emfs are much faster
936 (dfun-update gf
#'make-caching-dfun
))
938 (let ((ncache (fill-cache cache wrappers nindex
)))
939 (unless (eq ncache cache
)
940 (funcall update-fn ncache
)))))
945 ((not (pcl-instance-p object
))
947 ((or (neq ntype otype
) (listp wrappers
))
952 (setq oindex
(dfun-info-index dfun-info
))
953 (setq ow0
(dfun-info-wrapper0 dfun-info
))
954 (unless (eq ow0 wrappers
)
955 (if (eql nindex oindex
)
956 (two-class nindex ow0 wrappers
)
959 (setq oindex
(dfun-info-index dfun-info
))
960 (setq ow0
(dfun-info-wrapper0 dfun-info
))
961 (setq ow1
(dfun-info-wrapper1 dfun-info
))
962 (unless (or (eq ow0 wrappers
) (eq ow1 wrappers
))
963 (if (eql nindex oindex
)
967 (setq oindex
(dfun-info-index dfun-info
))
968 (setq cache
(dfun-info-cache dfun-info
))
969 (if (eql nindex oindex
)
970 (do-fill (lambda (ncache)
971 (one-index nindex ncache
)))
974 (setq cache
(dfun-info-cache dfun-info
))
977 (do-fill #'n-n
))))))))))
979 (defun checking-miss (generic-function args dfun-info
)
980 (let ((oemf (dfun-info-function dfun-info
))
981 (cache (dfun-info-cache dfun-info
)))
982 (dfun-miss (generic-function args wrappers invalidp nemf
)
985 ;; The cache of a checking dfun doesn't hold any values,
986 ;; so this NIL appears to be just a dummy-value we use in
987 ;; order to insert the wrappers into the cache.
988 (let ((ncache (fill-cache cache wrappers nil
)))
989 (unless (eq ncache cache
)
990 (dfun-update generic-function
#'make-checking-dfun
993 (dfun-update generic-function
#'make-caching-dfun
))))))
995 (defun caching-miss (generic-function args dfun-info
)
996 (let ((ocache (dfun-info-cache dfun-info
)))
997 (dfun-miss (generic-function args wrappers invalidp emf nil nil t
)
1000 (let ((ncache (fill-cache ocache wrappers emf
)))
1001 (unless (eq ncache ocache
)
1002 (dfun-update generic-function
1003 #'make-caching-dfun ncache
))))))))
1005 (defun constant-value-miss (generic-function args dfun-info
)
1006 (let ((ocache (dfun-info-cache dfun-info
)))
1007 (dfun-miss (generic-function args wrappers invalidp emf nil nil t
)
1011 (constant-fast-method-call
1012 (constant-fast-method-call-value emf
))
1013 (constant-method-call
1014 (constant-method-call-value emf
))
1016 (bug "~S with non-constant EMF ~S" 'constant-value-miss emf
))))
1017 (ncache (fill-cache ocache wrappers value
)))
1018 (unless (eq ncache ocache
)
1019 (dfun-update generic-function
1020 #'make-constant-value-dfun ncache
)))))))
1022 ;;; Given a generic function and a set of arguments to that generic
1023 ;;; function, return a mess of values.
1025 ;;; <function> The compiled effective method function for this set of
1028 ;;; <applicable> Sorted list of applicable methods.
1030 ;;; <wrappers> Is a single wrapper if the generic function has only
1031 ;;; one key, that is arg-info-nkeys of the arg-info is 1.
1032 ;;; Otherwise a list of the wrappers of the specialized
1033 ;;; arguments to the generic function.
1035 ;;; Note that all these wrappers are valid. This function
1036 ;;; does invalid wrapper traps when it finds an invalid
1037 ;;; wrapper and then returns the new, valid wrapper.
1039 ;;; <invalidp> True if any of the specialized arguments had an invalid
1040 ;;; wrapper, false otherwise.
1042 ;;; <type> READER or WRITER when the only method that would be run
1043 ;;; is a standard reader or writer method. To be specific,
1044 ;;; the value is READER when the method combination is eq to
1045 ;;; *standard-method-combination*; there are no applicable
1046 ;;; :before, :after or :around methods; and the most specific
1047 ;;; primary method is a standard reader method.
1049 ;;; <index> If <type> is READER or WRITER, and the slot accessed is
1050 ;;; an :instance slot, this is the index number of that slot
1051 ;;; in the object argument.
1052 (defvar *cache-miss-values-stack
* ())
1054 (defun cache-miss-values (gf args state
)
1055 (multiple-value-bind (nreq applyp metatypes nkeys arg-info
)
1056 (get-generic-fun-info gf
)
1057 (declare (ignore nreq applyp nkeys
))
1058 (with-dfun-wrappers (args metatypes
)
1059 (dfun-wrappers invalid-wrapper-p wrappers classes types
)
1060 (error-need-at-least-n-args gf
(length metatypes
))
1061 (multiple-value-bind (emf methods accessor-type index
)
1062 (cache-miss-values-internal
1063 gf arg-info wrappers classes types state
)
1067 accessor-type index
)))))
1069 (defun cache-miss-values-internal (gf arg-info wrappers classes types state
)
1070 (if (and classes
(equal classes
(cdr (assq gf
*cache-miss-values-stack
*))))
1071 (break-vicious-metacircle gf classes arg-info
)
1072 (let ((*cache-miss-values-stack
*
1073 (acons gf classes
*cache-miss-values-stack
*))
1074 (cam-std-p (or (null arg-info
)
1075 (gf-info-c-a-m-emf-std-p arg-info
))))
1076 (multiple-value-bind (methods all-applicable-and-sorted-p
)
1078 (compute-applicable-methods-using-types gf types
)
1079 (compute-applicable-methods-using-classes gf classes
))
1081 (let* ((for-accessor-p (eq state
'accessor
))
1082 (for-cache-p (or (eq state
'caching
) (eq state
'accessor
)))
1083 (emf (if (or cam-std-p all-applicable-and-sorted-p
)
1085 (get-secondary-dispatch-function1
1086 gf methods types nil
(and for-cache-p wrappers
)
1087 all-applicable-and-sorted-p
)))
1088 (make-callable generator nil
(and for-cache-p wrappers
)))
1089 (default-secondary-dispatch-function gf
))))
1090 (multiple-value-bind (index accessor-type
)
1091 (and for-accessor-p all-applicable-and-sorted-p methods
1092 (accessor-values gf arg-info classes methods
))
1093 (values (if (integerp index
) index emf
)
1094 methods accessor-type index
)))))))
1096 ;;; Try to break a vicious circle while computing a cache miss.
1097 ;;; GF is the generic function, CLASSES are the classes of actual
1098 ;;; arguments, and ARG-INFO is the generic functions' arg-info.
1100 ;;; A vicious circle can be entered when the computation of the cache
1101 ;;; miss values itself depends on the values being computed. For
1102 ;;; instance, adding a method which is an instance of a subclass of
1103 ;;; STANDARD-METHOD leads to cache misses for slot accessors of
1104 ;;; STANDARD-METHOD like METHOD-SPECIALIZERS, and METHOD-SPECIALIZERS
1105 ;;; is itself used while we compute cache miss values.
1106 (defun break-vicious-metacircle (gf classes arg-info
)
1107 (when (typep gf
'standard-generic-function
)
1108 (multiple-value-bind (class slotd accessor-type
)
1109 (accesses-standard-class-slot-p gf
)
1111 (let ((method (find-standard-class-accessor-method
1112 gf class accessor-type
))
1113 (index (standard-slot-value/eslotd slotd
'location
))
1114 (type (gf-info-simple-accessor-type arg-info
)))
1116 (let ((method-class (ecase accessor-type
1117 ((reader) (car classes
))
1118 ((writer) (cadr classes
)))))
1119 (or (eq method-class class
)
1120 ;; SUBTYPEP doesn't work because it calls the CLASS-WRAPPER GF.
1122 (sb-kernel::do-subclassoids
((subclassoid layout
)
1123 (layout-classoid (standard-slot-value/class class
'wrapper
)))
1124 (declare (ignore layout
))
1125 (when (eq method-class
(classoid-pcl-class subclassoid
))
1127 (return-from break-vicious-metacircle
1128 (values index
(list method
) type index
)))))))
1129 (error "~@<vicious metacircle: The computation of an ~
1130 effective method of ~s for arguments of types ~s uses ~
1131 the effective method being computed.~@:>"
1134 ;;; Return (CLASS SLOTD ACCESSOR-TYPE) if some method of generic
1135 ;;; function GF accesses a slot of some class in +STANDARD-CLASSES+.
1136 ;;; CLASS is the class accessed, SLOTD is the effective slot definition
1137 ;;; object of the slot accessed, and ACCESSOR-TYPE is one of the symbols
1138 ;;; READER or WRITER describing the slot access.
1139 (defun accesses-standard-class-slot-p (gf)
1141 ((all-dslotds (class &aux done
)
1142 (labels ((all-dslotds-aux (class)
1143 (if (or (member class done
) (not (eq (class-of class
) *the-class-standard-class
*)))
1147 (append (standard-slot-value/class class
'direct-slots
)
1148 (mapcan #'(lambda (c)
1149 (copy-list (all-dslotds-aux c
)))
1150 (standard-slot-value/class class
'direct-superclasses
)))))))
1151 (all-dslotds-aux class
)))
1152 (standard-class-slot-access (gf class
)
1154 (loop with gf-name
= (standard-slot-value/gf gf
'name
)
1155 with eslotds
= (standard-slot-value/class class
'slots
)
1156 with dslotds
= (all-dslotds class
)
1157 for dslotd in dslotds
1158 as readers
= (standard-slot-value/dslotd dslotd
'readers
)
1159 as writers
= (standard-slot-value/dslotd dslotd
'writers
)
1160 as name
= (standard-slot-value/dslotd dslotd
'name
)
1161 as eslotd
= (find name eslotds
:key
(lambda (x) (standard-slot-value/eslotd x
'name
)))
1162 if
(member gf-name readers
:test
#'equal
)
1163 return
(values eslotd
'reader
)
1164 else if
(member gf-name writers
:test
#'equal
)
1165 return
(values eslotd
'writer
))))
1166 (dolist (class-name +standard-classes
+)
1167 (let ((class (find-class class-name
)))
1168 (multiple-value-bind (slotd accessor-type
)
1169 (standard-class-slot-access gf class
)
1171 (return (values class slotd accessor-type
))))))))
1173 ;;; Find a slot reader/writer method among the methods of generic
1174 ;;; function GF which reads/writes instances of class CLASS.
1175 ;;; TYPE is one of the symbols READER or WRITER.
1176 (defun find-standard-class-accessor-method (gf class type
)
1177 (let ((cpl (standard-slot-value/class class
'%class-precedence-list
))
1178 (found-specializer *the-class-t
*)
1180 (dolist (method (standard-slot-value/gf gf
'methods
) found-method
)
1181 (let ((specializers (standard-slot-value/method method
'specializers
))
1182 (qualifiers (standard-slot-value/method method
'qualifiers
)))
1183 (when (and (null qualifiers
)
1184 (let ((subcpl (member (ecase type
1185 (reader (car specializers
))
1186 (writer (cadr specializers
)))
1188 (and subcpl
(member found-specializer subcpl
:test
#'eq
))))
1189 (setf found-specializer
(ecase type
1190 (reader (car specializers
))
1191 (writer (cadr specializers
))))
1192 (setf found-method method
))))))
1194 (defun accessor-values (gf arg-info classes methods
)
1195 (declare (ignore gf
))
1196 (let* ((accessor-type (gf-info-simple-accessor-type arg-info
))
1197 (accessor-class (case accessor-type
1198 ((reader boundp makunbound
) (car classes
))
1199 (writer (cadr classes
)))))
1200 (accessor-values-internal accessor-type accessor-class methods
)))
1202 (defun accessor-values1 (gf accessor-type accessor-class
)
1203 (let* ((type `(class-eq ,accessor-class
))
1204 (types (ecase accessor-type
1205 ((reader boundp makunbound
) `(,type
))
1206 (writer `(t ,type
))))
1207 (methods (compute-applicable-methods-using-types gf types
)))
1208 (accessor-values-internal accessor-type accessor-class methods
)))
1210 (defun accessor-values-internal (accessor-type accessor-class methods
)
1211 (unless accessor-class
1212 (return-from accessor-values-internal
(values nil nil
)))
1213 (dolist (meth methods
)
1214 (when (if (consp meth
)
1215 (early-method-qualifiers meth
)
1216 (safe-method-qualifiers meth
))
1217 (return-from accessor-values-internal
(values nil nil
))))
1218 (let* ((meth (car methods
))
1219 (early-p (not (eq **boot-state
** 'complete
)))
1223 (early-method-standard-accessor-p meth
))
1224 (early-method-standard-accessor-slot-name meth
))
1225 ((and (accessor-method-p meth
)
1226 (member *the-class-standard-object
*
1228 (early-class-precedence-list accessor-class
)
1229 (class-precedence-list accessor-class
))))
1230 (accessor-method-slot-name meth
))
1231 (t (return-from accessor-values-internal
(values nil nil
)))))
1233 (dolist (slot (early-class-slotds accessor-class
) nil
)
1234 (when (eql slot-name
(early-slot-definition-name slot
))
1236 (find-slot-definition accessor-class slot-name
))))
1238 (or early-p
(slot-accessor-std-p slotd accessor-type
))
1239 (or early-p
(not (safe-p accessor-class
))))
1241 (early-slot-definition-location slotd
)
1242 (slot-definition-location slotd
))
1245 (defun make-accessor-table (gf type
&optional table
)
1246 (unless table
(setq table
(make-hash-table :test
'eq
)))
1247 (let ((methods (if (early-gf-p gf
)
1248 (early-gf-methods gf
)
1249 (generic-function-methods gf
)))
1251 (no-class-slots-p t
)
1252 (early-p (not (eq **boot-state
** 'complete
)))
1253 first second
(size 0))
1254 (declare (fixnum size
))
1255 ;; class -> {(specl slotd)}
1256 (dolist (method methods
)
1257 (let* ((specializers (if (consp method
)
1258 (early-method-specializers method t
)
1259 (method-specializers method
)))
1261 ((reader boundp makunbound
) (car specializers
))
1262 (writer (cadr specializers
))))
1263 (specl-cpl (if early-p
1264 (early-class-precedence-list specl
)
1265 (when (class-finalized-p specl
)
1266 (class-precedence-list specl
))))
1267 (so-p (member *the-class-standard-object
* specl-cpl
:test
#'eq
))
1268 (slot-name (if (consp method
)
1269 (and (early-method-standard-accessor-p method
)
1270 (early-method-standard-accessor-slot-name
1272 (accessor-method-slot-name method
))))
1273 (when (or (null specl-cpl
)
1275 (member *the-class-structure-object
* specl-cpl
:test
#'eq
))
1276 (return-from make-accessor-table nil
))
1277 ;; Collect all the slot-definitions for SLOT-NAME from SPECL and
1278 ;; all of its subclasses. If either SPECL or one of the subclasses
1279 ;; is not a standard-class, bail out.
1280 (labels ((aux (class)
1281 (let ((slotd (find-slot-definition class slot-name
)))
1283 (unless (or early-p
(slot-accessor-std-p slotd type
))
1284 (return-from make-accessor-table nil
))
1285 (push (cons specl slotd
) (gethash class table
))))
1286 (dolist (subclass (sb-pcl::class-direct-subclasses class
))
1287 (unless (class-finalized-p subclass
)
1288 (return-from make-accessor-table nil
))
1291 (maphash (lambda (class specl
+slotd-list
)
1292 (dolist (sclass (if early-p
1293 (early-class-precedence-list class
)
1294 (class-precedence-list class
))
1295 (error "This can't happen."))
1296 (let ((a (assq sclass specl
+slotd-list
)))
1298 (let* ((slotd (cdr a
))
1300 (early-slot-definition-location slotd
)
1301 (slot-definition-location slotd
))))
1302 (unless index
(return-from make-accessor-table nil
))
1303 (setf (gethash class table
) index
)
1304 (when (consp index
) (setq no-class-slots-p nil
))
1305 (setq all-index
(if (or (null all-index
)
1306 (eql all-index index
))
1309 (cond ((= size
1) (setq first class
))
1310 ((= size
2) (setq second class
)))
1313 (values table all-index first second size no-class-slots-p
)))
1315 (defun compute-applicable-methods-using-types (generic-function types
)
1316 (let ((definite-p t
) (possibly-applicable-methods nil
))
1317 (dolist (method (if (early-gf-p generic-function
)
1318 (early-gf-methods generic-function
)
1319 (safe-generic-function-methods generic-function
)))
1320 (let ((specls (if (consp method
)
1321 (early-method-specializers method t
)
1322 (safe-method-specializers method
)))
1324 (possibly-applicable-p t
) (applicable-p t
))
1325 (dolist (specl specls
)
1326 (multiple-value-bind (specl-applicable-p specl-possibly-applicable-p
)
1327 (specializer-applicable-using-type-p specl
(pop types
))
1328 (unless specl-applicable-p
1329 (setq applicable-p nil
))
1330 (unless specl-possibly-applicable-p
1331 (setq possibly-applicable-p nil
)
1333 (when possibly-applicable-p
1334 (unless applicable-p
(setq definite-p nil
))
1335 (push method possibly-applicable-methods
))))
1336 (multiple-value-bind (nreq applyp metatypes nkeys arg-info
)
1337 (get-generic-fun-info generic-function
)
1338 (declare (ignore nreq applyp metatypes nkeys
))
1339 (let* ((precedence (arg-info-precedence arg-info
)))
1340 (values (sort-applicable-methods precedence
1341 (nreverse possibly-applicable-methods
)
1345 (defun sort-applicable-methods (precedence methods types
)
1346 (sort-methods methods
1348 (lambda (class1 class2 index
)
1349 (let* ((class (type-class (nth index types
)))
1350 (cpl (if (eq **boot-state
** 'complete
)
1351 (class-precedence-list class
)
1352 (early-class-precedence-list class
))))
1353 (if (memq class2
(memq class1 cpl
))
1356 (defun sort-methods (methods precedence compare-classes-function
)
1357 (flet ((sorter (method1 method2
)
1358 (dolist (index precedence
)
1359 (let* ((specl1 (nth index
(if (listp method1
)
1360 (early-method-specializers method1
1362 (method-specializers method1
))))
1363 (specl2 (nth index
(if (listp method2
)
1364 (early-method-specializers method2
1366 (method-specializers method2
))))
1367 (order (order-specializers
1368 specl1 specl2 index compare-classes-function
)))
1370 (return-from sorter
(eq order specl1
)))))))
1371 (stable-sort methods
#'sorter
)))
1373 (defun order-specializers (specl1 specl2 index compare-classes-function
)
1374 (let ((type1 (if (eq **boot-state
** 'complete
)
1375 (specializer-type specl1
)
1376 (!bootstrap-get-slot
'specializer specl1
'%type
)))
1377 (type2 (if (eq **boot-state
** 'complete
)
1378 (specializer-type specl2
)
1379 (!bootstrap-get-slot
'specializer specl2
'%type
))))
1380 (cond ((eq specl1 specl2
)
1388 (class (case (car type2
)
1389 (class (funcall compare-classes-function
1390 specl1 specl2 index
))
1392 (prototype (case (car type2
)
1393 (class (funcall compare-classes-function
1394 specl1 specl2 index
))
1396 (class-eq (case (car type2
)
1398 ;; FIXME: This says that all CLASS-EQ
1399 ;; specializers are equally specific, which
1400 ;; is fair enough because only one CLASS-EQ
1401 ;; specializer can ever be appliable. If
1402 ;; ORDER-SPECIALIZERS should only ever be
1403 ;; called on specializers from applicable
1404 ;; methods, we could replace this with a BUG.
1407 (eql (case (car type2
)
1412 (defun map-all-orders (methods precedence function
)
1413 (let ((choices nil
))
1414 (flet ((compare-classes-function (class1 class2 index
)
1415 (declare (ignore index
))
1417 (dolist (c choices nil
)
1418 (when (or (and (eq (first c
) class1
)
1419 (eq (second c
) class2
))
1420 (and (eq (first c
) class2
)
1421 (eq (second c
) class1
)))
1422 (return (setq choice c
))))
1425 (if (class-might-precede-p class1 class2
)
1426 (if (class-might-precede-p class2 class1
)
1427 (list class1 class2 nil t
)
1428 (list class1 class2 t
))
1429 (if (class-might-precede-p class2 class1
)
1430 (list class2 class1 t
)
1431 (let ((name1 (class-name class1
))
1432 (name2 (class-name class2
)))
1437 (string< (symbol-name name1
)
1438 (symbol-name name2
)))
1439 (list class1 class2 t
)
1440 (list class2 class1 t
))))))
1441 (push choice choices
))
1443 (loop (funcall function
1444 (sort-methods methods
1446 #'compare-classes-function
))
1447 (unless (dolist (c choices nil
)
1449 (rotatef (car c
) (cadr c
))
1450 (return (setf (third c
) t
))))
1453 ;;; CMUCL comment: used only in map-all-orders
1454 (defun class-might-precede-p (class1 class2
)
1455 (not (member class1
(cdr (class-precedence-list class2
)) :test
#'eq
)))
1457 (defun compute-precedence (lambda-list nreq argument-precedence-order
)
1458 (if (null argument-precedence-order
)
1460 (dotimes-fixnum (i nreq list
) (push (- (1- nreq
) i
) list
)))
1461 (mapcar (lambda (x) (position x lambda-list
))
1462 argument-precedence-order
)))
1464 (defun cpl-or-nil (class)
1465 (if (eq **boot-state
** 'complete
)
1467 ;; KLUDGE: why not use (slot-boundp class
1468 ;; 'class-precedence-list)? Well, unfortunately, CPL-OR-NIL is
1469 ;; used within COMPUTE-APPLICABLE-METHODS, including for
1470 ;; SLOT-BOUNDP-USING-CLASS... and the available mechanism for
1471 ;; breaking such nasty cycles in effective method computation
1472 ;; only works for readers and writers, not boundps. It might
1473 ;; not be too hard to make it work for BOUNDP accessors, but in
1474 ;; the meantime we use an extra slot for exactly the result of
1475 ;; the SLOT-BOUNDP that we want. (We cannot use
1476 ;; CLASS-FINALIZED-P, because in the process of class
1477 ;; finalization we need to use the CPL which has been computed
1478 ;; to cache effective methods for slot accessors.) -- CSR,
1481 (when (cpl-available-p class
)
1482 (return-from cpl-or-nil
(class-precedence-list class
)))
1484 ;; if we can finalize an unfinalized class, then do so
1485 (when (and (not (class-finalized-p class
))
1486 (not (class-has-a-forward-referenced-superclass-p class
))
1487 (not (class-has-a-cpl-protocol-violation-p class
)))
1488 (finalize-inheritance class
)
1489 (class-precedence-list class
)))
1491 (early-class-precedence-list class
)))
1493 (defun saut-and (specl type
)
1494 (let ((applicable nil
)
1495 (possibly-applicable t
))
1496 (dolist (type (cdr type
))
1497 (multiple-value-bind (appl poss-appl
)
1498 (specializer-applicable-using-type-p specl type
)
1499 (when appl
(return (setq applicable t
)))
1500 (unless poss-appl
(return (setq possibly-applicable nil
)))))
1501 (values applicable possibly-applicable
)))
1503 (defun saut-not (specl type
)
1504 (let ((ntype (cadr type
)))
1507 (class (saut-not-class specl ntype
))
1508 (class-eq (saut-not-class-eq specl ntype
))
1509 (prototype (saut-not-prototype specl ntype
))
1510 (eql (saut-not-eql specl ntype
))
1511 (t (error "~S cannot handle the second argument ~S"
1512 'specializer-applicable-using-type-p type
))))))
1514 (defun saut-not-class (specl ntype
)
1515 (let* ((class (type-class specl
))
1516 (cpl (cpl-or-nil class
)))
1517 (not (memq (cadr ntype
) cpl
))))
1519 (defun saut-not-prototype (specl ntype
)
1520 (let* ((class (case (car specl
)
1521 (eql (class-of (cadr specl
)))
1522 (class-eq (cadr specl
))
1523 (prototype (cadr specl
))
1524 (class (cadr specl
))))
1525 (cpl (cpl-or-nil class
)))
1526 (not (memq (cadr ntype
) cpl
))))
1528 (defun saut-not-class-eq (specl ntype
)
1529 (let ((class (case (car specl
)
1530 (eql (class-of (cadr specl
)))
1531 (class-eq (cadr specl
)))))
1532 (not (eq class
(cadr ntype
)))))
1534 (defun saut-not-eql (specl ntype
)
1536 (eql (not (eql (cadr specl
) (cadr ntype
))))
1539 (defun class-applicable-using-class-p (specl type
)
1540 (let ((pred (memq specl
(cpl-or-nil type
))))
1543 (if (not *in-
*subtypep
*)
1544 ;; classes might get common subclass
1545 (superclasses-compatible-p specl type
)
1546 ;; worry only about existing classes
1547 (classes-have-common-subclass-p specl type
))))))
1549 (defun classes-have-common-subclass-p (class1 class2
)
1550 (or (eq class1 class2
)
1551 (let ((class1-subs (class-direct-subclasses class1
)))
1552 (or (memq class2 class1-subs
)
1553 (dolist (class1-sub class1-subs nil
)
1554 (when (classes-have-common-subclass-p class1-sub class2
)
1557 (defun saut-class (specl type
)
1559 (class (class-applicable-using-class-p (cadr specl
) (cadr type
)))
1560 (t (values nil
(let ((class (type-class specl
)))
1562 (cpl-or-nil class
)))))))
1564 (defun saut-class-eq (specl type
)
1565 (if (eq (car specl
) 'eql
)
1566 (values nil
(eq (class-of (cadr specl
)) (cadr type
)))
1567 (let ((pred (case (car specl
)
1569 (eq (cadr specl
) (cadr type
)))
1571 (or (eq (cadr specl
) (cadr type
))
1572 (memq (cadr specl
) (cpl-or-nil (cadr type
))))))))
1573 (values pred pred
))))
1575 (defun saut-prototype (specl type
)
1576 (declare (ignore specl type
))
1577 (values nil nil
)) ; XXX original PCL comment: fix this someday
1579 (defun saut-eql (specl type
)
1580 (let ((pred (case (car specl
)
1581 (eql (eql (cadr specl
) (cadr type
)))
1582 (class-eq (eq (cadr specl
) (class-of (cadr type
))))
1583 (class (memq (cadr specl
)
1584 (let ((class (class-of (cadr type
))))
1585 (cpl-or-nil class
)))))))
1586 (values pred pred
)))
1588 (defun specializer-applicable-using-type-p (specl type
)
1589 (setq specl
(type-from-specializer specl
))
1591 (return-from specializer-applicable-using-type-p
(values t t
)))
1592 ;; This is used by C-A-M-U-T and GENERATE-DISCRIMINATION-NET-INTERNAL,
1593 ;; and has only what they need.
1594 (if (or (atom type
) (eq (car type
) t
))
1597 (and (saut-and specl type
))
1598 (not (saut-not specl type
))
1599 (class (saut-class specl type
))
1600 (prototype (saut-prototype specl type
))
1601 (class-eq (saut-class-eq specl type
))
1602 (eql (saut-eql specl type
))
1603 (t (error "~S cannot handle the second argument ~S."
1604 'specializer-applicable-using-type-p
1607 (defun map-all-classes (fun &optional
(root t
))
1608 (let ((all-classes (make-hash-table :test
'eq
))
1609 (braid-p (or (eq **boot-state
** 'braid
)
1610 (eq **boot-state
** 'complete
))))
1611 (labels ((do-class (class)
1612 (unless (gethash class all-classes
)
1613 (setf (gethash class all-classes
) t
)
1617 (class-direct-subclasses class
)
1618 (early-class-direct-subclasses class
))))))
1619 (do-class (if (symbolp root
)
1624 (defun flush-effective-method-cache (generic-function)
1625 (dolist (method (generic-function-methods generic-function
))
1627 (if (listp method
) (sixth method
) (method-em-cache method
))))
1630 (rplacd cache nil
)))))
1632 (defun get-secondary-dispatch-function (gf methods types
1633 &optional method-alist wrappers
)
1635 (get-secondary-dispatch-function1
1636 gf methods types
(not (null method-alist
)) (not (null wrappers
))
1637 (not (methods-contain-eql-specializer-p methods
)))))
1638 (make-callable generator method-alist wrappers
)))
1640 (defun get-secondary-dispatch-function1 (gf methods types method-alist-p
1647 (lambda (method-alist wrappers
)
1648 (declare (ignore method-alist wrappers
))
1649 (lambda (&rest args
)
1650 (call-no-applicable-method gf args
)))
1651 (let* ((key (car methods
))
1653 (if (listp key
) ; early method
1654 (sixth key
) ; See !EARLY-MAKE-A-METHOD
1655 (or (method-em-cache key
)
1656 (setf (method-em-cache key
) (cons nil nil
))))))
1657 (if (and (null (cdr methods
)) all-applicable-p
; the most common case
1658 (null method-alist-p
) wrappers-p
(not function-p
))
1661 (get-secondary-dispatch-function2
1662 gf methods types method-alist-p wrappers-p
1663 all-applicable-p all-sorted-p function-p
)))
1664 (let ((akey (list methods
1665 (if all-applicable-p
'all-applicable types
)
1666 method-alist-p wrappers-p function-p
)))
1667 (or (cdr (assoc akey
(cdr cache
) :test
#'equal
))
1668 (let ((value (get-secondary-dispatch-function2
1669 gf methods types method-alist-p wrappers-p
1670 all-applicable-p all-sorted-p function-p
)))
1671 (push (cons akey value
) (cdr cache
))
1674 (defun get-secondary-dispatch-function2 (gf methods types method-alist-p
1675 wrappers-p all-applicable-p
1676 all-sorted-p function-p
)
1677 (flet ((maybe-wrap (effective)
1678 (if (gf-requires-emf-keyword-checks gf
)
1679 (multiple-value-bind (valid-keys keyargs-start
)
1680 (compute-applicable-keywords gf methods
)
1681 (wrap-with-applicable-keyword-check effective valid-keys keyargs-start
))
1684 ((not (and all-applicable-p all-sorted-p
(not function-p
)))
1685 (let ((net (generate-discrimination-net
1686 gf methods types all-sorted-p
)))
1687 (compute-secondary-dispatch-function1 gf net function-p
)))
1688 ((eq **boot-state
** 'complete
)
1689 (let ((combin (generic-function-method-combination gf
)))
1690 (if (null (compute-primary-methods gf combin methods
))
1691 (lambda (method-alist wrappers
)
1692 (declare (ignore method-alist wrappers
))
1693 (lambda (&rest args
)
1694 (call-no-primary-method gf args
)))
1695 (let ((effective (maybe-wrap (compute-effective-method gf combin methods
))))
1696 (make-effective-method-function1
1697 gf effective method-alist-p wrappers-p
)))))
1698 ((eq (generic-function-name gf
) 'make-specializer-form-using-class
)
1699 ;; FIXME: instead of the above form, this should be
1700 ;; (eq (generic-function-method-combination gf) *or-method-combination*)
1701 ;; but that does not work for reasons I (JM) do not understand.
1702 (let* ((combin (generic-function-method-combination gf
))
1703 (effective (maybe-wrap (short-compute-effective-method gf combin methods
))))
1704 (make-effective-method-function1
1705 gf effective method-alist-p wrappers-p
)))
1707 (let ((effective (maybe-wrap (standard-compute-effective-method gf nil methods
))))
1708 (make-effective-method-function1
1709 gf effective method-alist-p wrappers-p
))))))
1711 (defun get-effective-method-function (gf methods
1712 &optional method-alist wrappers
)
1714 (get-secondary-dispatch-function1
1715 gf methods nil
(not (null method-alist
)) (not (null wrappers
)) t
)))
1716 (make-callable generator method-alist wrappers
)))
1718 (defun get-effective-method-function1 (gf methods
&optional
(sorted-p t
))
1719 (get-secondary-dispatch-function1 gf methods nil nil nil t sorted-p
))
1721 (defun methods-contain-eql-specializer-p (methods)
1722 (and (eq **boot-state
** 'complete
)
1723 (dolist (method methods nil
)
1724 (when (dolist (spec (method-specializers method
) nil
)
1725 (when (eql-specializer-p spec
) (return t
)))
1728 (defun update-dfun (generic-function &optional dfun cache info
)
1729 (let ((early-p (early-gf-p generic-function
)))
1731 ;; If GENERIC-FUNCTION has a CALL-NEXT-METHOD argument
1732 ;; checker, the methods of the checker (the checker is a
1733 ;; generic function, each method caches a computation for
1734 ;; a combination of original and C-N-M argument classes)
1735 ;; must be re-computed.
1736 (when (eq **boot-state
** 'complete
)
1737 (let ((checker (gf-info-cnm-checker (gf-arg-info generic-function
))))
1739 (remove-methods checker
))))
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
)
1748 ;; This needs to be atomic per generic function, consider:
1749 ;; 1. T1 sets dfun-state to S1 and computes discr. fun using S1
1750 ;; 2. T2 sets dfun-state to S2 and computes discr. fun using S2
1753 ;; Oops: now dfun-state and fin don't match! Since just calling
1754 ;; a generic can cause the dispatch function to be updated we
1755 ;; need a lock here.
1757 ;; We need to accept recursion, because PCL is nasty and twisty,
1758 ;; and we need to disable interrupts because it would be bad if
1759 ;; we updated the DFUN-STATE but not the dispatch function.
1761 ;; This is sufficient, because all the other calls to SET-DFUN
1762 ;; are part of this same code path (done while the lock is held),
1765 ;; KLUDGE: No need to lock during bootstrap.
1768 (let ((lock (gf-lock generic-function
)))
1769 ;; FIXME: GF-LOCK is a generic function... Are there cases
1770 ;; where we can end up in a metacircular loop here? In
1771 ;; case there are, better fetch it while interrupts are
1773 (sb-thread::call-with-recursive-system-lock
#'update lock
))))))
1775 ;;; These functions aren't used in SBCL, or documented anywhere that
1776 ;;; I'm aware of, but they look like they might be useful for
1777 ;;; debugging or performance tweaking or something, so I've just
1778 ;;; commented them out instead of deleting them. -- WHN 2001-03-28
1780 (defvar *dfun-count
* nil
)
1781 (defvar *dfun-list
* nil
)
1782 (defvar *minimum-cache-size-to-list
*)
1784 (defun list-dfun (gf)
1785 (let* ((sym (type-of (gf-dfun-info gf
)))
1786 (a (assq sym
*dfun-list
*)))
1788 (push (setq a
(list sym
)) *dfun-list
*))
1789 (push (generic-function-name gf
) (cdr a
))))
1791 (defun list-all-dfuns ()
1792 (setq *dfun-list
* nil
)
1793 (map-all-generic-functions #'list-dfun
)
1796 (defun list-large-cache (gf)
1797 (let* ((sym (type-of (gf-dfun-info gf
)))
1798 (cache (gf-dfun-cache gf
)))
1800 (let ((size (cache-size cache
)))
1801 (when (>= size
*minimum-cache-size-to-list
*)
1802 (let ((a (assoc size
*dfun-list
*)))
1804 (push (setq a
(list size
)) *dfun-list
*))
1805 (push (let ((name (generic-function-name gf
)))
1806 (if (eq sym
'caching
) name
(list name sym
)))
1809 (defun list-large-caches (&optional
(*minimum-cache-size-to-list
* 130))
1810 (setq *dfun-list
* nil
)
1811 (map-all-generic-functions #'list-large-cache
)
1812 (setq *dfun-list
* (sort *dfun-list
* #'< :key
#'car
))
1813 (mapc #'print
*dfun-list
*)
1816 (defun count-dfun (gf)
1817 (let* ((sym (type-of (gf-dfun-info gf
)))
1818 (cache (gf-dfun-cache gf
))
1819 (a (assq sym
*dfun-count
*)))
1821 (push (setq a
(list sym
0 nil
)) *dfun-count
*))
1824 (let* ((size (cache-size cache
))
1825 (b (assoc size
(third a
))))
1827 (push (setq b
(cons size
0)) (third a
)))
1830 (defun count-all-dfuns ()
1831 (setq *dfun-count
* (mapcar (lambda (type) (list type
0 nil
))
1832 '(ONE-CLASS TWO-CLASS DEFAULT-METHOD-ONLY
1833 ONE-INDEX N-N CHECKING CACHING
1835 (map-all-generic-functions #'count-dfun
)
1836 (mapc (lambda (type+count
+sizes
)
1837 (setf (third type
+count
+sizes
)
1838 (sort (third type
+count
+sizes
) #'< :key
#'car
)))
1840 (mapc (lambda (type+count
+sizes
)
1841 (format t
"~&There are ~W dfuns of type ~S."
1842 (cadr type
+count
+sizes
) (car type
+count
+sizes
))
1843 (format t
"~% ~S~%" (caddr type
+count
+sizes
)))
1848 (defun gfs-of-type (type)
1849 (unless (consp type
) (setq type
(list type
)))
1850 (let ((gf-list nil
))
1851 (map-all-generic-functions (lambda (gf)
1852 (when (memq (type-of (gf-dfun-info gf
))
1854 (push gf gf-list
))))