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)
99 (sb-sys:without-interrupts
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)
105 `(sb-sys:without-interrupts
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 #.sb-kernel: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 ;;; some facilities for allocation and freeing caches as they are needed
148 ;;; This is done on the assumption that a better port of PCL will
149 ;;; arrange to cons these all in the same static area. Given that, the
150 ;;; fact that PCL tries to reuse them should be a win.
152 (defvar *free-cache-vectors* (make-hash-table :size 16 :test 'eql))
154 ;;; Return a cache that has had FLUSH-CACHE-VECTOR-INTERNAL called on
155 ;;; it. This returns a cache of exactly the size requested, it won't
156 ;;; ever return a larger cache.
157 (defun get-cache-vector (size)
158 (let ((entry (gethash size *free-cache-vectors*)))
159 (sb-sys:without-interrupts
161 (setf (gethash size *free-cache-vectors*) (cons 0 nil))
162 (get-cache-vector size))
165 (flush-cache-vector-internal (allocate-cache-vector size)))
167 (let ((cache (cdr entry)))
168 (setf (cdr entry) (cache-vector-ref cache 0))
169 (flush-cache-vector-internal cache)))))))
171 (defun free-cache-vector (cache-vector)
172 (let ((entry (gethash (cache-vector-size cache-vector) *free-cache-vectors*)))
173 (sb-sys:without-interrupts
176 "attempt to free a cache-vector not allocated by GET-CACHE-VECTOR")
177 (let ((thread (cdr entry)))
178 (loop (unless thread (return))
179 (when (eq thread cache-vector)
180 (error "freeing a cache twice"))
181 (setq thread (cache-vector-ref thread 0)))
182 (flush-cache-vector-internal cache-vector) ; to help the GC
183 (setf (cache-vector-ref cache-vector 0) (cdr entry))
184 (setf (cdr entry) cache-vector)
187 ;;; This is just for debugging and analysis. It shows the state of the
188 ;;; free cache resource.
190 (defun show-free-cache-vectors ()
192 (maphash #'(lambda (s e) (push (list s e) elements)) *free-cache-vectors*)
193 (setq elements (sort elements #'< :key #'car))
195 (let* ((size (car e))
197 (allocated (car entry))
200 (loop (when (null head) (return t))
201 (setq head (cache-vector-ref head 0))
204 "~&There ~4D are caches of size ~4D. (~D free ~3D%)"
208 (floor (* 100 (/ free (float allocated)))))))))
210 ;;;; wrapper cache numbers
212 ;;; The constant WRAPPER-CACHE-NUMBER-ADDS-OK controls the number of
213 ;;; non-zero bits wrapper cache numbers will have.
215 ;;; The value of this constant is the number of wrapper cache numbers
216 ;;; which can be added and still be certain the result will be a
217 ;;; fixnum. This is used by all the code that computes primary cache
218 ;;; locations from multiple wrappers.
220 ;;; The value of this constant is used to derive the next two which
221 ;;; are the forms of this constant which it is more convenient for the
222 ;;; runtime code to use.
223 (defconstant wrapper-cache-number-length
224 (integer-length sb-kernel:layout-clos-hash-max))
225 (defconstant wrapper-cache-number-mask sb-kernel:layout-clos-hash-max)
226 (defconstant wrapper-cache-number-adds-ok
227 (truncate most-positive-fixnum sb-kernel:layout-clos-hash-max))
229 ;;;; wrappers themselves
231 ;;; This caching algorithm requires that wrappers have more than one
232 ;;; wrapper cache number. You should think of these multiple numbers
233 ;;; as being in columns. That is, for a given cache, the same column
234 ;;; of wrapper cache numbers will be used.
236 ;;; If at some point the cache distribution of a cache gets bad, the
237 ;;; cache can be rehashed by switching to a different column.
239 ;;; The columns are referred to by field number which is that number
240 ;;; which, when used as a second argument to wrapper-ref, will return
241 ;;; that column of wrapper cache number.
243 ;;; This code is written to allow flexibility as to how many wrapper
244 ;;; cache numbers will be in each wrapper, and where they will be
245 ;;; located. It is also set up to allow port specific modifications to
246 ;;; `pack' the wrapper cache numbers on machines where the addressing
247 ;;; modes make that a good idea.
249 ;;; In SBCL, as in CMU CL, we want to do type checking as early as
250 ;;; possible; structures help this. The structures are hard-wired to
251 ;;; have a fixed number of cache hash values, and that number must
252 ;;; correspond to the number of cache lines we use.
253 (defconstant wrapper-cache-number-vector-length
254 sb-kernel:layout-clos-hash-length)
256 (unless (boundp '*the-class-t*)
257 (setq *the-class-t* nil))
259 ;;; Note that for SBCL, as for CMU CL, the WRAPPER of a built-in or
260 ;;; structure class will be some other kind of SB-KERNEL:LAYOUT, but
261 ;;; this shouldn't matter, since the only two slots that WRAPPER adds
262 ;;; are meaningless in those cases.
264 (:include sb-kernel:layout
265 ;; KLUDGE: In CMU CL, the initialization default
266 ;; for LAYOUT-INVALID was NIL. In SBCL, that has
267 ;; changed to :UNINITIALIZED, but PCL code might
268 ;; still expect NIL for the initialization
269 ;; default of WRAPPER-INVALID. Instead of trying
270 ;; to find out, I just overrode the LAYOUT
271 ;; default here. -- WHN 19991204
273 (:conc-name %wrapper-)
274 (:constructor make-wrapper-internal))
275 (instance-slots-layout nil :type list)
276 (class-slots nil :type list))
277 #-sb-fluid (declaim (sb-ext:freeze-type wrapper))
279 (defmacro wrapper-class (wrapper)
280 `(sb-kernel:class-pcl-class (sb-kernel:layout-class ,wrapper)))
281 (defmacro wrapper-no-of-instance-slots (wrapper)
282 `(sb-kernel:layout-length ,wrapper))
284 ;;; WRAPPER-STATE returns T (not generalized boolean, but T exactly)
285 ;;; iff the wrapper is valid. Any other return value denotes some
286 ;;; invalid state. Special conventions have been set up for certain
287 ;;; invalid states, e.g. obsoleteness or flushedness, but I (WHN
288 ;;; 19991204) haven't been motivated to reverse engineer them from the
289 ;;; code and document them here.
291 ;;; FIXME: This is awkward and unmnemonic. There is a function
292 ;;; (INVALID-WRAPPER-P) to test this return result abstractly for
293 ;;; invalidness but it's not called consistently; the functions that
294 ;;; need to know whether a wrapper is invalid often test (EQ
295 ;;; (WRAPPER-STATE X) T), ick. It would be good to use the abstract
296 ;;; test instead. It would probably be even better to switch the sense
297 ;;; of the WRAPPER-STATE function, renaming it to WRAPPER-INVALID and
298 ;;; making it synonymous with LAYOUT-INVALID. Then the
299 ;;; INVALID-WRAPPER-P function would become trivial and would go away
300 ;;; (replaced with WRAPPER-INVALID), since all the various invalid
301 ;;; wrapper states would become generalized boolean "true" values. --
303 #-sb-fluid (declaim (inline wrapper-state (setf wrapper-state)))
304 (defun wrapper-state (wrapper)
305 (let ((invalid (sb-kernel:layout-invalid wrapper)))
306 (cond ((null invalid)
309 ;; some non-PCL object. INVALID is probably :INVALID. We
310 ;; should arguably compute the new wrapper here instead of
311 ;; returning NIL, but we don't bother, since
312 ;; OBSOLETE-INSTANCE-TRAP can't use it.
316 (defun (setf wrapper-state) (new-value wrapper)
317 (setf (sb-kernel:layout-invalid wrapper)
322 (defmacro wrapper-instance-slots-layout (wrapper)
323 `(%wrapper-instance-slots-layout ,wrapper))
324 (defmacro wrapper-class-slots (wrapper)
325 `(%wrapper-class-slots ,wrapper))
326 (defmacro wrapper-cache-number-vector (x) x)
328 ;;; This is called in BRAID when we are making wrappers for classes
329 ;;; whose slots are not initialized yet, and which may be built-in
330 ;;; classes. We pass in the class name in addition to the class.
331 (defun boot-make-wrapper (length name &optional class)
332 (let ((found (cl:find-class name nil)))
335 (unless (sb-kernel:class-pcl-class found)
336 (setf (sb-kernel:class-pcl-class found) class))
337 (assert (eq (sb-kernel:class-pcl-class found) class))
338 (let ((layout (sb-kernel:class-layout found)))
342 (make-wrapper-internal
344 :class (sb-kernel:make-standard-class :name name :pcl-class class))))))
346 ;;; The following variable may be set to a standard-class that has
347 ;;; already been created by the lisp code and which is to be redefined
348 ;;; by PCL. This allows standard-classes to be defined and used for
349 ;;; type testing and dispatch before PCL is loaded.
350 (defvar *pcl-class-boot* nil)
352 ;;; In SBCL, as in CMU CL, the layouts (a.k.a wrappers) for built-in
353 ;;; and structure classes already exist when PCL is initialized, so we
354 ;;; don't necessarily always make a wrapper. Also, we help maintain
355 ;;; the mapping between cl:class and pcl::class objects.
356 (defun make-wrapper (length class)
358 ((typep class 'std-class)
359 (make-wrapper-internal
362 (let ((owrap (class-wrapper class)))
364 (sb-kernel:layout-class owrap))
365 ((*subtypep (class-of class)
366 *the-class-standard-class*)
367 (cond ((and *pcl-class-boot*
368 (eq (slot-value class 'name) *pcl-class-boot*))
369 (let ((found (cl:find-class (slot-value class 'name))))
370 (unless (sb-kernel:class-pcl-class found)
371 (setf (sb-kernel:class-pcl-class found) class))
372 (assert (eq (sb-kernel:class-pcl-class found) class))
375 (sb-kernel:make-standard-class :pcl-class class))))
377 (sb-kernel:make-random-pcl-class :pcl-class class))))))
379 (let* ((found (cl:find-class (slot-value class 'name)))
380 (layout (sb-kernel:class-layout found)))
381 (unless (sb-kernel:class-pcl-class found)
382 (setf (sb-kernel:class-pcl-class found) class))
383 (assert (eq (sb-kernel:class-pcl-class found) class))
387 ;;; FIXME: The immediately following macros could become inline functions.
389 (defmacro first-wrapper-cache-number-index ()
392 (defmacro next-wrapper-cache-number-index (field-number)
393 `(and (< ,field-number #.(1- wrapper-cache-number-vector-length))
396 (defmacro cache-number-vector-ref (cnv n)
397 `(wrapper-cache-number-vector-ref ,cnv ,n))
399 (defmacro wrapper-cache-number-vector-ref (wrapper n)
400 `(sb-kernel:layout-clos-hash ,wrapper ,n))
402 (defmacro class-no-of-instance-slots (class)
403 `(wrapper-no-of-instance-slots (class-wrapper ,class)))
405 (defmacro wrapper-class* (wrapper)
406 `(let ((wrapper ,wrapper))
407 (or (wrapper-class wrapper)
408 (find-structure-class
409 (cl:class-name (sb-kernel:layout-class wrapper))))))
411 ;;; The wrapper cache machinery provides general mechanism for
412 ;;; trapping on the next access to any instance of a given class. This
413 ;;; mechanism is used to implement the updating of instances when the
414 ;;; class is redefined (MAKE-INSTANCES-OBSOLETE). The same mechanism
415 ;;; is also used to update generic function caches when there is a
416 ;;; change to the superclasses of a class.
418 ;;; Basically, a given wrapper can be valid or invalid. If it is
419 ;;; invalid, it means that any attempt to do a wrapper cache lookup
420 ;;; using the wrapper should trap. Also, methods on
421 ;;; SLOT-VALUE-USING-CLASS check the wrapper validity as well. This is
422 ;;; done by calling CHECK-WRAPPER-VALIDITY.
424 ;;; FIXME: could become inline function
425 (defmacro invalid-wrapper-p (wrapper)
426 `(neq (wrapper-state ,wrapper) t))
428 (defvar *previous-nwrappers* (make-hash-table))
430 (defun invalidate-wrapper (owrapper state nwrapper)
433 (let ((new-previous ()))
434 ;; First off, a previous call to INVALIDATE-WRAPPER may have
435 ;; recorded OWRAPPER as an NWRAPPER to update to. Since
436 ;; OWRAPPER is about to be invalid, it no longer makes sense to
439 ;; We go back and change the previously invalidated wrappers so
440 ;; that they will now update directly to NWRAPPER. This
441 ;; corresponds to a kind of transitivity of wrapper updates.
442 (dolist (previous (gethash owrapper *previous-nwrappers*))
443 (when (eq state ':obsolete)
444 (setf (car previous) ':obsolete))
445 (setf (cadr previous) nwrapper)
446 (push previous new-previous))
448 (let ((ocnv (wrapper-cache-number-vector owrapper)))
449 (dotimes (i sb-kernel:layout-clos-hash-length)
450 (setf (cache-number-vector-ref ocnv i) 0)))
451 (push (setf (wrapper-state owrapper) (list state nwrapper))
454 (setf (gethash owrapper *previous-nwrappers*) ()
455 (gethash nwrapper *previous-nwrappers*) new-previous)))))
457 (defun check-wrapper-validity (instance)
458 (let* ((owrapper (wrapper-of instance))
459 (state (wrapper-state owrapper)))
465 (flush-cache-trap owrapper (cadr state) instance))
467 (obsolete-instance-trap owrapper (cadr state) instance)))))
468 ;; This little bit of error checking is superfluous. It only
469 ;; checks to see whether the person who implemented the trap
470 ;; handling screwed up. Since that person is hacking
471 ;; internal PCL code, and is not a user, this should be
472 ;; needless. Also, since this directly slows down instance
473 ;; update and generic function cache refilling, feel free to
474 ;; take it out sometime soon.
476 ;; FIXME: We probably need to add a #+SB-PARANOID feature to
477 ;; make stuff like this optional. Until then, it stays in.
478 (cond ((neq nwrapper (wrapper-of instance))
479 (error "wrapper returned from trap not wrapper of instance"))
480 ((invalid-wrapper-p nwrapper)
481 (error "wrapper returned from trap invalid")))
484 (defmacro check-wrapper-validity1 (object)
485 (let ((owrapper (gensym)))
486 `(let ((,owrapper (sb-kernel:layout-of object)))
487 (if (sb-kernel:layout-invalid ,owrapper)
488 (check-wrapper-validity ,object)
491 (defvar *free-caches* nil)
493 (defun get-cache (nkeys valuep limit-fn nlines)
494 (let ((cache (or (sb-sys:without-interrupts (pop *free-caches*))
496 (declare (type cache cache))
497 (multiple-value-bind (cache-mask actual-size line-size nlines)
498 (compute-cache-parameters nkeys valuep nlines)
499 (setf (cache-nkeys cache) nkeys
500 (cache-valuep cache) valuep
501 (cache-nlines cache) nlines
502 (cache-field cache) (first-wrapper-cache-number-index)
503 (cache-limit-fn cache) limit-fn
504 (cache-mask cache) cache-mask
505 (cache-size cache) actual-size
506 (cache-line-size cache) line-size
507 (cache-max-location cache) (let ((line (1- nlines)))
510 (1+ (* line line-size))))
511 (cache-vector cache) (get-cache-vector actual-size)
512 (cache-overflow cache) nil)
515 (defun get-cache-from-cache (old-cache new-nlines
516 &optional (new-field (first-wrapper-cache-number-index)))
517 (let ((nkeys (cache-nkeys old-cache))
518 (valuep (cache-valuep old-cache))
519 (cache (or (sb-sys:without-interrupts (pop *free-caches*))
521 (declare (type cache cache))
522 (multiple-value-bind (cache-mask actual-size line-size nlines)
523 (if (= new-nlines (cache-nlines old-cache))
524 (values (cache-mask old-cache) (cache-size old-cache)
525 (cache-line-size old-cache) (cache-nlines old-cache))
526 (compute-cache-parameters nkeys valuep new-nlines))
527 (setf (cache-owner cache) (cache-owner old-cache)
528 (cache-nkeys cache) nkeys
529 (cache-valuep cache) valuep
530 (cache-nlines cache) nlines
531 (cache-field cache) new-field
532 (cache-limit-fn cache) (cache-limit-fn old-cache)
533 (cache-mask cache) cache-mask
534 (cache-size cache) actual-size
535 (cache-line-size cache) line-size
536 (cache-max-location cache) (let ((line (1- nlines)))
539 (1+ (* line line-size))))
540 (cache-vector cache) (get-cache-vector actual-size)
541 (cache-overflow cache) nil)
544 (defun copy-cache (old-cache)
545 (let* ((new-cache (copy-cache-internal old-cache))
546 (size (cache-size old-cache))
547 (old-vector (cache-vector old-cache))
548 (new-vector (get-cache-vector size)))
549 (declare (simple-vector old-vector new-vector))
550 (dotimes-fixnum (i size)
551 (setf (svref new-vector i) (svref old-vector i)))
552 (setf (cache-vector new-cache) new-vector)
555 (defun free-cache (cache)
556 (free-cache-vector (cache-vector cache))
557 (setf (cache-vector cache) #())
558 (setf (cache-owner cache) nil)
559 (push cache *free-caches*)
562 (defun compute-line-size (x)
563 (power-of-two-ceiling x))
565 (defun compute-cache-parameters (nkeys valuep nlines-or-cache-vector)
566 ;;(declare (values cache-mask actual-size line-size nlines))
567 (declare (fixnum nkeys))
569 (let* ((line-size (if valuep 2 1))
570 (cache-size (if (typep nlines-or-cache-vector 'fixnum)
574 (power-of-two-ceiling
575 nlines-or-cache-vector))))
576 (cache-vector-size nlines-or-cache-vector))))
577 (declare (fixnum line-size cache-size))
578 (values (logxor (the fixnum (1- cache-size)) (the fixnum (1- line-size)))
581 (the fixnum (floor cache-size line-size))))
582 (let* ((line-size (power-of-two-ceiling (if valuep (1+ nkeys) nkeys)))
583 (cache-size (if (typep nlines-or-cache-vector 'fixnum)
587 (power-of-two-ceiling
588 nlines-or-cache-vector))))
589 (1- (cache-vector-size nlines-or-cache-vector)))))
590 (declare (fixnum line-size cache-size))
591 (values (logxor (the fixnum (1- cache-size)) (the fixnum (1- line-size)))
592 (the fixnum (1+ cache-size))
594 (the fixnum (floor cache-size line-size))))))
596 ;;; the various implementations of computing a primary cache location from
597 ;;; wrappers. Because some implementations of this must run fast there are
598 ;;; several implementations of the same algorithm.
600 ;;; The algorithm is:
602 ;;; SUM over the wrapper cache numbers,
603 ;;; ENSURING that the result is a fixnum
604 ;;; MASK the result against the mask argument.
606 ;;; COMPUTE-PRIMARY-CACHE-LOCATION
608 ;;; The basic functional version. This is used by the cache miss code to
609 ;;; compute the primary location of an entry.
610 (defun compute-primary-cache-location (field mask wrappers)
612 (declare (type field-type field) (fixnum mask))
613 (if (not (listp wrappers))
615 (the fixnum (wrapper-cache-number-vector-ref wrappers field)))
616 (let ((location 0) (i 0))
617 (declare (fixnum location i))
618 (dolist (wrapper wrappers)
619 ;; First add the cache number of this wrapper to location.
620 (let ((wrapper-cache-number (wrapper-cache-number-vector-ref wrapper
622 (declare (fixnum wrapper-cache-number))
623 (if (zerop wrapper-cache-number)
624 (return-from compute-primary-cache-location 0)
626 (the fixnum (+ location wrapper-cache-number)))))
627 ;; Then, if we are working with lots of wrappers, deal with
628 ;; the wrapper-cache-number-mask stuff.
629 (when (and (not (zerop i))
630 (zerop (mod i wrapper-cache-number-adds-ok)))
632 (logand location wrapper-cache-number-mask)))
634 (the fixnum (1+ (logand mask location))))))
636 ;;; COMPUTE-PRIMARY-CACHE-LOCATION-FROM-LOCATION
638 ;;; This version is called on a cache line. It fetches the wrappers
639 ;;; from the cache line and determines the primary location. Various
640 ;;; parts of the cache filling code call this to determine whether it
641 ;;; is appropriate to displace a given cache entry.
643 ;;; If this comes across a wrapper whose CACHE-NO is 0, it returns the
644 ;;; symbol invalid to suggest to its caller that it would be provident
645 ;;; to blow away the cache line in question.
646 (defun compute-primary-cache-location-from-location (to-cache
649 (from-cache to-cache))
650 (declare (type cache to-cache from-cache) (fixnum from-location))
652 (cache-vector (cache-vector from-cache))
653 (field (cache-field to-cache))
654 (mask (cache-mask to-cache))
655 (nkeys (cache-nkeys to-cache)))
656 (declare (type field-type field) (fixnum result mask nkeys)
657 (simple-vector cache-vector))
658 (dotimes-fixnum (i nkeys)
659 (let* ((wrapper (cache-vector-ref cache-vector (+ i from-location)))
660 (wcn (wrapper-cache-number-vector-ref wrapper field)))
661 (declare (fixnum wcn))
662 (setq result (+ result wcn)))
663 (when (and (not (zerop i))
664 (zerop (mod i wrapper-cache-number-adds-ok)))
665 (setq result (logand result wrapper-cache-number-mask))))
668 (the fixnum (1+ (logand mask result))))))
670 ;;; NIL means nothing so far, no actual arg info has NILs
672 ;;; CLASS seen all sorts of metaclasses
673 ;;; (specifically, more than one of the next 4 values)
674 ;;; T means everything so far is the class T
675 ;;; STANDARD-CLASS seen only standard classes
676 ;;; BUILT-IN-CLASS seen only built in classes
677 ;;; STRUCTURE-CLASS seen only structure classes
678 (defun raise-metatype (metatype new-specializer)
679 (let ((slot (find-class 'slot-class))
680 (std (find-class 'std-class))
681 (standard (find-class 'standard-class))
682 (fsc (find-class 'funcallable-standard-class))
683 (structure (find-class 'structure-class))
684 (built-in (find-class 'built-in-class)))
685 (flet ((specializer->metatype (x)
686 (let ((meta-specializer
687 (if (eq *boot-state* 'complete)
688 (class-of (specializer-class x))
690 (cond ((eq x *the-class-t*) t)
691 ((*subtypep meta-specializer std)
693 ((*subtypep meta-specializer standard)
695 ((*subtypep meta-specializer fsc)
697 ((*subtypep meta-specializer structure)
699 ((*subtypep meta-specializer built-in)
701 ((*subtypep meta-specializer slot)
703 (t (error "PCL cannot handle the specializer ~S (meta-specializer ~S)."
705 meta-specializer))))))
706 ;; We implement the following table. The notation is
707 ;; that X and Y are distinct meta specializer names.
709 ;; NIL <anything> ===> <anything>
712 (let ((new-metatype (specializer->metatype new-specializer)))
713 (cond ((eq new-metatype 'slot-instance) 'class)
714 ((null metatype) new-metatype)
715 ((eq metatype new-metatype) new-metatype)
718 (defmacro with-dfun-wrappers ((args metatypes)
719 (dfun-wrappers invalid-wrapper-p
720 &optional wrappers classes types)
721 invalid-arguments-form
723 `(let* ((args-tail ,args) (,invalid-wrapper-p nil) (invalid-arguments-p nil)
724 (,dfun-wrappers nil) (dfun-wrappers-tail nil)
726 `((wrappers-rev nil) (types-rev nil) (classes-rev nil))))
727 (dolist (mt ,metatypes)
729 (setq invalid-arguments-p t)
731 (let* ((arg (pop args-tail))
734 `((class *the-class-t*)
737 (setq wrapper (wrapper-of arg))
738 (when (invalid-wrapper-p wrapper)
739 (setq ,invalid-wrapper-p t)
740 (setq wrapper (check-wrapper-validity arg)))
741 (cond ((null ,dfun-wrappers)
742 (setq ,dfun-wrappers wrapper))
743 ((not (consp ,dfun-wrappers))
744 (setq dfun-wrappers-tail (list wrapper))
745 (setq ,dfun-wrappers (cons ,dfun-wrappers dfun-wrappers-tail)))
747 (let ((new-dfun-wrappers-tail (list wrapper)))
748 (setf (cdr dfun-wrappers-tail) new-dfun-wrappers-tail)
749 (setf dfun-wrappers-tail new-dfun-wrappers-tail))))
751 `((setq class (wrapper-class* wrapper))
752 (setq type `(class-eq ,class)))))
754 `((push wrapper wrappers-rev)
755 (push class classes-rev)
756 (push type types-rev)))))
757 (if invalid-arguments-p
758 ,invalid-arguments-form
759 (let* (,@(when wrappers
760 `((,wrappers (nreverse wrappers-rev))
761 (,classes (nreverse classes-rev))
762 (,types (mapcar #'(lambda (class)
767 ;;;; some support stuff for getting a hold of symbols that we need when
768 ;;;; building the discriminator codes. It's OK for these to be interned
769 ;;;; symbols because we don't capture any user code in the scope in which
770 ;;;; these symbols are bound.
772 (defvar *dfun-arg-symbols* '(.ARG0. .ARG1. .ARG2. .ARG3.))
774 (defun dfun-arg-symbol (arg-number)
775 (or (nth arg-number (the list *dfun-arg-symbols*))
776 (intern (format nil ".ARG~A." arg-number) *pcl-package*)))
778 (defvar *slot-vector-symbols* '(.SLOTS0. .SLOTS1. .SLOTS2. .SLOTS3.))
780 (defun slot-vector-symbol (arg-number)
781 (or (nth arg-number (the list *slot-vector-symbols*))
782 (intern (format nil ".SLOTS~A." arg-number) *pcl-package*)))
784 (defun make-dfun-lambda-list (metatypes applyp)
785 (gathering1 (collecting)
786 (iterate ((i (interval :from 0))
787 (s (list-elements metatypes)))
789 (gather1 (dfun-arg-symbol i)))
792 (gather1 '.dfun-rest-arg.))))
794 (defun make-dlap-lambda-list (metatypes applyp)
795 (gathering1 (collecting)
796 (iterate ((i (interval :from 0))
797 (s (list-elements metatypes)))
799 (gather1 (dfun-arg-symbol i)))
803 (defun make-emf-call (metatypes applyp fn-variable &optional emf-type)
805 (gathering1 (collecting)
806 (iterate ((i (interval :from 0))
807 (s (list-elements metatypes)))
809 (gather1 (dfun-arg-symbol i))))))
810 `(,(if (eq emf-type 'fast-method-call)
811 'invoke-effective-method-function-fast
812 'invoke-effective-method-function)
813 ,fn-variable ,applyp ,@required ,@(when applyp `(.dfun-rest-arg.)))))
815 (defun make-dfun-call (metatypes applyp fn-variable)
817 (gathering1 (collecting)
818 (iterate ((i (interval :from 0))
819 (s (list-elements metatypes)))
821 (gather1 (dfun-arg-symbol i))))))
823 `(function-apply ,fn-variable ,@required .dfun-rest-arg.)
824 `(function-funcall ,fn-variable ,@required))))
826 (defun make-dfun-arg-list (metatypes applyp)
828 (gathering1 (collecting)
829 (iterate ((i (interval :from 0))
830 (s (list-elements metatypes)))
832 (gather1 (dfun-arg-symbol i))))))
834 `(list* ,@required .dfun-rest-arg.)
835 `(list ,@required))))
837 (defun make-fast-method-call-lambda-list (metatypes applyp)
838 (gathering1 (collecting)
840 (gather1 '.next-method-call.)
841 (iterate ((i (interval :from 0))
842 (s (list-elements metatypes)))
844 (gather1 (dfun-arg-symbol i)))
846 (gather1 '.dfun-rest-arg.))))
848 ;;;; a comment from some PCL implementor:
849 ;;;; Its too bad Common Lisp compilers freak out when you have a
850 ;;;; DEFUN with a lot of LABELS in it. If I could do that I could
851 ;;;; make this code much easier to read and work with.
853 ;;;; In the absence of that, the following little macro makes the
854 ;;;; code that follows a little bit more reasonable. I would like to
855 ;;;; add that having to practically write my own compiler in order to
856 ;;;; get just this simple thing is something of a drag.
858 ;;;; KLUDGE: Maybe we could actually implement this as LABELS now,
859 ;;;; since AFAIK CMU CL doesn't freak out when you have a DEFUN with a
860 ;;;; lot of LABELS in it (and if it does we can fix it instead of
861 ;;;; working around it). -- WHN 19991204
863 (eval-when (:compile-toplevel :load-toplevel :execute)
867 ;;; FIXME: should be undefined after bootstrapping
868 (defparameter *local-cache-functions*
870 (nkeys () (cache-nkeys .cache.))
871 (line-size () (cache-line-size .cache.))
872 (vector () (cache-vector .cache.))
873 (valuep () (cache-valuep .cache.))
874 (nlines () (cache-nlines .cache.))
875 (max-location () (cache-max-location .cache.))
876 (limit-fn () (cache-limit-fn .cache.))
877 (size () (cache-size .cache.))
878 (mask () (cache-mask .cache.))
879 (field () (cache-field .cache.))
880 (overflow () (cache-overflow .cache.))
882 ;; Return T IFF this cache location is reserved. The only time
883 ;; this is true is for line number 0 of an nkeys=1 cache.
884 (line-reserved-p (line)
885 (declare (fixnum line))
888 (location-reserved-p (location)
889 (declare (fixnum location))
892 ;; Given a line number, return the cache location. This is the
893 ;; value that is the second argument to cache-vector-ref. Basically,
894 ;; this deals with the offset of nkeys>1 caches and multiplies
896 (line-location (line)
897 (declare (fixnum line))
898 (when (line-reserved-p line)
899 (error "Line is reserved."))
901 (the fixnum (* line (line-size)))
902 (the fixnum (1+ (the fixnum (* line (line-size)))))))
904 ;; Given a cache location, return the line. This is the inverse
906 (location-line (location)
907 (declare (fixnum location))
909 (floor location (line-size))
910 (floor (the fixnum (1- location)) (line-size))))
912 ;; Given a line number, return the wrappers stored at that line.
913 ;; As usual, if nkeys=1, this returns a single value. Only when
914 ;; nkeys>1 does it return a list. An error is signalled if the
916 (line-wrappers (line)
917 (declare (fixnum line))
918 (when (line-reserved-p line) (error "Line is reserved."))
919 (location-wrappers (line-location line)))
920 (location-wrappers (location) ; avoid multiplies caused by line-location
921 (declare (fixnum location))
923 (cache-vector-ref (vector) location)
924 (let ((list (make-list (nkeys)))
926 (declare (simple-vector vector))
927 (dotimes-fixnum (i (nkeys) list)
928 (setf (nth i list) (cache-vector-ref vector (+ location i)))))))
930 ;; Given a line number, return true IFF the line's
931 ;; wrappers are the same as wrappers.
932 (line-matches-wrappers-p (line wrappers)
933 (declare (fixnum line))
934 (and (not (line-reserved-p line))
935 (location-matches-wrappers-p (line-location line) wrappers)))
936 (location-matches-wrappers-p (loc wrappers) ; must not be reserved
937 (declare (fixnum loc))
938 (let ((cache-vector (vector)))
939 (declare (simple-vector cache-vector))
941 (eq wrappers (cache-vector-ref cache-vector loc))
942 (dotimes-fixnum (i (nkeys) t)
943 (unless (eq (pop wrappers)
944 (cache-vector-ref cache-vector (+ loc i)))
947 ;; Given a line number, return the value stored at that line.
948 ;; If valuep is NIL, this returns NIL. As with line-wrappers,
949 ;; an error is signalled if the line is reserved.
951 (declare (fixnum line))
952 (when (line-reserved-p line) (error "Line is reserved."))
953 (location-value (line-location line)))
954 (location-value (loc)
955 (declare (fixnum loc))
957 (cache-vector-ref (vector) (+ loc (nkeys)))))
959 ;; Given a line number, return true iff that line has data in
960 ;; it. The state of the wrappers stored in the line is not
961 ;; checked. An error is signalled if line is reserved.
963 (when (line-reserved-p line) (error "Line is reserved."))
964 (not (null (cache-vector-ref (vector) (line-location line)))))
966 ;; Given a line number, return true iff the line is full and
967 ;; there are no invalid wrappers in the line, and the line's
968 ;; wrappers are different from wrappers.
969 ;; An error is signalled if the line is reserved.
970 (line-valid-p (line wrappers)
971 (declare (fixnum line))
972 (when (line-reserved-p line) (error "Line is reserved."))
973 (location-valid-p (line-location line) wrappers))
974 (location-valid-p (loc wrappers)
975 (declare (fixnum loc))
976 (let ((cache-vector (vector))
977 (wrappers-mismatch-p (null wrappers)))
978 (declare (simple-vector cache-vector))
979 (dotimes-fixnum (i (nkeys) wrappers-mismatch-p)
980 (let ((wrapper (cache-vector-ref cache-vector (+ loc i))))
981 (when (or (null wrapper)
982 (invalid-wrapper-p wrapper))
984 (unless (and wrappers
986 (if (consp wrappers) (pop wrappers) wrappers)))
987 (setq wrappers-mismatch-p t))))))
989 ;; how many unreserved lines separate line-1 and line-2
990 (line-separation (line-1 line-2)
991 (declare (fixnum line-1 line-2))
992 (let ((diff (the fixnum (- line-2 line-1))))
993 (declare (fixnum diff))
995 (setq diff (+ diff (nlines)))
996 (when (line-reserved-p 0)
997 (setq diff (1- diff))))
1000 ;; Given a cache line, get the next cache line. This will not
1001 ;; return a reserved line.
1003 (declare (fixnum line))
1004 (if (= line (the fixnum (1- (nlines))))
1005 (if (line-reserved-p 0) 1 0)
1006 (the fixnum (1+ line))))
1007 (next-location (loc)
1008 (declare (fixnum loc))
1009 (if (= loc (max-location))
1013 (the fixnum (+ loc (line-size)))))
1015 ;; Given a line which has a valid entry in it, this will return
1016 ;; the primary cache line of the wrappers in that line. We just
1017 ;; call COMPUTE-PRIMARY-CACHE-LOCATION-FROM-LOCATION, this is an
1018 ;; easier packaging up of the call to it.
1019 (line-primary (line)
1020 (declare (fixnum line))
1021 (location-line (line-primary-location line)))
1022 (line-primary-location (line)
1023 (declare (fixnum line))
1024 (compute-primary-cache-location-from-location
1025 (cache) (line-location line)))))
1027 (defmacro with-local-cache-functions ((cache) &body body)
1028 `(let ((.cache. ,cache))
1029 (declare (type cache .cache.))
1030 (macrolet ,(mapcar #'(lambda (fn)
1031 `(,(car fn) ,(cadr fn)
1032 `(let (,,@(mapcar #'(lambda (var)
1036 *local-cache-functions*)
1041 ;;; Here is where we actually fill, recache and expand caches.
1043 ;;; The functions FILL-CACHE and PROBE-CACHE are the ONLY external
1044 ;;; entrypoints into this code.
1046 ;;; FILL-CACHE returns 1 value: a new cache
1048 ;;; a wrapper field number
1051 ;;; an absolute cache size (the size of the actual vector)
1052 ;;; It tries to re-adjust the cache every time it makes a new fill.
1053 ;;; The intuition here is that we want uniformity in the number of
1054 ;;; probes needed to find an entry. Furthermore, adjusting has the
1055 ;;; nice property of throwing out any entries that are invalid.
1056 (defvar *cache-expand-threshold* 1.25)
1058 (defun fill-cache (cache wrappers value &optional free-cache-p)
1060 ;; FILL-CACHE won't return if WRAPPERS is nil, might as well check..
1062 (error "fill-cache: WRAPPERS arg is NIL!"))
1064 (or (fill-cache-p nil cache wrappers value)
1065 (and (< (ceiling (* (cache-count cache) 1.25))
1066 (if (= (cache-nkeys cache) 1)
1067 (1- (cache-nlines cache))
1068 (cache-nlines cache)))
1069 (adjust-cache cache wrappers value free-cache-p))
1070 (expand-cache cache wrappers value free-cache-p)))
1072 (defvar *check-cache-p* nil)
1074 (defmacro maybe-check-cache (cache)
1076 (when *check-cache-p*
1077 (check-cache ,cache))
1080 (defun check-cache (cache)
1081 (with-local-cache-functions (cache)
1082 (let ((location (if (= (nkeys) 1) 0 1))
1083 (limit (funcall (limit-fn) (nlines))))
1084 (dotimes-fixnum (i (nlines) cache)
1085 (when (and (not (location-reserved-p location))
1087 (let* ((home-loc (compute-primary-cache-location-from-location
1089 (home (location-line (if (location-reserved-p home-loc)
1090 (next-location home-loc)
1092 (sep (when home (line-separation home i))))
1093 (when (and sep (> sep limit))
1094 (error "bad cache ~S ~@
1095 value at location ~D: ~D lines from its home. The limit is ~D."
1096 cache location sep limit))))
1097 (setq location (next-location location))))))
1099 (defun probe-cache (cache wrappers &optional default limit-fn)
1100 ;;(declare (values value))
1102 ;; FIXME: This and another earlier test on a WRAPPERS arg can
1103 ;; be compact assertoids.
1104 (error "WRAPPERS arg is NIL!"))
1105 (with-local-cache-functions (cache)
1106 (let* ((location (compute-primary-cache-location (field) (mask) wrappers))
1107 (limit (funcall (or limit-fn (limit-fn)) (nlines))))
1108 (declare (fixnum location limit))
1109 (when (location-reserved-p location)
1110 (setq location (next-location location)))
1111 (dotimes-fixnum (i (1+ limit))
1112 (when (location-matches-wrappers-p location wrappers)
1113 (return-from probe-cache (or (not (valuep))
1114 (location-value location))))
1115 (setq location (next-location location)))
1116 (dolist (entry (overflow))
1117 (when (equal (car entry) wrappers)
1118 (return-from probe-cache (or (not (valuep))
1122 (defun map-cache (function cache &optional set-p)
1123 (with-local-cache-functions (cache)
1124 (let ((set-p (and set-p (valuep))))
1125 (dotimes-fixnum (i (nlines) cache)
1126 (unless (or (line-reserved-p i) (not (line-valid-p i nil)))
1127 (let ((value (funcall function (line-wrappers i) (line-value i))))
1129 (setf (cache-vector-ref (vector) (+ (line-location i) (nkeys)))
1131 (dolist (entry (overflow))
1132 (let ((value (funcall function (car entry) (cdr entry))))
1134 (setf (cdr entry) value))))))
1137 (defun cache-count (cache)
1138 (with-local-cache-functions (cache)
1140 (declare (fixnum count))
1141 (dotimes-fixnum (i (nlines) count)
1142 (unless (line-reserved-p i)
1143 (when (line-full-p i)
1146 (defun entry-in-cache-p (cache wrappers value)
1147 (declare (ignore value))
1148 (with-local-cache-functions (cache)
1149 (dotimes-fixnum (i (nlines))
1150 (unless (line-reserved-p i)
1151 (when (equal (line-wrappers i) wrappers)
1154 ;;; returns T or NIL
1155 (defun fill-cache-p (forcep cache wrappers value)
1156 (with-local-cache-functions (cache)
1157 (let* ((location (compute-primary-cache-location (field) (mask) wrappers))
1158 (primary (location-line location)))
1159 (declare (fixnum location primary))
1160 (multiple-value-bind (free emptyp)
1161 (find-free-cache-line primary cache wrappers)
1162 (when (or forcep emptyp)
1164 (push (cons (line-wrappers free) (line-value free))
1165 (cache-overflow cache)))
1166 ;;(fill-line free wrappers value)
1168 (declare (fixnum line))
1169 (when (line-reserved-p line)
1170 (error "attempt to fill a reserved line"))
1171 (let ((loc (line-location line))
1172 (cache-vector (vector)))
1173 (declare (fixnum loc) (simple-vector cache-vector))
1174 (cond ((= (nkeys) 1)
1175 (setf (cache-vector-ref cache-vector loc) wrappers)
1177 (setf (cache-vector-ref cache-vector (1+ loc)) value)))
1180 (declare (fixnum i))
1181 (dolist (w wrappers)
1182 (setf (cache-vector-ref cache-vector (+ loc i)) w)
1183 (setq i (the fixnum (1+ i)))))
1185 (setf (cache-vector-ref cache-vector (+ loc (nkeys)))
1187 (maybe-check-cache cache))))))))
1189 (defun fill-cache-from-cache-p (forcep cache from-cache from-line)
1190 (declare (fixnum from-line))
1191 (with-local-cache-functions (cache)
1192 (let ((primary (location-line
1193 (compute-primary-cache-location-from-location
1194 cache (line-location from-line) from-cache))))
1195 (declare (fixnum primary))
1196 (multiple-value-bind (free emptyp)
1197 (find-free-cache-line primary cache)
1198 (when (or forcep emptyp)
1200 (push (cons (line-wrappers free) (line-value free))
1201 (cache-overflow cache)))
1202 ;;(transfer-line from-cache-vector from-line cache-vector free)
1203 (let ((from-cache-vector (cache-vector from-cache))
1204 (to-cache-vector (vector))
1206 (declare (fixnum to-line))
1207 (if (line-reserved-p to-line)
1208 (error "transferring something into a reserved cache line")
1209 (let ((from-loc (line-location from-line))
1210 (to-loc (line-location to-line)))
1211 (declare (fixnum from-loc to-loc))
1212 (modify-cache to-cache-vector
1213 (dotimes-fixnum (i (line-size))
1214 (setf (cache-vector-ref to-cache-vector
1216 (cache-vector-ref from-cache-vector
1217 (+ from-loc i)))))))
1218 (maybe-check-cache cache)))))))
1220 ;;; Returns NIL or (values <field> <cache-vector>)
1222 ;;; This is only called when it isn't possible to put the entry in the
1223 ;;; cache the easy way. That is, this function assumes that
1224 ;;; FILL-CACHE-P has been called as returned NIL.
1226 ;;; If this returns NIL, it means that it wasn't possible to find a
1227 ;;; wrapper field for which all of the entries could be put in the
1228 ;;; cache (within the limit).
1229 (defun adjust-cache (cache wrappers value free-old-cache-p)
1230 (with-local-cache-functions (cache)
1231 (let ((ncache (get-cache-from-cache cache (nlines) (field))))
1232 (do ((nfield (cache-field ncache) (next-wrapper-cache-number-index nfield)))
1233 ((null nfield) (free-cache ncache) nil)
1234 (setf (cache-field ncache) nfield)
1235 (labels ((try-one-fill-from-line (line)
1236 (fill-cache-from-cache-p nil ncache cache line))
1237 (try-one-fill (wrappers value)
1238 (fill-cache-p nil ncache wrappers value)))
1239 (if (and (dotimes-fixnum (i (nlines) t)
1240 (when (and (null (line-reserved-p i))
1241 (line-valid-p i wrappers))
1242 (unless (try-one-fill-from-line i) (return nil))))
1243 (dolist (wrappers+value (cache-overflow cache) t)
1244 (unless (try-one-fill (car wrappers+value) (cdr wrappers+value))
1246 (try-one-fill wrappers value))
1247 (progn (when free-old-cache-p (free-cache cache))
1248 (return (maybe-check-cache ncache)))
1249 (flush-cache-vector-internal (cache-vector ncache))))))))
1251 ;;; returns: (values <cache>)
1252 (defun expand-cache (cache wrappers value free-old-cache-p)
1253 ;;(declare (values cache))
1254 (with-local-cache-functions (cache)
1255 (let ((ncache (get-cache-from-cache cache (* (nlines) 2))))
1256 (labels ((do-one-fill-from-line (line)
1257 (unless (fill-cache-from-cache-p nil ncache cache line)
1258 (do-one-fill (line-wrappers line) (line-value line))))
1259 (do-one-fill (wrappers value)
1260 (setq ncache (or (adjust-cache ncache wrappers value t)
1261 (fill-cache-p t ncache wrappers value))))
1262 (try-one-fill (wrappers value)
1263 (fill-cache-p nil ncache wrappers value)))
1264 (dotimes-fixnum (i (nlines))
1265 (when (and (null (line-reserved-p i))
1266 (line-valid-p i wrappers))
1267 (do-one-fill-from-line i)))
1268 (dolist (wrappers+value (cache-overflow cache))
1269 (unless (try-one-fill (car wrappers+value) (cdr wrappers+value))
1270 (do-one-fill (car wrappers+value) (cdr wrappers+value))))
1271 (unless (try-one-fill wrappers value)
1272 (do-one-fill wrappers value))
1273 (when free-old-cache-p (free-cache cache))
1274 (maybe-check-cache ncache)))))
1276 ;;; This is the heart of the cache filling mechanism. It implements
1277 ;;; the decisions about where entries are placed.
1279 ;;; Find a line in the cache at which a new entry can be inserted.
1282 ;;; <empty?> is <line> in fact empty?
1283 (defun find-free-cache-line (primary cache &optional wrappers)
1284 ;;(declare (values line empty?))
1285 (declare (fixnum primary))
1286 (with-local-cache-functions (cache)
1287 (when (line-reserved-p primary) (setq primary (next-line primary)))
1288 (let ((limit (funcall (limit-fn) (nlines)))
1291 (p primary) (s primary))
1292 (declare (fixnum p s limit))
1295 ;; Try to find a free line starting at <s>. <p> is the
1296 ;; primary line of the entry we are finding a free
1297 ;; line for, it is used to compute the separations.
1298 (do* ((line s (next-line line))
1299 (nsep (line-separation p s) (1+ nsep)))
1301 (declare (fixnum line nsep))
1302 (when (null (line-valid-p line wrappers)) ;If this line is empty or
1303 (push line lines) ;invalid, just use it.
1304 (return-from find-free))
1305 (when (and wrappedp (>= line primary))
1306 ;; have gone all the way around the cache, time to quit
1307 (return-from find-free-cache-line (values primary nil)))
1308 (let ((osep (line-separation (line-primary line) line)))
1309 (when (>= osep limit)
1310 (return-from find-free-cache-line (values primary nil)))
1311 (when (cond ((= nsep limit) t)
1312 ((= nsep osep) (zerop (random 2)))
1315 ;; See whether we can displace what is in this line so that we
1316 ;; can use the line.
1317 (when (= line (the fixnum (1- (nlines)))) (setq wrappedp t))
1318 (setq p (line-primary line))
1319 (setq s (next-line line))
1322 (when (= line (the fixnum (1- (nlines)))) (setq wrappedp t)))))
1323 ;; Do all the displacing.
1325 (when (null (cdr lines)) (return nil))
1326 (let ((dline (pop lines))
1328 (declare (fixnum dline line))
1329 ;;Copy from line to dline (dline is known to be free).
1330 (let ((from-loc (line-location line))
1331 (to-loc (line-location dline))
1332 (cache-vector (vector)))
1333 (declare (fixnum from-loc to-loc) (simple-vector cache-vector))
1334 (modify-cache cache-vector
1335 (dotimes-fixnum (i (line-size))
1336 (setf (cache-vector-ref cache-vector
1338 (cache-vector-ref cache-vector
1340 (setf (cache-vector-ref cache-vector
1343 (values (car lines) t))))
1345 (defun default-limit-fn (nlines)
1351 (defvar *empty-cache* (make-cache)) ; for defstruct slot initial value forms
1353 ;;; Pre-allocate generic function caches. The hope is that this will
1354 ;;; put them nicely together in memory, and that that may be a win. Of
1355 ;;; course the first GC copy will probably blow that out, this really
1356 ;;; wants to be wrapped in something that declares the area static.
1358 ;;; This preallocation only creates about 25% more caches than PCL
1359 ;;; itself uses. Some ports may want to preallocate some more of
1362 ;;; KLUDGE: Isn't something very similar going on in precom1.lisp? Do
1363 ;;; we need it both here and there? Why? -- WHN 19991203
1364 (eval-when (:load-toplevel)
1365 (dolist (n-size '((1 513)(3 257)(3 129)(14 128)(6 65)(2 64)(7 33)(16 32)
1366 (16 17)(32 16)(64 9)(64 8)(6 5)(128 4)(35 2)))
1367 (let ((n (car n-size))
1368 (size (cadr n-size)))
1369 (mapcar #'free-cache-vector
1370 (mapcar #'get-cache-vector
1371 (make-list n :initial-element size))))))
1373 (defun caches-to-allocate ()
1374 (sort (let ((l nil))
1375 (maphash #'(lambda (size entry)
1376 (push (list (car entry) size) l))
1377 sb-pcl::*free-caches*)