1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
12 ;;;; This file implements abstract types which map globaldb Type-Number/Name
13 ;;;; pairs to data values. The database itself is defined in 'globaldb'.
15 ;;; Quasi-lockfree concurrent hashtable
16 ;;; ===================================
19 ;; http://trac.clozure.com/ccl/wiki/Internals/LockFreeHashTables
20 ;; https://github.com/boundary/high-scale-lib/blob/master/src/main/java/org/cliffc/high_scale_lib/NonBlockingHashMap.java
22 ;; The basic hashable is a lightweight one using prime number sizing strategy,
23 ;; and a secondary hash for re-probing that is not necessarily co-prime with
24 ;; the table size (as it would be, ideally), and no support for deletion.
26 ;; The lock-free logic differs from each of the preceding reference algorithms.
27 ;; The Java algorithm is truly lock-free: death of any thread will never impede
28 ;; progress in other threads. The CCL algorithm is only quasi-lock-free, as is
29 ;; ours. Were a rehashing thread to terminate abnormally while holding the
30 ;; rehash mutex, all other threads will likely starve at some point.
32 ;; Unlike the CCL algorithm, we allow reading/writing concurrently with rehash.
33 ;; Most operations will never notice that the rehasher has acquired a mutex,
34 ;; because the only case where writers compete for the mutex is in trying
35 ;; to claim a new cell in a storage vector that is being transported, and where
36 ;; the rehasher has already visited the desired cell. Since the rehasher will
37 ;; never re-visit a cell, it disallows updates that it could not perceive.
38 ;; The Java algorithm has additional complexity imposed upon it by the
39 ;; feature that rehashing work is parceling out fairly to threads,
40 ;; whereas we avoid that by having one rehasher transport all cells.
42 ;; A marker for cells that may be claimed by a writer
43 (defconstant +empty-key
+ 0)
44 ;; A marker for cells that may NOT be claimed by a writer.
45 (defconstant +unavailable-key
+ -
1)
47 ;; We have a 24-bit cell index. This is generous,
48 ;; allowing for ~5 million Names assuming 60% load factor.
49 (deftype info-cell-index
() `(unsigned-byte 24))
51 ;; An INFO-STORAGE is a vector of a 3-element header followed by all keys
52 ;; and then all values. This way we can simply use FILL to set all keys to 0
53 ;; and all values to NIL.
54 (defconstant +info-keys-offset
+ 3)
55 (defstruct (info-storage (:type vector
) (:constructor nil
) (:copier nil
))
56 ;; Capacity refers to pairs of vector elements. The lower bound is to
57 ;; try (in vain) to elide a check for division by 0 in the secondary hash.
58 ;; The upper bound is harmlessly wrong by a factor of 2 because
59 ;; INFO-CELL-INDEX should be the max physical cell that can be addressed.
60 ;; No problem - you'll run out of memory before hitting the edge anyway.
61 (capacity 0 :type
(and (integer 3 *) info-cell-index
)) ; immutable
62 ;; Make a new storage if count exceeds this
63 (threshold 0 :type info-cell-index
) ; immutable
64 (next nil
:type simple-vector
) ; Pending INFO-STORAGE during rehash
68 (defun make-info-storage (n-cells-min &optional
(load-factor .7))
69 ;; If you ask for 40 entries at 50% load, you get (PRIMIFY 80) entries.
70 (let* ((n-cells (primify (ceiling n-cells-min load-factor
)))
71 (a (make-array (+ +info-keys-offset
+ (* 2 n-cells
))))
72 (end (+ +info-keys-offset
+ n-cells
)))
73 (setf (info-storage-capacity a
) n-cells
74 ;; The threshold should be approximately the same as
75 ;; the number you asked for in the first place.
76 (info-storage-threshold a
) (floor (* n-cells load-factor
))
77 (info-storage-next a
) #()) ; type-correct initial value
78 (fill a
+empty-key
+ :start
+info-keys-offset
+ :end end
)
79 (fill a nil
:start end
)
82 (defstruct (info-hashtable (:conc-name info-env-
))
83 (storage (make-info-storage 30) :type simple-vector
)
84 (comparator #'equal
:type function
)
85 (hash-function #'globaldb-sxhashoid
:type function
)
86 (mutex (sb!thread
:make-mutex
))
87 ;; COUNT is always at *least* as large as the key count.
88 ;; If no insertions are in progress, it is exactly right.
91 (def!method print-object
((self info-hashtable
) stream
)
92 (declare (stream stream
))
93 (print-unreadable-object (self stream
:type t
:identity t
)
94 (format stream
"~D/~D entr~:@P" (info-env-count self
)
95 (info-storage-capacity (info-env-storage self
)))))
97 ;; We can't assume that the host lisp supports a CAS operation for info-gethash
98 ;; or info-puthash, but that's fine because there's only one thread.
99 (defmacro info-cas
(storage index oldval newval
)
101 `(let ((actual-old (svref ,storage
,index
)))
102 (if (eq actual-old
,oldval
)
103 (progn (setf (svref ,storage
,index
) ,newval
) actual-old
)
104 (error "xc bug. CAS expected ~S got ~S" actual-old
,oldval
)))
106 `(cas (svref ,storage
,index
) ,oldval
,newval
))
108 ;; Similarly we need a way to atomically adjust the hashtable count.
109 (declaim (inline info-env-adjust-count
))
110 (defun info-env-adjust-count (table delta
)
112 (prog1 (info-env-count table
) (incf (info-env-count table
) delta
))
114 ;; Inform the compiler that this is not possibly a bignum,
115 ;; since it's true upper bound is the info storage threshold.
116 (truly-the info-cell-index
(atomic-incf (info-env-count table
) delta
)))
118 (declaim (inline make-info-forwarding-pointer
119 info-forwarding-pointer-target
121 (ftype (sfunction (t) simple-vector
) info-env-rehash
)
122 (ftype (sfunction (t t
) simple-vector
) %wait-for-rehash
))
124 ;; Concurrent access relies on a forwarding pointer being placed into
125 ;; transported value cells. Since this is not a fully general hashtable,
126 ;; we can use fixnums as forwarding pointers, as they're otherwise
127 ;; never present as a value.
130 (defun make-info-forwarding-pointer (index) index
)
131 (defun info-forwarding-pointer-target (pointer) pointer
)
132 (defun info-value-moved-p (val) (fixnump val
)))
134 ;; However, here is a forwarding-pointer representation that allows fixnums
135 ;; as legal values in the table, so, since it's more general, ... why not?
138 (defun make-info-forwarding-pointer (index)
139 (declare (type info-cell-index index
) (optimize (safety 0)))
140 (%make-lisp-obj
(+ (ash index
8) sb
!vm
:unbound-marker-widetag
)))
141 (defun info-forwarding-pointer-target (marker)
142 (ash (get-lisp-obj-address marker
) -
8))
143 (defun info-value-moved-p (x)
144 (eq (logand (get-lisp-obj-address x
) #xff
)
145 sb
!vm
:unbound-marker-widetag
)))
147 ;; The common skeleton of {Get, Put, Rehash} operations. Probe key cells until
148 ;; either a hit occurs, in which case the :HIT form is executed and looping
149 ;; stops; or an empty slot is seen in which case the :MISS code is executed.
150 ;; :MISS should contain a GO or RETURN on success, otherwise probing continues.
151 ;; No precaution exists against probing forever, such as could happen if the
152 ;; probing strategy fails to visit all candidate free cells.
154 ;; Stepping is done by subtracting a secondary hash rather than adding,
155 ;; as this allows testing for wraparound by comparison to a constant
156 ;; instead of against storage capacity. Nonlinear probing jumps around in
157 ;; the L1 cache regardless, as they key space is a ring so the "direction"
158 ;; of probing is irrelevant. [In contrast, linear probing usually benefits
159 ;; from scanning physically forward, preferably within a cache line,
160 ;; but for practical purposes linear probing is worse.]
162 (defmacro do-probe-sequence
((storage key table
&optional hash
)
164 (with-unique-names (test miss-fn len key-index step
)
165 (once-only ((storage storage
) (key key
) (table table
)
169 `(funcall (info-env-hash-function ,table
) ,key
)))))
170 `(macrolet ((key-index () ; expose key+value indices to invoking code
173 '(+ (info-storage-capacity ,storage
) ,key-index
))
175 `(let ((probed-key (svref ,',storage
,',key-index
)))
176 ,',probe
; could keep a tally of the probes
177 ;; Optimistically test for hit first, then markers
178 (cond ((funcall (info-env-comparator ,',table
)
181 ((or (eql probed-key
+unavailable-key
+)
182 (eql probed-key
+empty-key
+))
184 (let* ((,len
(info-storage-capacity ,storage
))
185 (,key-index
(+ (rem ,hashval
,len
) +info-keys-offset
+))
187 (declare (type info-cell-index
,key-index
,step
))
188 (dx-flet ((,miss-fn
() ,miss
))
191 ;; only need the second hash if didn't hit on first try
192 (setq ,step
(1+ (rem ,hashval
(- ,len
2))))
194 (setq ,key-index
(let ((next (- ,key-index
,step
)))
195 (if (< next
+info-keys-offset
+)
203 ;; Wait for ENV's storage to change to something other than STORAGE, and
204 ;; return the new one. As long as rehash finishes in finite time, every thread
205 ;; makes progess. We don't protect against untimely death of the thread
206 ;; that holds the lock.
208 (defun %wait-for-rehash
(env storage
)
209 ;; kinda spin, except not quite that bad
210 (loop (sb!thread
:thread-yield
) ; relinquish time slice, supposing it helps
211 (if (eq (info-env-storage env
) storage
)
212 ;; Grab and release the mutex for no other reason than to
213 ;; observe that a rehasher doesn't (for the moment) have it.
214 (sb!thread
:with-mutex
((info-env-mutex env
))) ; do nothing, retry
215 (return (info-env-storage env
)))))
217 ;; Look in info-environment ENV for the name KEY. Arguments are like GETHASH.
219 ;; Invariant: any KEY's value is present in at most 1 storage.
220 ;; Uniqueness of the location ensures that when writing with CAS, the place is
221 ;; either current, or CAS fails in a way that informs the writer of a new place.
222 ;; At most one probing sequence occurs, in that hitting a key might entail
223 ;; more than one forwarding but never a re-probing.
224 ;; The same is not true on insert, for which probing restart is quite common.
225 ;; When chasing values, INFO-STORAGE-NEXT may or may not be EQ to what is in
226 ;; INFO-ENV-STORAGE, depending whether rehash is still running, has completed,
227 ;; or started all over again before the reader got a chance to chase one time.
229 (defun info-gethash (key env
&aux
(storage (info-env-storage env
)))
230 (do-probe-sequence (storage key env
)
231 :miss
(return-from info-gethash nil
)
232 ;; With 99% certainty the :READ barrier is needed for non-x86, and if not,
233 ;; it can't hurt. 'info-storage-next' can be empty until at least one cell
234 ;; has been forwarded, but in general an unsynchronized read might be
235 ;; perceived as executing before a conditional that guards whether the
236 ;; read should happen at all. 'storage-next' has the deceptive look of a
237 ;; data dependency but it's not - it's a control dependency which as per
238 ;; the 'memory-barriers.txt' document at kernel.org, demands a barrier.
239 ;; The subsequent ref to storage (if the loop iterates) has a
240 ;; data-dependency, and will never be reordered except on an Alpha :-(
241 ;; so we _don't_ need a barrier after resetting 'storage' and 'index'.
242 :hit
(let ((index (value-index)))
244 (sb!thread
:barrier
(:read
) (svref storage index
))))
245 (if (info-value-moved-p value
)
246 (setq storage
(info-storage-next storage
)
247 index
(info-forwarding-pointer-target value
))
250 ;; ENV and KEY are as above. UPDATE-PROC is invoked with the old value stored
251 ;; for KEY and it should return the possibly-unchanged new value to put.
253 ;; Note a tiny problem of semantics on 'miss' - for an instant after winning
254 ;; CAS of a new key, that key appears to exist with the default value NIL,
255 ;; causing readers to think "FOUND-P" = T which might have a different sense
256 ;; from "not found". If a Name is transiently "found" despite having no data,
257 ;; nobody cares, at least for globaldb. Remedies, if desired, involve either:
258 ;; 1. Making a "no value" marker that is distinct from NIL.
259 ;; 2. Placing keys/value pairs in adjacent cells and using double-wide CAS.
260 ;; The reason we don't care about this anomaly is that we have to
261 ;; look in an Info-Vector [q.v.] anyway to get the whole picture.
263 (defun info-puthash (env key update-proc
)
264 (aver (not (member key
'(0 -
1))))
265 (labels ((follow/update
(array value-index
)
266 (let ((value ; see INFO-GETHASH for this barrier's rationale
267 (sb!thread
:barrier
(:read
) (svref array value-index
))))
268 (if (info-value-moved-p value
)
269 (follow/update
(info-storage-next array
)
270 (info-forwarding-pointer-target value
))
271 (update array value-index value
))))
272 (update (array index oldval
)
273 ;; invariant: OLDVAL is not a forwarding pointer.
274 (let ((newval (funcall update-proc oldval
)))
275 (if (eq newval oldval
)
276 oldval
; forgo update
277 (named-let put
((array array
) (index index
))
278 (let ((actual-old (info-cas array index oldval newval
)))
279 ;; Unlike above, this read of storage-next can not
280 ;; be perceived as having occurred prior to CAS.
281 ;; x86 synchronizes at every locked instruction, and our
282 ;; PPC CAS vops sync as a nececessity of performing CAS.
283 (cond ((eq oldval actual-old
) newval
) ; win
284 ((info-value-moved-p actual-old
) ; forwarded
285 (put (info-storage-next array
)
286 (info-forwarding-pointer-target actual-old
)))
287 (t ; collision with another writer
288 (update array index actual-old
)))))))))
289 (named-let probe
((hashval (funcall (info-env-hash-function env
) key
))
290 (storage (info-env-storage env
)))
291 (do-probe-sequence (storage key env hashval
)
292 :hit
(follow/update storage
(value-index))
295 (let ((old-count (info-env-adjust-count env
1)))
296 (declare (type info-cell-index old-count
))
297 (when (>= old-count
(info-storage-threshold storage
))
298 (sb!thread
:with-mutex
((info-env-mutex env
))
299 ;; any thread could have beaten us to rehashing
300 (when (eq (info-env-storage env
) storage
)
301 (info-env-rehash env
)))
302 (info-env-adjust-count env -
1) ; prepare to retry
303 (return-from probe
(probe hashval
(info-env-storage env
)))))
304 ;; Attempt to claim KEY-INDEX
305 (let ((oldkey (info-cas storage
(key-index) +empty-key
+ key
)))
306 (when (eql oldkey
+empty-key
+) ; successful claim
307 ;; Optimistically assume that nobody else rewrote the value
308 (return-from probe
(update storage
(value-index) nil
)))
309 (info-env-adjust-count env -
1) ; failed
310 ;; The fallthrough branch of this COND is ordinary CAS failure where
311 ;; somebody else wanted this slot, and won. Looping tries again.
312 (cond ((funcall (info-env-comparator env
) oldkey key
) ; coincidence
313 (return-from probe
(follow/update storage
(value-index))))
314 ((eql oldkey
+unavailable-key
+) ; Highly unlikely
315 ;; as preemptive check up above ensured no rehash needed.
317 (probe hashval
(%wait-for-rehash env storage
)))))))))))
319 ;; Rehash ENV to a larger storage. When writing a key into new storage,
320 ;; key cells are uniquely owned by this thread without contention.
321 ;; Other threads may not look in new storage without first observing that
322 ;; a key's value was definitely moved.
323 ;; The rehasher marks empty cells as unusable so that writers can't insert
324 ;; into the subsequence of storage already visited. The rehasher must of
325 ;; course vie for the cell it is trying to mark as unusable.
327 (defun info-env-rehash (env)
328 (let* ((old-count (info-env-count env
))
329 (old-storage (info-env-storage env
))
330 ;; the new storage begins life at ~50% capacity
331 (new-storage (make-info-storage (ceiling old-count
.5)))
332 (old-capacity (info-storage-capacity old-storage
))
333 (new-capacity (info-storage-capacity new-storage
)))
335 (sb!thread
:barrier
(:write
) ; Publish NEW-STORAGE before scanning keys.
336 (setf (info-storage-next old-storage
) new-storage
))
338 (loop for old-key-index of-type info-cell-index
339 from
+info-keys-offset
+ below
(+ +info-keys-offset
+ old-capacity
)
340 ;; If the indexed cell is not in use, try once to prevent subsequent
341 ;; writes by changing the empty marker to 'unavailable'. The outcome
342 ;; determines whether to continue transporting the cell's value.
343 for key
= (let ((key (svref old-storage old-key-index
)))
344 (if (eql key
+empty-key
+)
345 (info-cas old-storage old-key-index
346 +empty-key
+ +unavailable-key
+)
348 unless
(eql key
+empty-key
+)
349 do
(let* ((new-key-index
351 (do-probe-sequence (new-storage key env
)
352 :hit
(bug "Globaldb rehash failure. Mutated key?")
353 :miss
(return (key-index)))))
354 (old-value-index (+ old-key-index old-capacity
))
355 (new-value-index (+ new-key-index new-capacity
))
356 (value (svref old-storage old-value-index
)))
357 (setf (svref new-storage new-key-index
) key
) ; Q: barrier needed?
358 ;; Copy the current value into the new storage,
359 ;; and CAS in a forwarding pointer. Repeat until successful.
361 ;; Force VALUE to memory before publishing relocated status
362 (sb!thread
:barrier
(:write
)
363 (setf (svref new-storage new-value-index
) value
))
366 old-storage old-value-index value
367 (make-info-forwarding-pointer new-value-index
))))
368 (if (eq value actual-old
)
370 (setq value actual-old
))))))
372 ;; Typical of most lockfree algorithms, we've no idea when
373 ;; the old storage can be freed. GC will figure it out.
374 ;; No write barrier needed. Threads still looking at old storage
375 ;; will eventually find all cells unavailable or forwarded.
376 (setf (info-env-storage env
) new-storage
)))
378 ;; This maphash implementation is not threadsafe.
379 ;; It can be made threadsafe by following forwarded values
380 ;; and skipping over unavailable keys.
382 (defmacro info-maphash
(fun env
) ; map FUN over each key/value
383 (with-unique-names (f storage capacity i key
)
385 (,storage
(info-env-storage ,env
))
386 (,capacity
(info-storage-capacity ,storage
)))
387 (loop for
,i below
,capacity
388 for
,key
= (svref ,storage
(+ ,i
+info-keys-offset
+))
389 unless
(eql ,key
+empty-key
+)
392 (+ ,i
+info-keys-offset
+ ,capacity
)))))))
394 ;; CAS is the primitive operation on an info hashtable,
395 ;; and SETF is implemented in terms of CAS. For the most part it is
396 ;; inadvisable to use this for anything other than tinkering at the REPL.
398 (defun (setf info-gethash
) (newval key env
)
399 (dx-flet ((update (old) (declare (ignore old
)) newval
))
400 (info-puthash env key
#'update
)))
402 (defun show-info-env (env)
403 (info-maphash (lambda (k v
) (format t
"~S -> ~S~%" k v
)) env
))
408 ;;; Info for a Name (an arbitrary object) is stored in an Info-Vector,
409 ;;; which is like is a 2-level association list. Info-Vectors are stored in
410 ;;; symbols for most names, or in the global hashtable for "complicated" names.
412 ;;; Such vectors exists in two variations: packed and unpacked.
413 ;;; The representations are nearly equivalent for lookup, but the packed format
414 ;;; is more space-efficient, though difficult to manipulate except by unpacking.
416 ;;; Consider a family of Names whose "root" is SB-MOP:STANDARD-INSTANCE-ACCESS.
417 ;;; 1. SB-MOP:STANDARD-INSTANCE-ACCESS
418 ;;; 2. (SETF SB-MOP:STANDARD-INSTANCE-ACCESS)
419 ;;; 3. (CAS SB-MOP:STANDARD-INSTANCE-ACCESS)
421 ;;; Those three names share one Info-Vector. Conceptually the outer alist key
422 ;;; is NIL for the first of those names, and SETF/CAS for the latter two.
423 ;;; The inner alist key is a number identifying a type of info.
424 ;;; If it were actually an alist, it would look like this:
426 ;;; ((nil (63 . #<fdefn SB-MOP:STANDARD-INSTANCE-ACCESS>) (1 . :FUNCTION) ...)
427 ;;; (SETF (63 . #<fdefn (SETF SB-MOP:STANDARD-INSTANCE-ACCESS)>) ...)
428 ;;; (CAS (63 . #<fdefn (CAS SB-MOP:STANDARD-INSTANCE-ACCESS)>) ...)
432 ;;; * The root name is exogenous to the vector - it is not stored.
433 ;;; * The type-number for (:FUNCTION :DEFINITION) is 63, :KIND is 1, etc.
434 ;;; * Names which are lists of length other than 2, or improper lists,
435 ;;; or whose elements are not both symbols, are disqualified.
437 ;;; Packed vector layout
438 ;;; --------------------
439 ;;; Because the keys to the inner lists are integers in the range 0 to 63,
440 ;;; either 5 or 10 keys will fit into a fixnum depending on word size.
441 ;;; This permits one memory read to retrieve a collection of keys. In packed
442 ;;; format, an ordered set of keys ("fields") is called a "descriptor".
444 ;;; Descriptors are stored from element 0 upward in the packed vector,
445 ;;; and data are indexed downward from the last element of the vector.
447 ;;; #(descriptor0 descriptor1 ... descriptorN valueN ... value1 value0)
449 ;;; e.g. The field at absolute index 3 - vector element 0, bit position 18 -
450 ;;; will find its data at index (- END 3). In this manner, it doesn't matter
451 ;;; how many more descriptors exist.
453 ;;; A "group" comprises all the info for a particular Name, and its list
454 ;;; of types may may span descriptors, though rarely.
455 ;;; An "auxilliary key" is the first element of a 2-list Name. It is interposed
456 ;;; within the data portion of the vector after the preceding info group.
457 ;;; Descriptors are self-delimiting in that the first field in a group
458 ;;; indicates the number of additional fields in the group.
460 ;;; Unpacked vector layout
461 ;;; ----------------------
462 ;;; This representation is used transiently during insertion/deletion.
463 ;;; It is a concatenation of plists as a vector, interposing at the splice
464 ;;; points the auxilliary key for the group, except for the root name which
465 ;;; does not store an auxilliary key.
467 ;;; Unpacked vector format looks like:
469 ;;; /- next group starts here
471 ;;; #(length type val type val ... KEY length type val ... KEY length ...)
473 ;;; info group for the primary Name, a/k/a "root symbol", starts here
475 ;;; One can envision that the first info group stores its auxilliary key
476 ;;; at vector index -1 when thinking about the correctness of algorithms
477 ;;; that process unpacked info-vectors.
478 ;;; See !TEST-PACKIFY-INFOS for examples of each format.
480 ;;;;; Some stuff moved from 'globaldb.lisp':
482 ;;;; At run time, we represent the type of info that we want by a small
483 ;;;; non-negative integer.
484 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
485 (def!constant type-number-bits
6))
486 (deftype type-number
() `(unsigned-byte ,type-number-bits
))
488 (defconstant info-type-mask
(ldb (byte type-number-bits
0) -
1)) ; #b111111
490 ;; Using 6 bits per packed field, 5 infos can be described in a 30-bit fixnum,
491 ;; or 10 in a fixnum on 64-bit machines (regardless of n-fixnum-tag-bits).
492 ;; The eval-when seems to be necessary for building with CCL as host.
493 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
494 (defconstant +infos-per-word
+ (floor sb
!vm
:n-fixnum-bits type-number-bits
)))
496 ;; Descriptors are target fixnums
497 (deftype info-descriptor
() `(signed-byte ,sb
!vm
:n-fixnum-bits
))
499 ;; Every Name amenable to storage in info-vectors has an auxilliary key
500 ;; as explained above, except that the root name itself has none.
501 (defconstant +no-auxilliary-key
+ 0)
503 ;; An empty info-vector. Its 0th field describes that there are no more fields.
504 (defconstant-eqx +nil-packed-infos
+ #(0) #'equalp
)
506 ;; FDEFINITIONs have a type-number that admits slightly clever logic
507 ;; for INFO-VECTOR-FDEFN. Do not change this constant without
508 ;; careful examination of that function.
509 (defconstant +fdefn-type-num
+ info-type-mask
)
511 ;; Extract a field from a packed info descriptor.
512 ;; A field is either a count of type-numbers, or a type-number.
513 (declaim (inline packed-info-field
))
514 (defun packed-info-field (vector desc-index field-index
)
515 ;; Should not need (THE TYPE-NUMBER) however type inference
516 ;; seems borked during cross-compilation due to the shadowed LDB
517 ;; (see "don't watch:" in cold/defun-load-or-cload-xcompiler)
518 ;; and in particular it sees especially weird that this message appears
519 ;; note: type assertion too complex to check:
520 ;; (VALUES (UNSIGNED-BYTE 6) &REST T).
521 ;; because nothing here should be possibly-multiple-value-producing.
523 (ldb (byte type-number-bits
524 (* (the (mod #.
+infos-per-word
+) field-index
) type-number-bits
))
525 (the info-descriptor
(svref vector desc-index
)))))
527 ;; Compute the number of elements needed to hold unpacked VECTOR after packing.
528 ;; This is not "compute-packed-info-size" since that could be misconstrued
529 ;; and wanting the vector to be already packed.
531 (defun compute-packified-info-size (vector &optional
(end (length vector
)))
532 (declare (simple-vector vector
)) ; unpacked format
533 (let ((index 0) ; index into the unpacked input vector
534 (n-fields 0)) ; will be the total number of packed fields
535 (declare (type index index end n-fields
))
537 ;; 'length' is the number of vector elements in this info group,
538 ;; including itself but not including its auxilliary key.
539 (let ((length (the index
(svref vector index
))))
540 ;; Divide by 2 because we only count one field for the entry, but the
541 ;; input vector had 2 cells per entry. Add 1 because the group's field
542 ;; count is accounted for in the total packed field count.
543 (incf n-fields
(1+ (ash length -
1)))
544 (incf index
(1+ length
)) ; jump over the entries, +1 for aux-key
546 ;; The first info group lacks an aux-key, making n-fields 1 too high
547 ;; in terms of data cells used, but correct for packed fields.
548 (return (+ (ceiling n-fields
+infos-per-word
+) (1- n-fields
))))))))
550 ;; MAKE-INFO-DESCRIPTOR is basically ASH-LEFT-MODFX, shifting VAL by SHIFT.
551 ;; It is important that info descriptors be target fixnums, but 'cross-modular'
552 ;; isn't loaded early enough to use 'mask-signed-field'.
553 ;; It's not needed on 64-bit host/target combination because 10 fields (60 bits)
554 ;; never touch the sign bit.
555 ;; FIXME: figure out why the definition of ash-left-modfx is
556 ;; conditionalized out for platforms other than x86[-64].
557 ;; It looks like it ought to work whether or not there are vops.
558 (defmacro make-info-descriptor
(val shift
)
559 (if (> sb
!vm
:n-fixnum-bits
30)
561 `(logior (if (logbitp (- 29 ,shift
) ,val
) sb
!xc
:most-negative-fixnum
0)
564 ;; Convert unpacked vector to packed vector.
565 ;; 'pack-infos' would be a hypothetical accessor for the 'infos' of a 'pack'
566 ;; (whatever that is ...) so verbifying as such makes it more mnemonic to me.
568 (defun packify-infos (input &optional
(end (length input
)))
569 (declare (simple-vector input
))
570 (let* ((output (make-array (compute-packified-info-size input end
)))
571 (i -
1) ; input index: pre-increment to read the next datum
572 (j -
1) ; descriptor index: pre-increment to write
573 (k (length output
)) ; data index: pre-decrement to write
576 (declare (type index-or-minus-1 i j k end
)
577 (type (mod #.
(1+ (* (1- +infos-per-word
+) type-number-bits
)))
579 (type info-descriptor word
))
580 (flet ((put-field (val) ; insert VAL into the current packed descriptor
581 (declare (type type-number val
))
582 (setq word
(logior (make-info-descriptor val field-shift
) word
))
583 (if (< field-shift
(* (1- +infos-per-word
+) type-number-bits
))
584 (incf field-shift type-number-bits
)
585 (setf (svref output
(incf j
)) word field-shift
0 word
0))))
586 ;; Truncating divide by 2: count = n-elements in the group @ 2 per entry,
587 ;; +1 for count itself but not including its aux-key.
588 (loop (let ((count (ash (the index
(svref input
(incf i
))) -
1)))
589 (put-field count
) ; how many infos to follow
590 (dotimes (iter count
)
591 (put-field (svref input
(incf i
))) ; an info type-number
592 (setf (svref output
(decf k
)) (svref input
(incf i
)))) ; value
593 (when (>= (incf i
) end
)
595 (setf (svref output
(decf k
)) (svref input i
))))) ; an aux-key
596 (unless (zerop field-shift
) ; store the final descriptor word
597 (setf (svref output
(incf j
)) word
))
598 (aver (eql (1+ j
) k
)) ; last descriptor must be adjacent final data cell
601 ;; Within the scope of BODY, bind GENERATOR to a local function which
602 ;; returns the next field from a descriptor in INPUT-VAR, a packed vector.
603 ;; The generator uses DESCRIPTOR-INDEX and updates it as a side-effect.
605 (defmacro with-packed-info-iterator
((generator input-var
606 &key descriptor-index
)
608 (with-unique-names (input word count
)
609 `(let* ((,input
(the simple-vector
,input-var
))
610 (,descriptor-index -
1)
613 (declare (type info-descriptor
,word
)
615 (type index-or-minus-1
,descriptor-index
))
616 (flet ((,generator
()
618 (incf ,descriptor-index
)
619 (setq ,word
(svref ,input
,descriptor-index
)
620 ,count
+infos-per-word
+))
621 (prog1 (logand ,word info-type-mask
)
622 (setq ,word
(ash ,word
(- type-number-bits
)))
626 ;; Iterate over VECTOR, binding DATA-INDEX to the index of each aux-key in turn.
627 ;; TOTAL-N-FIELDS is deliberately exposed to invoking code.
629 (defmacro do-packed-info-vector-aux-key
((vector &optional
(data-index (gensym)))
630 step-form
&optional result-form
)
631 (with-unique-names (descriptor-idx field-idx
)
632 (once-only ((vector vector
))
633 `(let ((,data-index
(length ,vector
))
637 (declare (type index
,data-index
,descriptor-idx total-n-fields
)
638 (type (mod #.
+infos-per-word
+) ,field-idx
))
639 ;; Loop through the descriptors in random-access fashion.
640 ;; Skip 1+ n-infos each time, because the 'n-infos' is itself a field
641 ;; that is not accounted for in its own value.
642 (loop (let ((n (1+ (packed-info-field ,vector
643 ,descriptor-idx
,field-idx
))))
644 (incf total-n-fields n
)
645 (multiple-value-setq (,descriptor-idx
,field-idx
)
646 (floor total-n-fields
+infos-per-word
+))
647 (decf ,data-index n
))
648 ;; Done when the ascending index and descending index meet
649 (unless (< ,descriptor-idx
,data-index
)
650 (return ,result-form
))
651 ,@(if step-form
(list step-form
)))))))
653 ;; Compute the number of elements needed to hold packed VECTOR after unpacking.
654 ;; The unpacked size is the number of auxilliary keys plus the number of entries
655 ;; @ 2 cells per entry, plus the number of length cells which indicate the
656 ;; number of data cells used (including length cells but not aux key cells).
657 ;; Equivalently, it's the number of packed fields times 2 minus 1.
659 (defun compute-unpackified-info-size (vector)
660 (declare (simple-vector vector
))
661 (do-packed-info-vector-aux-key (vector) ()
662 ;; off-by-one: the first info group's auxilliary key is imaginary
663 (1- (truly-the fixnum
(ash total-n-fields
1)))))
665 ;; Convert packed INPUT vector to unpacked.
666 ;; If optional OUTPUT is supplied, it is used, otherwise output is allocated.
667 ;; For efficiency the OUTPUT should be provided as a dynamic-extent array.
669 (defun unpackify-infos (input &optional
(output nil output-supplied-p
))
670 (declare (simple-vector input
))
671 (unless output-supplied-p
672 (setq output
(make-array (compute-unpackified-info-size input
))))
673 (let ((i (length input
)) (j -
1)) ; input index and output index respectively
674 (declare (type index-or-minus-1 i j
))
675 (with-packed-info-iterator (next-field input
:descriptor-index desc-idx
)
677 (let ((n-infos (next-field)))
678 ;; store the info group length, including the length cell in the length
679 (setf (svref output
(incf j
)) (1+ (ash n-infos
1)))
680 (dotimes (iter n-infos
) ; over info-types
681 (setf (svref output
(incf j
)) (next-field) ; type-num
682 (svref output
(incf j
)) (svref input
(decf i
))))) ; value
683 (if (< desc-idx
(decf i
)) ; as long as the indices haven't met
684 (setf (svref output
(incf j
)) (svref input i
)) ; copy next aux-key
685 (return (if output-supplied-p nil output
))))))) ; else done
687 ;; Return the index of the 'length' item for an info group having
688 ;; auxilliary-key KEY in unpacked VECTOR bounded by END (exclusive),
689 ;; or NIL if not found.
691 (defun info-find-aux-key/unpacked
(key vector end
)
692 (declare (type index end
))
693 (if (eql key
+no-auxilliary-key
+)
694 0 ; the first group's length (= end) is stored here always
696 (declare (type index index
))
698 ;; skip 'length' cells plus the next aux-key
699 (incf index
(1+ (the index
(svref vector index
))))
700 (cond ((>= index end
)
702 ;; backward a cell is where the aux-key resides.
703 ((eq (svref vector
(1- index
)) key
)
706 ;; In packed info VECTOR try to find the auxilliary key SYMBOL.
707 ;; If found, return indices of its data, info descriptor word, and field.
708 ;; If not found, the first value is NIL and the descriptor indices
709 ;; arbitrarily point to the next available descriptor field.
711 (defun info-find-aux-key/packed
(vector symbol
)
712 ;; explicit bounds checking is done by the code below
713 (declare (optimize (safety 0)))
714 (aver (simple-vector-p vector
))
715 (let ((descriptor-idx 0) ; physical index to vector
716 (field-idx 0) ; relative index within current descriptor
717 ;; On each iteration DATA-IDX points to an aux-key cell
718 ;; The first group's imaginary aux-key cell is past the end.
719 (data-idx (length (the simple-vector vector
))))
720 (declare (type index descriptor-idx data-idx
)
721 (fixnum field-idx
)) ; can briefly exceed +infos-per-word+
722 ;; Efficiently skip past N-INFOS infos. If decrementing the data index
723 ;; hits the descriptor index, we're done. Otherwise increment the field
724 ;; index and maybe descriptor index and check again for loop termination.
725 (flet ((skip (n-infos &aux
(n-fields (1+ n-infos
))) ; return T on success
726 (cond ((<= (decf data-idx n-fields
) descriptor-idx
) nil
)
727 ;; descriptor-idx < data-idx, so potentially more data.
728 ;; If current descriptor has another field, continue.
729 ((< (incf field-idx n-fields
) +infos-per-word
+) t
)
730 (t ; The descriptor index advances.
731 (loop (incf descriptor-idx
)
732 (when (< (decf field-idx
+infos-per-word
+)
734 (return (< descriptor-idx data-idx
))))))))
735 (declare (inline skip
))
736 ;; While this could compare aux-keys with #'EQUAL, it is not obvious how
737 ;; in general one would pick a symbol from the name as that which
738 ;; is delegated as the one to hold the info-vector.
739 (values (cond ((not (skip (packed-info-field vector
0 0))) nil
)
740 ;; At least one aux key is present.
741 ((eq (aref vector data-idx
) symbol
) data-idx
) ; yay
742 ;; aux-key order invariant allows early fail on SETF
743 ((eq symbol
'setf
) nil
)
746 (cond ((not (skip (packed-info-field vector descriptor-idx
749 ((eq (aref vector data-idx
) symbol
)
750 (return data-idx
))))))
751 descriptor-idx field-idx
)))) ; can be ignored if 1st val is nil
753 ;; Take a packed info-vector INPUT and insert (AUX-KEY,TYPE-NUMBER,VALUE).
754 ;; Packed info-vectors are immutable. Any alteration must create a copy.
755 ;; This is done by unpacking/repacking - it's easy enough and fairly
756 ;; efficient since the temporary vector is stack-allocated.
758 (defun %packed-info-insert
(input aux-key type-number value
)
759 (declare (simple-vector input
) (type type-number type-number
))
761 ;; Test if the aux-key has been seen before or needs to be added.
762 (if (and (not (eql aux-key
+no-auxilliary-key
+))
763 (not (info-find-aux-key/packed input aux-key
)))
764 4 ; need space for [aux-key, length, type-num, value]
765 2)) ; only space for [type-num, value]
766 (old-size (compute-unpackified-info-size input
))
767 (new-size (+ old-size n-extra-elts
))
768 (new (make-array new-size
)))
769 (declare (type index old-size new-size
)
770 (truly-dynamic-extent new
))
771 (unpackify-infos input new
)
772 (flet ((insert-at (point v0 v1
)
773 (unless (eql point old-size
) ; slide right
774 (replace new new
:start1
(+ point n-extra-elts
) :start2 point
))
775 (setf (svref new point
) v0
776 (svref new
(+ point
1)) v1
)))
777 (cond ((= n-extra-elts
4)
778 ;; creating a new aux key. SETF immediately follows the data
779 ;; for the primary Name. All other aux-keys go to the end.
780 (let ((point (if (eq aux-key
'setf
) (svref new
0) old-size
)))
781 (insert-at point aux-key
3) ; = add 3 data cells not incl. aux-key
782 (setf (svref new
(+ point
2)) type-number
783 (svref new
(+ point
3)) value
)))
785 (let ((data-start (info-find-aux-key/unpacked
786 aux-key new old-size
)))
787 ;; it had better be found - it was in the packed vector
789 ;; fdefn must be the first piece of info for any name.
790 ;; This facilitates implementing SYMBOL-FUNCTION without
791 ;; without completely decoding the vector.
792 (insert-at (+ data-start
(if (eql type-number
+fdefn-type-num
+)
793 1 (svref new data-start
)))
795 ;; add 2 cells, and verify that re-packing won't
796 ;; overflow the 'count' for this info group.
797 (aver (typep (ash (incf (svref new data-start
) 2) -
1)
799 (packify-infos new
)))
801 ;; Return T if INFO-VECTOR admits quicker insertion logic - it must have
802 ;; exactly one descriptor for the root name, space for >= 1 more field,
804 (declaim (inline info-quickly-insertable-p
))
805 (defun info-quickly-insertable-p (input)
806 (let ((n-infos (packed-info-field input
0 0)))
807 ;; We can easily determine if the no-aux-keys constraint is satisfied,
808 ;; because a secondary name's info occupies at least two cells,
809 ;; one for its aux-key and >= 1 for info values.
810 (and (< n-infos
(1- +infos-per-word
+))
811 (eql n-infos
(1- (length input
))))))
813 ;; Take a packed info-vector INPUT and return a new one with TYPE-NUMBER/VALUE
814 ;; added for the root name. The vector must satisfy INFO-QUICKLY-INSERTABLE-P.
815 ;; This code is separate from PACKED-INFO-INSERT to facilitate writing
816 ;; a unit test of this logic against the complete logic.
818 (defun quick-packed-info-insert (input type-number value
)
819 ;; Because INPUT contains 1 descriptor and its corresponding values,
820 ;; the current length is exactly NEW-N, the new number of fields.
821 (let* ((descriptor (svref input
0))
822 (new-n (truly-the type-number
(length input
)))
823 (new-vect (make-array (1+ new-n
))))
824 ;; Two cases: we're either inserting info for the fdefn, or not.
825 (cond ((eq type-number
+fdefn-type-num
+)
826 ;; fdefn, if present, must remain the first packed field.
827 ;; Replace the lowest field (the count) with +fdefn-type-num+,
828 ;; shift everything left 6 bits, then OR in the new count.
829 (setf (svref new-vect
0)
830 (logior (make-info-descriptor
831 (dpb +fdefn-type-num
+ (byte type-number-bits
0)
832 descriptor
) type-number-bits
) new-n
)
833 ;; Packed vectors are indexed "backwards". The first
834 ;; field's info is in the highest numbered cell.
835 (svref new-vect new-n
) value
)
836 (loop for i from
1 below new-n
837 do
(setf (svref new-vect i
) (svref input i
))))
839 ;; Add a field on the high end and increment the count.
840 (setf (svref new-vect
0)
841 (logior (make-info-descriptor
842 type-number
(* type-number-bits new-n
))
844 (svref new-vect
1) value
)
845 ;; Slide the old data up 1 cell.
846 (loop for i from
2 to new-n
847 do
(setf (svref new-vect i
) (svref input
(1- i
))))))
850 (declaim (maybe-inline packed-info-insert
))
851 (defun packed-info-insert (vector aux-key type-number newval
)
852 (if (and (eql aux-key
+no-auxilliary-key
+)
853 (info-quickly-insertable-p vector
))
854 (quick-packed-info-insert vector type-number newval
)
855 (%packed-info-insert vector aux-key type-number newval
)))
857 ;; Search packed VECTOR for AUX-KEY and TYPE-NUMBER, returning
858 ;; the index of the data if found, or NIL if not found.
860 (declaim (ftype (function (simple-vector (or (eql 0) symbol
) type-number
)
862 packed-info-value-index
))
864 (defun packed-info-value-index (vector aux-key type-num
)
865 (declare (optimize (safety 0))) ; vector bounds are AVERed
866 (let ((data-idx (length vector
)) (descriptor-idx 0) (field-idx 0))
867 (declare (type index descriptor-idx
)
868 (type (mod #.
+infos-per-word
+) field-idx
))
869 (unless (eql aux-key
+no-auxilliary-key
+)
870 (multiple-value-setq (data-idx descriptor-idx field-idx
)
871 (info-find-aux-key/packed vector aux-key
))
873 (return-from packed-info-value-index nil
)))
874 ;; Fetch a descriptor and shift out trailing bits that won't be scanned.
875 (let* ((descriptor (ash (the info-descriptor
(aref vector descriptor-idx
))
876 (* (- type-number-bits
) field-idx
)))
877 (n-infos (logand descriptor info-type-mask
))
878 ;; Compute n things in this descriptor after extracting one field. e.g.
879 ;; if starting index = 2, there's space for 7 more fields in 60 bits.
880 (swath (min (- +infos-per-word
+ field-idx
1) n-infos
)))
881 ;; Type inference on n-infos deems it to have no lower bound due to DECF.
882 (declare (type info-descriptor descriptor
)
883 (type (unsigned-byte #.type-number-bits
) n-infos
)
884 (type index data-idx
))
885 ;; Repeatedly shift and mask, which is quicker than extracting a field at
886 ;; varying positions. Start by shifting out the n-infos field.
887 (setq descriptor
(ash descriptor
(- type-number-bits
)))
890 (when (eql type-num
(logand descriptor info-type-mask
))
891 (return-from packed-info-value-index
892 (the index
(- data-idx j
1))))
893 (setq descriptor
(ash descriptor
(- type-number-bits
))))
894 (when (zerop (decf n-infos swath
))
896 (incf descriptor-idx
)
897 (decf data-idx swath
)
898 (aver (< descriptor-idx data-idx
))
899 (setq descriptor
(svref vector descriptor-idx
)
900 swath
(min n-infos
+infos-per-word
+))))))
902 ;; Helper for CLEAR-INFO-VALUES when Name has the efficient form.
903 ;; Given packed info-vector INPUT and auxilliary key KEY2
904 ;; return a new vector in which TYPE-NUMS are absent.
905 ;; When none of TYPE-NUMs were present to begin with, return NIL.
907 ;; While this could determine whether it would do anything before unpacking,
908 ;; clearing does not happen often enough to warrant the pre-check.
910 (defun packed-info-remove (input key2 type-nums
)
911 (declare (simple-vector input
))
912 (when (or (eql (length input
) (length +nil-packed-infos
+))
913 (and (not (eql key2
+no-auxilliary-key
+))
914 (not (info-find-aux-key/packed input key2
))))
915 (return-from packed-info-remove nil
)) ; do nothing
916 (let* ((end (compute-unpackified-info-size input
))
917 (new (make-array end
))
919 (declare (truly-dynamic-extent new
) (type index end data-start
))
920 (unpackify-infos input new
)
921 (let ((start (info-find-aux-key/unpacked key2 new end
)))
922 (aver start
) ; must be found - it was in the packed vector
923 (setq data-start start
)) ; point to the group's length cell
924 (dolist (type-num type-nums
)
925 (declare (type type-number type-num
))
926 (let ((i (loop for probe from
(1+ data-start
) by
2
927 repeat
(ash (svref new data-start
) -
1) ; =n-entries
928 when
(eql (svref new probe
) type-num
)
930 ;; N.B. the early termination checks aren't just optimizations,
931 ;; they're requirements, because otherwise the loop could smash
932 ;; data that does not belong to this auxilliary key.
933 (cond ((not i
)) ; not found - ignore
934 ;; Squash out 2 cells if deleting an info for primary name,
935 ;; or for secondary name with at least one more entry.
936 ((or (eql data-start
0) (> (svref new data-start
) 3))
937 (replace new new
:start1 i
:start2
(+ i
2))
939 (when (= (decf (svref new data-start
) 2) 1)
940 ;; the group is now comprised solely of its length cell
941 (return))) ; so bail out
943 ;; Delete the sole entry for a secondary name
944 ;; by removing aux-key, length, and one entry (a cell pair).
945 (replace new new
:start1
(- i
2) :start2
(+ i
2))
946 (decf end
4) ; shorten by 4 cells, and stop now
948 (let ((delta (- (length new
) end
)))
949 (cond ((zerop delta
) nil
)
950 ;; All empty info-vectors are equivalent, so if
951 ;; unpacked vector has no data, return a constant.
952 ((eql end
1) +nil-packed-infos
+)
953 (t (packify-infos new end
)))))) ; otherwise repack
955 ;; We need a few magic constants to be shared between the next two functions.
956 (defconstant-eqx !+pcl-reader-name
+ (make-symbol "READER") (constantly t
))
957 (defconstant-eqx !+pcl-writer-name
+ (make-symbol "WRITER") (constantly t
))
958 (defconstant-eqx !+pcl-boundp-name
+ (make-symbol "BOUNDP") (constantly t
))
960 ;; PCL names are physically 4-lists (see "pcl/slot-name")
961 ;; that get treated as 2-component names for globaldb's purposes.
962 ;; Return the kind of PCL slot accessor name that NAME is, if it is one.
963 ;; i.e. it matches (SLOT-ACCESSOR :GLOBAL <sym> READER|WRITER|BOUNDP)
964 ;; When called, NAME is already known to start with 'SLOT-ACCESSOR.
965 ;; This has to be defined before building PCL.
966 (defun pcl-global-accessor-name-p (name)
967 (let* ((cdr (cdr name
)) (tail (cdr cdr
)) last
969 (if (and (eq (car cdr
) :global
)
972 (listp (setq last
(cdr tail
)))
974 ;; Return symbols that can't conflict, in case somebody
975 ;; legitimates (BOUNDP <f>) via DEFINE-FUNCTION-NAME-SYNTAX.
976 ;; Especially important since BOUNDP is an external of CL.
977 (setq kind
(case (car last
)
978 (sb!pcl
::reader
!+pcl-reader-name
+)
979 (sb!pcl
::writer
!+pcl-writer-name
+)
980 (sb!pcl
::boundp
!+pcl-boundp-name
+))))
981 ;; The first return value is what matters to WITH-GLOBALDB-NAME
982 ;; for deciding whether the name is "simple".
983 ;; Return the KIND first, just in case somehow we end up with
984 ;; (SLOT-ACCESSOR :GLOBAL NIL WRITER) as a name.
985 ;; [It "can't happen" since NIL is a constant though]
986 (values kind
(car tail
))
989 ;; Construct a name from its parts.
990 ;; For PCL global accessors, produce the real name, not the 2-part name.
991 ;; This operation is not invoked in normal use of globaldb.
992 ;; It is only for mapping over all names.
993 (defun construct-globaldb-name (aux-symbol stem
)
994 (cond ((eq aux-symbol
!+pcl-reader-name
+) (sb!pcl
::slot-reader-name stem
))
995 ((eq aux-symbol
!+pcl-writer-name
+) (sb!pcl
::slot-writer-name stem
))
996 ((eq aux-symbol
!+pcl-boundp-name
+) (sb!pcl
::slot-boundp-name stem
))
997 (t (list aux-symbol stem
)))) ; something like (SETF frob)
999 ;; Call FUNCTION with each piece of info in packed VECT using ROOT-SYMBOL
1000 ;; as the primary name. FUNCTION must accept 3 values (NAME TYPE-NUMBER VALUE).
1001 (defun %call-with-each-info
(function vect root-symbol
)
1002 (let ((name root-symbol
)
1003 (data-idx (length vect
)))
1004 (declare (type index data-idx
))
1005 (with-packed-info-iterator (next-field vect
:descriptor-index desc-idx
)
1007 (dotimes (i (next-field)) ; number of infos for this name
1008 (funcall function name
(next-field) (svref vect
(decf data-idx
))))
1009 (if (< desc-idx
(decf data-idx
))
1011 (construct-globaldb-name (svref vect data-idx
) root-symbol
))
1015 Info packing example. This example has
2 auxilliary-keys
: SETF and CAS.
1017 (!test-packify-infos
'(13 :XYZ
18 "nine" 28 :BAR
7 T
)
1018 '(SETF 8 NIL
17 :FGX
)
1019 '(CAS 6 :MUMBLE
2 :BAZ
47 :FOO
))
1021 #(109006134805865284 3010 :FOO
:BAZ
:MUMBLE CAS
:FGX NIL SETF T
:BAR
"nine" :XYZ
)
1023 (format nil
"~4,'0o ~20,'0o" 3010 109006134805865284)
1024 => "5702 06032110020734221504"
1025 Reading from right-to-left
, converting each
2-digit octal number to decimal
:
1026 4, 13, 18, 28, 7, 2, 8, 17, 3, 6, 2, 47
1027 Which is interpreted as
:
1028 4 infos for the root name. type numbers
: 13, 18, 28, 7
1029 2 infos for SETF auxilliary-key. type numbers
: 8, 17
1030 3 infos for CAS auxilliary-key. type numbers
: 6, 2, 47
1032 (unpackify-infos (!test-packify-infos ...
)) ; same input
1033 => #(9 13 :XYZ
18 "nine" 28 :BAR
7 T
1034 SETF
5 8 NIL
17 :FGX
1035 CAS
7 6 :MUMBLE
2 :BAZ
47 :FOO
)
1036 This is interpreted as
1037 root name
, 9 cells
: {13-
>:XYZ
, 18-
>"nine", 28-
>:BAR
, 7-
>T
}
1038 SETF
, 5 cells
: {8-
>NIL
, 17-
>:FGX
}
1039 CAS
, 7 cells
: {6-
>:MUMBLE
, 2-
>:BAZ
, 47-
>:FOO
}
1042 ;; Reshape inputs per the above commented example to a packed vector.
1043 ;; The info for a symbol's fdefn must precede other type-numbers.
1044 ;; and SETF must be the first aux key if other aux keys are present.
1045 ;; The test function does not enforce these invariants.
1046 ;; N.B. As this function starts with "!", is is omitted from the target image.
1047 (defun !test-packify-infos
(&rest lists
)
1048 (flet ((check (plist)
1049 (and (evenp (length plist
))
1050 (loop for
(indicator value
) on plist by
#'cddr
1051 always
(typep indicator
'type-number
))))
1052 (add-length-prefix (list) ; computers count better than people
1053 (cons (1+ (length list
)) list
)))
1054 (unless (and (check (first lists
))
1055 (every (lambda (list)
1056 (and (symbolp (car list
)) (check (cdr list
))))
1058 (error "Malformed info entries"))
1060 (coerce (apply #'append
1061 (cons (add-length-prefix (first lists
))
1062 (mapcar (lambda (more)
1064 (add-length-prefix (cdr more
))))
1068 ;; Given a NAME naming a globaldb object, decide whether the NAME has
1069 ;; an efficient or "simple" form, versus a general or "hairy" form.
1070 ;; The efficient form is either a symbol, a (CONS SYMBOL (CONS SYMBOL NULL)),
1071 ;; or a PCL global slot {reader, writer, boundp} function name.
1073 ;; If NAME is "simple", bind KEY2 and KEY1 to the elements
1074 ;; in that order, and execute the SIMPLE code, otherwise execute the HAIRY code.
1075 ;; If ALLOW-ATOM is T - the default - then NAME can be just a symbol
1076 ;; in which case its second component is +NO-AUXILLIARY-KEY+.
1078 (defmacro with-globaldb-name
((key1 key2
&optional
(allow-atom t
))
1079 name
&key simple hairy
)
1080 (with-unique-names (rest)
1081 `(let ((,key1
,name
) (,key2
+NO-AUXILLIARY-KEY
+))
1082 (if (or ,@(if allow-atom
`((symbolp ,key1
)))
1084 (let ((,rest
(cdr ,key1
)))
1086 (cond ((not (cdr ,rest
))
1087 (setq ,key2
(car ,key1
) ,key1
(car ,rest
))
1088 (and (symbolp ,key1
) (symbolp ,key2
) ,rest
))
1089 ((eq (car ,key1
) 'sb
!pcl
::slot-accessor
)
1090 (multiple-value-setq (,key2
,key1
)
1091 (pcl-global-accessor-name-p ,key1
))))))))
1093 ;; The KEYs remain bound, but they should not be used for anything.
1096 ;; Given Info-Vector VECT, return the fdefn that it contains for its root name,
1097 ;; or nil if there is no value. NIL input is acceptable and will return NIL.
1098 (declaim (inline info-vector-fdefn
))
1099 (defun info-vector-fdefn (vect)
1101 ;; This is safe: Info-Vector invariant requires that it have length >= 1.
1102 (let ((word (the fixnum
(svref vect
0))))
1103 ;; Test that the first type-number is +fdefn-type-num+ and its n-infos
1104 ;; field is nonzero. These conditions can be tested simultaneously
1105 ;; using a SIMD-in-a-register idea. The low 6 bits must be nonzero
1106 ;; and the next 6 must be exactly #b111111, so considered together
1107 ;; as a 12-bit unsigned integer it must be >= #b111111000001
1108 (when (>= (ldb (byte (* type-number-bits
2) 0) word
)
1109 (1+ (ash +fdefn-type-num
+ type-number-bits
)))
1110 ;; DATA-REF-WITH-OFFSET doesn't know the info-vector length invariant,
1111 ;; so depite (safety 0) eliding bounds check, FOLD-INDEX-ADDRESSING
1112 ;; wasn't kicking in without (TRULY-THE (INTEGER 1 *)).
1113 (aref vect
(1- (truly-the (integer 1 *) (length vect
))))))))
1115 ;;; Some of this stuff might belong in 'symbol.lisp', but can't be,
1116 ;;; because 'symbol.lisp' is :NOT-HOST in build-order.
1119 ;; SYMBOL-INFO is a primitive object accessor defined in 'objdef.lisp'
1120 ;; and UPDATE-SYMBOL-INFO is defined in 'symbol.lisp'.
1121 ;; But in the host Lisp, there is no such thing as a symbol-info slot,
1122 ;; even if the host is SBCL. Instead, symbol-info is kept in the symbol-plist.
1123 (macrolet ((get-it () '(get symbol
:sb-xc-globaldb-info
)))
1124 (defun symbol-info (symbol) (get-it))
1125 (defun update-symbol-info (symbol update-fn
)
1126 ;; Never pass NIL to an update-fn. Pass the minimal info-vector instead,
1127 ;; a vector describing 0 infos and 0 auxilliary keys.
1128 (let ((newval (funcall update-fn
(or (get-it) +nil-packed-infos
+))))
1130 (setf (get-it) newval
))
1133 ;; Return the globaldb info for SYMBOL. With respect to the state diagram
1134 ;; presented at the definition of SYMBOL-PLIST, if the object in SYMBOL's
1135 ;; info slot is LISTP, it is in state 1 or 3. Either way, take the CDR.
1136 ;; Otherwise, it is in state 2 so return the value as-is.
1137 ;; In terms of this function being named "-vector", implying always a vector,
1138 ;; it is understood that NIL is a proxy for +NIL-PACKED-INFOS+, a vector.
1140 #!-symbol-info-vops
(declaim (inline symbol-info-vector
))
1141 (defun symbol-info-vector (symbol)
1142 (let ((info-holder (symbol-info symbol
)))
1143 (truly-the (or null simple-vector
)
1144 (if (listp info-holder
) (cdr info-holder
) info-holder
))))
1146 ;;; The current *INFO-ENVIRONMENT*, a structure of type INFO-HASHTABLE.
1147 (declaim (type info-hashtable
*info-environment
*))
1148 (defvar *info-environment
*)
1150 ;;; Update the info TYPE-NUMBER for NAME in the global environment,
1151 ;;; setting it to NEW-VALUE. This is thread-safe in the presence
1152 ;;; of multiple readers/writers. Threads simultaneously writing data
1153 ;;; for the same NAME will succeed if the info type numbers differ,
1154 ;;; and the winner is indeterminate if the type numbers are the same.
1156 ;;; It is not usually a good idea to think of globaldb as an arbitrary
1157 ;;; key-to-value map supporting rapid or frequent update for a key.
1158 ;;; This is because each update must shallow-copy any data that existed
1159 ;;; for NAME, as a consequence of the very minimal lockfree protocol.
1161 ;;; If, for example, you wanted to track how many times a full-call to
1162 ;;; each global function was emitted during compilation, you could create
1163 ;;; an info-type (:function :full-calls) and the value of that info-type
1164 ;;; could be a cons cell holding an integer. In this way incrementing
1165 ;;; the cell contents does not affecting the globaldb. In contrast,
1166 ;;; (INCF (INFO :function :full-calls myname)) would perform poorly.
1168 ;;; See also ATOMIC-SET-INFO-VALUE and GET-INFO-VALUE-INITIALIZING
1169 ;;; for atomic read/modify/write operations.
1171 ;;; Return the new value so that this can be conveniently used in a
1173 (defun set-info-value (name type-number new-value
)
1174 (when (typep name
'fixnum
)
1175 (error "~D is not a legal INFO name." name
))
1176 (let ((name (uncross name
)))
1177 ;; If the TYPE-NUMBER already exists in VECT, then copy it and
1178 ;; alter one cell; otherwise unpack it, grow the vector, and repack.
1179 (dx-flet ((augment (vect aux-key
) ; VECT is a packed vector, never NIL
1180 (declare (simple-vector vect
))
1182 (packed-info-value-index vect aux-key type-number
)))
1184 (packed-info-insert vect aux-key type-number new-value
)
1185 (let ((copy (copy-seq vect
)))
1186 (setf (svref copy index
) new-value
)
1188 (with-globaldb-name (key1 key2
) name
1190 ;; UPDATE-SYMBOL-INFO never supplies OLD-INFO as NIL.
1191 (dx-flet ((simple-name (old-info) (augment old-info key2
)))
1192 (update-symbol-info key1
#'simple-name
))
1194 ;; INFO-PUTHASH supplies NIL for OLD-INFO if NAME was absent.
1195 (dx-flet ((hairy-name (old-info)
1196 (augment (or old-info
+nil-packed-infos
+)
1197 +no-auxilliary-key
+)))
1198 (info-puthash *info-environment
* name
#'hairy-name
)))))
1201 ;; Instead of accepting a new-value, call NEW-VALUE-FUN to compute it
1202 ;; from the existing value. The function receives two arguments:
1203 ;; if there was already a value, that value and T; otherwise two NILs.
1204 ;; Return the newly-computed value. If NEW-VALUE-FUN returns the old value
1205 ;; (compared by EQ) when there was one, then no globaldb update is made.
1206 (defun %atomic-set-info-value
(name type-number new-value-fun
)
1207 (declare (function new-value-fun
))
1208 (when (typep name
'fixnum
)
1209 (error "~D is not a legal INFO name." name
))
1210 (let ((name (uncross name
)) new-value
)
1211 (dx-flet ((augment (vect aux-key
) ; VECT is a packed vector, never NIL
1212 (declare (simple-vector vect
))
1214 (packed-info-value-index vect aux-key type-number
)))
1217 vect aux-key type-number
1218 (setq new-value
(funcall new-value-fun nil nil
)))
1219 (let ((oldval (svref vect index
)))
1220 (setq new-value
(funcall new-value-fun oldval t
))
1221 (if (eq new-value oldval
)
1222 vect
; return the old vector
1223 (let ((copy (copy-seq vect
)))
1224 (setf (svref copy index
) new-value
)
1226 (with-globaldb-name (key1 key2
) name
1228 ;; UPDATE-SYMBOL-INFO never supplies OLD-INFO as NIL.
1229 (dx-flet ((simple-name (old-info) (augment old-info key2
)))
1230 (update-symbol-info key1
#'simple-name
))
1232 ;; INFO-PUTHASH supplies NIL for OLD-INFO if NAME was absent.
1233 (dx-flet ((hairy-name (old-info)
1234 (augment (or old-info
+nil-packed-infos
+)
1235 +no-auxilliary-key
+)))
1236 (info-puthash *info-environment
* name
#'hairy-name
))))
1239 ;; %GET-INFO-VALUE-INITIALIZING is provided as a low-level operation similar
1240 ;; to the above because it does not require info metadata for defaulting,
1241 ;; nor deal with the keyword-based info type designators at all.
1242 ;; In contrast, GET-INFO-VALUE requires metadata.
1243 ;; For this operation to make sense, the objects produced should be permanently
1244 ;; assigned to their name, such as are fdefns and classoid-cells.
1245 ;; Note also that we do not do an initial attempt to read once with INFO,
1246 ;; followed up by a double-checking get-or-set operation. It is assumed that
1247 ;; the user of this already did an initial check, if such is warranted.
1248 (defun %get-info-value-initializing
(name type-number creation-thunk
)
1249 (when (typep name
'fixnum
)
1250 (error "~D is not a legal INFO name." name
))
1251 (let ((name (uncross name
))
1253 (dx-flet ((get-or-set (info-vect aux-key
)
1255 (packed-info-value-index info-vect aux-key type-number
)))
1257 (setq result
(svref info-vect index
))
1258 nil
) ; no update to info-vector
1260 ;; Update conflicts possibly for unrelated type-number
1261 ;; can force re-execution. (UNLESS result ...) tries
1262 ;; to avoid calling the thunk more than once.
1264 (setq result
(funcall creation-thunk
)))
1265 (packed-info-insert info-vect aux-key type-number
1267 (with-globaldb-name (key1 key2
) name
1269 ;; UPDATE-SYMBOL-INFO never supplies OLD-INFO as NIL.
1270 (dx-flet ((simple-name (old-info) (get-or-set old-info key2
)))
1271 (update-symbol-info key1
#'simple-name
))
1273 ;; INFO-PUTHASH supplies NIL for OLD-INFO if NAME was absent.
1274 (dx-flet ((hairy-name (old-info)
1275 (or (get-or-set (or old-info
+nil-packed-infos
+)
1276 +no-auxilliary-key
+)
1277 ;; Return OLD-INFO to elide writeback. Unlike for
1278 ;; UPDATE-SYMBOL-INFO, NIL is not a no-op marker.
1280 (info-puthash *info-environment
* name
#'hairy-name
))))