1 ;;;; the basics of the PCL wrapper cache mechanism
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from software originally released by Xerox
7 ;;;; Corporation. Copyright and release statements follow. Later modifications
8 ;;;; to the software are in the public domain and are provided with
9 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
12 ;;;; copyright information from original PCL sources:
14 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
15 ;;;; All rights reserved.
17 ;;;; Use and copying of this software and preparation of derivative works based
18 ;;;; upon this software are permitted. Any distribution of this software or
19 ;;;; derivative works must comply with all applicable United States export
22 ;;;; This software is made available AS IS, and Xerox Corporation makes no
23 ;;;; warranty about the software, its performance or its conformity to any
28 ;;; Ye olde CMUCL comment follows, but it seems likely that the paper
29 ;;; that would be inserted would resemble Kiczales and Rodruigez,
30 ;;; Efficient Method Dispatch in PCL, ACM 1990. Some of the details
31 ;;; changed between that paper and "May Day PCL" of 1992; some other
32 ;;; details have changed since, but reading that paper gives the broad
35 ;;; The caching algorithm implemented:
37 ;;; << put a paper here >>
39 ;;; For now, understand that as far as most of this code goes, a cache
40 ;;; has two important properties. The first is the number of wrappers
41 ;;; used as keys in each cache line. Throughout this code, this value
42 ;;; is always called NKEYS. The second is whether or not the cache
43 ;;; lines of a cache store a value. Throughout this code, this always
46 ;;; Depending on these values, there are three kinds of caches.
48 ;;; NKEYS = 1, VALUEP = NIL
50 ;;; In this kind of cache, each line is 1 word long. No cache locking
51 ;;; is needed since all read's in the cache are a single value.
52 ;;; Nevertheless line 0 (location 0) is reserved, to ensure that
53 ;;; invalid wrappers will not get a first probe hit.
55 ;;; To keep the code simpler, a cache lock count does appear in
56 ;;; location 0 of these caches, that count is incremented whenever
57 ;;; data is written to the cache. But, the actual lookup code (see
58 ;;; make-dlap) doesn't need to do locking when reading the cache.
60 ;;; NKEYS = 1, VALUEP = T
62 ;;; In this kind of cache, each line is 2 words long. Cache locking
63 ;;; must be done to ensure the synchronization of cache reads. Line 0
64 ;;; of the cache (location 0) is reserved for the cache lock count.
65 ;;; Location 1 of the cache is unused (in effect wasted).
69 ;;; In this kind of cache, the 0 word of the cache holds the lock
70 ;;; count. The 1 word of the cache is line 0. Line 0 of these caches
73 ;;; This is done because in this sort of cache, the overhead of doing
74 ;;; the cache probe is high enough that the 1+ required to offset the
75 ;;; location is not a significant cost. In addition, because of the
76 ;;; larger line sizes, the space that would be wasted by reserving
77 ;;; line 0 to hold the lock count is more significant.
81 ;;; A cache is essentially just a vector. The use of the individual
82 ;;; `words' in the vector depends on particular properties of the
83 ;;; cache as described above.
85 ;;; This defines an abstraction for caches in terms of their most
86 ;;; obvious implementation as simple vectors. But, please notice that
87 ;;; part of the implementation of this abstraction, is the function
88 ;;; lap-out-cache-ref. This means that most port-specific
89 ;;; modifications to the implementation of caches will require
90 ;;; corresponding port-specific modifications to the lap code
92 (defmacro cache-vector-ref (cache-vector location)
93 `(svref (the simple-vector ,cache-vector)
94 (sb-ext:truly-the fixnum ,location)))
96 (defmacro cache-vector-size (cache-vector)
97 `(array-dimension (the simple-vector ,cache-vector) 0))
99 (defun allocate-cache-vector (size)
100 (make-array size :adjustable nil))
102 (defmacro cache-vector-lock-count (cache-vector)
103 `(cache-vector-ref ,cache-vector 0))
105 (defun flush-cache-vector-internal (cache-vector)
107 (fill (the simple-vector cache-vector) nil)
108 (setf (cache-vector-lock-count cache-vector) 0))
111 (defmacro modify-cache (cache-vector &body body)
113 (multiple-value-prog1
115 (let ((old-count (cache-vector-lock-count ,cache-vector)))
116 (declare (fixnum old-count))
117 (setf (cache-vector-lock-count ,cache-vector)
118 (if (= old-count most-positive-fixnum)
119 1 (the fixnum (1+ old-count))))))))
121 (deftype field-type ()
122 '(mod #.layout-clos-hash-length))
124 (eval-when (:compile-toplevel :load-toplevel :execute)
125 (defun power-of-two-ceiling (x)
127 ;;(expt 2 (ceiling (log x 2)))
128 (the fixnum (ash 1 (integer-length (1- x)))))
131 (defconstant +nkeys-limit+ 256)
133 (defstruct (cache (:constructor make-cache ())
134 (:copier copy-cache-internal))
136 (nkeys 1 :type (integer 1 #.+nkeys-limit+))
137 (valuep nil :type (member nil t))
138 (nlines 0 :type fixnum)
139 (field 0 :type field-type)
140 (limit-fn #'default-limit-fn :type function)
141 (mask 0 :type fixnum)
142 (size 0 :type fixnum)
143 (line-size 1 :type (integer 1 #.(power-of-two-ceiling (1+ +nkeys-limit+))))
144 (max-location 0 :type fixnum)
145 (vector #() :type simple-vector)
146 (overflow nil :type list))
148 #-sb-fluid (declaim (sb-ext:freeze-type cache))
150 (defmacro cache-lock-count (cache)
151 `(cache-vector-lock-count (cache-vector ,cache)))
153 ;;; Return a cache that has had FLUSH-CACHE-VECTOR-INTERNAL called on
154 ;;; it. This returns a cache of exactly the size requested, it won't
155 ;;; ever return a larger cache.
156 (defun get-cache-vector (size)
157 (flush-cache-vector-internal (make-array size)))
160 ;;;; wrapper cache numbers
162 ;;; The constant WRAPPER-CACHE-NUMBER-ADDS-OK controls the number of
163 ;;; non-zero bits wrapper cache numbers will have.
165 ;;; The value of this constant is the number of wrapper cache numbers
166 ;;; which can be added and still be certain the result will be a
167 ;;; fixnum. This is used by all the code that computes primary cache
168 ;;; locations from multiple wrappers.
170 ;;; The value of this constant is used to derive the next two which
171 ;;; are the forms of this constant which it is more convenient for the
172 ;;; runtime code to use.
173 (defconstant wrapper-cache-number-length
174 (integer-length layout-clos-hash-max))
175 (defconstant wrapper-cache-number-mask layout-clos-hash-max)
176 (defconstant wrapper-cache-number-adds-ok
177 (truncate most-positive-fixnum layout-clos-hash-max))
179 ;;;; wrappers themselves
181 ;;; This caching algorithm requires that wrappers have more than one
182 ;;; wrapper cache number. You should think of these multiple numbers
183 ;;; as being in columns. That is, for a given cache, the same column
184 ;;; of wrapper cache numbers will be used.
186 ;;; If at some point the cache distribution of a cache gets bad, the
187 ;;; cache can be rehashed by switching to a different column.
189 ;;; The columns are referred to by field number which is that number
190 ;;; which, when used as a second argument to wrapper-ref, will return
191 ;;; that column of wrapper cache number.
193 ;;; This code is written to allow flexibility as to how many wrapper
194 ;;; cache numbers will be in each wrapper, and where they will be
195 ;;; located. It is also set up to allow port specific modifications to
196 ;;; `pack' the wrapper cache numbers on machines where the addressing
197 ;;; modes make that a good idea.
199 ;;; In SBCL, as in CMU CL, we want to do type checking as early as
200 ;;; possible; structures help this. The structures are hard-wired to
201 ;;; have a fixed number of cache hash values, and that number must
202 ;;; correspond to the number of cache lines we use.
203 (defconstant wrapper-cache-number-vector-length
204 layout-clos-hash-length)
206 (unless (boundp '*the-class-t*)
207 (setq *the-class-t* nil))
209 (defmacro wrapper-class (wrapper)
210 `(classoid-pcl-class (layout-classoid ,wrapper)))
211 (defmacro wrapper-no-of-instance-slots (wrapper)
212 `(layout-length ,wrapper))
214 ;;; FIXME: Why are these macros?
215 (defmacro wrapper-instance-slots-layout (wrapper)
216 `(%wrapper-instance-slots-layout ,wrapper))
217 (defmacro wrapper-class-slots (wrapper)
218 `(%wrapper-class-slots ,wrapper))
219 (defmacro wrapper-cache-number-vector (x) x)
221 ;;; This is called in BRAID when we are making wrappers for classes
222 ;;; whose slots are not initialized yet, and which may be built-in
223 ;;; classes. We pass in the class name in addition to the class.
224 (defun boot-make-wrapper (length name &optional class)
225 (let ((found (find-classoid name nil)))
228 (unless (classoid-pcl-class found)
229 (setf (classoid-pcl-class found) class))
230 (aver (eq (classoid-pcl-class found) class))
231 (let ((layout (classoid-layout found)))
235 (make-wrapper-internal
237 :classoid (make-standard-classoid
238 :name name :pcl-class class))))))
240 ;;; The following variable may be set to a STANDARD-CLASS that has
241 ;;; already been created by the lisp code and which is to be redefined
242 ;;; by PCL. This allows STANDARD-CLASSes to be defined and used for
243 ;;; type testing and dispatch before PCL is loaded.
244 (defvar *pcl-class-boot* nil)
246 ;;; In SBCL, as in CMU CL, the layouts (a.k.a wrappers) for built-in
247 ;;; and structure classes already exist when PCL is initialized, so we
248 ;;; don't necessarily always make a wrapper. Also, we help maintain
249 ;;; the mapping between CL:CLASS and SB-KERNEL:CLASSOID objects.
250 (defun make-wrapper (length class)
252 ((or (typep class 'std-class)
253 (typep class 'forward-referenced-class))
254 (make-wrapper-internal
257 (let ((owrap (class-wrapper class)))
259 (layout-classoid owrap))
260 ((or (*subtypep (class-of class) *the-class-standard-class*)
261 (typep class 'forward-referenced-class))
262 (cond ((and *pcl-class-boot*
263 (eq (slot-value class 'name) *pcl-class-boot*))
264 (let ((found (find-classoid
265 (slot-value class 'name))))
266 (unless (classoid-pcl-class found)
267 (setf (classoid-pcl-class found) class))
268 (aver (eq (classoid-pcl-class found) class))
271 (let ((name (slot-value class 'name)))
272 (make-standard-classoid :pcl-class class
273 :name (and (symbolp name) name))))))
275 (make-random-pcl-classoid :pcl-class class))))))
277 (let* ((found (find-classoid (slot-value class 'name)))
278 (layout (classoid-layout found)))
279 (unless (classoid-pcl-class found)
280 (setf (classoid-pcl-class found) class))
281 (aver (eq (classoid-pcl-class found) class))
285 (defconstant +first-wrapper-cache-number-index+ 0)
287 (declaim (inline next-wrapper-cache-number-index))
288 (defun next-wrapper-cache-number-index (field-number)
289 (and (< field-number #.(1- wrapper-cache-number-vector-length))
292 ;;; FIXME: Why are there two layers here, with one operator trivially
293 ;;; defined in terms of the other? It'd be nice either to have a
294 ;;; comment explaining why the separation is valuable, or to collapse
295 ;;; it into a single layer.
297 ;;; FIXME (?): These are logically inline functions, but they need to
298 ;;; be SETFable, and for now it seems not worth the trouble to DEFUN
299 ;;; both inline FOO and inline (SETF FOO) for each one instead of a
300 ;;; single macro. Perhaps the best thing would be to make them
301 ;;; immutable (since it seems sort of surprising and gross to be able
302 ;;; to modify hash values) so that they can become inline functions
303 ;;; with no muss or fuss. I (WHN) didn't do this only because I didn't
304 ;;; know whether any code anywhere depends on the values being
306 (defmacro cache-number-vector-ref (cnv n)
307 `(wrapper-cache-number-vector-ref ,cnv ,n))
308 (defmacro wrapper-cache-number-vector-ref (wrapper n)
309 `(layout-clos-hash ,wrapper ,n))
311 (declaim (inline wrapper-class*))
312 (defun wrapper-class* (wrapper)
313 (or (wrapper-class wrapper)
314 (ensure-non-standard-class
315 (classoid-name (layout-classoid wrapper)))))
317 ;;; The wrapper cache machinery provides general mechanism for
318 ;;; trapping on the next access to any instance of a given class. This
319 ;;; mechanism is used to implement the updating of instances when the
320 ;;; class is redefined (MAKE-INSTANCES-OBSOLETE). The same mechanism
321 ;;; is also used to update generic function caches when there is a
322 ;;; change to the superclasses of a class.
324 ;;; Basically, a given wrapper can be valid or invalid. If it is
325 ;;; invalid, it means that any attempt to do a wrapper cache lookup
326 ;;; using the wrapper should trap. Also, methods on
327 ;;; SLOT-VALUE-USING-CLASS check the wrapper validity as well. This is
328 ;;; done by calling CHECK-WRAPPER-VALIDITY.
330 (declaim (inline invalid-wrapper-p))
331 (defun invalid-wrapper-p (wrapper)
332 (not (null (layout-invalid wrapper))))
334 (defvar *previous-nwrappers* (make-hash-table))
336 (defun invalidate-wrapper (owrapper state nwrapper)
337 (aver (member state '(:flush :obsolete) :test #'eq))
338 (let ((new-previous ()))
339 ;; First off, a previous call to INVALIDATE-WRAPPER may have
340 ;; recorded OWRAPPER as an NWRAPPER to update to. Since OWRAPPER
341 ;; is about to be invalid, it no longer makes sense to update to
344 ;; We go back and change the previously invalidated wrappers so
345 ;; that they will now update directly to NWRAPPER. This
346 ;; corresponds to a kind of transitivity of wrapper updates.
347 (dolist (previous (gethash owrapper *previous-nwrappers*))
348 (when (eq state :obsolete)
349 (setf (car previous) :obsolete))
350 (setf (cadr previous) nwrapper)
351 (push previous new-previous))
353 (let ((ocnv (wrapper-cache-number-vector owrapper)))
354 (dotimes (i layout-clos-hash-length)
355 (setf (cache-number-vector-ref ocnv i) 0)))
357 (push (setf (layout-invalid owrapper) (list state nwrapper))
360 (setf (gethash owrapper *previous-nwrappers*) ()
361 (gethash nwrapper *previous-nwrappers*) new-previous)))
363 (defun check-wrapper-validity (instance)
364 (let* ((owrapper (wrapper-of instance))
365 (state (layout-invalid owrapper)))
366 (aver (not (eq state :uninitialized)))
369 ;; FIXME: I can't help thinking that, while this does cure the
370 ;; symptoms observed from some class redefinitions, this isn't
371 ;; the place to be doing this flushing. Nevertheless... --
375 ;; We assume in this case, that the :INVALID is from a
376 ;; previous call to REGISTER-LAYOUT for a superclass of
377 ;; INSTANCE's class. See also the comment above
378 ;; FORCE-CACHE-FLUSHES. Paul Dietz has test cases for this.
380 (force-cache-flushes (class-of instance))
381 (check-wrapper-validity instance))
385 (flush-cache-trap owrapper (cadr state) instance))
387 (obsolete-instance-trap owrapper (cadr state) instance)))))))
389 (declaim (inline check-obsolete-instance))
390 (defun check-obsolete-instance (instance)
391 (when (invalid-wrapper-p (layout-of instance))
392 (check-wrapper-validity instance)))
395 (defun get-cache (nkeys valuep limit-fn nlines)
396 (let ((cache (make-cache)))
397 (declare (type cache cache))
398 (multiple-value-bind (cache-mask actual-size line-size nlines)
399 (compute-cache-parameters nkeys valuep nlines)
400 (setf (cache-nkeys cache) nkeys
401 (cache-valuep cache) valuep
402 (cache-nlines cache) nlines
403 (cache-field cache) +first-wrapper-cache-number-index+
404 (cache-limit-fn cache) limit-fn
405 (cache-mask cache) cache-mask
406 (cache-size cache) actual-size
407 (cache-line-size cache) line-size
408 (cache-max-location cache) (let ((line (1- nlines)))
411 (1+ (* line line-size))))
412 (cache-vector cache) (get-cache-vector actual-size)
413 (cache-overflow cache) nil)
416 (defun get-cache-from-cache (old-cache new-nlines
417 &optional (new-field +first-wrapper-cache-number-index+))
418 (let ((nkeys (cache-nkeys old-cache))
419 (valuep (cache-valuep old-cache))
420 (cache (make-cache)))
421 (declare (type cache cache))
422 (multiple-value-bind (cache-mask actual-size line-size nlines)
423 (if (= new-nlines (cache-nlines old-cache))
424 (values (cache-mask old-cache) (cache-size old-cache)
425 (cache-line-size old-cache) (cache-nlines old-cache))
426 (compute-cache-parameters nkeys valuep new-nlines))
427 (setf (cache-owner cache) (cache-owner old-cache)
428 (cache-nkeys cache) nkeys
429 (cache-valuep cache) valuep
430 (cache-nlines cache) nlines
431 (cache-field cache) new-field
432 (cache-limit-fn cache) (cache-limit-fn old-cache)
433 (cache-mask cache) cache-mask
434 (cache-size cache) actual-size
435 (cache-line-size cache) line-size
436 (cache-max-location cache) (let ((line (1- nlines)))
439 (1+ (* line line-size))))
440 (cache-vector cache) (get-cache-vector actual-size)
441 (cache-overflow cache) nil)
444 (defun copy-cache (old-cache)
445 (let* ((new-cache (copy-cache-internal old-cache))
446 (size (cache-size old-cache))
447 (old-vector (cache-vector old-cache))
448 (new-vector (get-cache-vector size)))
449 (declare (simple-vector old-vector new-vector))
450 (dotimes-fixnum (i size)
451 (setf (svref new-vector i) (svref old-vector i)))
452 (setf (cache-vector new-cache) new-vector)
455 (defun compute-line-size (x)
456 (power-of-two-ceiling x))
458 (defun compute-cache-parameters (nkeys valuep nlines-or-cache-vector)
459 ;;(declare (values cache-mask actual-size line-size nlines))
460 (declare (fixnum nkeys))
462 (let* ((line-size (if valuep 2 1))
463 (cache-size (if (typep nlines-or-cache-vector 'fixnum)
467 (power-of-two-ceiling
468 nlines-or-cache-vector))))
469 (cache-vector-size nlines-or-cache-vector))))
470 (declare (fixnum line-size cache-size))
471 (values (logxor (the fixnum (1- cache-size)) (the fixnum (1- line-size)))
474 (the (values fixnum t) (floor cache-size line-size))))
475 (let* ((line-size (power-of-two-ceiling (if valuep (1+ nkeys) nkeys)))
476 (cache-size (if (typep nlines-or-cache-vector 'fixnum)
480 (power-of-two-ceiling
481 nlines-or-cache-vector))))
482 (1- (cache-vector-size nlines-or-cache-vector)))))
483 (declare (fixnum line-size cache-size))
484 (values (logxor (the fixnum (1- cache-size)) (the fixnum (1- line-size)))
485 (the fixnum (1+ cache-size))
487 (the (values fixnum t) (floor cache-size line-size))))))
489 ;;; the various implementations of computing a primary cache location from
490 ;;; wrappers. Because some implementations of this must run fast there are
491 ;;; several implementations of the same algorithm.
493 ;;; The algorithm is:
495 ;;; SUM over the wrapper cache numbers,
496 ;;; ENSURING that the result is a fixnum
497 ;;; MASK the result against the mask argument.
499 ;;; The basic functional version. This is used by the cache miss code to
500 ;;; compute the primary location of an entry.
501 (defun compute-primary-cache-location (field mask wrappers)
503 (declare (type field-type field) (fixnum mask))
504 (if (not (listp wrappers))
506 (the fixnum (wrapper-cache-number-vector-ref wrappers field)))
507 (let ((location 0) (i 0))
508 (declare (fixnum location i))
509 (dolist (wrapper wrappers)
510 ;; First add the cache number of this wrapper to location.
511 (let ((wrapper-cache-number (wrapper-cache-number-vector-ref wrapper
513 (declare (fixnum wrapper-cache-number))
514 (if (zerop wrapper-cache-number)
515 (return-from compute-primary-cache-location 0)
517 (the fixnum (+ location wrapper-cache-number)))))
518 ;; Then, if we are working with lots of wrappers, deal with
519 ;; the wrapper-cache-number-mask stuff.
520 (when (and (not (zerop i))
521 (zerop (mod i wrapper-cache-number-adds-ok)))
523 (logand location wrapper-cache-number-mask)))
525 (the fixnum (1+ (logand mask location))))))
527 ;;; This version is called on a cache line. It fetches the wrappers
528 ;;; from the cache line and determines the primary location. Various
529 ;;; parts of the cache filling code call this to determine whether it
530 ;;; is appropriate to displace a given cache entry.
532 ;;; If this comes across a wrapper whose CACHE-NO is 0, it returns the
533 ;;; symbol invalid to suggest to its caller that it would be provident
534 ;;; to blow away the cache line in question.
535 (defun compute-primary-cache-location-from-location (to-cache
538 (from-cache to-cache))
539 (declare (type cache to-cache from-cache) (fixnum from-location))
541 (cache-vector (cache-vector from-cache))
542 (field (cache-field to-cache))
543 (mask (cache-mask to-cache))
544 (nkeys (cache-nkeys to-cache)))
545 (declare (type field-type field) (fixnum result mask nkeys)
546 (simple-vector cache-vector))
547 (dotimes-fixnum (i nkeys)
548 (let* ((wrapper (cache-vector-ref cache-vector (+ i from-location)))
549 (wcn (wrapper-cache-number-vector-ref wrapper field)))
550 (declare (fixnum wcn))
551 (setq result (+ result wcn)))
552 (when (and (not (zerop i))
553 (zerop (mod i wrapper-cache-number-adds-ok)))
554 (setq result (logand result wrapper-cache-number-mask))))
557 (the fixnum (1+ (logand mask result))))))
559 ;;; NIL means nothing so far, no actual arg info has NILs
561 ;;; CLASS seen all sorts of metaclasses
562 ;;; (specifically, more than one of the next 4 values)
563 ;;; T means everything so far is the class T
564 ;;; STANDARD-CLASS seen only standard classes
565 ;;; BUILT-IN-CLASS seen only built in classes
566 ;;; STRUCTURE-CLASS seen only structure classes
567 (defun raise-metatype (metatype new-specializer)
568 (let ((slot (find-class 'slot-class))
569 (standard (find-class 'standard-class))
570 (fsc (find-class 'funcallable-standard-class))
571 (condition (find-class 'condition-class))
572 (structure (find-class 'structure-class))
573 (built-in (find-class 'built-in-class)))
574 (flet ((specializer->metatype (x)
575 (let ((meta-specializer
576 (if (eq *boot-state* 'complete)
577 (class-of (specializer-class x))
580 ((eq x *the-class-t*) t)
581 ((*subtypep meta-specializer standard) 'standard-instance)
582 ((*subtypep meta-specializer fsc) 'standard-instance)
583 ((*subtypep meta-specializer condition) 'condition-instance)
584 ((*subtypep meta-specializer structure) 'structure-instance)
585 ((*subtypep meta-specializer built-in) 'built-in-instance)
586 ((*subtypep meta-specializer slot) 'slot-instance)
587 (t (error "~@<PCL cannot handle the specializer ~S ~
588 (meta-specializer ~S).~@:>"
590 meta-specializer))))))
591 ;; We implement the following table. The notation is
592 ;; that X and Y are distinct meta specializer names.
594 ;; NIL <anything> ===> <anything>
597 (let ((new-metatype (specializer->metatype new-specializer)))
598 (cond ((eq new-metatype 'slot-instance) 'class)
599 ((null metatype) new-metatype)
600 ((eq metatype new-metatype) new-metatype)
603 (defmacro with-dfun-wrappers ((args metatypes)
604 (dfun-wrappers invalid-wrapper-p
605 &optional wrappers classes types)
606 invalid-arguments-form
608 `(let* ((args-tail ,args) (,invalid-wrapper-p nil) (invalid-arguments-p nil)
609 (,dfun-wrappers nil) (dfun-wrappers-tail nil)
611 `((wrappers-rev nil) (types-rev nil) (classes-rev nil))))
612 (dolist (mt ,metatypes)
614 (setq invalid-arguments-p t)
616 (let* ((arg (pop args-tail))
619 `((class *the-class-t*)
622 (setq wrapper (wrapper-of arg))
623 (when (invalid-wrapper-p wrapper)
624 (setq ,invalid-wrapper-p t)
625 (setq wrapper (check-wrapper-validity arg)))
626 (cond ((null ,dfun-wrappers)
627 (setq ,dfun-wrappers wrapper))
628 ((not (consp ,dfun-wrappers))
629 (setq dfun-wrappers-tail (list wrapper))
630 (setq ,dfun-wrappers (cons ,dfun-wrappers dfun-wrappers-tail)))
632 (let ((new-dfun-wrappers-tail (list wrapper)))
633 (setf (cdr dfun-wrappers-tail) new-dfun-wrappers-tail)
634 (setf dfun-wrappers-tail new-dfun-wrappers-tail))))
636 `((setq class (wrapper-class* wrapper))
637 (setq type `(class-eq ,class)))))
639 `((push wrapper wrappers-rev)
640 (push class classes-rev)
641 (push type types-rev)))))
642 (if invalid-arguments-p
643 ,invalid-arguments-form
644 (let* (,@(when wrappers
645 `((,wrappers (nreverse wrappers-rev))
646 (,classes (nreverse classes-rev))
647 (,types (mapcar (lambda (class)
652 ;;;; some support stuff for getting a hold of symbols that we need when
653 ;;;; building the discriminator codes. It's OK for these to be interned
654 ;;;; symbols because we don't capture any user code in the scope in which
655 ;;;; these symbols are bound.
657 (defvar *dfun-arg-symbols* '(.ARG0. .ARG1. .ARG2. .ARG3.))
659 (defun dfun-arg-symbol (arg-number)
660 (or (nth arg-number (the list *dfun-arg-symbols*))
661 (format-symbol *pcl-package* ".ARG~A." arg-number)))
663 (defvar *slot-vector-symbols* '(.SLOTS0. .SLOTS1. .SLOTS2. .SLOTS3.))
665 (defun slot-vector-symbol (arg-number)
666 (or (nth arg-number (the list *slot-vector-symbols*))
667 (format-symbol *pcl-package* ".SLOTS~A." arg-number)))
669 ;; FIXME: There ought to be a good way to factor out the idiom:
671 ;; (dotimes (i (length metatypes))
672 ;; (push (dfun-arg-symbol i) lambda-list))
674 ;; used in the following four functions into common code that we can
675 ;; declare inline or something. --njf 2001-12-20
676 (defun make-dfun-lambda-list (metatypes applyp)
677 (let ((lambda-list nil))
678 (dotimes (i (length metatypes))
679 (push (dfun-arg-symbol i) lambda-list))
681 (push '&rest lambda-list)
682 (push '.dfun-rest-arg. lambda-list))
683 (nreverse lambda-list)))
685 (defun make-dlap-lambda-list (metatypes applyp)
686 (let ((lambda-list nil))
687 (dotimes (i (length metatypes))
688 (push (dfun-arg-symbol i) lambda-list))
689 ;; FIXME: This is translated directly from the old PCL code.
690 ;; It didn't have a (PUSH '.DFUN-REST-ARG. LAMBDA-LIST) or
691 ;; something similar, so we don't either. It's hard to see how
692 ;; this could be correct, since &REST wants an argument after
693 ;; it. This function works correctly because the caller
694 ;; magically tacks on something after &REST. The calling functions
695 ;; (in dlisp.lisp) should be fixed and this function rewritten.
698 (push '&rest lambda-list))
699 (nreverse lambda-list)))
701 ;; FIXME: The next two functions suffer from having a `.DFUN-REST-ARG.'
702 ;; in their lambda lists, but no corresponding `&REST' symbol. We assume
703 ;; this should be the case by analogy with the previous two functions.
704 ;; It works, and I don't know why. Check the calling functions and
705 ;; fix these too. --njf 2001-12-20
706 (defun make-emf-call (metatypes applyp fn-variable &optional emf-type)
708 (let ((required nil))
709 (dotimes (i (length metatypes))
710 (push (dfun-arg-symbol i) required))
711 (nreverse required))))
712 `(,(if (eq emf-type 'fast-method-call)
713 'invoke-effective-method-function-fast
714 'invoke-effective-method-function)
715 ,fn-variable ,applyp ,@required ,@(when applyp `(.dfun-rest-arg.)))))
717 (defun make-fast-method-call-lambda-list (metatypes applyp)
718 (let ((reversed-lambda-list nil))
719 (push '.pv-cell. reversed-lambda-list)
720 (push '.next-method-call. reversed-lambda-list)
721 (dotimes (i (length metatypes))
722 (push (dfun-arg-symbol i) reversed-lambda-list))
724 (push '.dfun-rest-arg. reversed-lambda-list))
725 (nreverse reversed-lambda-list)))
727 (defmacro with-local-cache-functions ((cache) &body body)
728 `(let ((.cache. ,cache))
729 (declare (type cache .cache.))
730 (labels ((cache () .cache.)
731 (nkeys () (cache-nkeys .cache.))
732 (line-size () (cache-line-size .cache.))
733 (vector () (cache-vector .cache.))
734 (valuep () (cache-valuep .cache.))
735 (nlines () (cache-nlines .cache.))
736 (max-location () (cache-max-location .cache.))
737 (limit-fn () (cache-limit-fn .cache.))
738 (size () (cache-size .cache.))
739 (mask () (cache-mask .cache.))
740 (field () (cache-field .cache.))
741 (overflow () (cache-overflow .cache.))
743 ;; Return T IFF this cache location is reserved. The
744 ;; only time this is true is for line number 0 of an
747 (line-reserved-p (line)
748 (declare (fixnum line))
752 (location-reserved-p (location)
753 (declare (fixnum location))
757 ;; Given a line number, return the cache location.
758 ;; This is the value that is the second argument to
759 ;; cache-vector-ref. Basically, this deals with the
760 ;; offset of nkeys>1 caches and multiplies by line
763 (line-location (line)
764 (declare (fixnum line))
765 (when (line-reserved-p line)
766 (error "line is reserved"))
768 (the fixnum (* line (line-size)))
769 (the fixnum (1+ (the fixnum (* line (line-size)))))))
771 ;; Given a cache location, return the line. This is
772 ;; the inverse of LINE-LOCATION.
774 (location-line (location)
775 (declare (fixnum location))
777 (floor location (line-size))
778 (floor (the fixnum (1- location)) (line-size))))
780 ;; Given a line number, return the wrappers stored at
781 ;; that line. As usual, if nkeys=1, this returns a
782 ;; single value. Only when nkeys>1 does it return a
783 ;; list. An error is signalled if the line is
786 (line-wrappers (line)
787 (declare (fixnum line))
788 (when (line-reserved-p line) (error "Line is reserved."))
789 (location-wrappers (line-location line)))
791 (location-wrappers (location) ; avoid multiplies caused by line-location
792 (declare (fixnum location))
794 (cache-vector-ref (vector) location)
795 (let ((list (make-list (nkeys)))
797 (declare (simple-vector vector))
798 (dotimes (i (nkeys) list)
801 (cache-vector-ref vector (+ location i)))))))
803 ;; Given a line number, return true IFF the line's
804 ;; wrappers are the same as wrappers.
806 (line-matches-wrappers-p (line wrappers)
807 (declare (fixnum line))
808 (and (not (line-reserved-p line))
809 (location-matches-wrappers-p (line-location line)
812 (location-matches-wrappers-p (loc wrappers) ; must not be reserved
813 (declare (fixnum loc))
814 (let ((cache-vector (vector)))
815 (declare (simple-vector cache-vector))
817 (eq wrappers (cache-vector-ref cache-vector loc))
818 (dotimes (i (nkeys) t)
820 (unless (eq (pop wrappers)
821 (cache-vector-ref cache-vector (+ loc i)))
824 ;; Given a line number, return the value stored at that line.
825 ;; If valuep is NIL, this returns NIL. As with line-wrappers,
826 ;; an error is signalled if the line is reserved.
829 (declare (fixnum line))
830 (when (line-reserved-p line) (error "Line is reserved."))
831 (location-value (line-location line)))
833 (location-value (loc)
834 (declare (fixnum loc))
836 (cache-vector-ref (vector) (+ loc (nkeys)))))
838 ;; Given a line number, return true IFF that line has data in
839 ;; it. The state of the wrappers stored in the line is not
840 ;; checked. An error is signalled if line is reserved.
842 (when (line-reserved-p line) (error "Line is reserved."))
843 (not (null (cache-vector-ref (vector) (line-location line)))))
845 ;; Given a line number, return true IFF the line is full and
846 ;; there are no invalid wrappers in the line, and the line's
847 ;; wrappers are different from wrappers.
848 ;; An error is signalled if the line is reserved.
850 (line-valid-p (line wrappers)
851 (declare (fixnum line))
852 (when (line-reserved-p line) (error "Line is reserved."))
853 (location-valid-p (line-location line) wrappers))
855 (location-valid-p (loc wrappers)
856 (declare (fixnum loc))
857 (let ((cache-vector (vector))
858 (wrappers-mismatch-p (null wrappers)))
859 (declare (simple-vector cache-vector))
860 (dotimes (i (nkeys) wrappers-mismatch-p)
862 (let ((wrapper (cache-vector-ref cache-vector (+ loc i))))
863 (when (or (null wrapper)
864 (invalid-wrapper-p wrapper))
866 (unless (and wrappers
871 (setq wrappers-mismatch-p t))))))
873 ;; How many unreserved lines separate line-1 and line-2.
875 (line-separation (line-1 line-2)
876 (declare (fixnum line-1 line-2))
877 (let ((diff (the fixnum (- line-2 line-1))))
878 (declare (fixnum diff))
880 (setq diff (+ diff (nlines)))
881 (when (line-reserved-p 0)
882 (setq diff (1- diff))))
885 ;; Given a cache line, get the next cache line. This will not
886 ;; return a reserved line.
889 (declare (fixnum line))
890 (if (= line (the fixnum (1- (nlines))))
891 (if (line-reserved-p 0) 1 0)
892 (the fixnum (1+ line))))
895 (declare (fixnum loc))
896 (if (= loc (max-location))
900 (the fixnum (+ loc (line-size)))))
902 ;; Given a line which has a valid entry in it, this
903 ;; will return the primary cache line of the wrappers
904 ;; in that line. We just call
905 ;; COMPUTE-PRIMARY-CACHE-LOCATION-FROM-LOCATION, this
906 ;; is an easier packaging up of the call to it.
909 (declare (fixnum line))
910 (location-line (line-primary-location line)))
912 (line-primary-location (line)
913 (declare (fixnum line))
914 (compute-primary-cache-location-from-location
915 (cache) (line-location line))))
916 (declare (ignorable #'cache #'nkeys #'line-size #'vector #'valuep
917 #'nlines #'max-location #'limit-fn #'size
918 #'mask #'field #'overflow #'line-reserved-p
919 #'location-reserved-p #'line-location
920 #'location-line #'line-wrappers #'location-wrappers
921 #'line-matches-wrappers-p
922 #'location-matches-wrappers-p
923 #'line-value #'location-value #'line-full-p
924 #'line-valid-p #'location-valid-p
925 #'line-separation #'next-line #'next-location
926 #'line-primary #'line-primary-location))
929 ;;; Here is where we actually fill, recache and expand caches.
931 ;;; The functions FILL-CACHE and PROBE-CACHE are the ONLY external
932 ;;; entrypoints into this code.
934 ;;; FILL-CACHE returns 1 value: a new cache
936 ;;; a wrapper field number
939 ;;; an absolute cache size (the size of the actual vector)
940 ;;; It tries to re-adjust the cache every time it makes a new fill.
941 ;;; The intuition here is that we want uniformity in the number of
942 ;;; probes needed to find an entry. Furthermore, adjusting has the
943 ;;; nice property of throwing out any entries that are invalid.
944 (defvar *cache-expand-threshold* 1.25)
946 (defun fill-cache (cache wrappers value)
947 ;; FILL-CACHE won't return if WRAPPERS is nil, might as well check..
950 (or (fill-cache-p nil cache wrappers value)
951 (and (< (ceiling (* (cache-count cache) *cache-expand-threshold*))
952 (if (= (cache-nkeys cache) 1)
953 (1- (cache-nlines cache))
954 (cache-nlines cache)))
955 (adjust-cache cache wrappers value))
956 (expand-cache cache wrappers value)))
958 (defvar *check-cache-p* nil)
960 (defmacro maybe-check-cache (cache)
962 (when *check-cache-p*
963 (check-cache ,cache))
966 (defun check-cache (cache)
967 (with-local-cache-functions (cache)
968 (let ((location (if (= (nkeys) 1) 0 1))
969 (limit (funcall (limit-fn) (nlines))))
970 (dotimes-fixnum (i (nlines) cache)
971 (when (and (not (location-reserved-p location))
973 (let* ((home-loc (compute-primary-cache-location-from-location
975 (home (location-line (if (location-reserved-p home-loc)
976 (next-location home-loc)
978 (sep (when home (line-separation home i))))
979 (when (and sep (> sep limit))
980 (error "bad cache ~S ~@
981 value at location ~W: ~W lines from its home. The limit is ~W."
982 cache location sep limit))))
983 (setq location (next-location location))))))
985 (defun probe-cache (cache wrappers &optional default limit-fn)
986 ;;(declare (values value))
988 (with-local-cache-functions (cache)
989 (let* ((location (compute-primary-cache-location (field) (mask) wrappers))
990 (limit (funcall (or limit-fn (limit-fn)) (nlines))))
991 (declare (fixnum location limit))
992 (when (location-reserved-p location)
993 (setq location (next-location location)))
994 (dotimes-fixnum (i (1+ limit))
995 (when (location-matches-wrappers-p location wrappers)
996 (return-from probe-cache (or (not (valuep))
997 (location-value location))))
998 (setq location (next-location location)))
999 (dolist (entry (overflow))
1000 (when (equal (car entry) wrappers)
1001 (return-from probe-cache (or (not (valuep))
1005 (defun map-cache (function cache &optional set-p)
1006 (with-local-cache-functions (cache)
1007 (let ((set-p (and set-p (valuep))))
1008 (dotimes-fixnum (i (nlines) cache)
1009 (unless (or (line-reserved-p i) (not (line-valid-p i nil)))
1010 (let ((value (funcall function (line-wrappers i) (line-value i))))
1012 (setf (cache-vector-ref (vector) (+ (line-location i) (nkeys)))
1014 (dolist (entry (overflow))
1015 (let ((value (funcall function (car entry) (cdr entry))))
1017 (setf (cdr entry) value))))))
1020 (defun cache-count (cache)
1021 (with-local-cache-functions (cache)
1023 (declare (fixnum count))
1024 (dotimes-fixnum (i (nlines) count)
1025 (unless (line-reserved-p i)
1026 (when (line-full-p i)
1029 (defun entry-in-cache-p (cache wrappers value)
1030 (declare (ignore value))
1031 (with-local-cache-functions (cache)
1032 (dotimes-fixnum (i (nlines))
1033 (unless (line-reserved-p i)
1034 (when (equal (line-wrappers i) wrappers)
1037 ;;; returns T or NIL
1038 (defun fill-cache-p (forcep cache wrappers value)
1039 (with-local-cache-functions (cache)
1040 (let* ((location (compute-primary-cache-location (field) (mask) wrappers))
1041 (primary (location-line location)))
1042 (declare (fixnum location primary))
1043 ;; FIXME: I tried (aver (> location 0)) and (aver (not
1044 ;; (location-reserved-p location))) here, on the basis that
1045 ;; particularly passing a LOCATION of 0 for a cache with more
1046 ;; than one key would cause PRIMARY to be -1. However, the
1047 ;; AVERs triggered during the bootstrap, and removing them
1048 ;; didn't cause anything to break, so I've left them removed.
1049 ;; I'm still confused as to what is right. -- CSR, 2006-04-20
1050 (multiple-value-bind (free emptyp)
1051 (find-free-cache-line primary cache wrappers)
1052 (when (or forcep emptyp)
1054 (push (cons (line-wrappers free) (line-value free))
1055 (cache-overflow cache)))
1056 ;;(fill-line free wrappers value)
1058 (declare (fixnum line))
1059 (when (line-reserved-p line)
1060 (error "attempt to fill a reserved line"))
1061 (let ((loc (line-location line))
1062 (cache-vector (vector)))
1063 (declare (fixnum loc) (simple-vector cache-vector))
1064 (cond ((= (nkeys) 1)
1065 (setf (cache-vector-ref cache-vector loc) wrappers)
1067 (setf (cache-vector-ref cache-vector (1+ loc)) value)))
1070 (declare (fixnum i))
1071 (dolist (w wrappers)
1072 (setf (cache-vector-ref cache-vector (+ loc i)) w)
1073 (setq i (the fixnum (1+ i)))))
1075 (setf (cache-vector-ref cache-vector (+ loc (nkeys)))
1077 (maybe-check-cache cache))))))))
1079 (defun fill-cache-from-cache-p (forcep cache from-cache from-line)
1080 (declare (fixnum from-line))
1081 (with-local-cache-functions (cache)
1082 (let ((primary (location-line
1083 (compute-primary-cache-location-from-location
1084 cache (line-location from-line) from-cache))))
1085 (declare (fixnum primary))
1086 (multiple-value-bind (free emptyp)
1087 (find-free-cache-line primary cache)
1088 (when (or forcep emptyp)
1090 (push (cons (line-wrappers free) (line-value free))
1091 (cache-overflow cache)))
1092 ;;(transfer-line from-cache-vector from-line cache-vector free)
1093 (let ((from-cache-vector (cache-vector from-cache))
1094 (to-cache-vector (vector))
1096 (declare (fixnum to-line))
1097 (if (line-reserved-p to-line)
1098 (error "transferring something into a reserved cache line")
1099 (let ((from-loc (line-location from-line))
1100 (to-loc (line-location to-line)))
1101 (declare (fixnum from-loc to-loc))
1102 (modify-cache to-cache-vector
1103 (dotimes-fixnum (i (line-size))
1104 (setf (cache-vector-ref to-cache-vector
1106 (cache-vector-ref from-cache-vector
1107 (+ from-loc i)))))))
1108 (maybe-check-cache cache)))))))
1110 ;;; Returns NIL or (values <field> <cache-vector>)
1112 ;;; This is only called when it isn't possible to put the entry in the
1113 ;;; cache the easy way. That is, this function assumes that
1114 ;;; FILL-CACHE-P has been called as returned NIL.
1116 ;;; If this returns NIL, it means that it wasn't possible to find a
1117 ;;; wrapper field for which all of the entries could be put in the
1118 ;;; cache (within the limit).
1119 (defun adjust-cache (cache wrappers value)
1120 (with-local-cache-functions (cache)
1121 (let ((ncache (get-cache-from-cache cache (nlines) (field))))
1122 (do ((nfield (cache-field ncache)
1123 (next-wrapper-cache-number-index nfield)))
1125 (setf (cache-field ncache) nfield)
1126 (labels ((try-one-fill-from-line (line)
1127 (fill-cache-from-cache-p nil ncache cache line))
1128 (try-one-fill (wrappers value)
1129 (fill-cache-p nil ncache wrappers value)))
1130 (if (and (dotimes-fixnum (i (nlines) t)
1131 (when (and (null (line-reserved-p i))
1132 (line-valid-p i wrappers))
1133 (unless (try-one-fill-from-line i) (return nil))))
1134 (dolist (wrappers+value (cache-overflow cache) t)
1135 (unless (try-one-fill (car wrappers+value) (cdr wrappers+value))
1137 (try-one-fill wrappers value))
1138 (return (maybe-check-cache ncache))
1139 (flush-cache-vector-internal (cache-vector ncache))))))))
1141 ;;; returns: (values <cache>)
1142 (defun expand-cache (cache wrappers value)
1143 ;;(declare (values cache))
1144 (with-local-cache-functions (cache)
1145 (let ((ncache (get-cache-from-cache cache (* (nlines) 2))))
1146 (labels ((do-one-fill-from-line (line)
1147 (unless (fill-cache-from-cache-p nil ncache cache line)
1148 (do-one-fill (line-wrappers line) (line-value line))))
1149 (do-one-fill (wrappers value)
1150 (setq ncache (or (adjust-cache ncache wrappers value)
1151 (fill-cache-p t ncache wrappers value))))
1152 (try-one-fill (wrappers value)
1153 (fill-cache-p nil ncache wrappers value)))
1154 (dotimes-fixnum (i (nlines))
1155 (when (and (null (line-reserved-p i))
1156 (line-valid-p i wrappers))
1157 (do-one-fill-from-line i)))
1158 (dolist (wrappers+value (cache-overflow cache))
1159 (unless (try-one-fill (car wrappers+value) (cdr wrappers+value))
1160 (do-one-fill (car wrappers+value) (cdr wrappers+value))))
1161 (unless (try-one-fill wrappers value)
1162 (do-one-fill wrappers value))
1163 (maybe-check-cache ncache)))))
1165 (defvar *pcl-misc-random-state* (make-random-state))
1167 ;;; This is the heart of the cache filling mechanism. It implements
1168 ;;; the decisions about where entries are placed.
1170 ;;; Find a line in the cache at which a new entry can be inserted.
1173 ;;; <empty?> is <line> in fact empty?
1174 (defun find-free-cache-line (primary cache &optional wrappers)
1175 ;;(declare (values line empty?))
1176 (declare (fixnum primary))
1177 (with-local-cache-functions (cache)
1178 (when (line-reserved-p primary) (setq primary (next-line primary)))
1179 (let ((limit (funcall (limit-fn) (nlines)))
1182 (p primary) (s primary))
1183 (declare (fixnum p s limit))
1186 ;; Try to find a free line starting at <s>. <p> is the
1187 ;; primary line of the entry we are finding a free
1188 ;; line for, it is used to compute the separations.
1189 (do* ((line s (next-line line))
1190 (nsep (line-separation p s) (1+ nsep)))
1192 (declare (fixnum line nsep))
1193 (when (null (line-valid-p line wrappers)) ;If this line is empty or
1194 (push line lines) ;invalid, just use it.
1195 (return-from find-free))
1196 (when (and wrappedp (>= line primary))
1197 ;; have gone all the way around the cache, time to quit
1198 (return-from find-free-cache-line (values primary nil)))
1199 (let ((osep (line-separation (line-primary line) line)))
1200 (when (>= osep limit)
1201 (return-from find-free-cache-line (values primary nil)))
1202 (when (cond ((= nsep limit) t)
1204 (zerop (random 2 *pcl-misc-random-state*)))
1207 ;; See whether we can displace what is in this line so that we
1208 ;; can use the line.
1209 (when (= line (the fixnum (1- (nlines)))) (setq wrappedp t))
1210 (setq p (line-primary line))
1211 (setq s (next-line line))
1214 (when (= line (the fixnum (1- (nlines)))) (setq wrappedp t)))))
1215 ;; Do all the displacing.
1217 (when (null (cdr lines)) (return nil))
1218 (let ((dline (pop lines))
1220 (declare (fixnum dline line))
1221 ;;Copy from line to dline (dline is known to be free).
1222 (let ((from-loc (line-location line))
1223 (to-loc (line-location dline))
1224 (cache-vector (vector)))
1225 (declare (fixnum from-loc to-loc) (simple-vector cache-vector))
1226 (modify-cache cache-vector
1227 (dotimes-fixnum (i (line-size))
1228 (setf (cache-vector-ref cache-vector
1230 (cache-vector-ref cache-vector
1232 (setf (cache-vector-ref cache-vector
1235 (values (car lines) t))))
1237 (defun default-limit-fn (nlines)