/[cmucl]/src/pcl/dfun.lisp
ViewVC logotype

Contents of /src/pcl/dfun.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.41 - (show annotations)
Mon Apr 19 02:31:14 2010 UTC (4 years ago) by rtoy
Branch: MAIN
CVS Tags: sparc-tramp-assem-base, release-20b-pre1, release-20b-pre2, sparc-tramp-assem-2010-07-19, GIT-CONVERSION, cross-sol-x86-merged, RELEASE_20b, cross-sol-x86-base, snapshot-2010-12, snapshot-2010-11, snapshot-2011-09, snapshot-2011-06, snapshot-2011-07, snapshot-2011-04, snapshot-2011-02, snapshot-2011-03, snapshot-2011-01, snapshot-2010-05, snapshot-2010-07, snapshot-2010-06, snapshot-2010-08, cross-sol-x86-2010-12-20, cross-sparc-branch-base, HEAD
Branch point for: cross-sparc-branch, RELEASE-20B-BRANCH, sparc-tramp-assem-branch, cross-sol-x86-branch
Changes since 1.40: +2 -2 lines
Remove _N"" reader macro from docstrings when possible.
1 ;;; -*- Mode:LISP; Package:PCL; Base:10; Syntax:Common-Lisp -*-
2 ;;;
3 ;;; *************************************************************************
4 ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
5 ;;; All rights reserved.
6 ;;;
7 ;;; Use and copying of this software and preparation of derivative works
8 ;;; based upon this software are permitted. Any distribution of this
9 ;;; software or derivative works must comply with all applicable United
10 ;;; States export control laws.
11 ;;;
12 ;;; This software is made available AS IS, and Xerox Corporation makes no
13 ;;; warranty about the software, its performance or its conformity to any
14 ;;; specification.
15 ;;;
16 ;;; Any person obtaining a copy of this software is requested to send their
17 ;;; name and post office or electronic mail address to:
18 ;;; CommonLoops Coordinator
19 ;;; Xerox PARC
20 ;;; 3333 Coyote Hill Rd.
21 ;;; Palo Alto, CA 94304
22 ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
23 ;;;
24 ;;; Suggestions, comments and requests for improvements are also welcome.
25 ;;; *************************************************************************
26
27 (file-comment
28 "$Header: /tiger/var/lib/cvsroots/cmucl/src/pcl/dfun.lisp,v 1.41 2010/04/19 02:31:14 rtoy Rel $")
29
30 (in-package :pcl)
31 (intl:textdomain "cmucl")
32
33 #|
34
35 This implementation of method lookup was redone in early August of 89.
36
37 It has the following properties:
38
39 - Its modularity makes it easy to modify the actual caching algorithm.
40 The caching algorithm is almost completely separated into the files
41 cache.lisp and dlap.lisp. This file just contains the various uses
42 of it. There will be more tuning as we get more results from Luis'
43 measurements of caching behavior.
44
45 - The metacircularity issues have been dealt with properly. All of
46 PCL now grounds out properly. Moreover, it is now possible to have
47 metaobject classes which are themselves not instances of standard
48 metaobject classes.
49
50 ** Modularity of the code **
51
52 The actual caching algorithm is isolated in a modest number of functions.
53 The code which generates cache lookup code is all found in cache.lisp and
54 dlap.lisp. Certain non-wrapper-caching special cases are in this file.
55
56
57 ** Handling the metacircularity **
58
59 In CLOS, method lookup is the potential source of infinite metacircular
60 regress. The metaobject protocol specification gives us wide flexibility
61 in how to address this problem. PCL uses a technique which handles the
62 problem not only for the metacircular language described in Chapter 3, but
63 also for the PCL protocol which includes additional generic functions
64 which control more aspects of the CLOS implementation.
65
66 The source of the metacircular regress can be seen in a number of ways.
67 One is that the specified method lookup protocol must, as part of doing
68 the method lookup (or at least the cache miss case), itself call generic
69 functions. It is easy to see that if the method lookup for a generic
70 function ends up calling that same generic function there can be trouble.
71
72 Fortunately, there is an easy solution at hand. The solution is based on
73 the restriction that portable code cannot change the class of a specified
74 metaobject. This restriction implies that for specified generic functions,
75 the method lookup protocol they follow is fixed.
76
77 More precisely, for such specified generic functions, most generic functions
78 that are called during their own method lookup will not run portable methods.
79 This allows the implementation to usurp the actual generic function call in
80 this case. In short, method lookup of a standard generic function, in the
81 case where the only applicable methods are themselves standard doesn't
82 have to do any method lookup to implement itself.
83
84 And so, we are saved.
85
86 |#
87
88
89 ;;;
90 ;;; An alist in which each entry is of the form (<generator>
91 ;;; . (<subentry> ...)) Each subentry is of the form: (<args>
92 ;;; <constructor> <system>)
93 ;;;
94 (defvar *dfun-constructors* ())
95
96 ;;;
97 ;;; If this is NIL, then the whole mechanism for caching dfun
98 ;;; constructors is turned off. The only time that makes sense is
99 ;;; when debugging LAP code.
100 ;;;
101 (defvar *enable-dfun-constructor-caching* t)
102
103 (defun get-dfun-constructor (generator &rest args)
104 (when (member generator '(emit-checking emit-caching
105 emit-in-checking-cache-p
106 emit-constant-value))
107 (loop for type in (car args)
108 if (eq type t)
109 collect type into types
110 else
111 collect 'class into types
112 finally
113 (setf (car args) types)))
114 (let* ((generator-entry (assq generator *dfun-constructors*))
115 (args-entry (assoc args (cdr generator-entry) :test #'equal)))
116 (cond ((null *enable-dfun-constructor-caching*)
117 (apply (symbol-function generator) args))
118 ((cadr args-entry)
119 (cadr args-entry))
120 (t
121 (multiple-value-bind (new not-best-p)
122 (apply (symbol-function generator) args)
123 (let ((entry (list (copy-list args)
124 new
125 (unless not-best-p 'pcl)
126 not-best-p)))
127 (if generator-entry
128 (push entry (cdr generator-entry))
129 (push (list generator entry) *dfun-constructors*)))
130 (values new not-best-p))))))
131
132 (defun load-precompiled-dfun-constructor (generator args system constructor)
133 (let* ((generator-entry (assq generator *dfun-constructors*))
134 (args-entry (assoc args (cdr generator-entry) :test #'equal)))
135 (if args-entry
136 (when (fourth args-entry)
137 (let* ((dfun-type (case generator
138 (emit-checking 'checking)
139 (emit-caching 'caching)
140 (emit-constant-value 'constant-value)
141 (emit-default-only 'default-method-only)))
142 (metatypes (car args))
143 (gfs (when dfun-type (gfs-of-type dfun-type))))
144 (dolist (gf gfs)
145 (when (and (equal metatypes
146 (arg-info-metatypes (gf-arg-info gf)))
147 (let ((gf-name (generic-function-name gf)))
148 (and (not (eq gf-name 'slot-value-using-class))
149 (not (equal gf-name
150 '(setf slot-value-using-class)))
151 (not (eq gf-name 'slot-boundp-using-class)))))
152 (update-dfun gf)))
153 (setf (second args-entry) constructor)
154 (setf (third args-entry) system)
155 (setf (fourth args-entry) nil)))
156 (let ((entry (list args constructor system nil)))
157 (if generator-entry
158 (push entry (cdr generator-entry))
159 (push (list generator entry) *dfun-constructors*))))))
160
161 (defmacro precompile-dfun-constructors (&optional system)
162 (let ((*precompiling-lap* t))
163 `(progn
164 ,@(let ((collected ()))
165 (dolist (generator-entry *dfun-constructors*
166 (nreverse collected))
167 (dolist (args-entry (cdr generator-entry))
168 (when (or (null (caddr args-entry))
169 (eq (caddr args-entry) system))
170 (when system
171 (setf (caddr args-entry) system))
172 (push
173 (make-top-level-form `(precompile-dfun-constructor
174 ,(car generator-entry))
175 '(:load-toplevel)
176 `(load-precompiled-dfun-constructor
177 ',(car generator-entry)
178 ',(car args-entry)
179 ',system
180 ,(apply (symbol-function (car generator-entry))
181 (car args-entry))))
182 collected))))))))
183
184
185 ;;; **********************************
186 ;;; Standard Class Slot Access *******
187 ;;; **********************************
188 ;;;
189 ;;; When trying to break vicious metacircles, we need a way to get at
190 ;;; the values of slots of some standard classes without going through
191 ;;; the whole meta machinery, because that would likely enter the
192 ;;; vicious circle again. That's what the following is for.
193 ;;;
194
195 (defvar *standard-classes*
196 '(standard-method standard-generic-function standard-class
197 standard-effective-slot-definition))
198
199 (defvar *standard-slot-locations* (make-hash-table :test 'equal))
200
201 (defun compute-standard-slot-locations ()
202 (clrhash *standard-slot-locations*)
203 (dolist (class-name *standard-classes*)
204 (let ((class (find-class class-name)))
205 (dolist (slot (class-slots class))
206 (setf (gethash (cons class (slot-definition-name slot))
207 *standard-slot-locations*)
208 (slot-definition-location slot))))))
209
210 (defun maybe-update-standard-class-locations (class)
211 (when (and (eq *boot-state* 'complete)
212 (memq (class-name class) *standard-classes*))
213 (compute-standard-slot-locations)))
214
215 (defun standard-slot-value (object slot-name class)
216 (let ((location (gethash (cons class slot-name) *standard-slot-locations*)))
217 (if location
218 (let ((value (if (funcallable-instance-p object)
219 (funcallable-standard-instance-access object location)
220 (standard-instance-access object location))))
221 (when (eq +slot-unbound+ value)
222 (error _"~@<Slot ~s of class ~s is unbound in object ~s~@:>"
223 slot-name class object))
224 value)
225 (error _"~@<Cannot get standard value of slot ~s of class ~s ~
226 in object ~s~@:>"
227 slot-name class object))))
228
229 (defun standard-slot-value/gf (gf slot-name)
230 (standard-slot-value gf slot-name *the-class-standard-generic-function*))
231
232 (defun standard-slot-value/method (method slot-name)
233 (standard-slot-value method slot-name *the-class-standard-method*))
234
235 (defun standard-slot-value/eslotd (slotd slot-name)
236 (standard-slot-value slotd slot-name
237 *the-class-standard-effective-slot-definition*))
238
239 (defun standard-slot-value/class (class slot-name)
240 (standard-slot-value class slot-name *the-class-standard-class*))
241
242
243 ;;;
244 ;;; When all the methods of a generic function are automatically generated
245 ;;; reader or writer methods a number of special optimizations are possible.
246 ;;; These are important because of the large number of generic functions of
247 ;;; this type.
248 ;;;
249 ;;; There are a number of cases:
250 ;;;
251 ;;; ONE-CLASS-ACCESSOR
252 ;;; In this case, the accessor generic function has only been called
253 ;;; with one class of argument. There is no cache vector, the wrapper
254 ;;; of the one class, and the slot index are stored directly as closure
255 ;;; variables of the discriminating function. This case can convert to
256 ;;; either of the next kind.
257 ;;;
258 ;;; TWO-CLASS-ACCESSOR
259 ;;; Like above, but two classes. This is common enough to do specially.
260 ;;; There is no cache vector. The two classes are stored a separate
261 ;;; closure variables.
262 ;;;
263 ;;; ONE-INDEX-ACCESSOR
264 ;;; In this case, the accessor generic function has seen more than one
265 ;;; class of argument, but the index of the slot is the same for all
266 ;;; the classes that have been seen. A cache vector is used to store
267 ;;; the wrappers that have been seen, the slot index is stored directly
268 ;;; as a closure variable of the discriminating function. This case
269 ;;; can convert to the next kind.
270 ;;;
271 ;;; N-N-ACCESSOR
272 ;;; This is the most general case. In this case, the accessor generic
273 ;;; function has seen more than one class of argument and more than one
274 ;;; slot index. A cache vector stores the wrappers and corresponding
275 ;;; slot indexes. Because each cache line is more than one element
276 ;;; long, a cache lock count is used.
277 ;;;
278 (defstruct (dfun-info
279 (:constructor nil)
280 (:print-function print-dfun-info))
281 (cache nil))
282
283 (defun print-dfun-info (dfun-info stream depth)
284 (declare (ignore depth) (stream stream))
285 (print-unreadable-object (dfun-info stream :identity t)
286 (format stream "~A" (type-of dfun-info))))
287
288 (defstruct (no-methods
289 (:constructor make-no-methods-dfun-info ())
290 (:include dfun-info)))
291
292 (defstruct (initial
293 (:constructor make-initial-dfun-info ())
294 (:include dfun-info)))
295
296 (defstruct (initial-dispatch
297 (:constructor make-initial-dispatch-dfun-info ())
298 (:include dfun-info)))
299
300 (defstruct (dispatch
301 (:constructor make-dispatch-dfun-info ())
302 (:include dfun-info)))
303
304 (defstruct (default-method-only
305 (:constructor make-default-method-only-dfun-info ())
306 (:include dfun-info)))
307
308 ;without caching:
309 ; dispatch one-class two-class default-method-only
310
311 ;with caching:
312 ; one-index n-n checking caching
313
314 ;accessor:
315 ; one-class two-class one-index n-n
316
317 (defstruct (accessor-dfun-info
318 (:constructor nil)
319 (:conc-name dfun-info-)
320 (:include dfun-info))
321 accessor-type) ; (member reader writer)
322
323 (defstruct (one-index-dfun-info
324 (:constructor nil)
325 (:conc-name dfun-info-)
326 (:include accessor-dfun-info))
327 index)
328
329 (defstruct (n-n
330 (:constructor make-n-n-dfun-info (accessor-type cache))
331 (:include accessor-dfun-info)))
332
333 (defstruct (one-class
334 (:constructor make-one-class-dfun-info
335 (accessor-type index wrapper0))
336 (:conc-name dfun-info-)
337 (:include one-index-dfun-info))
338 wrapper0)
339
340 (defstruct (two-class
341 (:constructor make-two-class-dfun-info
342 (accessor-type index wrapper0 wrapper1))
343 (:conc-name dfun-info-)
344 (:include one-class))
345 wrapper1)
346
347 (defstruct (one-index
348 (:constructor make-one-index-dfun-info
349 (accessor-type index cache))
350 (:include one-index-dfun-info)))
351
352 (defstruct (checking
353 (:constructor make-checking-dfun-info (function cache))
354 (:conc-name dfun-info-)
355 (:include dfun-info))
356 function)
357
358 (defstruct (caching
359 (:constructor make-caching-dfun-info (cache))
360 (:include dfun-info)))
361
362 (defstruct (constant-value
363 (:constructor make-constant-value-dfun-info (cache))
364 (:include dfun-info)))
365
366 (defmacro dfun-update (generic-function function &rest args)
367 `(multiple-value-bind (dfun cache info)
368 (funcall ,function ,generic-function ,@args)
369 (update-dfun ,generic-function dfun cache info)))
370
371 (defun accessor-miss-function (gf dfun-info)
372 (ecase (dfun-info-accessor-type dfun-info)
373 ((reader boundp)
374 (lambda (arg)
375 (accessor-miss gf nil arg dfun-info)))
376 (writer
377 (lambda (new arg)
378 (accessor-miss gf new arg dfun-info)))))
379
380 (declaim (freeze-type dfun-info))
381
382
383 ;;;
384 ;;; ONE-CLASS-ACCESSOR
385 ;;;
386 (defun make-one-class-accessor-dfun (gf type wrapper index)
387 (let ((emit (ecase type
388 (reader 'emit-one-class-reader)
389 (boundp 'emit-one-class-boundp)
390 (writer 'emit-one-class-writer)))
391 (dfun-info (make-one-class-dfun-info type index wrapper)))
392 (values
393 (funcall (get-dfun-constructor emit (consp index))
394 wrapper index
395 (accessor-miss-function gf dfun-info))
396 nil
397 dfun-info)))
398
399 ;;;
400 ;;; TWO-CLASS-ACCESSOR
401 ;;;
402 (defun make-two-class-accessor-dfun (gf type w0 w1 index)
403 (let ((emit (ecase type
404 (reader 'emit-two-class-reader)
405 (boundp 'emit-two-class-boundp)
406 (writer 'emit-two-class-writer)))
407 (dfun-info (make-two-class-dfun-info type index w0 w1)))
408 (values
409 (funcall (get-dfun-constructor emit (consp index))
410 w0 w1 index
411 (accessor-miss-function gf dfun-info))
412 nil
413 dfun-info)))
414
415 ;;;
416 ;;; std accessors same index dfun
417 ;;;
418 (defun make-one-index-accessor-dfun (gf type index &optional cache)
419 (let* ((emit (ecase type
420 (reader 'emit-one-index-readers)
421 (boundp 'emit-one-index-boundps)
422 (writer 'emit-one-index-writers)))
423 (cache (or cache (get-cache 1 nil 4)))
424 (dfun-info (make-one-index-dfun-info type index cache)))
425 (declare (type cache cache))
426 (values
427 (funcall (get-dfun-constructor emit (consp index))
428 cache
429 index
430 (accessor-miss-function gf dfun-info))
431 cache
432 dfun-info)))
433
434 (defun make-final-one-index-accessor-dfun (gf type index table)
435 (let ((cache (fill-dfun-cache table nil 1)))
436 (make-one-index-accessor-dfun gf type index cache)))
437
438 (defun make-n-n-accessor-dfun (gf type &optional cache)
439 (let* ((emit (ecase type
440 (reader 'emit-n-n-readers)
441 (boundp 'emit-n-n-boundps)
442 (writer 'emit-n-n-writers)))
443 (cache (or cache (get-cache 1 t 2)))
444 (dfun-info (make-n-n-dfun-info type cache)))
445 (declare (type cache cache))
446 (values
447 (funcall (get-dfun-constructor emit)
448 cache
449 (accessor-miss-function gf dfun-info))
450 cache
451 dfun-info)))
452
453 (defun make-final-n-n-accessor-dfun (gf type table)
454 (let ((cache (fill-dfun-cache table t 1)))
455 (make-n-n-accessor-dfun gf type cache)))
456
457 (defun make-checking-dfun (generic-function function &optional cache)
458 (unless cache
459 (when (use-caching-dfun-p generic-function)
460 (return-from make-checking-dfun (make-caching-dfun generic-function)))
461 (when (use-dispatch-dfun-p generic-function)
462 (return-from make-checking-dfun (make-dispatch-dfun generic-function))))
463 (multiple-value-bind (nreq applyp metatypes nkeys)
464 (get-generic-function-info generic-function)
465 (declare (ignore nreq))
466 (if (every (lambda (mt) (eq mt t)) metatypes)
467 (let ((dfun-info (make-default-method-only-dfun-info)))
468 (values
469 (funcall (get-dfun-constructor 'emit-default-only metatypes applyp)
470 function)
471 nil
472 dfun-info))
473 (let* ((cache (or cache (get-cache nkeys nil 2)))
474 (dfun-info (make-checking-dfun-info function cache)))
475 (values
476 (funcall (get-dfun-constructor 'emit-checking metatypes applyp)
477 cache
478 function
479 (lambda (&rest args)
480 (checking-miss generic-function args dfun-info)))
481 cache
482 dfun-info)))))
483
484 (defun make-final-checking-dfun (generic-function function
485 classes-list new-class)
486 (let ((metatypes (arg-info-metatypes (gf-arg-info generic-function))))
487 (if (every (lambda (mt) (eq mt t)) metatypes)
488 (values (lambda (&rest args)
489 (invoke-emf function args))
490 nil
491 (make-default-method-only-dfun-info))
492 (let ((cache (make-final-ordinary-dfun-internal
493 generic-function nil
494 classes-list new-class)))
495 (make-checking-dfun generic-function function cache)))))
496
497 (defun use-caching-dfun-p (gf)
498 (some (lambda (method)
499 (let ((fmf (if (listp method)
500 (third method)
501 (method-fast-function method))))
502 (method-function-get fmf :slot-name-lists)))
503 (generic-function-methods* gf)))
504
505
506 ;;;
507 ;;;
508 ;;;
509 (defun make-caching-dfun (generic-function &optional cache)
510 (unless cache
511 (when (use-constant-value-dfun-p generic-function)
512 (return-from make-caching-dfun (make-constant-value-dfun generic-function)))
513 (when (use-dispatch-dfun-p generic-function)
514 (return-from make-caching-dfun (make-dispatch-dfun generic-function))))
515 (multiple-value-bind (nreq applyp metatypes nkeys)
516 (get-generic-function-info generic-function)
517 (declare (ignore nreq))
518 (let* ((cache (or cache (get-cache nkeys t 2)))
519 (dfun-info (make-caching-dfun-info cache)))
520 (values
521 (funcall (get-dfun-constructor 'emit-caching metatypes applyp)
522 cache
523 (lambda (&rest args)
524 (caching-miss generic-function args dfun-info)))
525 cache
526 dfun-info))))
527
528 (defun make-final-caching-dfun (generic-function classes-list new-class)
529 (let ((cache (make-final-ordinary-dfun-internal
530 generic-function t classes-list new-class)))
531 (make-caching-dfun generic-function cache)))
532
533 (defun insure-caching-dfun (gf)
534 (multiple-value-bind (nreq applyp metatypes nkeys)
535 (get-generic-function-info gf)
536 (declare (ignore nreq nkeys))
537 (when (and metatypes
538 (not (null (car metatypes)))
539 (dolist (mt metatypes nil)
540 (unless (eq mt t) (return t))))
541 (get-dfun-constructor 'emit-caching metatypes applyp))))
542
543 (defun use-constant-value-dfun-p (gf &optional boolean-values-p)
544 (multiple-value-bind (nreq applyp metatypes nkeys)
545 (get-generic-function-info gf)
546 (declare (ignore nreq metatypes nkeys))
547 (let ((methods (generic-function-methods* gf)))
548 (and (null applyp)
549 (or (not (eq *boot-state* 'complete))
550 ;;
551 ;; If COMPUTE-APPLICABLE-METHODS is specialized, we
552 ;; can't use this, of course, because we can't tell
553 ;; which methods will be considered applicable.
554 ;;
555 ;; Also, don't use this method if the generic function
556 ;; has a non-standard method combination, because if it
557 ;; has, it's not sure that method functions are used
558 ;; directly as effective methods, which
559 ;; CONSTANT-VALUE-MISS depends on. The pre-defined
560 ;; method combinations like LIST are examples of that.
561 (and (compute-applicable-methods-emf-std-p gf)
562 (eq (generic-function-method-combination gf)
563 *standard-method-combination*)))
564 ;;
565 ;; Check that no method is eql-specialized, and that all
566 ;; methods return a constant value. If BOOLEAN-VALUES-P,
567 ;; check that all return T or NIL. Also, check that no
568 ;; method has specializers, to make sure that emfs are
569 ;; really method functions; see above.
570 (dolist (method methods t)
571 (when (eq *boot-state* 'complete)
572 (when (or (some #'eql-specializer-p
573 (method-specializers method))
574 (method-qualifiers method))
575 (return nil)))
576 (let* ((mf (if (consp method)
577 (or (third method) (second method))
578 (or (method-fast-function method)
579 (method-function method))))
580 (constant (method-function-get mf :constant-value mf)))
581 (when (or (eq constant mf)
582 (and boolean-values-p
583 (not (member constant '(t nil)))))
584 (return nil))))))))
585
586 (defun make-constant-value-dfun (generic-function &optional cache)
587 (multiple-value-bind (nreq applyp metatypes nkeys)
588 (get-generic-function-info generic-function)
589 (declare (ignore nreq applyp))
590 (let* ((cache (or cache (get-cache nkeys t 2)))
591 (dfun-info (make-constant-value-dfun-info cache)))
592 (values
593 (funcall (get-dfun-constructor 'emit-constant-value metatypes)
594 cache
595 (lambda (&rest args)
596 (constant-value-miss generic-function args dfun-info)))
597 cache
598 dfun-info))))
599
600 (defun make-final-constant-value-dfun (generic-function classes-list new-class)
601 (let ((cache (make-final-ordinary-dfun-internal
602 generic-function :constant-value
603 classes-list new-class)))
604 (make-constant-value-dfun generic-function cache)))
605
606 (defun use-dispatch-dfun-p (gf &optional (caching-p (use-caching-dfun-p gf)))
607 (when (eq *boot-state* 'complete)
608 (unless (or caching-p (emfs-must-check-applicable-keywords-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.
615
616 #||;;; Original found in cmu 17f -- S L O W
617 (< (dispatch-dfun-cost gf) (caching-dfun-cost gf))
618 ||#
619 ;; This uses improved dispatch-dfun-cost below
620 (let ((cdc (caching-dfun-cost gf))) ; fast
621 (> cdc (dispatch-dfun-cost gf cdc))))))
622
623 ;; Try this on print-object, find-method-combination, and documentation.
624 ;; Look at pcl/generic-functions.lisp for other potential test cases.
625 (defun show-dfun-costs (gf)
626 (when (or (symbolp gf) (consp gf))
627 (setq gf (gdefinition gf)))
628 (format t _"~&Name ~S caching cost ~D dispatch cost ~D~%"
629 (generic-function-name gf)
630 (caching-dfun-cost gf)
631 (dispatch-dfun-cost gf)))
632
633 (defparameter *non-built-in-typep-cost* 1)
634 (defparameter *structure-typep-cost* 1)
635 (defparameter *built-in-typep-cost* 0)
636
637 ;; This version is from the pcl found in the gcl-2.1 distribution.
638 ;; Someone added a cost limit so as to keep the execution time controlled
639 (defun dispatch-dfun-cost (gf &optional limit)
640 (generate-discrimination-net-internal
641 gf (generic-function-methods gf) nil
642 (lambda (methods known-types)
643 (declare (ignore methods known-types))
644 0)
645 (lambda (position type true-value false-value)
646 (declare (ignore position))
647 (let* ((type-test-cost
648 (if (eq 'class (car type))
649 (let* ((metaclass (class-of (cadr type)))
650 (mcpl (class-precedence-list metaclass)))
651 (cond ((memq *the-class-built-in-class* mcpl)
652 *built-in-typep-cost*)
653 ((memq *the-class-structure-class* mcpl)
654 *structure-typep-cost*)
655 (t
656 *non-built-in-typep-cost*)))
657 0))
658 (max-cost-so-far
659 (+ (max true-value false-value) type-test-cost)))
660 (when (and limit (<= limit max-cost-so-far))
661 (return-from dispatch-dfun-cost max-cost-so-far))
662 max-cost-so-far))
663 #'identity))
664
665
666 (defparameter *cache-lookup-cost* 1)
667 (defparameter *wrapper-of-cost* 0)
668 (defparameter *secondary-dfun-call-cost* 1)
669
670 (defun caching-dfun-cost (gf)
671 (let* ((arg-info (gf-arg-info gf))
672 (nreq (length (arg-info-metatypes arg-info))))
673 (+ *cache-lookup-cost*
674 (* *wrapper-of-cost* nreq)
675 (if (methods-contain-eql-specializer-p
676 (generic-function-methods gf))
677 *secondary-dfun-call-cost*
678 0))))
679
680 (progn
681 (setq *non-built-in-typep-cost* 100)
682 (setq *structure-typep-cost* 15)
683 (setq *built-in-typep-cost* 5)
684 (setq *cache-lookup-cost* 30)
685 (setq *wrapper-of-cost* 15)
686 (setq *secondary-dfun-call-cost* 30))
687
688
689 (defun make-dispatch-dfun (gf)
690 (values (get-dispatch-function gf) nil (make-dispatch-dfun-info)))
691
692 (defun get-dispatch-function (gf)
693 (let* ((methods (generic-function-methods gf))
694 (generator (get-secondary-dispatch-function1
695 gf methods nil nil nil nil nil t)))
696 (make-callable gf methods generator nil nil)))
697
698 (defun make-final-dispatch-dfun (gf)
699 (make-dispatch-dfun gf))
700
701 (defun update-dispatch-dfuns ()
702 (dolist (gf (gfs-of-type '(dispatch initial-dispatch)))
703 (dfun-update gf #'make-dispatch-dfun)))
704
705 (defun fill-dfun-cache (table valuep nkeys &optional cache)
706 (let ((cache (or cache (get-cache nkeys valuep
707 (+ (hash-table-count table) 3)))))
708 (maphash (lambda (classes value)
709 (setq cache (fill-cache cache
710 (class-wrapper classes)
711 value)))
712 table)
713 cache))
714
715 (defun make-final-ordinary-dfun-internal (generic-function valuep
716 classes-list new-class)
717 (let* ((arg-info (gf-arg-info generic-function))
718 (nkeys (arg-info-nkeys arg-info))
719 (new-class (and new-class
720 (equal (type-of (gf-dfun-info generic-function))
721 (cond ((eq valuep t)
722 'caching)
723 ((eq valuep :constant-value)
724 'constant-value)
725 ((null valuep)
726 'checking)))
727 new-class))
728 (cache (if new-class
729 (copy-cache (gf-dfun-cache generic-function))
730 (get-cache nkeys (not (null valuep)) 4))))
731 (make-emf-cache generic-function valuep cache classes-list new-class)))
732
733 (defvar *dfun-miss-gfs-on-stack* ())
734
735 (defmacro dfun-miss ((gf args wrappers invalidp nemf
736 &optional type index caching-p applicable)
737 &body body)
738 (unless applicable
739 (setq applicable (gensym)))
740 `(multiple-value-bind (,nemf ,applicable ,wrappers ,invalidp
741 ,@(when type `(,type ,index)))
742 (cache-miss-values ,gf ,args ',(cond (caching-p 'caching)
743 (type 'accessor)
744 (t 'checking)))
745 (when (and ,applicable (not (memq ,gf *dfun-miss-gfs-on-stack*)))
746 (let ((*dfun-miss-gfs-on-stack* (cons ,gf *dfun-miss-gfs-on-stack*)))
747 ,@body))
748 ;;
749 ;; Create a FAST-INSTANCE-BOUNDP structure instance for a cached
750 ;; SLOT-BOUNDP so that INVOKE-EMF does the right thing, that is,
751 ;; does not signal a SLOT-UNBOUND error for a boundp test.
752 ,@(if type
753 `((if (and (eq ,type 'boundp) (integerp ,nemf))
754 (invoke-emf (make-fast-instance-boundp :index ,nemf) ,args)
755 (invoke-emf ,nemf ,args)))
756 `((invoke-emf ,nemf ,args)))))
757
758 ;;;
759 ;;; The dynamically adaptive method lookup algorithm is implemented is
760 ;;; implemented as a kind of state machine. The kinds of discriminating
761 ;;; function is the state, the various kinds of reasons for a cache miss
762 ;;; are the state transitions.
763 ;;;
764 ;;; The code which implements the transitions is all in the miss handlers
765 ;;; for each kind of dfun. Those appear here.
766 ;;;
767 ;;; Note that within the states that cache, there are dfun updates which
768 ;;; simply select a new cache or cache field. Those are not considered
769 ;;; as state transitions.
770 ;;;
771 (defvar *early-p* nil)
772
773 (defvar *max-emf-precomputation-methods* 100
774 "Precompute effective methods at method load time if the generic
775 function has less than this number of methods. If zero,
776 no effective methods are precomputed at method load time.")
777
778 ;;;
779 ;;; Try to finalize all unfinalized class specializers of all methods
780 ;;; of generic function GF. Value is true if successful.
781 ;;;
782 (defun finalize-specializers (gf)
783 (let ((methods (generic-function-methods gf)))
784 (when (< (length methods) *max-emf-precomputation-methods*)
785 (let ((all-finalized t))
786 (dolist (method (generic-function-methods gf) all-finalized)
787 (dolist (specializer (method-specializers method))
788 (when (and (classp specializer)
789 (not (class-finalized-p specializer)))
790 (if (class-has-a-forward-referenced-superclass-p specializer)
791 (setq all-finalized nil)
792 (finalize-inheritance specializer)))))))))
793
794 (defun make-initial-dfun (gf)
795 (let ((initial-dfun
796 #'(kernel:instance-lambda (&rest args)
797 (initial-dfun gf args))))
798 (multiple-value-bind (dfun cache info)
799 (cond ((and (eq *boot-state* 'complete)
800 (not (finalize-specializers gf)))
801 (values initial-dfun nil (make-initial-dfun-info)))
802
803 ((and (eq *boot-state* 'complete)
804 (compute-applicable-methods-emf-std-p gf))
805 (let* ((caching-p (use-caching-dfun-p gf)))
806 ;;
807 ;; The call to PRECOMPUTE-EFFECTIVE-METHODS should
808 ;; not be removed, because effective method
809 ;; computation will detect invalid methods (invalid
810 ;; qualifiers) early, which is desirable.
811 (precompute-effective-methods gf caching-p)
812 (cond ((use-dispatch-dfun-p gf caching-p)
813 (values initial-dfun nil (make-initial-dispatch-dfun-info)))
814 (caching-p
815 (insure-caching-dfun gf)
816 (values initial-dfun nil (make-initial-dfun-info)))
817 (t
818 (values initial-dfun nil (make-initial-dfun-info))))))
819 (t
820 (let ((arg-info (gf-arg-info* gf))
821 (type nil))
822 (if (and (gf-precompute-dfun-and-emf-p arg-info)
823 (setq type (final-accessor-dfun-type gf)))
824 (if *early-p*
825 (values (make-early-accessor gf type) nil nil)
826 (make-final-accessor-dfun gf type))
827 (values initial-dfun nil (make-initial-dfun-info))))))
828 (set-dfun gf dfun cache info))))
829
830 (defun make-early-accessor (gf type)
831 (let* ((methods (early-gf-methods gf))
832 (slot-name (early-method-standard-accessor-slot-name (car methods))))
833 (ecase type
834 (reader #'(kernel:instance-lambda (instance)
835 (let* ((class (class-of instance))
836 (class-name (bootstrap-get-slot 'class class 'name)))
837 (bootstrap-get-slot class-name instance slot-name))))
838 (boundp #'(kernel:instance-lambda (instance)
839 (let* ((class (class-of instance))
840 (class-name (bootstrap-get-slot 'class class 'name)))
841 (not (eq +slot-unbound+
842 (bootstrap-get-slot class-name instance
843 slot-name))))))
844 (writer #'(kernel:instance-lambda (new-value instance)
845 (let* ((class (class-of instance))
846 (class-name (bootstrap-get-slot 'class class 'name)))
847 (bootstrap-set-slot class-name instance slot-name new-value)))))))
848
849 (defun initial-dfun (gf args)
850 (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
851 (cond (invalidp)
852 ((and ntype nindex)
853 (dfun-update gf #'make-one-class-accessor-dfun
854 ntype wrappers nindex))
855 ((use-caching-dfun-p gf)
856 (dfun-update gf #'make-caching-dfun))
857 (t
858 (dfun-update gf #'make-checking-dfun
859 (cache-miss-values gf args 'checking))))))
860
861 (defun make-final-dfun (gf &optional classes-list)
862 (multiple-value-bind (dfun cache info)
863 (make-final-dfun-internal gf classes-list)
864 (set-dfun gf dfun cache info)))
865
866 (defun update-dfun (gf &optional dfun cache info)
867 (let* ((early-p (early-gf-p gf))
868 (gf-name (generic-function-name* gf)))
869 (set-dfun gf dfun cache info)
870 (let ((dfun (if early-p
871 (or dfun (make-initial-dfun gf))
872 (compute-discriminating-function gf))))
873 (set-funcallable-instance-function gf dfun)
874 (set-function-name gf gf-name)
875 (update-pv-calls-for-gf gf)
876 dfun)))
877
878 (defun gfs-of-type (type)
879 (unless (consp type) (setq type (list type)))
880 (let ((gf-list nil))
881 (map-all-generic-functions (lambda (gf)
882 (when (memq (type-of (gf-dfun-info gf)) type)
883 (push gf gf-list))))
884 gf-list))
885
886 (defvar *new-class* nil)
887
888 (defvar *free-hash-tables* (mapcar #'list '(eq equal eql)))
889
890 (defmacro with-hash-table ((table test) &body forms)
891 `(let* ((.free. (assoc ',test *free-hash-tables*))
892 (,table (if (cdr .free.)
893 (pop (cdr .free.))
894 (make-hash-table :test ',test))))
895 (multiple-value-prog1
896 (progn ,@forms)
897 (clrhash ,table)
898 (push ,table (cdr .free.)))))
899
900 (defmacro with-eq-hash-table ((table) &body forms)
901 `(with-hash-table (,table eq) ,@forms))
902
903 (defun final-accessor-dfun-type (gf)
904 (let ((methods (generic-function-methods* gf)))
905 (cond ((every (lambda (method)
906 (if (consp method)
907 (eq *the-class-standard-reader-method*
908 (early-method-class method))
909 (standard-reader-method-p method)))
910 methods)
911 'reader)
912 ((every (lambda (method)
913 (if (consp method)
914 (eq *the-class-standard-boundp-method*
915 (early-method-class method))
916 (standard-boundp-method-p method)))
917 methods)
918 'boundp)
919 ((every (lambda (method)
920 (if (consp method)
921 (eq *the-class-standard-writer-method*
922 (early-method-class method))
923 (standard-writer-method-p method)))
924 methods)
925 'writer))))
926
927 (defun make-final-accessor-dfun (gf type &optional classes-list new-class)
928 (with-eq-hash-table (table)
929 (multiple-value-bind (table all-index first second size no-class-slots-p)
930 (make-accessor-table gf type table)
931 (if table
932 (cond ((= size 1)
933 (let ((w (class-wrapper first)))
934 (make-one-class-accessor-dfun gf type w all-index)))
935 ((and (= size 2) (or (integerp all-index) (consp all-index)))
936 (let ((w0 (class-wrapper first))
937 (w1 (class-wrapper second)))
938 (make-two-class-accessor-dfun gf type w0 w1 all-index)))
939 ((or (integerp all-index) (consp all-index))
940 (make-final-one-index-accessor-dfun
941 gf type all-index table))
942 (no-class-slots-p
943 (make-final-n-n-accessor-dfun gf type table))
944 (t
945 (make-final-caching-dfun gf classes-list new-class)))
946 (make-final-caching-dfun gf classes-list new-class)))))
947
948 (defun make-final-dfun-internal (gf &optional classes-list)
949 (let ((methods (generic-function-methods gf)) type
950 (new-class *new-class*) (*new-class* nil)
951 specls all-same-p)
952 (cond ((null methods)
953 (values
954 #'(kernel:instance-lambda (&rest args)
955 (apply #'no-applicable-method gf args))
956 nil
957 (make-no-methods-dfun-info)))
958 ((setq type (final-accessor-dfun-type gf))
959 (make-final-accessor-dfun gf type classes-list new-class))
960 ((and (not (and (every (lambda (specl) (eq specl *the-class-t*))
961 (setq specls (method-specializers (car methods))))
962 (setq all-same-p
963 (every (lambda (method)
964 (and (equal specls
965 (method-specializers method))))
966 methods))))
967 (use-constant-value-dfun-p gf))
968 (make-final-constant-value-dfun gf classes-list new-class))
969 ((use-dispatch-dfun-p gf)
970 (make-final-dispatch-dfun gf))
971 ((and all-same-p (not (use-caching-dfun-p gf)))
972 (let ((emf (get-secondary-dispatch-function gf methods nil)))
973 (make-final-checking-dfun gf emf classes-list new-class)))
974 (t
975 (make-final-caching-dfun gf classes-list new-class)))))
976
977 (defun accessor-miss (gf new object dfun-info)
978 (let* ((ostate (type-of dfun-info))
979 (otype (dfun-info-accessor-type dfun-info))
980 oindex ow0 ow1 cache
981 (args (ecase otype
982 ((reader boundp) (list object))
983 (writer (list new object)))))
984 (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
985 ;;
986 ;; The following lexical functions change the state of the
987 ;; dfun to that which is their name. They accept arguments
988 ;; which are the parameters of the new state, and get other
989 ;; information from the lexical variables bound above.
990 ;;
991 (flet ((two-class (index w0 w1)
992 (when (zerop (random 2)) (psetf w0 w1 w1 w0))
993 (dfun-update gf #'make-two-class-accessor-dfun
994 ntype w0 w1 index))
995 (one-index (index &optional cache)
996 (dfun-update gf #'make-one-index-accessor-dfun
997 ntype index cache))
998 (n-n (&optional cache)
999 (if (consp nindex)
1000 (dfun-update gf #'make-checking-dfun nemf)
1001 (dfun-update gf #'make-n-n-accessor-dfun ntype cache)))
1002 (caching () ; because cached accessor emfs are much faster for accessors
1003 (dfun-update gf #'make-caching-dfun))
1004 ;;
1005 (do-fill (update-fn)
1006 (let ((ncache (fill-cache cache wrappers nindex)))
1007 (unless (eq ncache cache)
1008 (funcall update-fn ncache)))))
1009 (cond ((null ntype)
1010 (caching))
1011 ((or invalidp
1012 (null nindex)))
1013 ((not (pcl-instance-p object))
1014 (caching))
1015 ((or (neq ntype otype) (listp wrappers))
1016 (caching))
1017 (t
1018 (ecase ostate
1019 (one-class
1020 (setq oindex (dfun-info-index dfun-info))
1021 (setq ow0 (dfun-info-wrapper0 dfun-info))
1022 (unless (eq ow0 wrappers)
1023 (if (eql nindex oindex)
1024 (two-class nindex ow0 wrappers)
1025 (n-n))))
1026 (two-class
1027 (setq oindex (dfun-info-index dfun-info))
1028 (setq ow0 (dfun-info-wrapper0 dfun-info))
1029 (setq ow1 (dfun-info-wrapper1 dfun-info))
1030 (unless (or (eq ow0 wrappers) (eq ow1 wrappers))
1031 (if (eql nindex oindex)
1032 (one-index nindex)
1033 (n-n))))
1034 (one-index
1035 (setq oindex (dfun-info-index dfun-info))
1036 (setq cache (dfun-info-cache dfun-info))
1037 (if (eql nindex oindex)
1038 (do-fill (lambda (ncache)
1039 (one-index nindex ncache)))
1040 (n-n)))
1041 (n-n
1042 (setq cache (dfun-info-cache dfun-info))
1043 (if (consp nindex)
1044 (caching)
1045 (do-fill #'n-n))))))))))
1046
1047 (defun checking-miss (generic-function args dfun-info)
1048 (let ((oemf (dfun-info-function dfun-info))
1049 (cache (dfun-info-cache dfun-info)))
1050 (dfun-miss (generic-function args wrappers invalidp nemf)
1051 (cond (invalidp)
1052 ((eq oemf nemf)
1053 (let ((ncache (fill-cache cache wrappers nil)))
1054 (unless (eq ncache cache)
1055 (dfun-update generic-function #'make-checking-dfun
1056 nemf ncache))))
1057 (t
1058 (dfun-update generic-function #'make-caching-dfun))))))
1059
1060 (defun caching-miss (generic-function args dfun-info)
1061 (let ((ocache (dfun-info-cache dfun-info)))
1062 (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
1063 (cond (invalidp)
1064 (t
1065 (let ((ncache (fill-cache ocache wrappers emf)))
1066 (unless (eq ncache ocache)
1067 (dfun-update generic-function
1068 #'make-caching-dfun ncache))))))))
1069
1070 (defun constant-value-miss (gf args dfun-info)
1071 (let ((ocache (dfun-info-cache dfun-info)))
1072 (dfun-miss (gf args wrappers invalidp emf nil nil t)
1073 (unless invalidp
1074 (let* ((mf (typecase emf
1075 (fast-method-call (fast-method-call-function emf))
1076 (method-call (method-call-function emf))))
1077 (value (let ((val (method-function-get mf :constant-value
1078 '.not-found.)))
1079 ;;
1080 ;; This way of retrieving the constant value
1081 ;; assumes that the emf is actually a method
1082 ;; function that's used directly as effective
1083 ;; method function. Let's check that is
1084 ;; indeed the case, because it sometimes
1085 ;; wasn't.
1086 (assert (neq val '.not-found.))
1087 val))
1088 (ncache (fill-cache ocache wrappers value)))
1089 (unless (eq ncache ocache)
1090 (dfun-update gf #'make-constant-value-dfun ncache)))))))
1091
1092 ;;; Given a generic function and a set of arguments to that generic function,
1093 ;;; returns a mess of values.
1094 ;;;
1095 ;;; <function> The compiled effective method function for this set of
1096 ;;; arguments.
1097 ;;;
1098 ;;; <applicable> Sorted list of applicable methods.
1099 ;;;
1100 ;;; <wrappers> Is a single wrapper if the generic function has only
1101 ;;; one key, that is arg-info-nkeys of the arg-info is 1.
1102 ;;; Otherwise a list of the wrappers of the specialized
1103 ;;; arguments to the generic function.
1104 ;;;
1105 ;;; Note that all these wrappers are valid. This function
1106 ;;; does invalid wrapper traps when it finds an invalid
1107 ;;; wrapper and then returns the new, valid wrapper.
1108 ;;;
1109 ;;; <invalidp> True if any of the specialized arguments had an invalid
1110 ;;; wrapper, false otherwise.
1111 ;;;
1112 ;;; <type> READER or WRITER when the only method that would be run
1113 ;;; is a standard reader or writer method. To be specific,
1114 ;;; the value is READER when the method combination is eq to
1115 ;;; *standard-method-combination*; there are no applicable
1116 ;;; :before, :after or :around methods; and the most specific
1117 ;;; primary method is a standard reader method.
1118 ;;;
1119 ;;; <index> If <type> is READER or WRITER, and the slot accessed is
1120 ;;; an :instance slot, this is the index number of that slot
1121 ;;; in the object argument.
1122 ;;;
1123 ;;; Only used in this file.
1124 ;;;
1125 (defun cache-miss-values (gf args state)
1126 (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
1127 (get-generic-function-info gf)
1128 (declare (ignore nreq applyp nkeys))
1129 (with-dfun-wrappers (args metatypes)
1130 (dfun-wrappers invalid-wrapper-p wrappers classes types)
1131 #+nil
1132 (error _"~@<The function ~S requires at least ~D arguments.~@:>"
1133 gf (length metatypes))
1134 (error 'kernel:simple-program-error
1135 :name gf
1136 :format-control _"~<The function ~S requires at least ~D arguments.~@:>"
1137 :format-arguments (list gf (length metatypes)))
1138 (multiple-value-bind (emf methods accessor-type index)
1139 (cache-miss-values-internal gf arg-info wrappers classes types state)
1140 (values emf methods
1141 dfun-wrappers
1142 invalid-wrapper-p
1143 accessor-type index)))))
1144
1145 ;;;
1146 ;;; Called from vector.lisp
1147 ;;;
1148 (defvar *cmv-stack* ())
1149
1150 (defun cache-miss-values-internal (gf arg-info wrappers classes types state)
1151 (if (and (eq state 'accessor)
1152 wrappers
1153 (equal wrappers (cdr (assq gf *cmv-stack*))))
1154 (break-vicious-metacircle gf classes arg-info)
1155 (let ((*cmv-stack* (cons (cons gf wrappers) *cmv-stack*))
1156 (cam-std-p (or (null arg-info)
1157 (gf-info-c-a-m-emf-std-p arg-info))))
1158 (multiple-value-bind (methods all-applicable-and-sorted-p)
1159 (if cam-std-p
1160 (compute-applicable-methods-using-types gf types)
1161 (compute-applicable-methods-using-classes gf classes))
1162 (let* ((for-accessor-p (eq state 'accessor))
1163 (for-cache-p (memq state '(caching accessor)))
1164 (emf (if (or cam-std-p all-applicable-and-sorted-p)
1165 (let ((generator (get-secondary-dispatch-function1
1166 gf methods types nil
1167 (and for-cache-p wrappers)
1168 all-applicable-and-sorted-p)))
1169 ;;
1170 ;; MAKE-CALLABLE can call GET-METHOD-FUNCTION,
1171 ;; which can do a PV-TABLE-LOOKUP, which
1172 ;; can call CACHE-MISS-VALUES-INTERNAL again
1173 ;; for the same emf we are computing.
1174 (make-callable gf methods generator nil
1175 (and for-cache-p wrappers)))
1176 (default-secondary-dispatch-function gf))))
1177 (multiple-value-bind (index accessor-type)
1178 (and for-accessor-p all-applicable-and-sorted-p methods
1179 (accessor-values gf arg-info classes methods))
1180 (values (if (integerp index) index emf)
1181 methods accessor-type index)))))))
1182
1183 ;;;
1184 ;;; Try to break a vicious circle while computing a cache miss.
1185 ;;; GF is the generic function, CLASSES are the classes of actual
1186 ;;; arguments, and ARG-INFO is the generic functions' arg-info.
1187 ;;;
1188 ;;; A vicious circle can be entered when the computation of the cache
1189 ;;; miss values itself depends on the values being computed. For
1190 ;;; instance, adding a method which is an instance of a subclass of
1191 ;;; STANDARD-METHOD leads to cache misses for slot accessors of
1192 ;;; STANDARD-METHOD like METHOD-SPECIALIZERS, and METHOD-SPECIALIZERS
1193 ;;; is itself used while we compute cache miss values.
1194 ;;;
1195 (defun break-vicious-metacircle (gf classes arg-info)
1196 (when (typep gf 'standard-generic-function)
1197 (multiple-value-bind (class slotd accessor-type)
1198 (accesses-standard-class-slot-p gf)
1199 (when class
1200 (let ((method (find-standard-class-accessor-method
1201 gf class accessor-type))
1202 (index (standard-slot-value/eslotd slotd 'location))
1203 (type (gf-info-simple-accessor-type arg-info)))
1204 (when (and method
1205 (subtypep (if (eq accessor-type 'reader)
1206 (car classes)
1207 (cadr classes))
1208 class))
1209 (return-from break-vicious-metacircle
1210 (values index (list method) type index)))))))
1211 (kernel:infinite-error-protect
1212 (error _"~@<Vicious metacircle: The computation of an ~
1213 effective method of ~s for arguments of types ~s uses ~
1214 the effective method being computed.~@:>"
1215 gf classes)))
1216
1217 ;;;
1218 ;;; Return (CLASS SLOTD ACCESSOR-TYPE) if some method of generic
1219 ;;; function GF accesses a slot of some class in *STANDARD-CLASSES*.
1220 ;;; CLASS is the class accessed, SLOTD is the effective slot definition
1221 ;;; object of the slot accessed, and ACCESSOR-TYPE is one of the symbols
1222 ;;; READER or WRITER describing the slot access.
1223 ;;;
1224 (defun accesses-standard-class-slot-p (gf)
1225 (flet ((standard-class-slot-access (gf class)
1226 (loop with gf-name = (standard-slot-value/gf gf 'name)
1227 for slotd in (standard-slot-value/class class 'slots)
1228 as readers = (standard-slot-value/eslotd slotd 'readers)
1229 as writers = (standard-slot-value/eslotd slotd 'writers)
1230 if (member gf-name readers :test #'equal)
1231 return (values slotd 'reader)
1232 else if (member gf-name writers :test #'equal)
1233 return (values slotd 'writer))))
1234 (dolist (class-name *standard-classes*)
1235 (let ((class (find-class class-name)))
1236 (multiple-value-bind (slotd accessor-type)
1237 (standard-class-slot-access gf class)
1238 (when slotd
1239 (return (values class slotd accessor-type))))))))
1240
1241 ;;;
1242 ;;; Find a slot reader/writer method among the methods of generic
1243 ;;; function GF which reads/writes instances of class CLASS.
1244 ;;; TYPE is one of the symbols READER or WRITER.
1245 ;;;
1246 (defun find-standard-class-accessor-method (gf class type)
1247 (dolist (method (standard-slot-value/gf gf 'methods))
1248 (let ((specializers (standard-slot-value/method method 'specializers))
1249 (qualifiers (plist-value method 'qualifiers)))
1250 (when (and (null qualifiers)
1251 (eq (ecase type
1252 (reader (car specializers))
1253 (writer (cadr specializers)))
1254 class))
1255 (return method)))))
1256
1257 ;;;
1258 ;;; Only used in this file.
1259 ;;;
1260 (defun accessor-values (gf arg-info classes methods)
1261 (declare (ignore gf))
1262 (let* ((accessor-type (gf-info-simple-accessor-type arg-info))
1263 (accessor-class (case accessor-type
1264 ((reader boundp) (car classes))
1265 (writer (cadr classes)))))
1266 (accessor-values-internal accessor-type accessor-class methods)))
1267
1268 ;;;
1269 ;;; Called from vector.lisp
1270 ;;;
1271 (defun accessor-values1 (gf accessor-type accessor-class)
1272 (let* ((type `(class-eq ,accessor-class))
1273 (types (ecase accessor-type
1274 (writer `(t ,type))
1275 ((reader boundp) `(,type))))
1276 (methods (compute-applicable-methods-using-types gf types)))
1277 (accessor-values-internal accessor-type accessor-class methods)))
1278
1279 ;;;
1280 ;;; Only used in this file.
1281 ;;;
1282 (declaim (inline cpl-maybe-early slot-location-maybe-early))
1283
1284 (defun cpl-maybe-early (class)
1285 (if (eq *boot-state* 'complete)
1286 (class-precedence-list class)
1287 (early-class-precedence-list class)))
1288
1289 ;;;
1290 ;;; Return the class precedence list of CLASS or null if we can't
1291 ;;; tell, which is the case when the class isn't finalized yet.
1292 ;;;
1293 (defun cpl-or-nil (class)
1294 (if (eq *boot-state* 'complete)
1295 (when (cpl-available-p class)
1296 (class-precedence-list class))
1297 (early-class-precedence-list class)))
1298
1299 (defun slot-location-maybe-early (slotd)
1300 (if (eq *boot-state* 'complete)
1301 (slot-definition-location slotd)
1302 (early-slot-definition-location slotd)))
1303
1304 ;;;
1305 ;;; Only used in this file.
1306 ;;;
1307 (defun accessor-values-internal (accessor-type accessor-class methods)
1308 (when (some (lambda (method)
1309 (if (consp method)
1310 (early-method-qualifiers method)
1311 (method-qualifiers method)))
1312 methods)
1313 (return-from accessor-values-internal (values nil nil)))
1314 (let* ((meth (car methods))
1315 (early-p (not (eq *boot-state* 'complete)))
1316 (slot-name (when accessor-class
1317 (if (consp meth)
1318 (and (early-method-standard-accessor-p meth)
1319 (early-method-standard-accessor-slot-name meth))
1320 (and (member *the-class-standard-object*
1321 (cpl-maybe-early accessor-class))
1322 (if early-p
1323 (not (eq *the-class-standard-method*
1324 (early-method-class meth)))
1325 (standard-accessor-method-p meth))
1326 (if early-p
1327 (early-accessor-method-slot-name meth)
1328 (accessor-method-slot-name meth))))))
1329 (slotd (and accessor-class
1330 (if early-p
1331 (dolist (slot (early-class-slotds accessor-class) nil)
1332 (when (eql slot-name (early-slot-definition-name slot))
1333 (return slot)))
1334 (find-slot-definition accessor-class slot-name)))))
1335 (when (and slotd
1336 (or early-p
1337 (slot-accessor-std-p slotd accessor-type)))
1338 (values (slot-location-maybe-early slotd)
1339 accessor-type))))
1340
1341 ;;;
1342 ;;; Only used in this file.
1343 ;;;
1344 (defun make-accessor-table (gf type table)
1345 (let ((table (or table (make-hash-table :test 'eq)))
1346 (methods (generic-function-methods* gf))
1347 (all-index nil)
1348 (no-class-slots-p t)
1349 (early-p (not (eq *boot-state* 'complete)))
1350 first second (size 0))
1351 (declare (fixnum size))
1352 (flet ((precedence (class)
1353 (cpl-maybe-early class))
1354 (slot-name (method)
1355 (if (consp method)
1356 (and (early-method-standard-accessor-p method)
1357 (early-method-standard-accessor-slot-name method))
1358 (accessor-method-slot-name method)))
1359 (slot-location (slotd)
1360 (slot-location-maybe-early slotd)))
1361 ;; class -> {(specl slotd)}
1362 (dolist (method methods)
1363 (let* ((specializers (if (consp method)
1364 (early-method-specializers method t)
1365 (method-specializers method)))
1366 (specl (ecase type
1367 ((reader boundp) (car specializers))
1368 (writer (cadr specializers))))
1369 (specl-cpl (precedence specl))
1370 (so-p (member *the-class-standard-object* specl-cpl))
1371 (slot-name (slot-name method)))
1372 (when (or (null specl-cpl)
1373 (member *the-class-structure-object* specl-cpl))
1374 (return-from make-accessor-table nil))
1375 (maphash (lambda (class slotd)
1376 (let ((cpl (precedence class)))
1377 (when (memq specl cpl)
1378 (unless (and (or so-p
1379 (member *the-class-standard-object*
1380 cpl))
1381 (or early-p
1382 (slot-accessor-std-p slotd type)))
1383 (return-from make-accessor-table nil))
1384 (push (cons specl slotd) (gethash class table)))))
1385 (slot-name->class-table slot-name))))
1386 (maphash (lambda (class specl+slotd-list)
1387 (dolist (sclass (precedence class)
1388 (internal-error _"This can't happen."))
1389 (let ((a (assq sclass specl+slotd-list)))
1390 (when a
1391 (let* ((slotd (cdr a))
1392 (index (slot-location slotd)))
1393 (unless index
1394 (return-from make-accessor-table nil))
1395 (setf (gethash class table) index)
1396 (when (consp index)
1397 (setq no-class-slots-p nil))
1398 (setq all-index (if (or (null all-index)
1399 (eql all-index index))
1400 index t))
1401 (incf size)
1402 (cond ((= size 1) (setq first class))
1403 ((= size 2) (setq second class)))
1404 (return nil))))))
1405 table)
1406 (values table all-index first second size no-class-slots-p))))
1407
1408
1409 ;;;; ***********************************************
1410 ;;;; The guts of COMPUTE-APPLICABLE-METHODS *******
1411 ;;;; ***********************************************
1412
1413 ;;;
1414 ;;; First value is a sorted list of possibly applicable methods of
1415 ;;; generic function GF when applied to arguments of types TYPES.
1416 ;;; Second value is true if all methods are known to be applicable.
1417 ;;;
1418 (defun compute-applicable-methods-using-types (gf types)
1419 (let ((definite-p t)
1420 (possibly-applicable-methods ()))
1421 (dolist (method (generic-function-methods* gf))
1422 (let ((specls (if (consp method)
1423 (early-method-specializers method t)
1424 (method-specializers method)))
1425 (types types)
1426 (possibly-applicable-p t)
1427 (applicable-p t))
1428 (dolist (specl specls)
1429 (multiple-value-bind (specl-applicable-p specl-possibly-applicable-p)
1430 (specializer-applicable-using-type-p specl (pop types))
1431 (unless specl-applicable-p
1432 (setq applicable-p nil))
1433 (unless specl-possibly-applicable-p
1434 (setq possibly-applicable-p nil)
1435 (return))))
1436 (when possibly-applicable-p
1437 (unless applicable-p
1438 (setq definite-p nil))
1439 (push method possibly-applicable-methods))))
1440 (let ((precedence (arg-info-precedence (gf-arg-info* gf))))
1441 (values (sort-applicable-methods (nreverse possibly-applicable-methods)
1442 types precedence)
1443 definite-p))))
1444
1445 ;;;
1446 ;;; Sort the list of methods METHODS which are applicable to TYPES
1447 ;;; destructively according to PRECEDENCE. PRECEDENCE is a list of
1448 ;;; indices for the argument precedence order.
1449 ;;;
1450 ;;; Only used in this file.
1451 ;;;
1452 (defun sort-applicable-methods (methods types precedence)
1453 (sort-methods methods
1454 precedence
1455 (lambda (class1 class2 index)
1456 (let* ((class (type-class (nth index types)))
1457 (cpl (cpl-maybe-early class)))
1458 (if (memq class2 (memq class1 cpl))
1459 class1
1460 class2)))))
1461
1462 ;;;
1463 ;;; Sort the list of methods METHODS using argument precedence order
1464 ;;; PRECEDENCE. COMPARE-CLASSES is a function used to compare method
1465 ;;; specializers.
1466 ;;;
1467 ;;; Only used in this file.
1468 ;;;
1469 (defun sort-methods (methods precedence compare-classes)
1470 (flet ((sorter (method1 method2)
1471 (dolist (index precedence)
1472 (flet ((specializers (method)
1473 (if (listp method)
1474 (early-method-specializers method t)
1475 (method-specializers method))))
1476 (let* ((specl1 (nth index (specializers method1)))
1477 (specl2 (nth index (specializers method2)))
1478 (more-specific (more-specific-specializer
1479 specl1 specl2 index compare-classes)))
1480 (when more-specific
1481 (return-from sorter (eq more-specific specl1))))))))
1482 (stable-sort methods #'sorter)))
1483
1484 ;;;
1485 ;;; Return the more specific specializer of SPEC1 and SPEC2, or null
1486 ;;; if equally specific. INDEX is the index of SPEC1 and SPEC2 in the
1487 ;;; method specializers. COMPARE-CLASSES is a function to call for
1488 ;;; comparing class specializers.
1489 ;;;
1490 ;;; Only used in this file.
1491 ;;;
1492 (defun more-specific-specializer (specl1 specl2 index compare-classes)
1493 (flet ((spec-type (spec)
1494 (if (eq *boot-state* 'complete)
1495 (specializer-type spec)
1496 (bootstrap-get-slot 'specializer spec 'type))))
1497 (let ((type1 (spec-type specl1))
1498 (type2 (spec-type specl2)))
1499 (cond ((eq specl1 specl2)
1500 nil)
1501 ((atom type1)
1502 specl2)
1503 ((atom type2)
1504 specl1)
1505 (t
1506 (case (car type1)
1507 (class
1508 (case (car type2)
1509 (class (funcall compare-classes specl1 specl2 index))
1510 (t specl2)))
1511 (prototype
1512 (case (car type2)
1513 (class (funcall compare-classes specl1 specl2 index))
1514 (t specl2)))
1515 (class-eq
1516 (case (car type2) ; FIXME: ECASE?
1517 (eql specl2)
1518 (class-eq nil)
1519 (class type1)))
1520 (eql
1521 (case (car type2)
1522 (eql nil)
1523 (t specl1)))))))))
1524
1525
1526 ;;;; ***********************
1527 ;;;; MAP-ALL-ORDERS *******
1528 ;;;; ***********************
1529
1530 (defun map-all-orders (methods precedence function)
1531 (let ((choices nil))
1532 (flet ((compare-classes-function (class1 class2 index)
1533 (declare (ignore index))
1534 (let ((choice nil))
1535 (dolist (c choices nil)
1536 (when (or (and (eq (first c) class1)
1537 (eq (second c) class2))
1538 (and (eq (first c) class2)
1539 (eq (second c) class1)))
1540 (return (setq choice c))))
1541 (unless choice
1542 (setq choice
1543 (if (class-might-precede-p class1 class2)
1544 (if (class-might-precede-p class2 class1)
1545 (list class1 class2 nil t)
1546 (list class1 class2 t))
1547 (if (class-might-precede-p class2 class1)
1548 (list class2 class1 t)
1549 (let ((name1 (class-name class1))
1550 (name2 (class-name class2)))
1551 (if (and name1 name2 (symbolp name1) (symbolp name2)
1552 (string< (symbol-name name1)
1553 (symbol-name name2)))
1554 (list class1 class2 t)
1555 (list class2 class1 t))))))
1556 (push choice choices))
1557 (car choice))))
1558 (loop (funcall function
1559 (sort-methods methods precedence #'compare-classes-function))
1560 (unless (dolist (c choices nil)
1561 (unless (third c)
1562 (rotatef (car c) (cadr c))
1563 (return (setf (third c) t))))
1564 (return nil))))))
1565
1566 (defvar *in-precompute-effective-methods-p* nil)
1567
1568 ;used only in map-all-orders
1569 (defun class-might-precede-p (class1 class2)
1570 (if (not *in-precompute-effective-methods-p*)
1571 (not (member class1 (cdr (class-precedence-list class2))))
1572 (class-can-precede-p class1 class2)))
1573
1574
1575 ;;;; ********************************************
1576 ;;;; SPECIALIZER-APPLICABLE-USING-TYPE-P *******
1577 ;;;; ********************************************
1578
1579 ;;;
1580 ;;; First value is true if specializer SPECL is applicable to TYPE.
1581 ;;; Second value is true if its possibly applicable to TYPE. This is
1582 ;;; used by c-a-m-u-t and generate-discrimination-net-internal, and
1583 ;;; has only what they need.
1584 ;;;
1585 (defun specializer-applicable-using-type-p (specl type)
1586 (let ((specl (type-from-specializer specl)))
1587 (cond ((eq specl t)
1588 (values t t))
1589 ((or (atom type) (eq (car type) t))
1590 (values nil t))
1591 (t
1592 (case (car type)
1593 (class (saut-class specl type))
1594 (and (saut-and specl type))
1595 (not (saut-not specl type))
1596 (prototype (saut-prototype specl type))
1597 (class-eq (saut-class-eq specl type))
1598 (eql (saut-eql specl type))
1599 (t (internal-error
1600 _"~@<~s cannot handle the second argument ~s.~@:>"
1601 'specializer-applicable-using-type-p type)))))))
1602
1603 (defun saut-and (specl type)
1604 (let ((applicable nil)
1605 (possibly-applicable t))
1606 (dolist (type (cdr type))
1607 (multiple-value-bind (appl poss-appl)
1608 (specializer-applicable-using-type-p specl type)
1609 (when appl
1610 (return (setq applicable t)))
1611 (unless poss-appl
1612 (return (setq possibly-applicable nil)))))
1613 (values applicable possibly-applicable)))
1614
1615 (defun saut-not (specl type)
1616 (let ((ntype (cadr type)))
1617 (values
1618 nil
1619 (case (car ntype)
1620 (class
1621 (let* ((class (type-class specl))
1622 (cpl (cpl-or-nil class)))
1623 (not (memq (cadr ntype) cpl))))
1624 (class-eq
1625 (let ((class (case (car specl)
1626 (eql (class-of (cadr specl)))
1627 (class-eq (cadr specl)))))
1628 (not (eq class (cadr ntype)))))
1629 (prototype
1630 (let* ((class (case (car specl)
1631 (eql (class-of (cadr specl)))
1632 (class-eq (cadr specl))
1633 (prototype (cadr specl))
1634 (class (cadr specl))))
1635 (cpl (cpl-or-nil class)))
1636 (not (memq (cadr ntype) cpl))))
1637 (eql
1638 (case (car specl)
1639 (eql (not (eql (cadr specl) (cadr ntype))))
1640 (t t)))
1641 (t
1642 (internal-error _"~@<~s cannot handle the second argument ~s.~@:>"
1643 'specializer-applicable-using-type-p type))))))
1644
1645 (defun saut-class (specl type)
1646 (if (eq 'class (car specl))
1647 (let* ((specl (cadr specl))
1648 (type (cadr type))
1649 (cpl (cpl-or-nil type))
1650 (pred (memq specl cpl)))
1651 (values pred
1652 (or pred
1653 (if (not *in-precompute-effective-methods-p*)
1654 ;; classes might get common subclass
1655 (superclasses-compatible-p specl type)
1656 ;; worry only about existing classes
1657 (classes-have-common-subclass-p specl type)))))
1658 (values nil
1659 (let* ((class (type-class specl))
1660 (cpl (cpl-or-nil class)))
1661 (memq (cadr type) cpl)))))
1662
1663 (defun classes-have-common-subclass-p (class1 class2)
1664 (or (eq class1 class2)
1665 (let ((class1-subs (class-direct-subclasses class1)))
1666 (or (memq class2 class1-subs)
1667 (dolist (class1-sub class1-subs nil)
1668 (when (classes-have-common-subclass-p class1-sub class2)
1669 (return t)))))))
1670
1671 (defun saut-class-eq (specl type)
1672 (if (eq (car specl) 'eql)
1673 (values nil (eq (class-of (cadr specl)) (cadr type)))
1674 (let ((pred (case (car specl)
1675 (class-eq
1676 (eq (cadr specl) (cadr type)))
1677 (class
1678 (or (eq (cadr specl) (cadr type))
1679 (memq (cadr specl)
1680 (cpl-or-nil (cadr type))))))))
1681 (values pred pred))))
1682
1683 (defun saut-prototype (specl type)
1684 (declare (ignore specl type))
1685 (values nil nil)) ; fix this someday
1686
1687 (defun saut-eql (specl type)
1688 (let ((pred (case (car specl)
1689 (eql
1690 (eql (cadr specl) (cadr type)))
1691 (class-eq
1692 (eq (cadr specl) (class-of (cadr type))))
1693 (class
1694 (memq (cadr specl)
1695 (cpl-or-nil (class-of (cadr type))))))))
1696 (values pred pred)))
1697
1698
1699 (defvar *effective-method-cache* (make-hash-table :test 'eq))
1700
1701 (defun flush-effective-method-cache (gf)
1702 (dolist (method (generic-function-methods gf))
1703 (remhash method *effective-method-cache*)))
1704
1705 (defun get-secondary-dispatch-function (gf methods types &optional
1706 method-alist wrappers)
1707 (let ((generator (get-secondary-dispatch-function1
1708 gf methods types
1709 (not (null method-alist))
1710 (not (null wrappers))
1711 (not (methods-contain-eql-specializer-p methods)))))
1712 (make-callable gf methods generator method-alist wrappers)))
1713
1714 (defun get-secondary-dispatch-function1 (gf methods types method-alist-p wrappers-p
1715 &optional all-applicable-p
1716 (all-sorted-p t) function-p)
1717 (if (null methods)
1718 (if function-p
1719 (lambda (method-alist wrappers)
1720 (declare (ignore method-alist wrappers))
1721 #'(kernel:instance-lambda (&rest args)
1722 (apply #'no-applicable-method gf args)))
1723 (lambda (method-alist wrappers)
1724 (declare (ignore method-alist wrappers))
1725 (lambda (&rest args)
1726 (apply #'no-applicable-method gf args))))
1727 (let* ((key (car methods))
1728 (ht-value (or (gethash key *effective-method-cache*)
1729 (setf (gethash key *effective-method-cache*)
1730 (cons nil nil)))))
1731 (if (and (null (cdr methods)) all-applicable-p ; the most common case
1732 (null method-alist-p) wrappers-p (not function-p))
1733 (or (car ht-value)
1734 (setf (car ht-value)
1735 (get-secondary-dispatch-function2
1736 gf methods types method-alist-p wrappers-p
1737 all-applicable-p all-sorted-p function-p)))
1738 (let ((akey (list methods
1739 (if all-applicable-p 'all-applicable types)
1740 method-alist-p wrappers-p function-p)))
1741 (or (cdr (assoc akey (cdr ht-value) :test #'equal))
1742 (let ((value (get-secondary-dispatch-function2
1743 gf methods types method-alist-p wrappers-p
1744 all-applicable-p all-sorted-p function-p)))
1745 (push (cons akey value) (cdr ht-value))
1746 value)))))))
1747
1748 (defvar *computing-standard-effective-method-p* nil)
1749
1750 (defun get-secondary-dispatch-function2 (gf methods types method-alist-p
1751 wrappers-p all-applicable-p
1752 all-sorted-p function-p)
1753 (if (and all-applicable-p all-sorted-p (not function-p))
1754 (if (and (eq *boot-state* 'complete)
1755 ;; Because of loadable-pcl. See combin.lisp.
1756 (not (eq #'compute-effective-method gf))
1757 (not *computing-standard-effective-method-p*))
1758 (let* ((combin (generic-function-method-combination gf))
1759 (effective (compute-effective-method gf combin methods)))
1760 (callable-generator gf effective method-alist-p wrappers-p))
1761 (let ((*computing-standard-effective-method-p* t)
1762 (effective (standard-compute-effective-method gf nil methods)))
1763 (callable-generator gf effective method-alist-p wrappers-p)))
1764 (let ((net (generate-discrimination-net gf methods types all-sorted-p)))
1765 (compute-secondary-dispatch-function1 gf net function-p))))
1766
1767 (defun get-effective-method-function (gf methods
1768 &optional method-alist wrappers)
1769 (let ((generator (get-secondary-dispatch-function1
1770 gf methods nil (not (null method-alist))
1771 (not (null wrappers)))))
1772 (make-callable gf methods generator method-alist wrappers)))
1773
1774 (defun get-effective-method-function1 (gf methods &optional (sorted-p t))
1775 (get-secondary-dispatch-function1 gf methods nil nil nil t sorted-p))
1776
1777 (defun methods-contain-eql-specializer-p (methods)
1778 (and (eq *boot-state* 'complete)
1779 (some (lambda (method)
1780 (some #'eql-specializer-p (method-specializers method)))
1781 methods)))
1782
1783
1784 ;;;; ************************
1785 ;;;; Debugging Stuff *******
1786 ;;;; ************************
1787
1788 #+pcl-debug
1789 (progn
1790
1791 (defvar dfun-count nil)
1792 (defvar dfun-list nil)
1793 (defvar *minimum-cache-size-to-list*)
1794
1795 (defun list-dfun (gf)
1796 (let* ((sym (type-of (gf-dfun-info gf)))
1797 (a (assq sym dfun-list)))
1798 (unless a
1799 (push (setq a (list sym)) dfun-list))
1800 (push (generic-function-name gf) (cdr a))))
1801
1802 (defun list-all-dfuns ()
1803 (setq dfun-list nil)
1804 (map-all-generic-functions #'list-dfun)
1805 dfun-list)
1806
1807 (defun list-large-cache (gf)
1808 (let* ((sym (type-of (gf-dfun-info gf)))
1809 (cache (gf-dfun-cache gf)))
1810 (when cache
1811 (let ((size (cache-size cache)))
1812 (when (>= size *minimum-cache-size-to-list*)
1813 (let ((a (assoc size dfun-list)))
1814 (unless a
1815 (push (setq a (list size)) dfun-list))
1816 (push (let ((name (generic-function-name gf)))
1817 (if (eq sym 'caching) name (list name sym)))
1818 (cdr a))))))))
1819
1820 (defun list-large-caches (&optional (*minimum-cache-size-to-list* 130))
1821 (setq dfun-list nil)
1822 (map-all-generic-functions #'list-large-cache)
1823 (setq dfun-list (sort dfun-list #'< :key #'car))
1824 (mapc #'print dfun-list)
1825 (values))
1826
1827 (defun count-dfun (gf)
1828 (let* ((sym (type-of (gf-dfun-info gf)))
1829 (cache (gf-dfun-cache gf))
1830 (a (assq sym dfun-count)))
1831 (unless a
1832 (push (setq a (list sym 0 nil)) dfun-count))
1833 (incf (cadr a))
1834 (when cache
1835 (let* ((size (cache-size cache))
1836 (b (assoc size (third a))))
1837 (unless b
1838 (push (setq b (cons size 0)) (third a)))
1839 (incf (cdr b))))))
1840
1841 (defun count-all-dfuns ()
1842 (setq dfun-count (mapcar (lambda (type) (list type 0 nil))
1843 '(ONE-CLASS TWO-CLASS DEFAULT-METHOD-ONLY
1844 ONE-INDEX N-N CHECKING CACHING
1845 DISPATCH)))
1846 (map-all-generic-functions #'count-dfun)
1847 (mapc (lambda (type+count+sizes)
1848 (setf (third type+count+sizes)
1849 (sort (third type+count+sizes) #'< :key #'car)))
1850 dfun-count)
1851 (mapc (lambda (type+count+sizes)
1852 (format t _"~&There are ~4d dfuns of type ~s"
1853 (cadr type+count+sizes) (car type+count+sizes))
1854 (format t "~% ~S~%" (caddr type+count+sizes)))
1855 dfun-count)
1856 (values))
1857
1858
1859 (defun show-dfun-constructors ()
1860 (format t _"~&DFUN constructor caching is ~A."
1861 (if *enable-dfun-constructor-caching*
1862 _"enabled" _"disabled"))
1863 (dolist (generator-entry *dfun-constructors*)
1864 (dolist (args-entry (cdr generator-entry))
1865 (format t "~&~S ~S"
1866 (cons (car generator-entry) (caar args-entry))
1867 (caddr args-entry)))))
1868
1869 ) ; #+pcl-debug

  ViewVC Help
Powered by ViewVC 1.1.5