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 ;;; The caching algorithm implemented:
30 ;;; << put a paper here >>
32 ;;; For now, understand that as far as most of this code goes, a cache
33 ;;; has two important properties. The first is the number of wrappers
34 ;;; used as keys in each cache line. Throughout this code, this value
35 ;;; is always called NKEYS. The second is whether or not the cache
36 ;;; lines of a cache store a value. Throughout this code, this always
39 ;;; Depending on these values, there are three kinds of caches.
41 ;;; NKEYS = 1, VALUEP = NIL
43 ;;; In this kind of cache, each line is 1 word long. No cache locking
44 ;;; is needed since all read's in the cache are a single value.
45 ;;; Nevertheless line 0 (location 0) is reserved, to ensure that
46 ;;; invalid wrappers will not get a first probe hit.
48 ;;; To keep the code simpler, a cache lock count does appear in
49 ;;; location 0 of these caches, that count is incremented whenever
50 ;;; data is written to the cache. But, the actual lookup code (see
51 ;;; make-dlap) doesn't need to do locking when reading the cache.
53 ;;; NKEYS = 1, VALUEP = T
55 ;;; In this kind of cache, each line is 2 words long. Cache locking
56 ;;; must be done to ensure the synchronization of cache reads. Line 0
57 ;;; of the cache (location 0) is reserved for the cache lock count.
58 ;;; Location 1 of the cache is unused (in effect wasted).
62 ;;; In this kind of cache, the 0 word of the cache holds the lock
63 ;;; count. The 1 word of the cache is line 0. Line 0 of these caches
66 ;;; This is done because in this sort of cache, the overhead of doing
67 ;;; the cache probe is high enough that the 1+ required to offset the
68 ;;; location is not a significant cost. In addition, because of the
69 ;;; larger line sizes, the space that would be wasted by reserving
70 ;;; line 0 to hold the lock count is more significant.
74 ;;; A cache is essentially just a vector. The use of the individual
75 ;;; `words' in the vector depends on particular properties of the
76 ;;; cache as described above.
78 ;;; This defines an abstraction for caches in terms of their most
79 ;;; obvious implementation as simple vectors. But, please notice that
80 ;;; part of the implementation of this abstraction, is the function
81 ;;; lap-out-cache-ref. This means that most port-specific
82 ;;; modifications to the implementation of caches will require
83 ;;; corresponding port-specific modifications to the lap code
85 (defmacro cache-vector-ref (cache-vector location)
86 `(svref (the simple-vector ,cache-vector)
87 (sb-ext:truly-the fixnum ,location)))
89 (defmacro cache-vector-size (cache-vector)
90 `(array-dimension (the simple-vector ,cache-vector) 0))
92 (defun allocate-cache-vector (size)
93 (make-array size :adjustable nil))
95 (defmacro cache-vector-lock-count (cache-vector)
96 `(cache-vector-ref ,cache-vector 0))
98 (defun flush-cache-vector-internal (cache-vector)
100 (fill (the simple-vector cache-vector) nil)
101 (setf (cache-vector-lock-count cache-vector) 0))
104 (defmacro modify-cache (cache-vector &body body)
106 (multiple-value-prog1
108 (let ((old-count (cache-vector-lock-count ,cache-vector)))
109 (declare (fixnum old-count))
110 (setf (cache-vector-lock-count ,cache-vector)
111 (if (= old-count most-positive-fixnum)
112 1 (the fixnum (1+ old-count))))))))
114 (deftype field-type ()
115 '(mod #.layout-clos-hash-length))
117 (eval-when (:compile-toplevel :load-toplevel :execute)
118 (defun power-of-two-ceiling (x)
120 ;;(expt 2 (ceiling (log x 2)))
121 (the fixnum (ash 1 (integer-length (1- x)))))
124 (defconstant +nkeys-limit+ 256)
126 (defstruct (cache (:constructor make-cache ())
127 (:copier copy-cache-internal))
129 (nkeys 1 :type (integer 1 #.+nkeys-limit+))
130 (valuep nil :type (member nil t))
131 (nlines 0 :type fixnum)
132 (field 0 :type field-type)
133 (limit-fn #'default-limit-fn :type function)
134 (mask 0 :type fixnum)
135 (size 0 :type fixnum)
136 (line-size 1 :type (integer 1 #.(power-of-two-ceiling (1+ +nkeys-limit+))))
137 (max-location 0 :type fixnum)
138 (vector #() :type simple-vector)
139 (overflow nil :type list))
141 #-sb-fluid (declaim (sb-ext:freeze-type cache))
143 (defmacro cache-lock-count (cache)
144 `(cache-vector-lock-count (cache-vector ,cache)))
146 ;;; Return a cache that has had FLUSH-CACHE-VECTOR-INTERNAL called on
147 ;;; it. This returns a cache of exactly the size requested, it won't
148 ;;; ever return a larger cache.
149 (defun get-cache-vector (size)
150 (flush-cache-vector-internal (make-array size)))
153 ;;;; wrapper cache numbers
155 ;;; The constant WRAPPER-CACHE-NUMBER-ADDS-OK controls the number of
156 ;;; non-zero bits wrapper cache numbers will have.
158 ;;; The value of this constant is the number of wrapper cache numbers
159 ;;; which can be added and still be certain the result will be a
160 ;;; fixnum. This is used by all the code that computes primary cache
161 ;;; locations from multiple wrappers.
163 ;;; The value of this constant is used to derive the next two which
164 ;;; are the forms of this constant which it is more convenient for the
165 ;;; runtime code to use.
166 (defconstant wrapper-cache-number-length
167 (integer-length layout-clos-hash-max))
168 (defconstant wrapper-cache-number-mask layout-clos-hash-max)
169 (defconstant wrapper-cache-number-adds-ok
170 (truncate most-positive-fixnum layout-clos-hash-max))
172 ;;;; wrappers themselves
174 ;;; This caching algorithm requires that wrappers have more than one
175 ;;; wrapper cache number. You should think of these multiple numbers
176 ;;; as being in columns. That is, for a given cache, the same column
177 ;;; of wrapper cache numbers will be used.
179 ;;; If at some point the cache distribution of a cache gets bad, the
180 ;;; cache can be rehashed by switching to a different column.
182 ;;; The columns are referred to by field number which is that number
183 ;;; which, when used as a second argument to wrapper-ref, will return
184 ;;; that column of wrapper cache number.
186 ;;; This code is written to allow flexibility as to how many wrapper
187 ;;; cache numbers will be in each wrapper, and where they will be
188 ;;; located. It is also set up to allow port specific modifications to
189 ;;; `pack' the wrapper cache numbers on machines where the addressing
190 ;;; modes make that a good idea.
192 ;;; In SBCL, as in CMU CL, we want to do type checking as early as
193 ;;; possible; structures help this. The structures are hard-wired to
194 ;;; have a fixed number of cache hash values, and that number must
195 ;;; correspond to the number of cache lines we use.
196 (defconstant wrapper-cache-number-vector-length
197 layout-clos-hash-length)
199 (unless (boundp '*the-class-t*)
200 (setq *the-class-t* nil))
202 (defmacro wrapper-class (wrapper)
203 `(classoid-pcl-class (layout-classoid ,wrapper)))
204 (defmacro wrapper-no-of-instance-slots (wrapper)
205 `(layout-length ,wrapper))
207 (defmacro wrapper-instance-slots-layout (wrapper)
208 `(%wrapper-instance-slots-layout ,wrapper))
209 (defmacro wrapper-class-slots (wrapper)
210 `(%wrapper-class-slots ,wrapper))
211 (defmacro wrapper-cache-number-vector (x) x)
213 ;;; This is called in BRAID when we are making wrappers for classes
214 ;;; whose slots are not initialized yet, and which may be built-in
215 ;;; classes. We pass in the class name in addition to the class.
216 (defun boot-make-wrapper (length name &optional class)
217 (let ((found (find-classoid name nil)))
220 (unless (classoid-pcl-class found)
221 (setf (classoid-pcl-class found) class))
222 (aver (eq (classoid-pcl-class found) class))
223 (let ((layout (classoid-layout found)))
227 (make-wrapper-internal
229 :classoid (make-standard-classoid
230 :name name :pcl-class class))))))
232 ;;; The following variable may be set to a STANDARD-CLASS that has
233 ;;; already been created by the lisp code and which is to be redefined
234 ;;; by PCL. This allows STANDARD-CLASSes to be defined and used for
235 ;;; type testing and dispatch before PCL is loaded.
236 (defvar *pcl-class-boot* nil)
238 ;;; In SBCL, as in CMU CL, the layouts (a.k.a wrappers) for built-in
239 ;;; and structure classes already exist when PCL is initialized, so we
240 ;;; don't necessarily always make a wrapper. Also, we help maintain
241 ;;; the mapping between CL:CLASS and SB-KERNEL:CLASSOID objects.
242 (defun make-wrapper (length class)
244 ((or (typep class 'std-class)
245 (typep class 'forward-referenced-class))
246 (make-wrapper-internal
249 (let ((owrap (class-wrapper class)))
251 (layout-classoid owrap))
252 ((or (*subtypep (class-of class) *the-class-standard-class*)
253 (typep class 'forward-referenced-class))
254 (cond ((and *pcl-class-boot*
255 (eq (slot-value class 'name) *pcl-class-boot*))
256 (let ((found (find-classoid
257 (slot-value class 'name))))
258 (unless (classoid-pcl-class found)
259 (setf (classoid-pcl-class found) class))
260 (aver (eq (classoid-pcl-class found) class))
263 (make-standard-classoid :pcl-class class))))
265 (make-random-pcl-classoid :pcl-class class))))))
267 (let* ((found (find-classoid (slot-value class 'name)))
268 (layout (classoid-layout found)))
269 (unless (classoid-pcl-class found)
270 (setf (classoid-pcl-class found) class))
271 (aver (eq (classoid-pcl-class found) class))
275 (defconstant +first-wrapper-cache-number-index+ 0)
277 (declaim (inline next-wrapper-cache-number-index))
278 (defun next-wrapper-cache-number-index (field-number)
279 (and (< field-number #.(1- wrapper-cache-number-vector-length))
282 ;;; FIXME: Why are there two layers here, with one operator trivially
283 ;;; defined in terms of the other? It'd be nice either to have a
284 ;;; comment explaining why the separation is valuable, or to collapse
285 ;;; it into a single layer.
287 ;;; FIXME (?): These are logically inline functions, but they need to
288 ;;; be SETFable, and for now it seems not worth the trouble to DEFUN
289 ;;; both inline FOO and inline (SETF FOO) for each one instead of a
290 ;;; single macro. Perhaps the best thing would be to make them
291 ;;; immutable (since it seems sort of surprising and gross to be able
292 ;;; to modify hash values) so that they can become inline functions
293 ;;; with no muss or fuss. I (WHN) didn't do this only because I didn't
294 ;;; know whether any code anywhere depends on the values being
296 (defmacro cache-number-vector-ref (cnv n)
297 `(wrapper-cache-number-vector-ref ,cnv ,n))
298 (defmacro wrapper-cache-number-vector-ref (wrapper n)
299 `(layout-clos-hash ,wrapper ,n))
301 (declaim (inline wrapper-class*))
302 (defun wrapper-class* (wrapper)
303 (or (wrapper-class wrapper)
304 (ensure-non-standard-class
305 (classoid-name (layout-classoid wrapper)))))
307 ;;; The wrapper cache machinery provides general mechanism for
308 ;;; trapping on the next access to any instance of a given class. This
309 ;;; mechanism is used to implement the updating of instances when the
310 ;;; class is redefined (MAKE-INSTANCES-OBSOLETE). The same mechanism
311 ;;; is also used to update generic function caches when there is a
312 ;;; change to the superclasses of a class.
314 ;;; Basically, a given wrapper can be valid or invalid. If it is
315 ;;; invalid, it means that any attempt to do a wrapper cache lookup
316 ;;; using the wrapper should trap. Also, methods on
317 ;;; SLOT-VALUE-USING-CLASS check the wrapper validity as well. This is
318 ;;; done by calling CHECK-WRAPPER-VALIDITY.
320 (declaim (inline invalid-wrapper-p))
321 (defun invalid-wrapper-p (wrapper)
322 (not (null (layout-invalid wrapper))))
324 (defvar *previous-nwrappers* (make-hash-table))
326 (defun invalidate-wrapper (owrapper state nwrapper)
327 (aver (member state '(:flush :obsolete) :test #'eq))
328 (let ((new-previous ()))
329 ;; First off, a previous call to INVALIDATE-WRAPPER may have
330 ;; recorded OWRAPPER as an NWRAPPER to update to. Since OWRAPPER
331 ;; is about to be invalid, it no longer makes sense to update to
334 ;; We go back and change the previously invalidated wrappers so
335 ;; that they will now update directly to NWRAPPER. This
336 ;; corresponds to a kind of transitivity of wrapper updates.
337 (dolist (previous (gethash owrapper *previous-nwrappers*))
338 (when (eq state :obsolete)
339 (setf (car previous) :obsolete))
340 (setf (cadr previous) nwrapper)
341 (push previous new-previous))
343 (let ((ocnv (wrapper-cache-number-vector owrapper)))
344 (dotimes (i layout-clos-hash-length)
345 (setf (cache-number-vector-ref ocnv i) 0)))
347 (push (setf (layout-invalid owrapper) (list state nwrapper))
350 (setf (gethash owrapper *previous-nwrappers*) ()
351 (gethash nwrapper *previous-nwrappers*) new-previous)))
353 (defun check-wrapper-validity (instance)
354 (let* ((owrapper (wrapper-of instance))
355 (state (layout-invalid owrapper)))
360 (flush-cache-trap owrapper (cadr state) instance))
362 (obsolete-instance-trap owrapper (cadr state) instance))))))
364 (declaim (inline check-obsolete-instance))
365 (defun check-obsolete-instance (instance)
366 (when (invalid-wrapper-p (layout-of instance))
367 (check-wrapper-validity instance)))
370 (defun get-cache (nkeys valuep limit-fn nlines)
371 (let ((cache (make-cache)))
372 (declare (type cache cache))
373 (multiple-value-bind (cache-mask actual-size line-size nlines)
374 (compute-cache-parameters nkeys valuep nlines)
375 (setf (cache-nkeys cache) nkeys
376 (cache-valuep cache) valuep
377 (cache-nlines cache) nlines
378 (cache-field cache) +first-wrapper-cache-number-index+
379 (cache-limit-fn cache) limit-fn
380 (cache-mask cache) cache-mask
381 (cache-size cache) actual-size
382 (cache-line-size cache) line-size
383 (cache-max-location cache) (let ((line (1- nlines)))
386 (1+ (* line line-size))))
387 (cache-vector cache) (get-cache-vector actual-size)
388 (cache-overflow cache) nil)
391 (defun get-cache-from-cache (old-cache new-nlines
392 &optional (new-field +first-wrapper-cache-number-index+))
393 (let ((nkeys (cache-nkeys old-cache))
394 (valuep (cache-valuep old-cache))
395 (cache (make-cache)))
396 (declare (type cache cache))
397 (multiple-value-bind (cache-mask actual-size line-size nlines)
398 (if (= new-nlines (cache-nlines old-cache))
399 (values (cache-mask old-cache) (cache-size old-cache)
400 (cache-line-size old-cache) (cache-nlines old-cache))
401 (compute-cache-parameters nkeys valuep new-nlines))
402 (setf (cache-owner cache) (cache-owner old-cache)
403 (cache-nkeys cache) nkeys
404 (cache-valuep cache) valuep
405 (cache-nlines cache) nlines
406 (cache-field cache) new-field
407 (cache-limit-fn cache) (cache-limit-fn old-cache)
408 (cache-mask cache) cache-mask
409 (cache-size cache) actual-size
410 (cache-line-size cache) line-size
411 (cache-max-location cache) (let ((line (1- nlines)))
414 (1+ (* line line-size))))
415 (cache-vector cache) (get-cache-vector actual-size)
416 (cache-overflow cache) nil)
419 (defun copy-cache (old-cache)
420 (let* ((new-cache (copy-cache-internal old-cache))
421 (size (cache-size old-cache))
422 (old-vector (cache-vector old-cache))
423 (new-vector (get-cache-vector size)))
424 (declare (simple-vector old-vector new-vector))
425 (dotimes-fixnum (i size)
426 (setf (svref new-vector i) (svref old-vector i)))
427 (setf (cache-vector new-cache) new-vector)
430 (defun compute-line-size (x)
431 (power-of-two-ceiling x))
433 (defun compute-cache-parameters (nkeys valuep nlines-or-cache-vector)
434 ;;(declare (values cache-mask actual-size line-size nlines))
435 (declare (fixnum nkeys))
437 (let* ((line-size (if valuep 2 1))
438 (cache-size (if (typep nlines-or-cache-vector 'fixnum)
442 (power-of-two-ceiling
443 nlines-or-cache-vector))))
444 (cache-vector-size nlines-or-cache-vector))))
445 (declare (fixnum line-size cache-size))
446 (values (logxor (the fixnum (1- cache-size)) (the fixnum (1- line-size)))
449 (the (values fixnum t) (floor cache-size line-size))))
450 (let* ((line-size (power-of-two-ceiling (if valuep (1+ nkeys) nkeys)))
451 (cache-size (if (typep nlines-or-cache-vector 'fixnum)
455 (power-of-two-ceiling
456 nlines-or-cache-vector))))
457 (1- (cache-vector-size nlines-or-cache-vector)))))
458 (declare (fixnum line-size cache-size))
459 (values (logxor (the fixnum (1- cache-size)) (the fixnum (1- line-size)))
460 (the fixnum (1+ cache-size))
462 (the (values fixnum t) (floor cache-size line-size))))))
464 ;;; the various implementations of computing a primary cache location from
465 ;;; wrappers. Because some implementations of this must run fast there are
466 ;;; several implementations of the same algorithm.
468 ;;; The algorithm is:
470 ;;; SUM over the wrapper cache numbers,
471 ;;; ENSURING that the result is a fixnum
472 ;;; MASK the result against the mask argument.
474 ;;; The basic functional version. This is used by the cache miss code to
475 ;;; compute the primary location of an entry.
476 (defun compute-primary-cache-location (field mask wrappers)
478 (declare (type field-type field) (fixnum mask))
479 (if (not (listp wrappers))
481 (the fixnum (wrapper-cache-number-vector-ref wrappers field)))
482 (let ((location 0) (i 0))
483 (declare (fixnum location i))
484 (dolist (wrapper wrappers)
485 ;; First add the cache number of this wrapper to location.
486 (let ((wrapper-cache-number (wrapper-cache-number-vector-ref wrapper
488 (declare (fixnum wrapper-cache-number))
489 (if (zerop wrapper-cache-number)
490 (return-from compute-primary-cache-location 0)
492 (the fixnum (+ location wrapper-cache-number)))))
493 ;; Then, if we are working with lots of wrappers, deal with
494 ;; the wrapper-cache-number-mask stuff.
495 (when (and (not (zerop i))
496 (zerop (mod i wrapper-cache-number-adds-ok)))
498 (logand location wrapper-cache-number-mask)))
500 (the fixnum (1+ (logand mask location))))))
502 ;;; This version is called on a cache line. It fetches the wrappers
503 ;;; from the cache line and determines the primary location. Various
504 ;;; parts of the cache filling code call this to determine whether it
505 ;;; is appropriate to displace a given cache entry.
507 ;;; If this comes across a wrapper whose CACHE-NO is 0, it returns the
508 ;;; symbol invalid to suggest to its caller that it would be provident
509 ;;; to blow away the cache line in question.
510 (defun compute-primary-cache-location-from-location (to-cache
513 (from-cache to-cache))
514 (declare (type cache to-cache from-cache) (fixnum from-location))
516 (cache-vector (cache-vector from-cache))
517 (field (cache-field to-cache))
518 (mask (cache-mask to-cache))
519 (nkeys (cache-nkeys to-cache)))
520 (declare (type field-type field) (fixnum result mask nkeys)
521 (simple-vector cache-vector))
522 (dotimes-fixnum (i nkeys)
523 (let* ((wrapper (cache-vector-ref cache-vector (+ i from-location)))
524 (wcn (wrapper-cache-number-vector-ref wrapper field)))
525 (declare (fixnum wcn))
526 (setq result (+ result wcn)))
527 (when (and (not (zerop i))
528 (zerop (mod i wrapper-cache-number-adds-ok)))
529 (setq result (logand result wrapper-cache-number-mask))))
532 (the fixnum (1+ (logand mask result))))))
534 ;;; NIL means nothing so far, no actual arg info has NILs
536 ;;; CLASS seen all sorts of metaclasses
537 ;;; (specifically, more than one of the next 4 values)
538 ;;; T means everything so far is the class T
539 ;;; STANDARD-CLASS seen only standard classes
540 ;;; BUILT-IN-CLASS seen only built in classes
541 ;;; STRUCTURE-CLASS seen only structure classes
542 (defun raise-metatype (metatype new-specializer)
543 (let ((slot (find-class 'slot-class))
544 (std (find-class 'std-class))
545 (standard (find-class 'standard-class))
546 (fsc (find-class 'funcallable-standard-class))
547 (condition (find-class 'condition-class))
548 (structure (find-class 'structure-class))
549 (built-in (find-class 'built-in-class)))
550 (flet ((specializer->metatype (x)
551 (let ((meta-specializer
552 (if (eq *boot-state* 'complete)
553 (class-of (specializer-class x))
556 ((eq x *the-class-t*) t)
557 ((*subtypep meta-specializer std) 'standard-instance)
558 ((*subtypep meta-specializer standard) 'standard-instance)
559 ((*subtypep meta-specializer fsc) 'standard-instance)
560 ((*subtypep meta-specializer condition) 'condition-instance)
561 ((*subtypep meta-specializer structure) 'structure-instance)
562 ((*subtypep meta-specializer built-in) 'built-in-instance)
563 ((*subtypep meta-specializer slot) 'slot-instance)
564 (t (error "~@<PCL cannot handle the specializer ~S ~
565 (meta-specializer ~S).~@:>"
567 meta-specializer))))))
568 ;; We implement the following table. The notation is
569 ;; that X and Y are distinct meta specializer names.
571 ;; NIL <anything> ===> <anything>
574 (let ((new-metatype (specializer->metatype new-specializer)))
575 (cond ((eq new-metatype 'slot-instance) 'class)
576 ((null metatype) new-metatype)
577 ((eq metatype new-metatype) new-metatype)
580 (defmacro with-dfun-wrappers ((args metatypes)
581 (dfun-wrappers invalid-wrapper-p
582 &optional wrappers classes types)
583 invalid-arguments-form
585 `(let* ((args-tail ,args) (,invalid-wrapper-p nil) (invalid-arguments-p nil)
586 (,dfun-wrappers nil) (dfun-wrappers-tail nil)
588 `((wrappers-rev nil) (types-rev nil) (classes-rev nil))))
589 (dolist (mt ,metatypes)
591 (setq invalid-arguments-p t)
593 (let* ((arg (pop args-tail))
596 `((class *the-class-t*)
599 (setq wrapper (wrapper-of arg))
600 (when (invalid-wrapper-p wrapper)
601 (setq ,invalid-wrapper-p t)
602 (setq wrapper (check-wrapper-validity arg)))
603 (cond ((null ,dfun-wrappers)
604 (setq ,dfun-wrappers wrapper))
605 ((not (consp ,dfun-wrappers))
606 (setq dfun-wrappers-tail (list wrapper))
607 (setq ,dfun-wrappers (cons ,dfun-wrappers dfun-wrappers-tail)))
609 (let ((new-dfun-wrappers-tail (list wrapper)))
610 (setf (cdr dfun-wrappers-tail) new-dfun-wrappers-tail)
611 (setf dfun-wrappers-tail new-dfun-wrappers-tail))))
613 `((setq class (wrapper-class* wrapper))
614 (setq type `(class-eq ,class)))))
616 `((push wrapper wrappers-rev)
617 (push class classes-rev)
618 (push type types-rev)))))
619 (if invalid-arguments-p
620 ,invalid-arguments-form
621 (let* (,@(when wrappers
622 `((,wrappers (nreverse wrappers-rev))
623 (,classes (nreverse classes-rev))
624 (,types (mapcar (lambda (class)
629 ;;;; some support stuff for getting a hold of symbols that we need when
630 ;;;; building the discriminator codes. It's OK for these to be interned
631 ;;;; symbols because we don't capture any user code in the scope in which
632 ;;;; these symbols are bound.
634 (defvar *dfun-arg-symbols* '(.ARG0. .ARG1. .ARG2. .ARG3.))
636 (defun dfun-arg-symbol (arg-number)
637 (or (nth arg-number (the list *dfun-arg-symbols*))
638 (intern (format nil ".ARG~A." arg-number) *pcl-package*)))
640 (defvar *slot-vector-symbols* '(.SLOTS0. .SLOTS1. .SLOTS2. .SLOTS3.))
642 (defun slot-vector-symbol (arg-number)
643 (or (nth arg-number (the list *slot-vector-symbols*))
644 (intern (format nil ".SLOTS~A." arg-number) *pcl-package*)))
646 ;; FIXME: There ought to be a good way to factor out the idiom:
648 ;; (dotimes (i (length metatypes))
649 ;; (push (dfun-arg-symbol i) lambda-list))
651 ;; used in the following four functions into common code that we can
652 ;; declare inline or something. --njf 2001-12-20
653 (defun make-dfun-lambda-list (metatypes applyp)
654 (let ((lambda-list nil))
655 (dotimes (i (length metatypes))
656 (push (dfun-arg-symbol i) lambda-list))
658 (push '&rest lambda-list)
659 (push '.dfun-rest-arg. lambda-list))
660 (nreverse lambda-list)))
662 (defun make-dlap-lambda-list (metatypes applyp)
663 (let ((lambda-list nil))
664 (dotimes (i (length metatypes))
665 (push (dfun-arg-symbol i) lambda-list))
666 ;; FIXME: This is translated directly from the old PCL code.
667 ;; It didn't have a (PUSH '.DFUN-REST-ARG. LAMBDA-LIST) or
668 ;; something similar, so we don't either. It's hard to see how
669 ;; this could be correct, since &REST wants an argument after
670 ;; it. This function works correctly because the caller
671 ;; magically tacks on something after &REST. The calling functions
672 ;; (in dlisp.lisp) should be fixed and this function rewritten.
675 (push '&rest lambda-list))
676 (nreverse lambda-list)))
678 ;; FIXME: The next two functions suffer from having a `.DFUN-REST-ARG.'
679 ;; in their lambda lists, but no corresponding `&REST' symbol. We assume
680 ;; this should be the case by analogy with the previous two functions.
681 ;; It works, and I don't know why. Check the calling functions and
682 ;; fix these too. --njf 2001-12-20
683 (defun make-emf-call (metatypes applyp fn-variable &optional emf-type)
685 (let ((required nil))
686 (dotimes (i (length metatypes))
687 (push (dfun-arg-symbol i) required))
688 (nreverse required))))
689 `(,(if (eq emf-type 'fast-method-call)
690 'invoke-effective-method-function-fast
691 'invoke-effective-method-function)
692 ,fn-variable ,applyp ,@required ,@(when applyp `(.dfun-rest-arg.)))))
694 (defun make-fast-method-call-lambda-list (metatypes applyp)
695 (let ((reversed-lambda-list nil))
696 (push '.pv-cell. reversed-lambda-list)
697 (push '.next-method-call. reversed-lambda-list)
698 (dotimes (i (length metatypes))
699 (push (dfun-arg-symbol i) reversed-lambda-list))
701 (push '.dfun-rest-arg. reversed-lambda-list))
702 (nreverse reversed-lambda-list)))
704 (defmacro with-local-cache-functions ((cache) &body body)
705 `(let ((.cache. ,cache))
706 (declare (type cache .cache.))
707 (labels ((cache () .cache.)
708 (nkeys () (cache-nkeys .cache.))
709 (line-size () (cache-line-size .cache.))
710 (vector () (cache-vector .cache.))
711 (valuep () (cache-valuep .cache.))
712 (nlines () (cache-nlines .cache.))
713 (max-location () (cache-max-location .cache.))
714 (limit-fn () (cache-limit-fn .cache.))
715 (size () (cache-size .cache.))
716 (mask () (cache-mask .cache.))
717 (field () (cache-field .cache.))
718 (overflow () (cache-overflow .cache.))
720 ;; Return T IFF this cache location is reserved. The
721 ;; only time this is true is for line number 0 of an
724 (line-reserved-p (line)
725 (declare (fixnum line))
729 (location-reserved-p (location)
730 (declare (fixnum location))
734 ;; Given a line number, return the cache location.
735 ;; This is the value that is the second argument to
736 ;; cache-vector-ref. Basically, this deals with the
737 ;; offset of nkeys>1 caches and multiplies by line
740 (line-location (line)
741 (declare (fixnum line))
742 (when (line-reserved-p line)
743 (error "line is reserved"))
745 (the fixnum (* line (line-size)))
746 (the fixnum (1+ (the fixnum (* line (line-size)))))))
748 ;; Given a cache location, return the line. This is
749 ;; the inverse of LINE-LOCATION.
751 (location-line (location)
752 (declare (fixnum location))
754 (floor location (line-size))
755 (floor (the fixnum (1- location)) (line-size))))
757 ;; Given a line number, return the wrappers stored at
758 ;; that line. As usual, if nkeys=1, this returns a
759 ;; single value. Only when nkeys>1 does it return a
760 ;; list. An error is signalled if the line is
763 (line-wrappers (line)
764 (declare (fixnum line))
765 (when (line-reserved-p line) (error "Line is reserved."))
766 (location-wrappers (line-location line)))
768 (location-wrappers (location) ; avoid multiplies caused by line-location
769 (declare (fixnum location))
771 (cache-vector-ref (vector) location)
772 (let ((list (make-list (nkeys)))
774 (declare (simple-vector vector))
775 (dotimes (i (nkeys) list)
778 (cache-vector-ref vector (+ location i)))))))
780 ;; Given a line number, return true IFF the line's
781 ;; wrappers are the same as wrappers.
783 (line-matches-wrappers-p (line wrappers)
784 (declare (fixnum line))
785 (and (not (line-reserved-p line))
786 (location-matches-wrappers-p (line-location line)
789 (location-matches-wrappers-p (loc wrappers) ; must not be reserved
790 (declare (fixnum loc))
791 (let ((cache-vector (vector)))
792 (declare (simple-vector cache-vector))
794 (eq wrappers (cache-vector-ref cache-vector loc))
795 (dotimes (i (nkeys) t)
797 (unless (eq (pop wrappers)
798 (cache-vector-ref cache-vector (+ loc i)))
801 ;; Given a line number, return the value stored at that line.
802 ;; If valuep is NIL, this returns NIL. As with line-wrappers,
803 ;; an error is signalled if the line is reserved.
806 (declare (fixnum line))
807 (when (line-reserved-p line) (error "Line is reserved."))
808 (location-value (line-location line)))
810 (location-value (loc)
811 (declare (fixnum loc))
813 (cache-vector-ref (vector) (+ loc (nkeys)))))
815 ;; Given a line number, return true IFF that line has data in
816 ;; it. The state of the wrappers stored in the line is not
817 ;; checked. An error is signalled if line is reserved.
819 (when (line-reserved-p line) (error "Line is reserved."))
820 (not (null (cache-vector-ref (vector) (line-location line)))))
822 ;; Given a line number, return true IFF the line is full and
823 ;; there are no invalid wrappers in the line, and the line's
824 ;; wrappers are different from wrappers.
825 ;; An error is signalled if the line is reserved.
827 (line-valid-p (line wrappers)
828 (declare (fixnum line))
829 (when (line-reserved-p line) (error "Line is reserved."))
830 (location-valid-p (line-location line) wrappers))
832 (location-valid-p (loc wrappers)
833 (declare (fixnum loc))
834 (let ((cache-vector (vector))
835 (wrappers-mismatch-p (null wrappers)))
836 (declare (simple-vector cache-vector))
837 (dotimes (i (nkeys) wrappers-mismatch-p)
839 (let ((wrapper (cache-vector-ref cache-vector (+ loc i))))
840 (when (or (null wrapper)
841 (invalid-wrapper-p wrapper))
843 (unless (and wrappers
848 (setq wrappers-mismatch-p t))))))
850 ;; How many unreserved lines separate line-1 and line-2.
852 (line-separation (line-1 line-2)
853 (declare (fixnum line-1 line-2))
854 (let ((diff (the fixnum (- line-2 line-1))))
855 (declare (fixnum diff))
857 (setq diff (+ diff (nlines)))
858 (when (line-reserved-p 0)
859 (setq diff (1- diff))))
862 ;; Given a cache line, get the next cache line. This will not
863 ;; return a reserved line.
866 (declare (fixnum line))
867 (if (= line (the fixnum (1- (nlines))))
868 (if (line-reserved-p 0) 1 0)
869 (the fixnum (1+ line))))
872 (declare (fixnum loc))
873 (if (= loc (max-location))
877 (the fixnum (+ loc (line-size)))))
879 ;; Given a line which has a valid entry in it, this
880 ;; will return the primary cache line of the wrappers
881 ;; in that line. We just call
882 ;; COMPUTE-PRIMARY-CACHE-LOCATION-FROM-LOCATION, this
883 ;; is an easier packaging up of the call to it.
886 (declare (fixnum line))
887 (location-line (line-primary-location line)))
889 (line-primary-location (line)
890 (declare (fixnum line))
891 (compute-primary-cache-location-from-location
892 (cache) (line-location line))))
893 (declare (ignorable #'cache #'nkeys #'line-size #'vector #'valuep
894 #'nlines #'max-location #'limit-fn #'size
895 #'mask #'field #'overflow #'line-reserved-p
896 #'location-reserved-p #'line-location
897 #'location-line #'line-wrappers #'location-wrappers
898 #'line-matches-wrappers-p
899 #'location-matches-wrappers-p
900 #'line-value #'location-value #'line-full-p
901 #'line-valid-p #'location-valid-p
902 #'line-separation #'next-line #'next-location
903 #'line-primary #'line-primary-location))
906 ;;; Here is where we actually fill, recache and expand caches.
908 ;;; The functions FILL-CACHE and PROBE-CACHE are the ONLY external
909 ;;; entrypoints into this code.
911 ;;; FILL-CACHE returns 1 value: a new cache
913 ;;; a wrapper field number
916 ;;; an absolute cache size (the size of the actual vector)
917 ;;; It tries to re-adjust the cache every time it makes a new fill.
918 ;;; The intuition here is that we want uniformity in the number of
919 ;;; probes needed to find an entry. Furthermore, adjusting has the
920 ;;; nice property of throwing out any entries that are invalid.
921 (defvar *cache-expand-threshold* 1.25)
923 (defun fill-cache (cache wrappers value)
924 ;; FILL-CACHE won't return if WRAPPERS is nil, might as well check..
927 (or (fill-cache-p nil cache wrappers value)
928 (and (< (ceiling (* (cache-count cache) 1.25))
929 (if (= (cache-nkeys cache) 1)
930 (1- (cache-nlines cache))
931 (cache-nlines cache)))
932 (adjust-cache cache wrappers value))
933 (expand-cache cache wrappers value)))
935 (defvar *check-cache-p* nil)
937 (defmacro maybe-check-cache (cache)
939 (when *check-cache-p*
940 (check-cache ,cache))
943 (defun check-cache (cache)
944 (with-local-cache-functions (cache)
945 (let ((location (if (= (nkeys) 1) 0 1))
946 (limit (funcall (limit-fn) (nlines))))
947 (dotimes-fixnum (i (nlines) cache)
948 (when (and (not (location-reserved-p location))
950 (let* ((home-loc (compute-primary-cache-location-from-location
952 (home (location-line (if (location-reserved-p home-loc)
953 (next-location home-loc)
955 (sep (when home (line-separation home i))))
956 (when (and sep (> sep limit))
957 (error "bad cache ~S ~@
958 value at location ~W: ~W lines from its home. The limit is ~W."
959 cache location sep limit))))
960 (setq location (next-location location))))))
962 (defun probe-cache (cache wrappers &optional default limit-fn)
963 ;;(declare (values value))
965 ;; FIXME: This and another earlier test on a WRAPPERS arg can
966 ;; be compact assertoids.
967 (error "WRAPPERS arg is NIL!"))
968 (with-local-cache-functions (cache)
969 (let* ((location (compute-primary-cache-location (field) (mask) wrappers))
970 (limit (funcall (or limit-fn (limit-fn)) (nlines))))
971 (declare (fixnum location limit))
972 (when (location-reserved-p location)
973 (setq location (next-location location)))
974 (dotimes-fixnum (i (1+ limit))
975 (when (location-matches-wrappers-p location wrappers)
976 (return-from probe-cache (or (not (valuep))
977 (location-value location))))
978 (setq location (next-location location)))
979 (dolist (entry (overflow))
980 (when (equal (car entry) wrappers)
981 (return-from probe-cache (or (not (valuep))
985 (defun map-cache (function cache &optional set-p)
986 (with-local-cache-functions (cache)
987 (let ((set-p (and set-p (valuep))))
988 (dotimes-fixnum (i (nlines) cache)
989 (unless (or (line-reserved-p i) (not (line-valid-p i nil)))
990 (let ((value (funcall function (line-wrappers i) (line-value i))))
992 (setf (cache-vector-ref (vector) (+ (line-location i) (nkeys)))
994 (dolist (entry (overflow))
995 (let ((value (funcall function (car entry) (cdr entry))))
997 (setf (cdr entry) value))))))
1000 (defun cache-count (cache)
1001 (with-local-cache-functions (cache)
1003 (declare (fixnum count))
1004 (dotimes-fixnum (i (nlines) count)
1005 (unless (line-reserved-p i)
1006 (when (line-full-p i)
1009 (defun entry-in-cache-p (cache wrappers value)
1010 (declare (ignore value))
1011 (with-local-cache-functions (cache)
1012 (dotimes-fixnum (i (nlines))
1013 (unless (line-reserved-p i)
1014 (when (equal (line-wrappers i) wrappers)
1017 ;;; returns T or NIL
1018 (defun fill-cache-p (forcep cache wrappers value)
1019 (with-local-cache-functions (cache)
1020 (let* ((location (compute-primary-cache-location (field) (mask) wrappers))
1021 (primary (location-line location)))
1022 (declare (fixnum location primary))
1023 (multiple-value-bind (free emptyp)
1024 (find-free-cache-line primary cache wrappers)
1025 (when (or forcep emptyp)
1027 (push (cons (line-wrappers free) (line-value free))
1028 (cache-overflow cache)))
1029 ;;(fill-line free wrappers value)
1031 (declare (fixnum line))
1032 (when (line-reserved-p line)
1033 (error "attempt to fill a reserved line"))
1034 (let ((loc (line-location line))
1035 (cache-vector (vector)))
1036 (declare (fixnum loc) (simple-vector cache-vector))
1037 (cond ((= (nkeys) 1)
1038 (setf (cache-vector-ref cache-vector loc) wrappers)
1040 (setf (cache-vector-ref cache-vector (1+ loc)) value)))
1043 (declare (fixnum i))
1044 (dolist (w wrappers)
1045 (setf (cache-vector-ref cache-vector (+ loc i)) w)
1046 (setq i (the fixnum (1+ i)))))
1048 (setf (cache-vector-ref cache-vector (+ loc (nkeys)))
1050 (maybe-check-cache cache))))))))
1052 (defun fill-cache-from-cache-p (forcep cache from-cache from-line)
1053 (declare (fixnum from-line))
1054 (with-local-cache-functions (cache)
1055 (let ((primary (location-line
1056 (compute-primary-cache-location-from-location
1057 cache (line-location from-line) from-cache))))
1058 (declare (fixnum primary))
1059 (multiple-value-bind (free emptyp)
1060 (find-free-cache-line primary cache)
1061 (when (or forcep emptyp)
1063 (push (cons (line-wrappers free) (line-value free))
1064 (cache-overflow cache)))
1065 ;;(transfer-line from-cache-vector from-line cache-vector free)
1066 (let ((from-cache-vector (cache-vector from-cache))
1067 (to-cache-vector (vector))
1069 (declare (fixnum to-line))
1070 (if (line-reserved-p to-line)
1071 (error "transferring something into a reserved cache line")
1072 (let ((from-loc (line-location from-line))
1073 (to-loc (line-location to-line)))
1074 (declare (fixnum from-loc to-loc))
1075 (modify-cache to-cache-vector
1076 (dotimes-fixnum (i (line-size))
1077 (setf (cache-vector-ref to-cache-vector
1079 (cache-vector-ref from-cache-vector
1080 (+ from-loc i)))))))
1081 (maybe-check-cache cache)))))))
1083 ;;; Returns NIL or (values <field> <cache-vector>)
1085 ;;; This is only called when it isn't possible to put the entry in the
1086 ;;; cache the easy way. That is, this function assumes that
1087 ;;; FILL-CACHE-P has been called as returned NIL.
1089 ;;; If this returns NIL, it means that it wasn't possible to find a
1090 ;;; wrapper field for which all of the entries could be put in the
1091 ;;; cache (within the limit).
1092 (defun adjust-cache (cache wrappers value)
1093 (with-local-cache-functions (cache)
1094 (let ((ncache (get-cache-from-cache cache (nlines) (field))))
1095 (do ((nfield (cache-field ncache)
1096 (next-wrapper-cache-number-index nfield)))
1098 (setf (cache-field ncache) nfield)
1099 (labels ((try-one-fill-from-line (line)
1100 (fill-cache-from-cache-p nil ncache cache line))
1101 (try-one-fill (wrappers value)
1102 (fill-cache-p nil ncache wrappers value)))
1103 (if (and (dotimes-fixnum (i (nlines) t)
1104 (when (and (null (line-reserved-p i))
1105 (line-valid-p i wrappers))
1106 (unless (try-one-fill-from-line i) (return nil))))
1107 (dolist (wrappers+value (cache-overflow cache) t)
1108 (unless (try-one-fill (car wrappers+value) (cdr wrappers+value))
1110 (try-one-fill wrappers value))
1111 (return (maybe-check-cache ncache))
1112 (flush-cache-vector-internal (cache-vector ncache))))))))
1114 ;;; returns: (values <cache>)
1115 (defun expand-cache (cache wrappers value)
1116 ;;(declare (values cache))
1117 (with-local-cache-functions (cache)
1118 (let ((ncache (get-cache-from-cache cache (* (nlines) 2))))
1119 (labels ((do-one-fill-from-line (line)
1120 (unless (fill-cache-from-cache-p nil ncache cache line)
1121 (do-one-fill (line-wrappers line) (line-value line))))
1122 (do-one-fill (wrappers value)
1123 (setq ncache (or (adjust-cache ncache wrappers value)
1124 (fill-cache-p t ncache wrappers value))))
1125 (try-one-fill (wrappers value)
1126 (fill-cache-p nil ncache wrappers value)))
1127 (dotimes-fixnum (i (nlines))
1128 (when (and (null (line-reserved-p i))
1129 (line-valid-p i wrappers))
1130 (do-one-fill-from-line i)))
1131 (dolist (wrappers+value (cache-overflow cache))
1132 (unless (try-one-fill (car wrappers+value) (cdr wrappers+value))
1133 (do-one-fill (car wrappers+value) (cdr wrappers+value))))
1134 (unless (try-one-fill wrappers value)
1135 (do-one-fill wrappers value))
1136 (maybe-check-cache ncache)))))
1138 ;;; This is the heart of the cache filling mechanism. It implements
1139 ;;; the decisions about where entries are placed.
1141 ;;; Find a line in the cache at which a new entry can be inserted.
1144 ;;; <empty?> is <line> in fact empty?
1145 (defun find-free-cache-line (primary cache &optional wrappers)
1146 ;;(declare (values line empty?))
1147 (declare (fixnum primary))
1148 (with-local-cache-functions (cache)
1149 (when (line-reserved-p primary) (setq primary (next-line primary)))
1150 (let ((limit (funcall (limit-fn) (nlines)))
1153 (p primary) (s primary))
1154 (declare (fixnum p s limit))
1157 ;; Try to find a free line starting at <s>. <p> is the
1158 ;; primary line of the entry we are finding a free
1159 ;; line for, it is used to compute the separations.
1160 (do* ((line s (next-line line))
1161 (nsep (line-separation p s) (1+ nsep)))
1163 (declare (fixnum line nsep))
1164 (when (null (line-valid-p line wrappers)) ;If this line is empty or
1165 (push line lines) ;invalid, just use it.
1166 (return-from find-free))
1167 (when (and wrappedp (>= line primary))
1168 ;; have gone all the way around the cache, time to quit
1169 (return-from find-free-cache-line (values primary nil)))
1170 (let ((osep (line-separation (line-primary line) line)))
1171 (when (>= osep limit)
1172 (return-from find-free-cache-line (values primary nil)))
1173 (when (cond ((= nsep limit) t)
1174 ((= nsep osep) (zerop (random 2)))
1177 ;; See whether we can displace what is in this line so that we
1178 ;; can use the line.
1179 (when (= line (the fixnum (1- (nlines)))) (setq wrappedp t))
1180 (setq p (line-primary line))
1181 (setq s (next-line line))
1184 (when (= line (the fixnum (1- (nlines)))) (setq wrappedp t)))))
1185 ;; Do all the displacing.
1187 (when (null (cdr lines)) (return nil))
1188 (let ((dline (pop lines))
1190 (declare (fixnum dline line))
1191 ;;Copy from line to dline (dline is known to be free).
1192 (let ((from-loc (line-location line))
1193 (to-loc (line-location dline))
1194 (cache-vector (vector)))
1195 (declare (fixnum from-loc to-loc) (simple-vector cache-vector))
1196 (modify-cache cache-vector
1197 (dotimes-fixnum (i (line-size))
1198 (setf (cache-vector-ref cache-vector
1200 (cache-vector-ref cache-vector
1202 (setf (cache-vector-ref cache-vector
1205 (values (car lines) t))))
1207 (defun default-limit-fn (nlines)
1213 (defvar *empty-cache* (make-cache)) ; for defstruct slot initial value forms