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 ;;; FIXME: Why are these macros?
208 (defmacro wrapper-instance-slots-layout (wrapper)
209 `(%wrapper-instance-slots-layout ,wrapper))
210 (defmacro wrapper-class-slots (wrapper)
211 `(%wrapper-class-slots ,wrapper))
212 (defmacro wrapper-cache-number-vector (x) x)
214 ;;; This is called in BRAID when we are making wrappers for classes
215 ;;; whose slots are not initialized yet, and which may be built-in
216 ;;; classes. We pass in the class name in addition to the class.
217 (defun boot-make-wrapper (length name &optional class)
218 (let ((found (find-classoid name nil)))
221 (unless (classoid-pcl-class found)
222 (setf (classoid-pcl-class found) class))
223 (aver (eq (classoid-pcl-class found) class))
224 (let ((layout (classoid-layout found)))
228 (make-wrapper-internal
230 :classoid (make-standard-classoid
231 :name name :pcl-class class))))))
233 ;;; The following variable may be set to a STANDARD-CLASS that has
234 ;;; already been created by the lisp code and which is to be redefined
235 ;;; by PCL. This allows STANDARD-CLASSes to be defined and used for
236 ;;; type testing and dispatch before PCL is loaded.
237 (defvar *pcl-class-boot* nil)
239 ;;; In SBCL, as in CMU CL, the layouts (a.k.a wrappers) for built-in
240 ;;; and structure classes already exist when PCL is initialized, so we
241 ;;; don't necessarily always make a wrapper. Also, we help maintain
242 ;;; the mapping between CL:CLASS and SB-KERNEL:CLASSOID objects.
243 (defun make-wrapper (length class)
245 ((or (typep class 'std-class)
246 (typep class 'forward-referenced-class))
247 (make-wrapper-internal
250 (let ((owrap (class-wrapper class)))
252 (layout-classoid owrap))
253 ((or (*subtypep (class-of class) *the-class-standard-class*)
254 (typep class 'forward-referenced-class))
255 (cond ((and *pcl-class-boot*
256 (eq (slot-value class 'name) *pcl-class-boot*))
257 (let ((found (find-classoid
258 (slot-value class 'name))))
259 (unless (classoid-pcl-class found)
260 (setf (classoid-pcl-class found) class))
261 (aver (eq (classoid-pcl-class found) class))
264 (make-standard-classoid :pcl-class class))))
266 (make-random-pcl-classoid :pcl-class class))))))
268 (let* ((found (find-classoid (slot-value class 'name)))
269 (layout (classoid-layout found)))
270 (unless (classoid-pcl-class found)
271 (setf (classoid-pcl-class found) class))
272 (aver (eq (classoid-pcl-class found) class))
276 (defconstant +first-wrapper-cache-number-index+ 0)
278 (declaim (inline next-wrapper-cache-number-index))
279 (defun next-wrapper-cache-number-index (field-number)
280 (and (< field-number #.(1- wrapper-cache-number-vector-length))
283 ;;; FIXME: Why are there two layers here, with one operator trivially
284 ;;; defined in terms of the other? It'd be nice either to have a
285 ;;; comment explaining why the separation is valuable, or to collapse
286 ;;; it into a single layer.
288 ;;; FIXME (?): These are logically inline functions, but they need to
289 ;;; be SETFable, and for now it seems not worth the trouble to DEFUN
290 ;;; both inline FOO and inline (SETF FOO) for each one instead of a
291 ;;; single macro. Perhaps the best thing would be to make them
292 ;;; immutable (since it seems sort of surprising and gross to be able
293 ;;; to modify hash values) so that they can become inline functions
294 ;;; with no muss or fuss. I (WHN) didn't do this only because I didn't
295 ;;; know whether any code anywhere depends on the values being
297 (defmacro cache-number-vector-ref (cnv n)
298 `(wrapper-cache-number-vector-ref ,cnv ,n))
299 (defmacro wrapper-cache-number-vector-ref (wrapper n)
300 `(layout-clos-hash ,wrapper ,n))
302 (declaim (inline wrapper-class*))
303 (defun wrapper-class* (wrapper)
304 (or (wrapper-class wrapper)
305 (ensure-non-standard-class
306 (classoid-name (layout-classoid wrapper)))))
308 ;;; The wrapper cache machinery provides general mechanism for
309 ;;; trapping on the next access to any instance of a given class. This
310 ;;; mechanism is used to implement the updating of instances when the
311 ;;; class is redefined (MAKE-INSTANCES-OBSOLETE). The same mechanism
312 ;;; is also used to update generic function caches when there is a
313 ;;; change to the superclasses of a class.
315 ;;; Basically, a given wrapper can be valid or invalid. If it is
316 ;;; invalid, it means that any attempt to do a wrapper cache lookup
317 ;;; using the wrapper should trap. Also, methods on
318 ;;; SLOT-VALUE-USING-CLASS check the wrapper validity as well. This is
319 ;;; done by calling CHECK-WRAPPER-VALIDITY.
321 (declaim (inline invalid-wrapper-p))
322 (defun invalid-wrapper-p (wrapper)
323 (not (null (layout-invalid wrapper))))
325 (defvar *previous-nwrappers* (make-hash-table))
327 (defun invalidate-wrapper (owrapper state nwrapper)
328 (aver (member state '(:flush :obsolete) :test #'eq))
329 (let ((new-previous ()))
330 ;; First off, a previous call to INVALIDATE-WRAPPER may have
331 ;; recorded OWRAPPER as an NWRAPPER to update to. Since OWRAPPER
332 ;; is about to be invalid, it no longer makes sense to update to
335 ;; We go back and change the previously invalidated wrappers so
336 ;; that they will now update directly to NWRAPPER. This
337 ;; corresponds to a kind of transitivity of wrapper updates.
338 (dolist (previous (gethash owrapper *previous-nwrappers*))
339 (when (eq state :obsolete)
340 (setf (car previous) :obsolete))
341 (setf (cadr previous) nwrapper)
342 (push previous new-previous))
344 (let ((ocnv (wrapper-cache-number-vector owrapper)))
345 (dotimes (i layout-clos-hash-length)
346 (setf (cache-number-vector-ref ocnv i) 0)))
348 (push (setf (layout-invalid owrapper) (list state nwrapper))
351 (setf (gethash owrapper *previous-nwrappers*) ()
352 (gethash nwrapper *previous-nwrappers*) new-previous)))
354 (defun check-wrapper-validity (instance)
355 (let* ((owrapper (wrapper-of instance))
356 (state (layout-invalid owrapper)))
357 (aver (not (eq state :uninitialized)))
360 ;; FIXME: I can't help thinking that, while this does cure the
361 ;; symptoms observed from some class redefinitions, this isn't
362 ;; the place to be doing this flushing. Nevertheless... --
366 ;; We assume in this case, that the :INVALID is from a
367 ;; previous call to REGISTER-LAYOUT for a superclass of
368 ;; INSTANCE's class. See also the comment above
369 ;; FORCE-CACHE-FLUSHES. Paul Dietz has test cases for this.
371 (force-cache-flushes (class-of instance))
372 (check-wrapper-validity instance))
376 (flush-cache-trap owrapper (cadr state) instance))
378 (obsolete-instance-trap owrapper (cadr state) instance)))))))
380 (declaim (inline check-obsolete-instance))
381 (defun check-obsolete-instance (instance)
382 (when (invalid-wrapper-p (layout-of instance))
383 (check-wrapper-validity instance)))
386 (defun get-cache (nkeys valuep limit-fn nlines)
387 (let ((cache (make-cache)))
388 (declare (type cache cache))
389 (multiple-value-bind (cache-mask actual-size line-size nlines)
390 (compute-cache-parameters nkeys valuep nlines)
391 (setf (cache-nkeys cache) nkeys
392 (cache-valuep cache) valuep
393 (cache-nlines cache) nlines
394 (cache-field cache) +first-wrapper-cache-number-index+
395 (cache-limit-fn cache) limit-fn
396 (cache-mask cache) cache-mask
397 (cache-size cache) actual-size
398 (cache-line-size cache) line-size
399 (cache-max-location cache) (let ((line (1- nlines)))
402 (1+ (* line line-size))))
403 (cache-vector cache) (get-cache-vector actual-size)
404 (cache-overflow cache) nil)
407 (defun get-cache-from-cache (old-cache new-nlines
408 &optional (new-field +first-wrapper-cache-number-index+))
409 (let ((nkeys (cache-nkeys old-cache))
410 (valuep (cache-valuep old-cache))
411 (cache (make-cache)))
412 (declare (type cache cache))
413 (multiple-value-bind (cache-mask actual-size line-size nlines)
414 (if (= new-nlines (cache-nlines old-cache))
415 (values (cache-mask old-cache) (cache-size old-cache)
416 (cache-line-size old-cache) (cache-nlines old-cache))
417 (compute-cache-parameters nkeys valuep new-nlines))
418 (setf (cache-owner cache) (cache-owner old-cache)
419 (cache-nkeys cache) nkeys
420 (cache-valuep cache) valuep
421 (cache-nlines cache) nlines
422 (cache-field cache) new-field
423 (cache-limit-fn cache) (cache-limit-fn old-cache)
424 (cache-mask cache) cache-mask
425 (cache-size cache) actual-size
426 (cache-line-size cache) line-size
427 (cache-max-location cache) (let ((line (1- nlines)))
430 (1+ (* line line-size))))
431 (cache-vector cache) (get-cache-vector actual-size)
432 (cache-overflow cache) nil)
435 (defun copy-cache (old-cache)
436 (let* ((new-cache (copy-cache-internal old-cache))
437 (size (cache-size old-cache))
438 (old-vector (cache-vector old-cache))
439 (new-vector (get-cache-vector size)))
440 (declare (simple-vector old-vector new-vector))
441 (dotimes-fixnum (i size)
442 (setf (svref new-vector i) (svref old-vector i)))
443 (setf (cache-vector new-cache) new-vector)
446 (defun compute-line-size (x)
447 (power-of-two-ceiling x))
449 (defun compute-cache-parameters (nkeys valuep nlines-or-cache-vector)
450 ;;(declare (values cache-mask actual-size line-size nlines))
451 (declare (fixnum nkeys))
453 (let* ((line-size (if valuep 2 1))
454 (cache-size (if (typep nlines-or-cache-vector 'fixnum)
458 (power-of-two-ceiling
459 nlines-or-cache-vector))))
460 (cache-vector-size nlines-or-cache-vector))))
461 (declare (fixnum line-size cache-size))
462 (values (logxor (the fixnum (1- cache-size)) (the fixnum (1- line-size)))
465 (the (values fixnum t) (floor cache-size line-size))))
466 (let* ((line-size (power-of-two-ceiling (if valuep (1+ nkeys) nkeys)))
467 (cache-size (if (typep nlines-or-cache-vector 'fixnum)
471 (power-of-two-ceiling
472 nlines-or-cache-vector))))
473 (1- (cache-vector-size nlines-or-cache-vector)))))
474 (declare (fixnum line-size cache-size))
475 (values (logxor (the fixnum (1- cache-size)) (the fixnum (1- line-size)))
476 (the fixnum (1+ cache-size))
478 (the (values fixnum t) (floor cache-size line-size))))))
480 ;;; the various implementations of computing a primary cache location from
481 ;;; wrappers. Because some implementations of this must run fast there are
482 ;;; several implementations of the same algorithm.
484 ;;; The algorithm is:
486 ;;; SUM over the wrapper cache numbers,
487 ;;; ENSURING that the result is a fixnum
488 ;;; MASK the result against the mask argument.
490 ;;; The basic functional version. This is used by the cache miss code to
491 ;;; compute the primary location of an entry.
492 (defun compute-primary-cache-location (field mask wrappers)
494 (declare (type field-type field) (fixnum mask))
495 (if (not (listp wrappers))
497 (the fixnum (wrapper-cache-number-vector-ref wrappers field)))
498 (let ((location 0) (i 0))
499 (declare (fixnum location i))
500 (dolist (wrapper wrappers)
501 ;; First add the cache number of this wrapper to location.
502 (let ((wrapper-cache-number (wrapper-cache-number-vector-ref wrapper
504 (declare (fixnum wrapper-cache-number))
505 (if (zerop wrapper-cache-number)
506 (return-from compute-primary-cache-location 0)
508 (the fixnum (+ location wrapper-cache-number)))))
509 ;; Then, if we are working with lots of wrappers, deal with
510 ;; the wrapper-cache-number-mask stuff.
511 (when (and (not (zerop i))
512 (zerop (mod i wrapper-cache-number-adds-ok)))
514 (logand location wrapper-cache-number-mask)))
516 (the fixnum (1+ (logand mask location))))))
518 ;;; This version is called on a cache line. It fetches the wrappers
519 ;;; from the cache line and determines the primary location. Various
520 ;;; parts of the cache filling code call this to determine whether it
521 ;;; is appropriate to displace a given cache entry.
523 ;;; If this comes across a wrapper whose CACHE-NO is 0, it returns the
524 ;;; symbol invalid to suggest to its caller that it would be provident
525 ;;; to blow away the cache line in question.
526 (defun compute-primary-cache-location-from-location (to-cache
529 (from-cache to-cache))
530 (declare (type cache to-cache from-cache) (fixnum from-location))
532 (cache-vector (cache-vector from-cache))
533 (field (cache-field to-cache))
534 (mask (cache-mask to-cache))
535 (nkeys (cache-nkeys to-cache)))
536 (declare (type field-type field) (fixnum result mask nkeys)
537 (simple-vector cache-vector))
538 (dotimes-fixnum (i nkeys)
539 (let* ((wrapper (cache-vector-ref cache-vector (+ i from-location)))
540 (wcn (wrapper-cache-number-vector-ref wrapper field)))
541 (declare (fixnum wcn))
542 (setq result (+ result wcn)))
543 (when (and (not (zerop i))
544 (zerop (mod i wrapper-cache-number-adds-ok)))
545 (setq result (logand result wrapper-cache-number-mask))))
548 (the fixnum (1+ (logand mask result))))))
550 ;;; NIL means nothing so far, no actual arg info has NILs
552 ;;; CLASS seen all sorts of metaclasses
553 ;;; (specifically, more than one of the next 4 values)
554 ;;; T means everything so far is the class T
555 ;;; STANDARD-CLASS seen only standard classes
556 ;;; BUILT-IN-CLASS seen only built in classes
557 ;;; STRUCTURE-CLASS seen only structure classes
558 (defun raise-metatype (metatype new-specializer)
559 (let ((slot (find-class 'slot-class))
560 (std (find-class 'std-class))
561 (standard (find-class 'standard-class))
562 (fsc (find-class 'funcallable-standard-class))
563 (condition (find-class 'condition-class))
564 (structure (find-class 'structure-class))
565 (built-in (find-class 'built-in-class)))
566 (flet ((specializer->metatype (x)
567 (let ((meta-specializer
568 (if (eq *boot-state* 'complete)
569 (class-of (specializer-class x))
572 ((eq x *the-class-t*) t)
573 ((*subtypep meta-specializer std) 'standard-instance)
574 ((*subtypep meta-specializer standard) 'standard-instance)
575 ((*subtypep meta-specializer fsc) 'standard-instance)
576 ((*subtypep meta-specializer condition) 'condition-instance)
577 ((*subtypep meta-specializer structure) 'structure-instance)
578 ((*subtypep meta-specializer built-in) 'built-in-instance)
579 ((*subtypep meta-specializer slot) 'slot-instance)
580 (t (error "~@<PCL cannot handle the specializer ~S ~
581 (meta-specializer ~S).~@:>"
583 meta-specializer))))))
584 ;; We implement the following table. The notation is
585 ;; that X and Y are distinct meta specializer names.
587 ;; NIL <anything> ===> <anything>
590 (let ((new-metatype (specializer->metatype new-specializer)))
591 (cond ((eq new-metatype 'slot-instance) 'class)
592 ((null metatype) new-metatype)
593 ((eq metatype new-metatype) new-metatype)
596 (defmacro with-dfun-wrappers ((args metatypes)
597 (dfun-wrappers invalid-wrapper-p
598 &optional wrappers classes types)
599 invalid-arguments-form
601 `(let* ((args-tail ,args) (,invalid-wrapper-p nil) (invalid-arguments-p nil)
602 (,dfun-wrappers nil) (dfun-wrappers-tail nil)
604 `((wrappers-rev nil) (types-rev nil) (classes-rev nil))))
605 (dolist (mt ,metatypes)
607 (setq invalid-arguments-p t)
609 (let* ((arg (pop args-tail))
612 `((class *the-class-t*)
615 (setq wrapper (wrapper-of arg))
616 (when (invalid-wrapper-p wrapper)
617 (setq ,invalid-wrapper-p t)
618 (setq wrapper (check-wrapper-validity arg)))
619 (cond ((null ,dfun-wrappers)
620 (setq ,dfun-wrappers wrapper))
621 ((not (consp ,dfun-wrappers))
622 (setq dfun-wrappers-tail (list wrapper))
623 (setq ,dfun-wrappers (cons ,dfun-wrappers dfun-wrappers-tail)))
625 (let ((new-dfun-wrappers-tail (list wrapper)))
626 (setf (cdr dfun-wrappers-tail) new-dfun-wrappers-tail)
627 (setf dfun-wrappers-tail new-dfun-wrappers-tail))))
629 `((setq class (wrapper-class* wrapper))
630 (setq type `(class-eq ,class)))))
632 `((push wrapper wrappers-rev)
633 (push class classes-rev)
634 (push type types-rev)))))
635 (if invalid-arguments-p
636 ,invalid-arguments-form
637 (let* (,@(when wrappers
638 `((,wrappers (nreverse wrappers-rev))
639 (,classes (nreverse classes-rev))
640 (,types (mapcar (lambda (class)
645 ;;;; some support stuff for getting a hold of symbols that we need when
646 ;;;; building the discriminator codes. It's OK for these to be interned
647 ;;;; symbols because we don't capture any user code in the scope in which
648 ;;;; these symbols are bound.
650 (defvar *dfun-arg-symbols* '(.ARG0. .ARG1. .ARG2. .ARG3.))
652 (defun dfun-arg-symbol (arg-number)
653 (or (nth arg-number (the list *dfun-arg-symbols*))
654 (format-symbol *pcl-package* ".ARG~A." arg-number)))
656 (defvar *slot-vector-symbols* '(.SLOTS0. .SLOTS1. .SLOTS2. .SLOTS3.))
658 (defun slot-vector-symbol (arg-number)
659 (or (nth arg-number (the list *slot-vector-symbols*))
660 (format-symbol *pcl-package* ".SLOTS~A." arg-number)))
662 ;; FIXME: There ought to be a good way to factor out the idiom:
664 ;; (dotimes (i (length metatypes))
665 ;; (push (dfun-arg-symbol i) lambda-list))
667 ;; used in the following four functions into common code that we can
668 ;; declare inline or something. --njf 2001-12-20
669 (defun make-dfun-lambda-list (metatypes applyp)
670 (let ((lambda-list nil))
671 (dotimes (i (length metatypes))
672 (push (dfun-arg-symbol i) lambda-list))
674 (push '&rest lambda-list)
675 (push '.dfun-rest-arg. lambda-list))
676 (nreverse lambda-list)))
678 (defun make-dlap-lambda-list (metatypes applyp)
679 (let ((lambda-list nil))
680 (dotimes (i (length metatypes))
681 (push (dfun-arg-symbol i) lambda-list))
682 ;; FIXME: This is translated directly from the old PCL code.
683 ;; It didn't have a (PUSH '.DFUN-REST-ARG. LAMBDA-LIST) or
684 ;; something similar, so we don't either. It's hard to see how
685 ;; this could be correct, since &REST wants an argument after
686 ;; it. This function works correctly because the caller
687 ;; magically tacks on something after &REST. The calling functions
688 ;; (in dlisp.lisp) should be fixed and this function rewritten.
691 (push '&rest lambda-list))
692 (nreverse lambda-list)))
694 ;; FIXME: The next two functions suffer from having a `.DFUN-REST-ARG.'
695 ;; in their lambda lists, but no corresponding `&REST' symbol. We assume
696 ;; this should be the case by analogy with the previous two functions.
697 ;; It works, and I don't know why. Check the calling functions and
698 ;; fix these too. --njf 2001-12-20
699 (defun make-emf-call (metatypes applyp fn-variable &optional emf-type)
701 (let ((required nil))
702 (dotimes (i (length metatypes))
703 (push (dfun-arg-symbol i) required))
704 (nreverse required))))
705 `(,(if (eq emf-type 'fast-method-call)
706 'invoke-effective-method-function-fast
707 'invoke-effective-method-function)
708 ,fn-variable ,applyp ,@required ,@(when applyp `(.dfun-rest-arg.)))))
710 (defun make-fast-method-call-lambda-list (metatypes applyp)
711 (let ((reversed-lambda-list nil))
712 (push '.pv-cell. reversed-lambda-list)
713 (push '.next-method-call. reversed-lambda-list)
714 (dotimes (i (length metatypes))
715 (push (dfun-arg-symbol i) reversed-lambda-list))
717 (push '.dfun-rest-arg. reversed-lambda-list))
718 (nreverse reversed-lambda-list)))
720 (defmacro with-local-cache-functions ((cache) &body body)
721 `(let ((.cache. ,cache))
722 (declare (type cache .cache.))
723 (labels ((cache () .cache.)
724 (nkeys () (cache-nkeys .cache.))
725 (line-size () (cache-line-size .cache.))
726 (vector () (cache-vector .cache.))
727 (valuep () (cache-valuep .cache.))
728 (nlines () (cache-nlines .cache.))
729 (max-location () (cache-max-location .cache.))
730 (limit-fn () (cache-limit-fn .cache.))
731 (size () (cache-size .cache.))
732 (mask () (cache-mask .cache.))
733 (field () (cache-field .cache.))
734 (overflow () (cache-overflow .cache.))
736 ;; Return T IFF this cache location is reserved. The
737 ;; only time this is true is for line number 0 of an
740 (line-reserved-p (line)
741 (declare (fixnum line))
745 (location-reserved-p (location)
746 (declare (fixnum location))
750 ;; Given a line number, return the cache location.
751 ;; This is the value that is the second argument to
752 ;; cache-vector-ref. Basically, this deals with the
753 ;; offset of nkeys>1 caches and multiplies by line
756 (line-location (line)
757 (declare (fixnum line))
758 (when (line-reserved-p line)
759 (error "line is reserved"))
761 (the fixnum (* line (line-size)))
762 (the fixnum (1+ (the fixnum (* line (line-size)))))))
764 ;; Given a cache location, return the line. This is
765 ;; the inverse of LINE-LOCATION.
767 (location-line (location)
768 (declare (fixnum location))
770 (floor location (line-size))
771 (floor (the fixnum (1- location)) (line-size))))
773 ;; Given a line number, return the wrappers stored at
774 ;; that line. As usual, if nkeys=1, this returns a
775 ;; single value. Only when nkeys>1 does it return a
776 ;; list. An error is signalled if the line is
779 (line-wrappers (line)
780 (declare (fixnum line))
781 (when (line-reserved-p line) (error "Line is reserved."))
782 (location-wrappers (line-location line)))
784 (location-wrappers (location) ; avoid multiplies caused by line-location
785 (declare (fixnum location))
787 (cache-vector-ref (vector) location)
788 (let ((list (make-list (nkeys)))
790 (declare (simple-vector vector))
791 (dotimes (i (nkeys) list)
794 (cache-vector-ref vector (+ location i)))))))
796 ;; Given a line number, return true IFF the line's
797 ;; wrappers are the same as wrappers.
799 (line-matches-wrappers-p (line wrappers)
800 (declare (fixnum line))
801 (and (not (line-reserved-p line))
802 (location-matches-wrappers-p (line-location line)
805 (location-matches-wrappers-p (loc wrappers) ; must not be reserved
806 (declare (fixnum loc))
807 (let ((cache-vector (vector)))
808 (declare (simple-vector cache-vector))
810 (eq wrappers (cache-vector-ref cache-vector loc))
811 (dotimes (i (nkeys) t)
813 (unless (eq (pop wrappers)
814 (cache-vector-ref cache-vector (+ loc i)))
817 ;; Given a line number, return the value stored at that line.
818 ;; If valuep is NIL, this returns NIL. As with line-wrappers,
819 ;; an error is signalled if the line is reserved.
822 (declare (fixnum line))
823 (when (line-reserved-p line) (error "Line is reserved."))
824 (location-value (line-location line)))
826 (location-value (loc)
827 (declare (fixnum loc))
829 (cache-vector-ref (vector) (+ loc (nkeys)))))
831 ;; Given a line number, return true IFF that line has data in
832 ;; it. The state of the wrappers stored in the line is not
833 ;; checked. An error is signalled if line is reserved.
835 (when (line-reserved-p line) (error "Line is reserved."))
836 (not (null (cache-vector-ref (vector) (line-location line)))))
838 ;; Given a line number, return true IFF the line is full and
839 ;; there are no invalid wrappers in the line, and the line's
840 ;; wrappers are different from wrappers.
841 ;; An error is signalled if the line is reserved.
843 (line-valid-p (line wrappers)
844 (declare (fixnum line))
845 (when (line-reserved-p line) (error "Line is reserved."))
846 (location-valid-p (line-location line) wrappers))
848 (location-valid-p (loc wrappers)
849 (declare (fixnum loc))
850 (let ((cache-vector (vector))
851 (wrappers-mismatch-p (null wrappers)))
852 (declare (simple-vector cache-vector))
853 (dotimes (i (nkeys) wrappers-mismatch-p)
855 (let ((wrapper (cache-vector-ref cache-vector (+ loc i))))
856 (when (or (null wrapper)
857 (invalid-wrapper-p wrapper))
859 (unless (and wrappers
864 (setq wrappers-mismatch-p t))))))
866 ;; How many unreserved lines separate line-1 and line-2.
868 (line-separation (line-1 line-2)
869 (declare (fixnum line-1 line-2))
870 (let ((diff (the fixnum (- line-2 line-1))))
871 (declare (fixnum diff))
873 (setq diff (+ diff (nlines)))
874 (when (line-reserved-p 0)
875 (setq diff (1- diff))))
878 ;; Given a cache line, get the next cache line. This will not
879 ;; return a reserved line.
882 (declare (fixnum line))
883 (if (= line (the fixnum (1- (nlines))))
884 (if (line-reserved-p 0) 1 0)
885 (the fixnum (1+ line))))
888 (declare (fixnum loc))
889 (if (= loc (max-location))
893 (the fixnum (+ loc (line-size)))))
895 ;; Given a line which has a valid entry in it, this
896 ;; will return the primary cache line of the wrappers
897 ;; in that line. We just call
898 ;; COMPUTE-PRIMARY-CACHE-LOCATION-FROM-LOCATION, this
899 ;; is an easier packaging up of the call to it.
902 (declare (fixnum line))
903 (location-line (line-primary-location line)))
905 (line-primary-location (line)
906 (declare (fixnum line))
907 (compute-primary-cache-location-from-location
908 (cache) (line-location line))))
909 (declare (ignorable #'cache #'nkeys #'line-size #'vector #'valuep
910 #'nlines #'max-location #'limit-fn #'size
911 #'mask #'field #'overflow #'line-reserved-p
912 #'location-reserved-p #'line-location
913 #'location-line #'line-wrappers #'location-wrappers
914 #'line-matches-wrappers-p
915 #'location-matches-wrappers-p
916 #'line-value #'location-value #'line-full-p
917 #'line-valid-p #'location-valid-p
918 #'line-separation #'next-line #'next-location
919 #'line-primary #'line-primary-location))
922 ;;; Here is where we actually fill, recache and expand caches.
924 ;;; The functions FILL-CACHE and PROBE-CACHE are the ONLY external
925 ;;; entrypoints into this code.
927 ;;; FILL-CACHE returns 1 value: a new cache
929 ;;; a wrapper field number
932 ;;; an absolute cache size (the size of the actual vector)
933 ;;; It tries to re-adjust the cache every time it makes a new fill.
934 ;;; The intuition here is that we want uniformity in the number of
935 ;;; probes needed to find an entry. Furthermore, adjusting has the
936 ;;; nice property of throwing out any entries that are invalid.
937 (defvar *cache-expand-threshold* 1.25)
939 (defun fill-cache (cache wrappers value)
940 ;; FILL-CACHE won't return if WRAPPERS is nil, might as well check..
943 (or (fill-cache-p nil cache wrappers value)
944 (and (< (ceiling (* (cache-count cache) *cache-expand-threshold*))
945 (if (= (cache-nkeys cache) 1)
946 (1- (cache-nlines cache))
947 (cache-nlines cache)))
948 (adjust-cache cache wrappers value))
949 (expand-cache cache wrappers value)))
951 (defvar *check-cache-p* nil)
953 (defmacro maybe-check-cache (cache)
955 (when *check-cache-p*
956 (check-cache ,cache))
959 (defun check-cache (cache)
960 (with-local-cache-functions (cache)
961 (let ((location (if (= (nkeys) 1) 0 1))
962 (limit (funcall (limit-fn) (nlines))))
963 (dotimes-fixnum (i (nlines) cache)
964 (when (and (not (location-reserved-p location))
966 (let* ((home-loc (compute-primary-cache-location-from-location
968 (home (location-line (if (location-reserved-p home-loc)
969 (next-location home-loc)
971 (sep (when home (line-separation home i))))
972 (when (and sep (> sep limit))
973 (error "bad cache ~S ~@
974 value at location ~W: ~W lines from its home. The limit is ~W."
975 cache location sep limit))))
976 (setq location (next-location location))))))
978 (defun probe-cache (cache wrappers &optional default limit-fn)
979 ;;(declare (values value))
981 ;; FIXME: This and another earlier test on a WRAPPERS arg can
982 ;; be compact assertoids.
983 (error "WRAPPERS arg is NIL!"))
984 (with-local-cache-functions (cache)
985 (let* ((location (compute-primary-cache-location (field) (mask) wrappers))
986 (limit (funcall (or limit-fn (limit-fn)) (nlines))))
987 (declare (fixnum location limit))
988 (when (location-reserved-p location)
989 (setq location (next-location location)))
990 (dotimes-fixnum (i (1+ limit))
991 (when (location-matches-wrappers-p location wrappers)
992 (return-from probe-cache (or (not (valuep))
993 (location-value location))))
994 (setq location (next-location location)))
995 (dolist (entry (overflow))
996 (when (equal (car entry) wrappers)
997 (return-from probe-cache (or (not (valuep))
1001 (defun map-cache (function cache &optional set-p)
1002 (with-local-cache-functions (cache)
1003 (let ((set-p (and set-p (valuep))))
1004 (dotimes-fixnum (i (nlines) cache)
1005 (unless (or (line-reserved-p i) (not (line-valid-p i nil)))
1006 (let ((value (funcall function (line-wrappers i) (line-value i))))
1008 (setf (cache-vector-ref (vector) (+ (line-location i) (nkeys)))
1010 (dolist (entry (overflow))
1011 (let ((value (funcall function (car entry) (cdr entry))))
1013 (setf (cdr entry) value))))))
1016 (defun cache-count (cache)
1017 (with-local-cache-functions (cache)
1019 (declare (fixnum count))
1020 (dotimes-fixnum (i (nlines) count)
1021 (unless (line-reserved-p i)
1022 (when (line-full-p i)
1025 (defun entry-in-cache-p (cache wrappers value)
1026 (declare (ignore value))
1027 (with-local-cache-functions (cache)
1028 (dotimes-fixnum (i (nlines))
1029 (unless (line-reserved-p i)
1030 (when (equal (line-wrappers i) wrappers)
1033 ;;; returns T or NIL
1034 (defun fill-cache-p (forcep cache wrappers value)
1035 (with-local-cache-functions (cache)
1036 (let* ((location (compute-primary-cache-location (field) (mask) wrappers))
1037 (primary (location-line location)))
1038 (declare (fixnum location primary))
1039 (multiple-value-bind (free emptyp)
1040 (find-free-cache-line primary cache wrappers)
1041 (when (or forcep emptyp)
1043 (push (cons (line-wrappers free) (line-value free))
1044 (cache-overflow cache)))
1045 ;;(fill-line free wrappers value)
1047 (declare (fixnum line))
1048 (when (line-reserved-p line)
1049 (error "attempt to fill a reserved line"))
1050 (let ((loc (line-location line))
1051 (cache-vector (vector)))
1052 (declare (fixnum loc) (simple-vector cache-vector))
1053 (cond ((= (nkeys) 1)
1054 (setf (cache-vector-ref cache-vector loc) wrappers)
1056 (setf (cache-vector-ref cache-vector (1+ loc)) value)))
1059 (declare (fixnum i))
1060 (dolist (w wrappers)
1061 (setf (cache-vector-ref cache-vector (+ loc i)) w)
1062 (setq i (the fixnum (1+ i)))))
1064 (setf (cache-vector-ref cache-vector (+ loc (nkeys)))
1066 (maybe-check-cache cache))))))))
1068 (defun fill-cache-from-cache-p (forcep cache from-cache from-line)
1069 (declare (fixnum from-line))
1070 (with-local-cache-functions (cache)
1071 (let ((primary (location-line
1072 (compute-primary-cache-location-from-location
1073 cache (line-location from-line) from-cache))))
1074 (declare (fixnum primary))
1075 (multiple-value-bind (free emptyp)
1076 (find-free-cache-line primary cache)
1077 (when (or forcep emptyp)
1079 (push (cons (line-wrappers free) (line-value free))
1080 (cache-overflow cache)))
1081 ;;(transfer-line from-cache-vector from-line cache-vector free)
1082 (let ((from-cache-vector (cache-vector from-cache))
1083 (to-cache-vector (vector))
1085 (declare (fixnum to-line))
1086 (if (line-reserved-p to-line)
1087 (error "transferring something into a reserved cache line")
1088 (let ((from-loc (line-location from-line))
1089 (to-loc (line-location to-line)))
1090 (declare (fixnum from-loc to-loc))
1091 (modify-cache to-cache-vector
1092 (dotimes-fixnum (i (line-size))
1093 (setf (cache-vector-ref to-cache-vector
1095 (cache-vector-ref from-cache-vector
1096 (+ from-loc i)))))))
1097 (maybe-check-cache cache)))))))
1099 ;;; Returns NIL or (values <field> <cache-vector>)
1101 ;;; This is only called when it isn't possible to put the entry in the
1102 ;;; cache the easy way. That is, this function assumes that
1103 ;;; FILL-CACHE-P has been called as returned NIL.
1105 ;;; If this returns NIL, it means that it wasn't possible to find a
1106 ;;; wrapper field for which all of the entries could be put in the
1107 ;;; cache (within the limit).
1108 (defun adjust-cache (cache wrappers value)
1109 (with-local-cache-functions (cache)
1110 (let ((ncache (get-cache-from-cache cache (nlines) (field))))
1111 (do ((nfield (cache-field ncache)
1112 (next-wrapper-cache-number-index nfield)))
1114 (setf (cache-field ncache) nfield)
1115 (labels ((try-one-fill-from-line (line)
1116 (fill-cache-from-cache-p nil ncache cache line))
1117 (try-one-fill (wrappers value)
1118 (fill-cache-p nil ncache wrappers value)))
1119 (if (and (dotimes-fixnum (i (nlines) t)
1120 (when (and (null (line-reserved-p i))
1121 (line-valid-p i wrappers))
1122 (unless (try-one-fill-from-line i) (return nil))))
1123 (dolist (wrappers+value (cache-overflow cache) t)
1124 (unless (try-one-fill (car wrappers+value) (cdr wrappers+value))
1126 (try-one-fill wrappers value))
1127 (return (maybe-check-cache ncache))
1128 (flush-cache-vector-internal (cache-vector ncache))))))))
1130 ;;; returns: (values <cache>)
1131 (defun expand-cache (cache wrappers value)
1132 ;;(declare (values cache))
1133 (with-local-cache-functions (cache)
1134 (let ((ncache (get-cache-from-cache cache (* (nlines) 2))))
1135 (labels ((do-one-fill-from-line (line)
1136 (unless (fill-cache-from-cache-p nil ncache cache line)
1137 (do-one-fill (line-wrappers line) (line-value line))))
1138 (do-one-fill (wrappers value)
1139 (setq ncache (or (adjust-cache ncache wrappers value)
1140 (fill-cache-p t ncache wrappers value))))
1141 (try-one-fill (wrappers value)
1142 (fill-cache-p nil ncache wrappers value)))
1143 (dotimes-fixnum (i (nlines))
1144 (when (and (null (line-reserved-p i))
1145 (line-valid-p i wrappers))
1146 (do-one-fill-from-line i)))
1147 (dolist (wrappers+value (cache-overflow cache))
1148 (unless (try-one-fill (car wrappers+value) (cdr wrappers+value))
1149 (do-one-fill (car wrappers+value) (cdr wrappers+value))))
1150 (unless (try-one-fill wrappers value)
1151 (do-one-fill wrappers value))
1152 (maybe-check-cache ncache)))))
1154 ;;; This is the heart of the cache filling mechanism. It implements
1155 ;;; the decisions about where entries are placed.
1157 ;;; Find a line in the cache at which a new entry can be inserted.
1160 ;;; <empty?> is <line> in fact empty?
1161 (defun find-free-cache-line (primary cache &optional wrappers)
1162 ;;(declare (values line empty?))
1163 (declare (fixnum primary))
1164 (with-local-cache-functions (cache)
1165 (when (line-reserved-p primary) (setq primary (next-line primary)))
1166 (let ((limit (funcall (limit-fn) (nlines)))
1169 (p primary) (s primary))
1170 (declare (fixnum p s limit))
1173 ;; Try to find a free line starting at <s>. <p> is the
1174 ;; primary line of the entry we are finding a free
1175 ;; line for, it is used to compute the separations.
1176 (do* ((line s (next-line line))
1177 (nsep (line-separation p s) (1+ nsep)))
1179 (declare (fixnum line nsep))
1180 (when (null (line-valid-p line wrappers)) ;If this line is empty or
1181 (push line lines) ;invalid, just use it.
1182 (return-from find-free))
1183 (when (and wrappedp (>= line primary))
1184 ;; have gone all the way around the cache, time to quit
1185 (return-from find-free-cache-line (values primary nil)))
1186 (let ((osep (line-separation (line-primary line) line)))
1187 (when (>= osep limit)
1188 (return-from find-free-cache-line (values primary nil)))
1189 (when (cond ((= nsep limit) t)
1190 ((= nsep osep) (zerop (random 2)))
1193 ;; See whether we can displace what is in this line so that we
1194 ;; can use the line.
1195 (when (= line (the fixnum (1- (nlines)))) (setq wrappedp t))
1196 (setq p (line-primary line))
1197 (setq s (next-line line))
1200 (when (= line (the fixnum (1- (nlines)))) (setq wrappedp t)))))
1201 ;; Do all the displacing.
1203 (when (null (cdr lines)) (return nil))
1204 (let ((dline (pop lines))
1206 (declare (fixnum dline line))
1207 ;;Copy from line to dline (dline is known to be free).
1208 (let ((from-loc (line-location line))
1209 (to-loc (line-location dline))
1210 (cache-vector (vector)))
1211 (declare (fixnum from-loc to-loc) (simple-vector cache-vector))
1212 (modify-cache cache-vector
1213 (dotimes-fixnum (i (line-size))
1214 (setf (cache-vector-ref cache-vector
1216 (cache-vector-ref cache-vector
1218 (setf (cache-vector-ref cache-vector
1221 (values (car lines) t))))
1223 (defun default-limit-fn (nlines)
1229 (defvar *empty-cache* (make-cache)) ; for defstruct slot initial value forms