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.
80 ;;; an alist in which each entry is of the form
81 ;;; (<generator> . (<subentry> ...)).
82 ;;; Each subentry is of the form
83 ;;; (<args> <constructor> <system>).
84 (defvar *dfun-constructors* ())
86 ;;; If this is NIL, then the whole mechanism for caching dfun constructors is
87 ;;; turned off. The only time that makes sense is when debugging LAP code.
88 (defvar *enable-dfun-constructor-caching* t)
90 (defun show-dfun-constructors ()
91 (format t "~&DFUN constructor caching is ~A."
92 (if *enable-dfun-constructor-caching*
93 "enabled" "disabled"))
94 (dolist (generator-entry *dfun-constructors*)
95 (dolist (args-entry (cdr generator-entry))
97 (cons (car generator-entry) (caar args-entry))
98 (caddr args-entry)))))
100 (defvar *raise-metatypes-to-class-p* t)
102 (defun get-dfun-constructor (generator &rest args)
103 (when (and *raise-metatypes-to-class-p*
104 (member generator '(emit-checking emit-caching
105 emit-in-checking-cache-p emit-constant-value)))
106 (setq args (cons (mapcar (lambda (mt)
112 (let* ((generator-entry (assq generator *dfun-constructors*))
113 (args-entry (assoc args (cdr generator-entry) :test #'equal)))
114 (if (null *enable-dfun-constructor-caching*)
115 (apply (fdefinition generator) args)
116 (or (cadr args-entry)
117 (multiple-value-bind (new not-best-p)
118 (apply (symbol-function generator) args)
119 (let ((entry (list (copy-list args) new (unless not-best-p 'pcl)
122 (push entry (cdr generator-entry))
123 (push (list generator entry)
124 *dfun-constructors*)))
125 (values new not-best-p))))))
127 (defun load-precompiled-dfun-constructor (generator args system constructor)
128 (let* ((generator-entry (assq generator *dfun-constructors*))
129 (args-entry (assoc args (cdr generator-entry) :test #'equal)))
131 (when (fourth args-entry)
132 (let* ((dfun-type (case generator
133 (emit-checking 'checking)
134 (emit-caching 'caching)
135 (emit-constant-value 'constant-value)
136 (emit-default-only 'default-method-only)))
137 (metatypes (car args))
138 (gfs (when dfun-type (gfs-of-type dfun-type))))
140 (when (and (equal metatypes
141 (arg-info-metatypes (gf-arg-info gf)))
142 (let ((gf-name (generic-function-name gf)))
143 (and (not (eq gf-name 'slot-value-using-class))
145 '(setf slot-value-using-class)))
146 (not (eq gf-name 'slot-boundp-using-class)))))
148 (setf (second args-entry) constructor)
149 (setf (third args-entry) system)
150 (setf (fourth args-entry) nil)))
151 (let ((entry (list args constructor system nil)))
153 (push entry (cdr generator-entry))
154 (push (list generator entry) *dfun-constructors*))))))
156 (defmacro precompile-dfun-constructors (&optional system)
157 (let ((*precompiling-lap* t))
160 (dolist (generator-entry *dfun-constructors*)
161 (dolist (args-entry (cdr generator-entry))
162 (when (or (null (caddr args-entry))
163 (eq (caddr args-entry) system))
164 (when system (setf (caddr args-entry) system))
165 (push `(load-precompiled-dfun-constructor
166 ',(car generator-entry)
169 ,(apply (fdefinition (car generator-entry))
172 (nreverse collect)))))
174 ;;; When all the methods of a generic function are automatically
175 ;;; generated reader or writer methods a number of special
176 ;;; optimizations are possible. These are important because of the
177 ;;; large number of generic functions of this type.
179 ;;; There are a number of cases:
181 ;;; ONE-CLASS-ACCESSOR
182 ;;; In this case, the accessor generic function has only been
183 ;;; called with one class of argument. There is no cache vector,
184 ;;; the wrapper of the one class, and the slot index are stored
185 ;;; directly as closure variables of the discriminating function.
186 ;;; This case can convert to either of the next kind.
188 ;;; TWO-CLASS-ACCESSOR
189 ;;; Like above, but two classes. This is common enough to do
190 ;;; specially. There is no cache vector. The two classes are
191 ;;; stored a separate closure variables.
193 ;;; ONE-INDEX-ACCESSOR
194 ;;; In this case, the accessor generic function has seen more than
195 ;;; one class of argument, but the index of the slot is the same
196 ;;; for all the classes that have been seen. A cache vector is
197 ;;; used to store the wrappers that have been seen, the slot index
198 ;;; is stored directly as a closure variable of the discriminating
199 ;;; function. This case can convert to the next kind.
202 ;;; This is the most general case. In this case, the accessor
203 ;;; generic function has seen more than one class of argument and
204 ;;; more than one slot index. A cache vector stores the wrappers
205 ;;; and corresponding slot indexes. Because each cache line is
206 ;;; more than one element long, a cache lock count is used.
207 (defstruct (dfun-info (:constructor nil)
211 (defstruct (no-methods (:constructor no-methods-dfun-info ())
215 (defstruct (initial (:constructor initial-dfun-info ())
219 (defstruct (initial-dispatch (:constructor initial-dispatch-dfun-info ())
223 (defstruct (dispatch (:constructor dispatch-dfun-info ())
227 (defstruct (default-method-only (:constructor default-method-only-dfun-info ())
232 ; dispatch one-class two-class default-method-only
235 ; one-index n-n checking caching
238 ; one-class two-class one-index n-n
239 (defstruct (accessor-dfun-info (:constructor nil)
242 accessor-type) ; (member reader writer)
244 (defmacro dfun-info-accessor-type (di)
245 `(accessor-dfun-info-accessor-type ,di))
247 (defstruct (one-index-dfun-info (:constructor nil)
248 (:include accessor-dfun-info)
252 (defmacro dfun-info-index (di)
253 `(one-index-dfun-info-index ,di))
255 (defstruct (n-n (:constructor n-n-dfun-info (accessor-type cache))
256 (:include accessor-dfun-info)
259 (defstruct (one-class (:constructor one-class-dfun-info
260 (accessor-type index wrapper0))
261 (:include one-index-dfun-info)
265 (defmacro dfun-info-wrapper0 (di)
266 `(one-class-wrapper0 ,di))
268 (defstruct (two-class (:constructor two-class-dfun-info
269 (accessor-type index wrapper0 wrapper1))
274 (defmacro dfun-info-wrapper1 (di)
275 `(two-class-wrapper1 ,di))
277 (defstruct (one-index (:constructor one-index-dfun-info
278 (accessor-type index cache))
279 (:include one-index-dfun-info)
282 (defstruct (checking (:constructor checking-dfun-info (function cache))
287 (defmacro dfun-info-function (di)
288 `(checking-function ,di))
290 (defstruct (caching (:constructor caching-dfun-info (cache))
294 (defstruct (constant-value (:constructor constant-value-dfun-info (cache))
298 (defmacro dfun-update (generic-function function &rest args)
299 `(multiple-value-bind (dfun cache info)
300 (funcall ,function ,generic-function ,@args)
301 (update-dfun ,generic-function dfun cache info)))
303 (defun accessor-miss-function (gf dfun-info)
304 (ecase (dfun-info-accessor-type dfun-info)
307 (accessor-miss gf nil arg dfun-info)))
310 (accessor-miss gf new arg dfun-info)))))
312 #-sb-fluid (declaim (sb-ext:freeze-type dfun-info))
314 (defun make-one-class-accessor-dfun (gf type wrapper index)
315 (let ((emit (ecase type
316 (reader 'emit-one-class-reader)
317 (boundp 'emit-one-class-boundp)
318 (writer 'emit-one-class-writer)))
319 (dfun-info (one-class-dfun-info type index wrapper)))
321 (funcall (get-dfun-constructor emit (consp index))
323 (accessor-miss-function gf dfun-info))
327 (defun make-two-class-accessor-dfun (gf type w0 w1 index)
328 (let ((emit (ecase type
329 (reader 'emit-two-class-reader)
330 (boundp 'emit-two-class-boundp)
331 (writer 'emit-two-class-writer)))
332 (dfun-info (two-class-dfun-info type index w0 w1)))
334 (funcall (get-dfun-constructor emit (consp index))
336 (accessor-miss-function gf dfun-info))
340 ;;; std accessors same index dfun
341 (defun make-one-index-accessor-dfun (gf type index &optional cache)
342 (let* ((emit (ecase type
343 (reader 'emit-one-index-readers)
344 (boundp 'emit-one-index-boundps)
345 (writer 'emit-one-index-writers)))
346 (cache (or cache (get-cache 1 nil #'one-index-limit-fn 4)))
347 (dfun-info (one-index-dfun-info type index cache)))
348 (declare (type cache cache))
350 (funcall (get-dfun-constructor emit (consp index))
353 (accessor-miss-function gf dfun-info))
357 (defun make-final-one-index-accessor-dfun (gf type index table)
358 (let ((cache (fill-dfun-cache table nil 1 #'one-index-limit-fn)))
359 (make-one-index-accessor-dfun gf type index cache)))
361 (defun one-index-limit-fn (nlines)
362 (default-limit-fn nlines))
364 (defun make-n-n-accessor-dfun (gf type &optional cache)
365 (let* ((emit (ecase type
366 (reader 'emit-n-n-readers)
367 (boundp 'emit-n-n-boundps)
368 (writer 'emit-n-n-writers)))
369 (cache (or cache (get-cache 1 t #'n-n-accessors-limit-fn 2)))
370 (dfun-info (n-n-dfun-info type cache)))
371 (declare (type cache cache))
373 (funcall (get-dfun-constructor emit)
375 (accessor-miss-function gf dfun-info))
379 (defun make-final-n-n-accessor-dfun (gf type table)
380 (let ((cache (fill-dfun-cache table t 1 #'n-n-accessors-limit-fn)))
381 (make-n-n-accessor-dfun gf type cache)))
383 (defun n-n-accessors-limit-fn (nlines)
384 (default-limit-fn nlines))
386 (defun make-checking-dfun (generic-function function &optional cache)
388 (when (use-caching-dfun-p generic-function)
389 (return-from make-checking-dfun (make-caching-dfun generic-function)))
390 (when (use-dispatch-dfun-p generic-function)
391 (return-from make-checking-dfun (make-dispatch-dfun generic-function))))
392 (multiple-value-bind (nreq applyp metatypes nkeys)
393 (get-generic-fun-info generic-function)
394 (declare (ignore nreq))
395 (if (every (lambda (mt) (eq mt t)) metatypes)
396 (let ((dfun-info (default-method-only-dfun-info)))
398 (funcall (get-dfun-constructor 'emit-default-only metatypes applyp)
402 (let* ((cache (or cache (get-cache nkeys nil #'checking-limit-fn 2)))
403 (dfun-info (checking-dfun-info function cache)))
405 (funcall (get-dfun-constructor 'emit-checking metatypes applyp)
409 (checking-miss generic-function args dfun-info)))
413 (defun make-final-checking-dfun (generic-function function
414 classes-list new-class)
415 (let ((metatypes (arg-info-metatypes (gf-arg-info generic-function))))
416 (if (every (lambda (mt) (eq mt t)) metatypes)
417 (values (lambda (&rest args)
418 (invoke-emf function args))
419 nil (default-method-only-dfun-info))
420 (let ((cache (make-final-ordinary-dfun-internal
421 generic-function nil #'checking-limit-fn
422 classes-list new-class)))
423 (make-checking-dfun generic-function function cache)))))
425 (defun use-default-method-only-dfun-p (generic-function)
426 (multiple-value-bind (nreq applyp metatypes nkeys)
427 (get-generic-fun-info generic-function)
428 (declare (ignore nreq applyp nkeys))
429 (every (lambda (mt) (eq mt t)) metatypes)))
431 (defun use-caching-dfun-p (generic-function)
432 (some (lambda (method)
433 (let ((fmf (if (listp method)
435 (method-fast-function method))))
436 (method-function-get fmf :slot-name-lists)))
437 ;; KLUDGE: As of sbcl-0.6.4, it's very important for
438 ;; efficiency to know the type of the sequence argument to
439 ;; quantifiers (SOME/NOTANY/etc.) at compile time, but
440 ;; the compiler isn't smart enough to understand the :TYPE
441 ;; slot option for DEFCLASS, so we just tell
442 ;; it the type by hand here.
444 (if (early-gf-p generic-function)
445 (early-gf-methods generic-function)
446 (generic-function-methods generic-function)))))
448 (defun checking-limit-fn (nlines)
449 (default-limit-fn nlines))
451 (defun make-caching-dfun (generic-function &optional cache)
453 (when (use-constant-value-dfun-p generic-function)
454 (return-from make-caching-dfun
455 (make-constant-value-dfun generic-function)))
456 (when (use-dispatch-dfun-p generic-function)
457 (return-from make-caching-dfun
458 (make-dispatch-dfun generic-function))))
459 (multiple-value-bind (nreq applyp metatypes nkeys)
460 (get-generic-fun-info generic-function)
461 (declare (ignore nreq))
462 (let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
463 (dfun-info (caching-dfun-info cache)))
465 (funcall (get-dfun-constructor 'emit-caching metatypes applyp)
468 (caching-miss generic-function args dfun-info)))
472 (defun make-final-caching-dfun (generic-function classes-list new-class)
473 (let ((cache (make-final-ordinary-dfun-internal
474 generic-function t #'caching-limit-fn
475 classes-list new-class)))
476 (make-caching-dfun generic-function cache)))
478 (defun caching-limit-fn (nlines)
479 (default-limit-fn nlines))
481 (defun insure-caching-dfun (gf)
482 (multiple-value-bind (nreq applyp metatypes nkeys)
483 (get-generic-fun-info gf)
484 (declare (ignore nreq nkeys))
486 (not (null (car metatypes)))
487 (dolist (mt metatypes nil)
488 (unless (eq mt t) (return t))))
489 (get-dfun-constructor 'emit-caching metatypes applyp))))
491 (defun use-constant-value-dfun-p (gf &optional boolean-values-p)
492 (multiple-value-bind (nreq applyp metatypes nkeys)
493 (get-generic-fun-info gf)
494 (declare (ignore nreq metatypes nkeys))
495 (let* ((early-p (early-gf-p gf))
497 (early-gf-methods gf)
498 (generic-function-methods gf)))
499 (default '(unknown)))
501 (or (not (eq *boot-state* 'complete))
502 (compute-applicable-methods-emf-std-p gf))
503 (notany (lambda (method)
504 (or (and (eq *boot-state* 'complete)
505 (some #'eql-specializer-p
506 (method-specializers method)))
507 (let ((value (method-function-get
509 (or (third method) (second method))
510 (or (method-fast-function method)
511 (method-function method)))
512 :constant-value default)))
514 (not (or (eq value t) (eq value nil)))
515 (eq value default)))))
518 (defun make-constant-value-dfun (generic-function &optional cache)
519 (multiple-value-bind (nreq applyp metatypes nkeys)
520 (get-generic-fun-info generic-function)
521 (declare (ignore nreq applyp))
522 (let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
523 (dfun-info (constant-value-dfun-info cache)))
525 (funcall (get-dfun-constructor 'emit-constant-value metatypes)
528 (constant-value-miss generic-function args dfun-info)))
532 (defun make-final-constant-value-dfun (generic-function classes-list new-class)
533 (let ((cache (make-final-ordinary-dfun-internal
534 generic-function :constant-value #'caching-limit-fn
535 classes-list new-class)))
536 (make-constant-value-dfun generic-function cache)))
538 (defun use-dispatch-dfun-p (gf &optional (caching-p (use-caching-dfun-p gf)))
539 (when (eq *boot-state* 'complete)
541 ;; This should return T when almost all dispatching is by
542 ;; eql specializers or built-in classes. In other words,
543 ;; return NIL if we might ever need to do more than
544 ;; one (non built-in) typep.
545 ;; Otherwise, it is probably at least as fast to use
546 ;; a caching dfun first, possibly followed by secondary dispatching.
548 #||;;; Original found in cmu 17f -- S L O W
549 (< (dispatch-dfun-cost gf) (caching-dfun-cost gf))
551 ;; This uses improved dispatch-dfun-cost below
552 (let ((cdc (caching-dfun-cost gf))) ; fast
553 (> cdc (dispatch-dfun-cost gf cdc))))))
555 (defparameter *non-built-in-typep-cost* 1)
556 (defparameter *structure-typep-cost* 1)
557 (defparameter *built-in-typep-cost* 0)
559 ;;; According to comments in the original CMU CL version of PCL,
560 ;;; the cost LIMIT is important to cut off exponential growth for
561 ;;; large numbers of gf methods and argument lists.
562 (defun dispatch-dfun-cost (gf &optional limit)
563 (generate-discrimination-net-internal
564 gf (generic-function-methods gf) nil
565 (lambda (methods known-types)
566 (declare (ignore methods known-types))
568 (lambda (position type true-value false-value)
569 (declare (ignore position))
570 (let* ((type-test-cost
571 (if (eq 'class (car type))
572 (let* ((metaclass (class-of (cadr type)))
573 (mcpl (class-precedence-list metaclass)))
574 (cond ((memq *the-class-built-in-class* mcpl)
575 *built-in-typep-cost*)
576 ((memq *the-class-structure-class* mcpl)
577 *structure-typep-cost*)
579 *non-built-in-typep-cost*)))
582 (+ (max true-value false-value) type-test-cost)))
583 (when (and limit (<= limit max-cost-so-far))
584 (return-from dispatch-dfun-cost max-cost-so-far))
588 (defparameter *cache-lookup-cost* 1)
589 (defparameter *wrapper-of-cost* 0)
590 (defparameter *secondary-dfun-call-cost* 1)
592 (defun caching-dfun-cost (gf)
593 (let* ((arg-info (gf-arg-info gf))
594 (nreq (length (arg-info-metatypes arg-info))))
595 (+ *cache-lookup-cost*
596 (* *wrapper-of-cost* nreq)
597 (if (methods-contain-eql-specializer-p
598 (generic-function-methods gf))
599 *secondary-dfun-call-cost*
602 (setq *non-built-in-typep-cost* 100)
603 (setq *structure-typep-cost* 15)
604 (setq *built-in-typep-cost* 5)
605 (setq *cache-lookup-cost* 30)
606 (setq *wrapper-of-cost* 15)
607 (setq *secondary-dfun-call-cost* 30)
609 (defun make-dispatch-dfun (gf)
610 (values (get-dispatch-function gf) nil (dispatch-dfun-info)))
612 (defun get-dispatch-function (gf)
613 (let ((methods (generic-function-methods gf)))
614 (function-funcall (get-secondary-dispatch-function1 gf methods nil nil nil
618 (defun make-final-dispatch-dfun (gf)
619 (make-dispatch-dfun gf))
621 (defun update-dispatch-dfuns ()
622 (dolist (gf (gfs-of-type '(dispatch initial-dispatch)))
623 (dfun-update gf #'make-dispatch-dfun)))
625 (defun fill-dfun-cache (table valuep nkeys limit-fn &optional cache)
626 (let ((cache (or cache (get-cache nkeys valuep limit-fn
627 (+ (hash-table-count table) 3)))))
628 (maphash (lambda (classes value)
629 (setq cache (fill-cache cache
630 (class-wrapper classes)
635 (defun make-final-ordinary-dfun-internal (generic-function valuep limit-fn
636 classes-list new-class)
637 (let* ((arg-info (gf-arg-info generic-function))
638 (nkeys (arg-info-nkeys arg-info))
639 (new-class (and new-class
640 (equal (type-of (gf-dfun-info generic-function))
641 (cond ((eq valuep t) 'caching)
642 ((eq valuep :constant-value) 'constant-value)
643 ((null valuep) 'checking)))
646 (copy-cache (gf-dfun-cache generic-function))
647 (get-cache nkeys (not (null valuep)) limit-fn 4))))
648 (make-emf-cache generic-function valuep cache classes-list new-class)))
650 (defvar *dfun-miss-gfs-on-stack* ())
652 (defmacro dfun-miss ((gf args wrappers invalidp nemf
653 &optional type index caching-p applicable)
655 (unless applicable (setq applicable (gensym)))
656 `(multiple-value-bind (,nemf ,applicable ,wrappers ,invalidp
657 ,@(when type `(,type ,index)))
658 (cache-miss-values ,gf ,args ',(cond (caching-p 'caching)
661 (when (and ,applicable (not (memq ,gf *dfun-miss-gfs-on-stack*)))
662 (let ((*dfun-miss-gfs-on-stack* (cons ,gf *dfun-miss-gfs-on-stack*)))
664 ;; Create a FAST-INSTANCE-BOUNDP structure instance for a cached
665 ;; SLOT-BOUNDP so that INVOKE-EMF does the right thing, that is,
666 ;; does not signal a SLOT-UNBOUND error for a boundp test.
668 ;; FIXME: could the NEMF not be a CONS (for :CLASS-allocated
670 `((if (and (eq ,type 'boundp) (integerp ,nemf))
671 (invoke-emf (make-fast-instance-boundp :index ,nemf) ,args)
672 (invoke-emf ,nemf ,args)))
673 `((invoke-emf ,nemf ,args)))))
675 ;;; The dynamically adaptive method lookup algorithm is implemented is
676 ;;; implemented as a kind of state machine. The kinds of
677 ;;; discriminating function is the state, the various kinds of reasons
678 ;;; for a cache miss are the state transitions.
680 ;;; The code which implements the transitions is all in the miss
681 ;;; handlers for each kind of dfun. Those appear here.
683 ;;; Note that within the states that cache, there are dfun updates
684 ;;; which simply select a new cache or cache field. Those are not
685 ;;; considered as state transitions.
686 (defvar *lazy-dfun-compute-p* t)
687 (defvar *early-p* nil)
689 (defun make-initial-dfun (gf)
691 #'(instance-lambda (&rest args)
692 (initial-dfun gf args))))
693 (multiple-value-bind (dfun cache info)
694 (if (and (eq *boot-state* 'complete)
695 (compute-applicable-methods-emf-std-p gf))
696 (let* ((caching-p (use-caching-dfun-p gf))
697 (classes-list (precompute-effective-methods
699 (not *lazy-dfun-compute-p*))))
700 (if *lazy-dfun-compute-p*
701 (cond ((use-dispatch-dfun-p gf caching-p)
704 (initial-dispatch-dfun-info)))
706 (insure-caching-dfun gf)
707 (values initial-dfun nil (initial-dfun-info)))
709 (values initial-dfun nil (initial-dfun-info))))
710 (make-final-dfun-internal gf classes-list)))
711 (let ((arg-info (if (early-gf-p gf)
712 (early-gf-arg-info gf)
715 (if (and (gf-precompute-dfun-and-emf-p arg-info)
716 (setq type (final-accessor-dfun-type gf)))
718 (values (make-early-accessor gf type) nil nil)
719 (make-final-accessor-dfun gf type))
720 (values initial-dfun nil (initial-dfun-info)))))
721 (set-dfun gf dfun cache info))))
723 (defun make-early-accessor (gf type)
724 (let* ((methods (early-gf-methods gf))
725 (slot-name (early-method-standard-accessor-slot-name (car methods))))
727 (reader #'(instance-lambda (instance)
728 (let* ((class (class-of instance))
729 (class-name (!bootstrap-get-slot 'class class 'name)))
730 (!bootstrap-get-slot class-name instance slot-name))))
731 (boundp #'(instance-lambda (instance)
732 (let* ((class (class-of instance))
733 (class-name (!bootstrap-get-slot 'class class 'name)))
734 (not (eq +slot-unbound+
735 (!bootstrap-get-slot class-name
736 instance slot-name))))))
737 (writer #'(instance-lambda (new-value instance)
738 (let* ((class (class-of instance))
739 (class-name (!bootstrap-get-slot 'class class 'name)))
740 (!bootstrap-set-slot class-name instance slot-name new-value)))))))
742 (defun initial-dfun (gf args)
743 (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
747 gf #'make-one-class-accessor-dfun ntype wrappers nindex))
748 ((use-caching-dfun-p gf)
749 (dfun-update gf #'make-caching-dfun))
752 gf #'make-checking-dfun
753 ;; nemf is suitable only for caching, have to do this:
754 (cache-miss-values gf args 'checking))))))
756 (defun make-final-dfun (gf &optional classes-list)
757 (multiple-value-bind (dfun cache info)
758 (make-final-dfun-internal gf classes-list)
759 (set-dfun gf dfun cache info)))
761 (defvar *new-class* nil)
763 (defvar *free-hash-tables* (mapcar #'list '(eq equal eql)))
765 (defmacro with-hash-table ((table test) &body forms)
766 `(let* ((.free. (assoc ',test *free-hash-tables*))
767 (,table (if (cdr .free.)
769 (make-hash-table :test ',test))))
770 (multiple-value-prog1
773 (push ,table (cdr .free.)))))
775 (defmacro with-eq-hash-table ((table) &body forms)
776 `(with-hash-table (,table eq) ,@forms))
778 (defun final-accessor-dfun-type (gf)
779 (let ((methods (if (early-gf-p gf)
780 (early-gf-methods gf)
781 (generic-function-methods gf))))
782 (cond ((every (lambda (method)
784 (eq *the-class-standard-reader-method*
785 (early-method-class method))
786 (standard-reader-method-p method)))
789 ((every (lambda (method)
791 (eq *the-class-standard-boundp-method*
792 (early-method-class method))
793 (standard-boundp-method-p method)))
796 ((every (lambda (method)
798 (eq *the-class-standard-writer-method*
799 (early-method-class method))
800 (standard-writer-method-p method)))
804 (defun make-final-accessor-dfun (gf type &optional classes-list new-class)
805 (with-eq-hash-table (table)
806 (multiple-value-bind (table all-index first second size no-class-slots-p)
807 (make-accessor-table gf type table)
810 (let ((w (class-wrapper first)))
811 (make-one-class-accessor-dfun gf type w all-index)))
812 ((and (= size 2) (or (integerp all-index) (consp all-index)))
813 (let ((w0 (class-wrapper first))
814 (w1 (class-wrapper second)))
815 (make-two-class-accessor-dfun gf type w0 w1 all-index)))
816 ((or (integerp all-index) (consp all-index))
817 (make-final-one-index-accessor-dfun
818 gf type all-index table))
820 (make-final-n-n-accessor-dfun gf type table))
822 (make-final-caching-dfun gf classes-list new-class)))
823 (make-final-caching-dfun gf classes-list new-class)))))
825 (defun make-final-dfun-internal (gf &optional classes-list)
826 (let ((methods (generic-function-methods gf)) type
827 (new-class *new-class*) (*new-class* nil)
829 (cond ((null methods)
831 #'(instance-lambda (&rest args)
832 (apply #'no-applicable-method gf args))
834 (no-methods-dfun-info)))
835 ((setq type (final-accessor-dfun-type gf))
836 (make-final-accessor-dfun gf type classes-list new-class))
837 ((and (not (and (every (lambda (specl) (eq specl *the-class-t*))
839 (method-specializers (car methods))))
841 (every (lambda (method)
846 (use-constant-value-dfun-p gf))
847 (make-final-constant-value-dfun gf classes-list new-class))
848 ((use-dispatch-dfun-p gf)
849 (make-final-dispatch-dfun gf))
850 ((and all-same-p (not (use-caching-dfun-p gf)))
851 (let ((emf (get-secondary-dispatch-function gf methods nil)))
852 (make-final-checking-dfun gf emf classes-list new-class)))
854 (make-final-caching-dfun gf classes-list new-class)))))
856 (defun accessor-miss (gf new object dfun-info)
857 (let* ((ostate (type-of dfun-info))
858 (otype (dfun-info-accessor-type dfun-info))
861 ;; The congruence rules ensure that this is safe
862 ;; despite not knowing the new type yet.
863 ((reader boundp) (list object))
864 (writer (list new object)))))
865 (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
867 ;; The following lexical functions change the state of the
868 ;; dfun to that which is their name. They accept arguments
869 ;; which are the parameters of the new state, and get other
870 ;; information from the lexical variables bound above.
871 (flet ((two-class (index w0 w1)
872 (when (zerop (random 2)) (psetf w0 w1 w1 w0))
874 #'make-two-class-accessor-dfun
879 (one-index (index &optional cache)
881 #'make-one-index-accessor-dfun
885 (n-n (&optional cache)
887 (dfun-update gf #'make-checking-dfun nemf)
888 (dfun-update gf #'make-n-n-accessor-dfun ntype cache)))
889 (caching () ; because cached accessor emfs are much faster
891 (dfun-update gf #'make-caching-dfun))
893 (let ((ncache (fill-cache cache wrappers nindex)))
894 (unless (eq ncache cache)
895 (funcall update-fn ncache)))))
901 ((not (pcl-instance-p object))
903 ((or (neq ntype otype) (listp wrappers))
908 (setq oindex (dfun-info-index dfun-info))
909 (setq ow0 (dfun-info-wrapper0 dfun-info))
910 (unless (eq ow0 wrappers)
911 (if (eql nindex oindex)
912 (two-class nindex ow0 wrappers)
915 (setq oindex (dfun-info-index dfun-info))
916 (setq ow0 (dfun-info-wrapper0 dfun-info))
917 (setq ow1 (dfun-info-wrapper1 dfun-info))
918 (unless (or (eq ow0 wrappers) (eq ow1 wrappers))
919 (if (eql nindex oindex)
923 (setq oindex (dfun-info-index dfun-info))
924 (setq cache (dfun-info-cache dfun-info))
925 (if (eql nindex oindex)
926 (do-fill (lambda (ncache)
927 (one-index nindex ncache)))
930 (setq cache (dfun-info-cache dfun-info))
933 (do-fill #'n-n))))))))))
935 (defun checking-miss (generic-function args dfun-info)
936 (let ((oemf (dfun-info-function dfun-info))
937 (cache (dfun-info-cache dfun-info)))
938 (dfun-miss (generic-function args wrappers invalidp nemf)
941 (let ((ncache (fill-cache cache wrappers nil)))
942 (unless (eq ncache cache)
943 (dfun-update generic-function #'make-checking-dfun
946 (dfun-update generic-function #'make-caching-dfun))))))
948 (defun caching-miss (generic-function args dfun-info)
949 (let ((ocache (dfun-info-cache dfun-info)))
950 (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
953 (let ((ncache (fill-cache ocache wrappers emf)))
954 (unless (eq ncache ocache)
955 (dfun-update generic-function
956 #'make-caching-dfun ncache))))))))
958 (defun constant-value-miss (generic-function args dfun-info)
959 (let ((ocache (dfun-info-cache dfun-info)))
960 (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
963 (let* ((function (typecase emf
964 (fast-method-call (fast-method-call-function
966 (method-call (method-call-function emf))))
967 (value (method-function-get function :constant-value))
968 (ncache (fill-cache ocache wrappers value)))
969 (unless (eq ncache ocache)
970 (dfun-update generic-function
971 #'make-constant-value-dfun ncache))))))))
973 ;;; Given a generic function and a set of arguments to that generic
974 ;;; function, return a mess of values.
976 ;;; <function> The compiled effective method function for this set of
979 ;;; <applicable> Sorted list of applicable methods.
981 ;;; <wrappers> Is a single wrapper if the generic function has only
982 ;;; one key, that is arg-info-nkeys of the arg-info is 1.
983 ;;; Otherwise a list of the wrappers of the specialized
984 ;;; arguments to the generic function.
986 ;;; Note that all these wrappers are valid. This function
987 ;;; does invalid wrapper traps when it finds an invalid
988 ;;; wrapper and then returns the new, valid wrapper.
990 ;;; <invalidp> True if any of the specialized arguments had an invalid
991 ;;; wrapper, false otherwise.
993 ;;; <type> READER or WRITER when the only method that would be run
994 ;;; is a standard reader or writer method. To be specific,
995 ;;; the value is READER when the method combination is eq to
996 ;;; *standard-method-combination*; there are no applicable
997 ;;; :before, :after or :around methods; and the most specific
998 ;;; primary method is a standard reader method.
1000 ;;; <index> If <type> is READER or WRITER, and the slot accessed is
1001 ;;; an :instance slot, this is the index number of that slot
1002 ;;; in the object argument.
1003 (defun cache-miss-values (gf args state)
1004 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
1005 (get-generic-fun-info gf)
1006 (declare (ignore nreq applyp nkeys))
1007 (with-dfun-wrappers (args metatypes)
1008 (dfun-wrappers invalid-wrapper-p wrappers classes types)
1009 (error-need-at-least-n-args gf (length metatypes))
1010 (multiple-value-bind (emf methods accessor-type index)
1011 (cache-miss-values-internal
1012 gf arg-info wrappers classes types state)
1016 accessor-type index)))))
1018 (defun cache-miss-values-internal (gf arg-info wrappers classes types state)
1019 (let* ((for-accessor-p (eq state 'accessor))
1020 (for-cache-p (or (eq state 'caching) (eq state 'accessor)))
1021 (cam-std-p (or (null arg-info)
1022 (gf-info-c-a-m-emf-std-p arg-info))))
1023 (multiple-value-bind (methods all-applicable-and-sorted-p)
1025 (compute-applicable-methods-using-types gf types)
1026 (compute-applicable-methods-using-classes gf classes))
1027 (let ((emf (if (or cam-std-p all-applicable-and-sorted-p)
1028 (function-funcall (get-secondary-dispatch-function1
1029 gf methods types nil (and for-cache-p
1031 all-applicable-and-sorted-p)
1032 nil (and for-cache-p wrappers))
1033 (default-secondary-dispatch-function gf))))
1034 (multiple-value-bind (index accessor-type)
1035 (and for-accessor-p all-applicable-and-sorted-p methods
1036 (accessor-values gf arg-info classes methods))
1037 (values (if (integerp index) index emf)
1038 methods accessor-type index))))))
1040 (defun accessor-values (gf arg-info classes methods)
1041 (declare (ignore gf))
1042 (let* ((accessor-type (gf-info-simple-accessor-type arg-info))
1043 (accessor-class (case accessor-type
1044 ((reader boundp) (car classes))
1045 (writer (cadr classes)))))
1046 (accessor-values-internal accessor-type accessor-class methods)))
1048 (defun accessor-values1 (gf accessor-type accessor-class)
1049 (let* ((type `(class-eq ,accessor-class))
1050 (types (ecase accessor-type
1051 ((reader boundp) `(,type))
1052 (writer `(t ,type))))
1053 (methods (compute-applicable-methods-using-types gf types)))
1054 (accessor-values-internal accessor-type accessor-class methods)))
1056 (defun accessor-values-internal (accessor-type accessor-class methods)
1057 (dolist (meth methods)
1058 (when (if (consp meth)
1059 (early-method-qualifiers meth)
1060 (method-qualifiers meth))
1061 (return-from accessor-values-internal (values nil nil))))
1062 (let* ((meth (car methods))
1063 (early-p (not (eq *boot-state* 'complete)))
1064 (slot-name (when accessor-class
1066 (and (early-method-standard-accessor-p meth)
1067 (early-method-standard-accessor-slot-name meth))
1068 (and (member *the-class-std-object*
1070 (early-class-precedence-list
1072 (class-precedence-list
1075 (not (eq *the-class-standard-method*
1076 (early-method-class meth)))
1077 (standard-accessor-method-p meth))
1079 (early-accessor-method-slot-name meth)
1080 (accessor-method-slot-name meth))))))
1081 (slotd (and accessor-class
1083 (dolist (slot (early-class-slotds accessor-class) nil)
1084 (when (eql slot-name
1085 (early-slot-definition-name slot))
1087 (find-slot-definition accessor-class slot-name)))))
1090 (slot-accessor-std-p slotd accessor-type)))
1092 (early-slot-definition-location slotd)
1093 (slot-definition-location slotd))
1096 (defun make-accessor-table (gf type &optional table)
1097 (unless table (setq table (make-hash-table :test 'eq)))
1098 (let ((methods (if (early-gf-p gf)
1099 (early-gf-methods gf)
1100 (generic-function-methods gf)))
1102 (no-class-slots-p t)
1103 (early-p (not (eq *boot-state* 'complete)))
1104 first second (size 0))
1105 (declare (fixnum size))
1106 ;; class -> {(specl slotd)}
1107 (dolist (method methods)
1108 (let* ((specializers (if (consp method)
1109 (early-method-specializers method t)
1110 (method-specializers method)))
1112 ((reader boundp) (car specializers))
1113 (writer (cadr specializers))))
1114 (specl-cpl (if early-p
1115 (early-class-precedence-list specl)
1116 (and (class-finalized-p specl)
1117 (class-precedence-list specl))))
1118 (so-p (member *the-class-std-object* specl-cpl))
1119 (slot-name (if (consp method)
1120 (and (early-method-standard-accessor-p method)
1121 (early-method-standard-accessor-slot-name
1123 (accessor-method-slot-name method))))
1124 (when (or (null specl-cpl)
1125 (member *the-class-structure-object* specl-cpl))
1126 (return-from make-accessor-table nil))
1127 (maphash (lambda (class slotd)
1128 (let ((cpl (if early-p
1129 (early-class-precedence-list class)
1130 (class-precedence-list class))))
1131 (when (memq specl cpl)
1132 (unless (and (or so-p
1133 (member *the-class-std-object* cpl))
1135 (slot-accessor-std-p slotd type)))
1136 (return-from make-accessor-table nil))
1137 (push (cons specl slotd) (gethash class table)))))
1138 (gethash slot-name *name->class->slotd-table*))))
1139 (maphash (lambda (class specl+slotd-list)
1140 (dolist (sclass (if early-p
1141 (early-class-precedence-list class)
1142 (class-precedence-list class))
1143 (error "This can't happen."))
1144 (let ((a (assq sclass specl+slotd-list)))
1146 (let* ((slotd (cdr a))
1148 (early-slot-definition-location slotd)
1149 (slot-definition-location slotd))))
1150 (unless index (return-from make-accessor-table nil))
1151 (setf (gethash class table) index)
1152 (when (consp index) (setq no-class-slots-p nil))
1153 (setq all-index (if (or (null all-index)
1154 (eql all-index index))
1157 (cond ((= size 1) (setq first class))
1158 ((= size 2) (setq second class)))
1161 (values table all-index first second size no-class-slots-p)))
1163 (defun compute-applicable-methods-using-types (generic-function types)
1164 (let ((definite-p t) (possibly-applicable-methods nil))
1165 (dolist (method (if (early-gf-p generic-function)
1166 (early-gf-methods generic-function)
1167 (generic-function-methods generic-function)))
1168 (let ((specls (if (consp method)
1169 (early-method-specializers method t)
1170 (method-specializers method)))
1172 (possibly-applicable-p t) (applicable-p t))
1173 (dolist (specl specls)
1174 (multiple-value-bind (specl-applicable-p specl-possibly-applicable-p)
1175 (specializer-applicable-using-type-p specl (pop types))
1176 (unless specl-applicable-p
1177 (setq applicable-p nil))
1178 (unless specl-possibly-applicable-p
1179 (setq possibly-applicable-p nil)
1181 (when possibly-applicable-p
1182 (unless applicable-p (setq definite-p nil))
1183 (push method possibly-applicable-methods))))
1184 (let ((precedence (arg-info-precedence (if (early-gf-p generic-function)
1188 generic-function)))))
1189 (values (sort-applicable-methods precedence
1190 (nreverse possibly-applicable-methods)
1194 (defun sort-applicable-methods (precedence methods types)
1195 (sort-methods methods
1197 (lambda (class1 class2 index)
1198 (let* ((class (type-class (nth index types)))
1199 (cpl (if (eq *boot-state* 'complete)
1200 (class-precedence-list class)
1201 (early-class-precedence-list class))))
1202 (if (memq class2 (memq class1 cpl))
1205 (defun sort-methods (methods precedence compare-classes-function)
1206 (flet ((sorter (method1 method2)
1207 (dolist (index precedence)
1208 (let* ((specl1 (nth index (if (listp method1)
1209 (early-method-specializers method1
1211 (method-specializers method1))))
1212 (specl2 (nth index (if (listp method2)
1213 (early-method-specializers method2
1215 (method-specializers method2))))
1216 (order (order-specializers
1217 specl1 specl2 index compare-classes-function)))
1219 (return-from sorter (eq order specl1)))))))
1220 (stable-sort methods #'sorter)))
1222 (defun order-specializers (specl1 specl2 index compare-classes-function)
1223 (let ((type1 (if (eq *boot-state* 'complete)
1224 (specializer-type specl1)
1225 (!bootstrap-get-slot 'specializer specl1 'type)))
1226 (type2 (if (eq *boot-state* 'complete)
1227 (specializer-type specl2)
1228 (!bootstrap-get-slot 'specializer specl2 'type))))
1229 (cond ((eq specl1 specl2)
1237 (class (case (car type2)
1238 (class (funcall compare-classes-function
1239 specl1 specl2 index))
1241 (prototype (case (car type2)
1242 (class (funcall compare-classes-function
1243 specl1 specl2 index))
1245 (class-eq (case (car type2)
1249 (eql (case (car type2)
1253 (defun map-all-orders (methods precedence function)
1254 (let ((choices nil))
1255 (flet ((compare-classes-function (class1 class2 index)
1256 (declare (ignore index))
1258 (dolist (c choices nil)
1259 (when (or (and (eq (first c) class1)
1260 (eq (second c) class2))
1261 (and (eq (first c) class2)
1262 (eq (second c) class1)))
1263 (return (setq choice c))))
1266 (if (class-might-precede-p class1 class2)
1267 (if (class-might-precede-p class2 class1)
1268 (list class1 class2 nil t)
1269 (list class1 class2 t))
1270 (if (class-might-precede-p class2 class1)
1271 (list class2 class1 t)
1272 (let ((name1 (class-name class1))
1273 (name2 (class-name class2)))
1278 (string< (symbol-name name1)
1279 (symbol-name name2)))
1280 (list class1 class2 t)
1281 (list class2 class1 t))))))
1282 (push choice choices))
1284 (loop (funcall function
1285 (sort-methods methods
1287 #'compare-classes-function))
1288 (unless (dolist (c choices nil)
1290 (rotatef (car c) (cadr c))
1291 (return (setf (third c) t))))
1294 (defvar *in-precompute-effective-methods-p* nil)
1296 ;used only in map-all-orders
1297 (defun class-might-precede-p (class1 class2)
1298 (if (not *in-precompute-effective-methods-p*)
1299 (not (member class1 (cdr (class-precedence-list class2))))
1300 (class-can-precede-p class1 class2)))
1302 (defun compute-precedence (lambda-list nreq argument-precedence-order)
1303 (if (null argument-precedence-order)
1305 (dotimes-fixnum (i nreq list) (push (- (1- nreq) i) list)))
1306 (mapcar (lambda (x) (position x lambda-list))
1307 argument-precedence-order)))
1309 (defun saut-and (specl type)
1310 (let ((applicable nil)
1311 (possibly-applicable t))
1312 (dolist (type (cdr type))
1313 (multiple-value-bind (appl poss-appl)
1314 (specializer-applicable-using-type-p specl type)
1315 (when appl (return (setq applicable t)))
1316 (unless poss-appl (return (setq possibly-applicable nil)))))
1317 (values applicable possibly-applicable)))
1319 (defun saut-not (specl type)
1320 (let ((ntype (cadr type)))
1323 (class (saut-not-class specl ntype))
1324 (class-eq (saut-not-class-eq specl ntype))
1325 (prototype (saut-not-prototype specl ntype))
1326 (eql (saut-not-eql specl ntype))
1327 (t (error "~S cannot handle the second argument ~S"
1328 'specializer-applicable-using-type-p type))))))
1330 (defun saut-not-class (specl ntype)
1331 (let* ((class (type-class specl))
1332 (cpl (class-precedence-list class)))
1333 (not (memq (cadr ntype) cpl))))
1335 (defun saut-not-prototype (specl ntype)
1336 (let* ((class (case (car specl)
1337 (eql (class-of (cadr specl)))
1338 (class-eq (cadr specl))
1339 (prototype (cadr specl))
1340 (class (cadr specl))))
1341 (cpl (class-precedence-list class)))
1342 (not (memq (cadr ntype) cpl))))
1344 (defun saut-not-class-eq (specl ntype)
1345 (let ((class (case (car specl)
1346 (eql (class-of (cadr specl)))
1347 (class-eq (cadr specl)))))
1348 (not (eq class (cadr ntype)))))
1350 (defun saut-not-eql (specl ntype)
1352 (eql (not (eql (cadr specl) (cadr ntype))))
1355 (defun class-applicable-using-class-p (specl type)
1356 (let ((pred (memq specl (if (eq *boot-state* 'complete)
1357 (class-precedence-list type)
1358 (early-class-precedence-list type)))))
1361 (if (not *in-precompute-effective-methods-p*)
1362 ;; classes might get common subclass
1363 (superclasses-compatible-p specl type)
1364 ;; worry only about existing classes
1365 (classes-have-common-subclass-p specl type))))))
1367 (defun classes-have-common-subclass-p (class1 class2)
1368 (or (eq class1 class2)
1369 (let ((class1-subs (class-direct-subclasses class1)))
1370 (or (memq class2 class1-subs)
1371 (dolist (class1-sub class1-subs nil)
1372 (when (classes-have-common-subclass-p class1-sub class2)
1375 (defun saut-class (specl type)
1377 (class (class-applicable-using-class-p (cadr specl) (cadr type)))
1378 (t (values nil (let ((class (type-class specl)))
1380 (class-precedence-list class)))))))
1382 (defun saut-class-eq (specl type)
1383 (if (eq (car specl) 'eql)
1384 (values nil (eq (class-of (cadr specl)) (cadr type)))
1385 (let ((pred (case (car specl)
1387 (eq (cadr specl) (cadr type)))
1389 (or (eq (cadr specl) (cadr type))
1391 (if (eq *boot-state* 'complete)
1392 (class-precedence-list (cadr type))
1393 (early-class-precedence-list
1395 (values pred pred))))
1397 (defun saut-prototype (specl type)
1398 (declare (ignore specl type))
1399 (values nil nil)) ; XXX original PCL comment: fix this someday
1401 (defun saut-eql (specl type)
1402 (let ((pred (case (car specl)
1403 (eql (eql (cadr specl) (cadr type)))
1404 (class-eq (eq (cadr specl) (class-of (cadr type))))
1405 (class (memq (cadr specl)
1406 (let ((class (class-of (cadr type))))
1407 (if (eq *boot-state* 'complete)
1408 (class-precedence-list class)
1409 (early-class-precedence-list
1411 (values pred pred)))
1413 (defun specializer-applicable-using-type-p (specl type)
1414 (setq specl (type-from-specializer specl))
1416 (return-from specializer-applicable-using-type-p (values t t)))
1417 ;; This is used by C-A-M-U-T and GENERATE-DISCRIMINATION-NET-INTERNAL,
1418 ;; and has only what they need.
1419 (if (or (atom type) (eq (car type) t))
1422 (and (saut-and specl type))
1423 (not (saut-not specl type))
1424 (class (saut-class specl type))
1425 (prototype (saut-prototype specl type))
1426 (class-eq (saut-class-eq specl type))
1427 (eql (saut-eql specl type))
1428 (t (error "~S cannot handle the second argument ~S."
1429 'specializer-applicable-using-type-p
1432 (defun map-all-classes (function &optional (root t))
1433 (let ((braid-p (or (eq *boot-state* 'braid)
1434 (eq *boot-state* 'complete))))
1435 (labels ((do-class (class)
1438 (class-direct-subclasses class)
1439 (early-class-direct-subclasses class)))
1440 (funcall function class)))
1441 (do-class (if (symbolp root)
1445 ;;; NOTE: We are assuming a restriction on user code that the method
1446 ;;; combination must not change once it is connected to the
1447 ;;; generic function.
1449 ;;; This has to be legal, because otherwise any kind of method
1450 ;;; lookup caching couldn't work. See this by saying that this
1451 ;;; cache, is just a backing cache for the fast cache. If that
1452 ;;; cache is legal, this one must be too.
1454 ;;; Don't clear this table!
1455 (defvar *effective-method-table* (make-hash-table :test 'eq))
1457 (defun get-secondary-dispatch-function (gf methods types &optional
1458 method-alist wrappers)
1459 (function-funcall (get-secondary-dispatch-function1
1461 (not (null method-alist))
1462 (not (null wrappers))
1463 (not (methods-contain-eql-specializer-p methods)))
1464 method-alist wrappers))
1466 (defun get-secondary-dispatch-function1 (gf methods types method-alist-p
1474 (lambda (method-alist wrappers)
1475 (declare (ignore method-alist wrappers))
1476 #'(instance-lambda (&rest args)
1477 (apply #'no-applicable-method gf args)))
1478 (lambda (method-alist wrappers)
1479 (declare (ignore method-alist wrappers))
1480 (lambda (&rest args)
1481 (apply #'no-applicable-method gf args))))
1482 (let* ((key (car methods))
1483 (ht-value (or (gethash key *effective-method-table*)
1484 (setf (gethash key *effective-method-table*)
1486 (if (and (null (cdr methods)) all-applicable-p ; the most common case
1487 (null method-alist-p) wrappers-p (not function-p))
1489 (setf (car ht-value)
1490 (get-secondary-dispatch-function2
1491 gf methods types method-alist-p wrappers-p
1492 all-applicable-p all-sorted-p function-p)))
1493 (let ((akey (list methods
1494 (if all-applicable-p 'all-applicable types)
1495 method-alist-p wrappers-p function-p)))
1496 (or (cdr (assoc akey (cdr ht-value) :test #'equal))
1497 (let ((value (get-secondary-dispatch-function2
1498 gf methods types method-alist-p wrappers-p
1499 all-applicable-p all-sorted-p function-p)))
1500 (push (cons akey value) (cdr ht-value))
1503 (defun get-secondary-dispatch-function2 (gf methods types method-alist-p
1504 wrappers-p all-applicable-p
1505 all-sorted-p function-p)
1506 (if (and all-applicable-p all-sorted-p (not function-p))
1507 (if (eq *boot-state* 'complete)
1508 (let* ((combin (generic-function-method-combination gf))
1509 (effective (compute-effective-method gf combin methods)))
1510 (make-effective-method-function1 gf effective method-alist-p
1512 (let ((effective (standard-compute-effective-method gf nil methods)))
1513 (make-effective-method-function1 gf effective method-alist-p
1515 (let ((net (generate-discrimination-net
1516 gf methods types all-sorted-p)))
1517 (compute-secondary-dispatch-function1 gf net function-p))))
1519 (defun get-effective-method-function (gf methods
1520 &optional method-alist wrappers)
1521 (function-funcall (get-secondary-dispatch-function1 gf methods nil
1522 (not (null method-alist))
1523 (not (null wrappers))
1525 method-alist wrappers))
1527 (defun get-effective-method-function1 (gf methods &optional (sorted-p t))
1528 (get-secondary-dispatch-function1 gf methods nil nil nil t sorted-p))
1530 (defun methods-contain-eql-specializer-p (methods)
1531 (and (eq *boot-state* 'complete)
1532 (dolist (method methods nil)
1533 (when (dolist (spec (method-specializers method) nil)
1534 (when (eql-specializer-p spec) (return t)))
1537 (defun update-dfun (generic-function &optional dfun cache info)
1538 (let* ((early-p (early-gf-p generic-function))
1539 (gf-name (if early-p
1540 (!early-gf-name generic-function)
1541 (generic-function-name generic-function))))
1542 (set-dfun generic-function dfun cache info)
1543 (let ((dfun (if early-p
1544 (or dfun (make-initial-dfun generic-function))
1545 (compute-discriminating-function generic-function))))
1546 (set-funcallable-instance-function generic-function dfun)
1547 (set-fun-name generic-function gf-name)
1550 (defvar *dfun-count* nil)
1551 (defvar *dfun-list* nil)
1552 (defvar *minimum-cache-size-to-list*)
1554 ;;; These functions aren't used in SBCL, or documented anywhere that
1555 ;;; I'm aware of, but they look like they might be useful for
1556 ;;; debugging or performance tweaking or something, so I've just
1557 ;;; commented them out instead of deleting them. -- WHN 2001-03-28
1559 (defun list-dfun (gf)
1560 (let* ((sym (type-of (gf-dfun-info gf)))
1561 (a (assq sym *dfun-list*)))
1563 (push (setq a (list sym)) *dfun-list*))
1564 (push (generic-function-name gf) (cdr a))))
1566 (defun list-all-dfuns ()
1567 (setq *dfun-list* nil)
1568 (map-all-generic-functions #'list-dfun)
1571 (defun list-large-cache (gf)
1572 (let* ((sym (type-of (gf-dfun-info gf)))
1573 (cache (gf-dfun-cache gf)))
1575 (let ((size (cache-size cache)))
1576 (when (>= size *minimum-cache-size-to-list*)
1577 (let ((a (assoc size *dfun-list*)))
1579 (push (setq a (list size)) *dfun-list*))
1580 (push (let ((name (generic-function-name gf)))
1581 (if (eq sym 'caching) name (list name sym)))
1584 (defun list-large-caches (&optional (*minimum-cache-size-to-list* 130))
1585 (setq *dfun-list* nil)
1586 (map-all-generic-functions #'list-large-cache)
1587 (setq *dfun-list* (sort *dfun-list* #'< :key #'car))
1588 (mapc #'print *dfun-list*)
1591 (defun count-dfun (gf)
1592 (let* ((sym (type-of (gf-dfun-info gf)))
1593 (cache (gf-dfun-cache gf))
1594 (a (assq sym *dfun-count*)))
1596 (push (setq a (list sym 0 nil)) *dfun-count*))
1599 (let* ((size (cache-size cache))
1600 (b (assoc size (third a))))
1602 (push (setq b (cons size 0)) (third a)))
1605 (defun count-all-dfuns ()
1606 (setq *dfun-count* (mapcar (lambda (type) (list type 0 nil))
1607 '(ONE-CLASS TWO-CLASS DEFAULT-METHOD-ONLY
1608 ONE-INDEX N-N CHECKING CACHING
1610 (map-all-generic-functions #'count-dfun)
1611 (mapc (lambda (type+count+sizes)
1612 (setf (third type+count+sizes)
1613 (sort (third type+count+sizes) #'< :key #'car)))
1615 (mapc (lambda (type+count+sizes)
1616 (format t "~&There are ~W dfuns of type ~S."
1617 (cadr type+count+sizes) (car type+count+sizes))
1618 (format t "~% ~S~%" (caddr type+count+sizes)))
1623 (defun gfs-of-type (type)
1624 (unless (consp type) (setq type (list type)))
1625 (let ((gf-list nil))
1626 (map-all-generic-functions (lambda (gf)
1627 (when (memq (type-of (gf-dfun-info gf))
1629 (push gf gf-list))))