d6c7560319f98dffaea2ce53da6e3e92e017830e
[sbcl.git] / src / code / target-hash-table.lisp
1 ;;;; that part of the implementation of HASH-TABLE which lives solely
2 ;;;; on the target system, not on the cross-compilation host
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!IMPL")
14 \f
15 ;;;; utilities
16
17 ;; This stuff is performance critical and unwind-protect is too
18 ;; slow. And without the locking the next vector can get cyclic
19 ;; causing looping in a WITHOUT-GCING form, SHRINK-VECTOR can corrupt
20 ;; memory and who knows what else.
21 (defmacro with-spinlock-and-without-gcing ((spinlock) &body body)
22   #!-sb-thread
23   (declare (ignore spinlock))
24   (with-unique-names (old-gc-inhibit old-interrupts-enabled)
25     `(let ((,old-gc-inhibit *gc-inhibit*)
26            (,old-interrupts-enabled *interrupts-enabled*)
27            (*interrupts-enabled* nil)
28            (*gc-inhibit* t))
29        (unwind-protect
30             (progn
31               #!+sb-thread
32               (sb!thread::get-spinlock ,spinlock)
33               ,@body)
34          #!+sb-thread
35          (sb!thread::release-spinlock ,spinlock)
36          (let ((*interrupts-enabled* ,old-interrupts-enabled)
37                (*gc-inhibit* ,old-gc-inhibit))
38            ;; the test is racy, but it can err only on the overeager
39            ;; side
40            (sb!kernel::maybe-handle-pending-gc))))))
41
42 (eval-when (:compile-toplevel :load-toplevel :execute)
43   (defconstant max-hash sb!xc:most-positive-fixnum))
44
45 (deftype hash ()
46   `(integer 0 ,max-hash))
47
48 ;;; FIXME: Does this always make a nonnegative FIXNUM? If so, then
49 ;;; explain why. If not (or if the reason it always makes a
50 ;;; nonnegative FIXNUM is only the accident that pointers in supported
51 ;;; architectures happen to be in the lower half of the address
52 ;;; space), then fix it.
53 #!-sb-fluid (declaim (inline pointer-hash))
54 (defun pointer-hash (key)
55   (declare (values hash))
56   (truly-the hash (%primitive sb!c:make-fixnum key)))
57
58 #!-sb-fluid (declaim (inline eq-hash))
59 (defun eq-hash (key)
60   (declare (values hash (member t nil)))
61   (values (pointer-hash key)
62           (oddp (get-lisp-obj-address key))))
63
64 #!-sb-fluid (declaim (inline equal-hash))
65 (defun equal-hash (key)
66   (declare (values hash (member t nil)))
67   (typecase key
68     ;; For some types the definition of EQUAL implies a special hash
69     ((or string cons number bit-vector pathname)
70      (values (sxhash key) nil))
71     ;; Otherwise use an EQ hash, rather than SXHASH, since the values
72     ;; of SXHASH will be extremely badly distributed due to the
73     ;; requirements of the spec fitting badly with our implementation
74     ;; strategy.
75     (t
76      (eq-hash key))))
77
78 #!-sb-fluid (declaim (inline eql-hash))
79 (defun eql-hash (key)
80   (declare (values hash (member t nil)))
81   (if (numberp key)
82       (equal-hash key)
83       (eq-hash key)))
84
85 (defun equalp-hash (key)
86   (declare (values hash (member t nil)))
87   (values (psxhash key) nil))
88
89 (defun almost-primify (num)
90   (declare (type index num))
91   #!+sb-doc
92   "Return an almost prime number greater than or equal to NUM."
93   (if (= (rem num 2) 0)
94       (setq num (+ 1 num)))
95   (if (= (rem num 3) 0)
96       (setq num (+ 2 num)))
97   (if (= (rem num 7) 0)
98       (setq num (+ 4 num)))
99   num)
100 \f
101 ;;;; user-defined hash table tests
102
103 (defvar *hash-table-tests* nil)
104
105 (defun define-hash-table-test (name test-fun hash-fun)
106   #!+sb-doc
107   "Define a new kind of hash table test."
108   (declare (type symbol name)
109            (type function test-fun hash-fun))
110   (setf *hash-table-tests*
111         (cons (list name test-fun hash-fun)
112               (remove name *hash-table-tests* :test #'eq :key #'car)))
113   name)
114 \f
115 ;;;; construction and simple accessors
116
117 (defconstant +min-hash-table-size+ 16)
118 (defconstant +min-hash-table-rehash-threshold+ (float 1/16 1.0))
119
120 (defun make-hash-table (&key (test 'eql)
121                         (size +min-hash-table-size+)
122                         (rehash-size 1.5)
123                         (rehash-threshold 1)
124                         (weakness nil))
125   #!+sb-doc
126   "Create and return a new hash table. The keywords are as follows:
127      :TEST -- Indicates what kind of test to use.
128      :SIZE -- A hint as to how many elements will be put in this hash
129        table.
130      :REHASH-SIZE -- Indicates how to expand the table when it fills up.
131        If an integer, add space for that many elements. If a floating
132        point number (which must be greater than 1.0), multiply the size
133        by that amount.
134      :REHASH-THRESHOLD -- Indicates how dense the table can become before
135        forcing a rehash. Can be any positive number <=1, with density
136        approaching zero as the threshold approaches 0. Density 1 means an
137        average of one entry per bucket.
138      :WEAKNESS -- IF NIL (the default) it is a normal non-weak hash table.
139        If one of :KEY, :VALUE, :KEY-AND-VALUE, :KEY-OR-VALUE it is a weak
140        hash table.
141        Depending on the type of weakness the lack of references to the
142        key and the value may allow for removal of the entry. If WEAKNESS
143        is :KEY and the key would otherwise be garbage the entry is eligible
144        for removal from the hash table. Similarly, if WEAKNESS is :VALUE
145        the life of an entry depends on its value's references. If WEAKNESS
146        is :KEY-AND-VALUE and either the key or the value would otherwise be
147        garbage the entry can be removed. If WEAKNESS is :KEY-OR-VALUE and
148        both the key and the value would otherwise be garbage the entry can
149        be removed."
150   (declare (type (or function symbol) test))
151   (declare (type unsigned-byte size))
152   (multiple-value-bind (test test-fun hash-fun)
153       (cond ((or (eq test #'eq) (eq test 'eq))
154              (values 'eq #'eq #'eq-hash))
155             ((or (eq test #'eql) (eq test 'eql))
156              (values 'eql #'eql #'eql-hash))
157             ((or (eq test #'equal) (eq test 'equal))
158              (values 'equal #'equal #'equal-hash))
159             ((or (eq test #'equalp) (eq test 'equalp))
160              (values 'equalp #'equalp #'equalp-hash))
161             (t
162              ;; FIXME: I'd like to remove *HASH-TABLE-TESTS* stuff.
163              ;; Failing that, I'd like to rename it to
164              ;; *USER-HASH-TABLE-TESTS*.
165              (dolist (info *hash-table-tests*
166                       (error "unknown :TEST for MAKE-HASH-TABLE: ~S"
167                              test))
168                (destructuring-bind (test-name test-fun hash-fun) info
169                  (when (or (eq test test-name) (eq test test-fun))
170                    (return (values test-name test-fun hash-fun)))))))
171     (let* ((size (max +min-hash-table-size+
172                       (min size
173                            ;; SIZE is just a hint, so if the user asks
174                            ;; for a SIZE which'd be too big for us to
175                            ;; easily implement, we bump it down.
176                            (floor array-dimension-limit 1024))))
177            (rehash-size (if (integerp rehash-size)
178                             rehash-size
179                             (float rehash-size 1.0)))
180            ;; FIXME: Original REHASH-THRESHOLD default should be 1.0,
181            ;; not 1, to make it easier for the compiler to avoid
182            ;; boxing.
183            (rehash-threshold (max +min-hash-table-rehash-threshold+
184                                   (float rehash-threshold 1.0)))
185            (size+1 (1+ size))       ; The first element is not usable.
186            ;; KLUDGE: The most natural way of expressing the below is
187            ;; (round (/ (float size+1) rehash-threshold)), and indeed
188            ;; it was expressed like that until 0.7.0. However,
189            ;; MAKE-HASH-TABLE is called very early in cold-init, and
190            ;; the SPARC has no primitive instructions for rounding,
191            ;; but only for truncating; therefore, we fudge this issue
192            ;; a little. The other uses of truncate, below, similarly
193            ;; used to be round. -- CSR, 2002-10-01
194            ;;
195            ;; Note that this has not yet been audited for
196            ;; correctness. It just seems to work. -- CSR, 2002-11-02
197            (scaled-size (truncate (/ (float size+1) rehash-threshold)))
198            (length (almost-primify (max scaled-size
199                                         (1+ +min-hash-table-size+))))
200            (index-vector (make-array length
201                                      :element-type
202                                      '(unsigned-byte #.sb!vm:n-word-bits)
203                                      :initial-element 0))
204            ;; Needs to be the half the length of the KV vector to link
205            ;; KV entries - mapped to indeces at 2i and 2i+1 -
206            ;; together.
207            (next-vector (make-array size+1
208                                     :element-type
209                                     '(unsigned-byte #.sb!vm:n-word-bits)))
210            (kv-vector (make-array (* 2 size+1)
211                                   :initial-element +empty-ht-slot+))
212            (table (%make-hash-table
213                    :test test
214                    :test-fun test-fun
215                    :hash-fun hash-fun
216                    :rehash-size rehash-size
217                    :rehash-threshold rehash-threshold
218                    :rehash-trigger size
219                    :table kv-vector
220                    :weakness weakness
221                    :index-vector index-vector
222                    :next-vector next-vector
223                    :hash-vector
224                    (unless (eq test 'eq)
225                      (make-array size+1
226                                  :element-type '(unsigned-byte
227                                                  #.sb!vm:n-word-bits)
228                                  :initial-element +magic-hash-vector-value+))
229                    :spinlock (sb!thread::make-spinlock))))
230       (declare (type index size+1 scaled-size length))
231       ;; Set up the free list, all free. These lists are 0 terminated.
232       (do ((i 1 (1+ i)))
233           ((>= i size))
234         (setf (aref next-vector i) (1+ i)))
235       (setf (aref next-vector size) 0)
236       (setf (hash-table-next-free-kv table) 1)
237       (setf (hash-table-needing-rehash table) 0)
238       (setf (aref kv-vector 0) table)
239       table)))
240
241 (defun hash-table-count (hash-table)
242   #!+sb-doc
243   "Return the number of entries in the given HASH-TABLE."
244   (declare (type hash-table hash-table)
245            (values index))
246   (hash-table-number-entries hash-table))
247
248 #!+sb-doc
249 (setf (fdocumentation 'hash-table-rehash-size 'function)
250       "Return the rehash-size HASH-TABLE was created with.")
251
252 #!+sb-doc
253 (setf (fdocumentation 'hash-table-rehash-threshold 'function)
254       "Return the rehash-threshold HASH-TABLE was created with.")
255
256 (defun hash-table-size (hash-table)
257   #!+sb-doc
258   "Return a size that can be used with MAKE-HASH-TABLE to create a hash
259    table that can hold however many entries HASH-TABLE can hold without
260    having to be grown."
261   (hash-table-rehash-trigger hash-table))
262
263 #!+sb-doc
264 (setf (fdocumentation 'hash-table-test 'function)
265       "Return the test HASH-TABLE was created with.")
266
267 #!+sb-doc
268 (setf (fdocumentation 'hash-table-weakness 'function)
269       "Return the WEAKNESS of HASH-TABLE which is one of NIL, :KEY,
270 :VALUE, :KEY-AND-VALUE, :KEY-OR-VALUE.")
271 \f
272 ;;;; accessing functions
273
274 ;;; Make new vectors for the table, extending the table based on the
275 ;;; rehash-size.
276 (defun rehash (table)
277   (declare (type hash-table table))
278   (let* ((old-kv-vector (hash-table-table table))
279          (old-next-vector (hash-table-next-vector table))
280          (old-hash-vector (hash-table-hash-vector table))
281          (old-size (length old-next-vector))
282          (new-size
283           (let ((rehash-size (hash-table-rehash-size table)))
284             (etypecase rehash-size
285               (fixnum
286                (+ rehash-size old-size))
287               (float
288                (the index (truncate (* rehash-size old-size)))))))
289          (new-kv-vector (make-array (* 2 new-size)
290                                     :initial-element +empty-ht-slot+))
291          (new-next-vector
292           (make-array new-size
293                       :element-type '(unsigned-byte #.sb!vm:n-word-bits)
294                       :initial-element 0))
295          (new-hash-vector
296           (when old-hash-vector
297             (make-array new-size
298                         :element-type '(unsigned-byte #.sb!vm:n-word-bits)
299                         :initial-element +magic-hash-vector-value+)))
300          (old-index-vector (hash-table-index-vector table))
301          (new-length (almost-primify
302                       (truncate (/ (float new-size)
303                                    (hash-table-rehash-threshold table)))))
304          (new-index-vector
305           (make-array new-length
306                       :element-type '(unsigned-byte #.sb!vm:n-word-bits)
307                       :initial-element 0)))
308     (declare (type index new-size new-length old-size))
309
310     ;; Disable GC tricks on the OLD-KV-VECTOR.
311     (set-header-data old-kv-vector sb!vm:vector-normal-subtype)
312
313     ;; Non-empty weak hash tables always need GC support.
314     (when (and (hash-table-weakness table) (plusp (hash-table-count table)))
315       (set-header-data new-kv-vector sb!vm:vector-valid-hashing-subtype))
316
317     ;; FIXME: here and in several other places in the hash table code,
318     ;; loops like this one are used when FILL or REPLACE would be
319     ;; appropriate.  why are standard CL functions not used?
320     ;; Performance issues?  General laziness?  -- NJF, 2004-03-10
321
322     ;; Copy over the kv-vector. The element positions should not move
323     ;; in case there are active scans.
324     (dotimes (i (* old-size 2))
325       (declare (type index i))
326       (setf (aref new-kv-vector i) (aref old-kv-vector i)))
327
328     ;; Copy over the hash-vector.
329     (when old-hash-vector
330       (dotimes (i old-size)
331         (setf (aref new-hash-vector i) (aref old-hash-vector i))))
332
333     (setf (hash-table-next-free-kv table) 0)
334     (setf (hash-table-needing-rehash table) 0)
335     ;; Rehash all the entries; last to first so that after the pushes
336     ;; the chains are first to last.
337     (do ((i (1- new-size) (1- i)))
338         ((zerop i))
339       (declare (type index/2 i))
340       (let ((key (aref new-kv-vector (* 2 i)))
341             (value (aref new-kv-vector (1+ (* 2 i)))))
342         (cond ((and (eq key +empty-ht-slot+)
343                     (eq value +empty-ht-slot+))
344                ;; Slot is empty, push it onto the free list.
345                (setf (aref new-next-vector i)
346                      (hash-table-next-free-kv table))
347                (setf (hash-table-next-free-kv table) i))
348               ((and new-hash-vector
349                     (not (= (aref new-hash-vector i)
350                             +magic-hash-vector-value+)))
351                ;; Can use the existing hash value (not EQ based)
352                (let* ((hashing (aref new-hash-vector i))
353                       (index (rem hashing new-length))
354                       (next (aref new-index-vector index)))
355                  (declare (type index index)
356                           (type hash hashing))
357                  ;; Push this slot into the next chain.
358                  (setf (aref new-next-vector i) next)
359                  (setf (aref new-index-vector index) i)))
360               (t
361                ;; EQ base hash.
362                ;; Enable GC tricks.
363                (set-header-data new-kv-vector
364                                 sb!vm:vector-valid-hashing-subtype)
365                (let* ((hashing (pointer-hash key))
366                       (index (rem hashing new-length))
367                       (next (aref new-index-vector index)))
368                  (declare (type index index)
369                           (type hash hashing))
370                  ;; Push this slot onto the next chain.
371                  (setf (aref new-next-vector i) next)
372                  (setf (aref new-index-vector index) i))))))
373     (setf (hash-table-table table) new-kv-vector)
374     (setf (hash-table-index-vector table) new-index-vector)
375     (setf (hash-table-next-vector table) new-next-vector)
376     (setf (hash-table-hash-vector table) new-hash-vector)
377     ;; Shrink the old vectors to 0 size to help the conservative GC.
378     (%shrink-vector old-kv-vector 0)
379     (%shrink-vector old-index-vector 0)
380     (%shrink-vector old-next-vector 0)
381     (when old-hash-vector
382       (%shrink-vector old-hash-vector 0))
383     (setf (hash-table-rehash-trigger table) new-size))
384   (values))
385
386 ;;; Use the same size as before, re-using the vectors.
387 (defun rehash-without-growing (table)
388   (declare (type hash-table table))
389   (let* ((kv-vector (hash-table-table table))
390          (next-vector (hash-table-next-vector table))
391          (hash-vector (hash-table-hash-vector table))
392          (size (length next-vector))
393          (index-vector (hash-table-index-vector table))
394          (length (length index-vector)))
395     (declare (type index size length))
396
397     ;; Non-empty weak hash tables always need GC support.
398     (unless (and (hash-table-weakness table) (plusp (hash-table-count table)))
399       ;; Disable GC tricks, they will be re-enabled during the re-hash
400       ;; if necessary.
401       (set-header-data kv-vector sb!vm:vector-normal-subtype))
402
403     ;; Rehash all the entries.
404     (setf (hash-table-next-free-kv table) 0)
405     (setf (hash-table-needing-rehash table) 0)
406     (dotimes (i size)
407       (setf (aref next-vector i) 0))
408     (dotimes (i length)
409       (setf (aref index-vector i) 0))
410     (do ((i (1- size) (1- i)))
411         ((zerop i))
412       (declare (type index/2 i))
413       (let ((key (aref kv-vector (* 2 i)))
414             (value (aref kv-vector (1+ (* 2 i)))))
415         (cond ((and (eq key +empty-ht-slot+)
416                     (eq value +empty-ht-slot+))
417                ;; Slot is empty, push it onto free list.
418                (setf (aref next-vector i) (hash-table-next-free-kv table))
419                (setf (hash-table-next-free-kv table) i))
420               ((and hash-vector (not (= (aref hash-vector i)
421                                         +magic-hash-vector-value+)))
422                ;; Can use the existing hash value (not EQ based)
423                (let* ((hashing (aref hash-vector i))
424                       (index (rem hashing length))
425                       (next (aref index-vector index)))
426                  (declare (type index index))
427                  ;; Push this slot into the next chain.
428                  (setf (aref next-vector i) next)
429                  (setf (aref index-vector index) i)))
430               (t
431                ;; EQ base hash.
432                ;; Enable GC tricks.
433                (set-header-data kv-vector sb!vm:vector-valid-hashing-subtype)
434                (let* ((hashing (pointer-hash key))
435                       (index (rem hashing length))
436                       (next (aref index-vector index)))
437                  (declare (type index index)
438                           (type hash hashing))
439                  ;; Push this slot into the next chain.
440                  (setf (aref next-vector i) next)
441                  (setf (aref index-vector index) i)))))))
442   (values))
443
444 (defun flush-needing-rehash (table)
445   (let* ((kv-vector (hash-table-table table))
446          (index-vector (hash-table-index-vector table))
447          (next-vector (hash-table-next-vector table))
448          (length (length index-vector)))
449     (do ((next (hash-table-needing-rehash table)))
450         ((zerop next))
451       (declare (type index/2 next))
452       (let* ((key (aref kv-vector (* 2 next)))
453              (hashing (pointer-hash key))
454              (index (rem hashing length))
455              (temp (aref next-vector next)))
456         (setf (aref next-vector next) (aref index-vector index))
457         (setf (aref index-vector index) next)
458         (setf next temp))))
459   (setf (hash-table-needing-rehash table) 0)
460   (values))
461
462 (defun gethash (key hash-table &optional default)
463   #!+sb-doc
464   "Finds the entry in HASH-TABLE whose key is KEY and returns the associated
465    value and T as multiple values, or returns DEFAULT and NIL if there is no
466    such entry. Entries can be added using SETF."
467   (declare (type hash-table hash-table)
468            (values t (member t nil)))
469   (gethash3 key hash-table default))
470
471 (defun gethash2 (key hash-table)
472   #!+sb-doc
473   "Two argument version of GETHASH"
474   (declare (type hash-table hash-table)
475            (values t (member t nil)))
476   (gethash3 key hash-table nil))
477
478 (defun gethash3 (key hash-table default)
479   #!+sb-doc
480   "Three argument version of GETHASH"
481   (declare (type hash-table hash-table)
482            (values t (member t nil)))
483   (with-spinlock-and-without-gcing ((hash-table-spinlock hash-table))
484    (cond ((= (get-header-data (hash-table-table hash-table))
485              sb!vm:vector-must-rehash-subtype)
486           (rehash-without-growing hash-table))
487          ((not (zerop (hash-table-needing-rehash hash-table)))
488           (flush-needing-rehash hash-table)))
489
490    ;; First check the cache.  Use EQ here for speed.
491    (let ((cache (hash-table-cache hash-table))
492          (table (hash-table-table hash-table)))
493
494      (if (and cache (< cache (length table)) (eq (aref table cache) key))
495          (values (aref table (1+ cache)) t)
496
497        ;; Search for key in the hash table.
498        (multiple-value-bind (hashing eq-based)
499            (funcall (hash-table-hash-fun hash-table) key)
500          (declare (type hash hashing))
501          (let* ((index-vector (hash-table-index-vector hash-table))
502                 (length (length index-vector))
503                 (index (rem hashing length))
504                 (next (aref index-vector index))
505                 (next-vector (hash-table-next-vector hash-table))
506                 (hash-vector (hash-table-hash-vector hash-table))
507                 (test-fun (hash-table-test-fun hash-table)))
508            (declare (type index index))
509            ;; Search next-vector chain for a matching key.
510            (if (or eq-based (not hash-vector))
511                (do ((next next (aref next-vector next)))
512                    ((zerop next) (values default nil))
513                  (declare (type index/2 next))
514                  (when (eq key (aref table (* 2 next)))
515                    (setf (hash-table-cache hash-table) (* 2 next))
516                    (return (values (aref table (1+ (* 2 next))) t))))
517              (do ((next next (aref next-vector next)))
518                  ((zerop next) (values default nil))
519                (declare (type index/2 next))
520                (when (and (= hashing (aref hash-vector next))
521                           (funcall test-fun key (aref table (* 2 next))))
522                  ;; Found.
523                  (setf (hash-table-cache hash-table) (* 2 next))
524                  (return (values (aref table (1+ (* 2 next))) t)))))))))))
525
526 ;;; so people can call #'(SETF GETHASH)
527 (defun (setf gethash) (new-value key table &optional default)
528   (declare (ignore default))
529   (%puthash key table new-value))
530
531 (defun %puthash (key hash-table value)
532   (declare (type hash-table hash-table))
533   (aver (hash-table-index-vector hash-table))
534   (with-spinlock-and-without-gcing ((hash-table-spinlock hash-table))
535    ;; We need to rehash here so that a current key can be found if it
536    ;; exists. Check that there is room for one more entry. May not be
537    ;; needed if the key is already present.
538    (cond ((zerop (hash-table-next-free-kv hash-table))
539           (rehash hash-table))
540          ((= (get-header-data (hash-table-table hash-table))
541              sb!vm:vector-must-rehash-subtype)
542           (rehash-without-growing hash-table))
543          ((not (zerop (hash-table-needing-rehash hash-table)))
544           (flush-needing-rehash hash-table)))
545
546    (let ((cache (hash-table-cache hash-table))
547          (kv-vector (hash-table-table hash-table)))
548
549      ;; Check the cache
550      (if (and cache (< cache (length kv-vector))
551               (eq (aref kv-vector cache) key))
552          ;; If cached, just store here
553          (setf (aref kv-vector (1+ cache)) value)
554
555        ;; Search for key in the hash table.
556        (multiple-value-bind (hashing eq-based)
557            (funcall (hash-table-hash-fun hash-table) key)
558          (declare (type hash hashing))
559          (let* ((index-vector (hash-table-index-vector hash-table))
560                 (length (length index-vector))
561                 (index (rem hashing length))
562                 (next (aref index-vector index))
563                 (kv-vector (hash-table-table hash-table))
564                 (next-vector (hash-table-next-vector hash-table))
565                 (hash-vector (hash-table-hash-vector hash-table))
566                 (test-fun (hash-table-test-fun hash-table)))
567            (declare (type index index next))
568            (when (hash-table-weakness hash-table)
569              (set-header-data kv-vector sb!vm:vector-valid-hashing-subtype))
570            (cond ((or eq-based (not hash-vector))
571                   (when eq-based
572                     (set-header-data kv-vector
573                                      sb!vm:vector-valid-hashing-subtype))
574
575                   ;; Search next-vector chain for a matching key.
576                   (do ((next next (aref next-vector next)))
577                       ((zerop next))
578                     (declare (type index/2 next))
579                     (when (eq key (aref kv-vector (* 2 next)))
580                       ;; Found, just replace the value.
581                       (setf (hash-table-cache hash-table) (* 2 next))
582                       (setf (aref kv-vector (1+ (* 2 next))) value)
583                       (return-from %puthash value))))
584                  (t
585                   ;; Search next-vector chain for a matching key.
586                   (do ((next next (aref next-vector next)))
587                       ((zerop next))
588                     (declare (type index/2 next))
589                     (when (and (= hashing (aref hash-vector next))
590                                (funcall test-fun key
591                                         (aref kv-vector (* 2 next))))
592                       ;; Found, just replace the value.
593                       (setf (hash-table-cache hash-table) (* 2 next))
594                       (setf (aref kv-vector (1+ (* 2 next))) value)
595                       (return-from %puthash value)))))
596
597            ;; Pop a KV slot off the free list
598            (let ((free-kv-slot (hash-table-next-free-kv hash-table)))
599              (declare (type index/2 free-kv-slot))
600              ;; Double-check for overflow.
601              (aver (not (zerop free-kv-slot)))
602              (setf (hash-table-next-free-kv hash-table)
603                    (aref next-vector free-kv-slot))
604              (incf (hash-table-number-entries hash-table))
605
606              (setf (hash-table-cache hash-table) (* 2 free-kv-slot))
607              (setf (aref kv-vector (* 2 free-kv-slot)) key)
608              (setf (aref kv-vector (1+ (* 2 free-kv-slot))) value)
609
610              ;; Setup the hash-vector if necessary.
611              (when hash-vector
612                (if (not eq-based)
613                    (setf (aref hash-vector free-kv-slot) hashing)
614                    (aver (= (aref hash-vector free-kv-slot)
615                             +magic-hash-vector-value+))))
616
617              ;; Push this slot into the next chain.
618              (setf (aref next-vector free-kv-slot) next)
619              (setf (aref index-vector index) free-kv-slot)))))))
620   value)
621
622 (defun remhash (key hash-table)
623   #!+sb-doc
624   "Remove the entry in HASH-TABLE associated with KEY. Return T if there
625    was such an entry, or NIL if not."
626   (declare (type hash-table hash-table)
627            (values (member t nil)))
628   (with-spinlock-and-without-gcing ((hash-table-spinlock hash-table))
629    ;; We need to rehash here so that a current key can be found if it
630    ;; exists.
631    (cond ((= (get-header-data (hash-table-table hash-table))
632              sb!vm:vector-must-rehash-subtype)
633           (rehash-without-growing hash-table))
634          ((not (zerop (hash-table-needing-rehash hash-table)))
635           (flush-needing-rehash hash-table)))
636
637    ;; For now, just clear the cache
638    (setf (hash-table-cache hash-table) nil)
639
640    ;; Search for key in the hash table.
641    (multiple-value-bind (hashing eq-based)
642        (funcall (hash-table-hash-fun hash-table) key)
643      (declare (type hash hashing))
644      (let* ((index-vector (hash-table-index-vector hash-table))
645             (length (length index-vector))
646             (index (rem hashing length))
647             (next (aref index-vector index))
648             (table (hash-table-table hash-table))
649             (next-vector (hash-table-next-vector hash-table))
650             (hash-vector (hash-table-hash-vector hash-table))
651             (test-fun (hash-table-test-fun hash-table)))
652        (declare (type index index)
653                 (type index/2 next))
654        (flet ((clear-slot (chain-vector prior-slot-location slot-location)
655                 (declare (type index/2 slot-location))
656                 ;; Mark slot as empty.
657                 (setf (aref table (* 2 slot-location)) +empty-ht-slot+
658                       (aref table (1+ (* 2 slot-location))) +empty-ht-slot+)
659                 ;; Update the prior pointer in the chain to skip this.
660                 (setf (aref chain-vector prior-slot-location)
661                       (aref next-vector slot-location))
662                 ;; Push KV slot onto free chain.
663                 (setf (aref next-vector slot-location)
664                       (hash-table-next-free-kv hash-table))
665                 (setf (hash-table-next-free-kv hash-table) slot-location)
666                 (when hash-vector
667                   (setf (aref hash-vector slot-location)
668                         +magic-hash-vector-value+))
669                 (decf (hash-table-number-entries hash-table))
670                 t))
671          (cond ((zerop next)
672                 nil)
673                ((if (or eq-based (not hash-vector))
674                     (eq key (aref table (* 2 next)))
675                     (and (= hashing (aref hash-vector next))
676                          (funcall test-fun key (aref table (* 2 next)))))
677                 (clear-slot index-vector index next))
678                ;; Search next-vector chain for a matching key.
679                ((or eq-based (not hash-vector))
680                 ;; EQ based
681                 (do ((prior next next)
682                      (next (aref next-vector next) (aref next-vector next)))
683                     ((zerop next) nil)
684                   (declare (type index next))
685                   (when (eq key (aref table (* 2 next)))
686                     (return-from remhash (clear-slot next-vector prior next)))))
687                (t
688                 ;; not EQ based
689                 (do ((prior next next)
690                      (next (aref next-vector next) (aref next-vector next)))
691                     ((zerop next) nil)
692                   (declare (type index/2 next))
693                   (when (and (= hashing (aref hash-vector next))
694                              (funcall test-fun key (aref table (* 2 next))))
695                     (return-from remhash
696                       (clear-slot next-vector prior next)))))))))))
697
698 (defun clrhash (hash-table)
699   #!+sb-doc
700   "This removes all the entries from HASH-TABLE and returns the hash table
701    itself."
702   (declare (optimize speed))
703   (with-spinlock-and-without-gcing ((hash-table-spinlock hash-table))
704     (let* ((kv-vector (hash-table-table hash-table))
705            (next-vector (hash-table-next-vector hash-table))
706            (hash-vector (hash-table-hash-vector hash-table))
707            (size (length next-vector))
708            (index-vector (hash-table-index-vector hash-table)))
709       ;; Disable GC tricks.
710       (set-header-data kv-vector sb!vm:vector-normal-subtype)
711       ;; Mark all slots as empty by setting all keys and values to magic
712       ;; tag.
713       (aver (eq (aref kv-vector 0) hash-table))
714       (fill kv-vector +empty-ht-slot+ :start 2)
715       ;; Set up the free list, all free.
716       (do ((i 1 (1+ i)))
717           ((>= i (1- size)))
718         (setf (aref next-vector i) (1+ i)))
719       (setf (aref next-vector (1- size)) 0)
720       (setf (hash-table-next-free-kv hash-table) 1)
721       (setf (hash-table-needing-rehash hash-table) 0)
722       ;; Clear the index-vector.
723       (fill index-vector 0)
724       ;; Clear the hash-vector.
725       (when hash-vector
726         (fill hash-vector +magic-hash-vector-value+)))
727     (setf (hash-table-cache hash-table) nil)
728     (setf (hash-table-number-entries hash-table) 0))
729   hash-table)
730 \f
731 ;;;; MAPHASH
732
733 ;;; FIXME: This should be made into a compiler transform for two reasons:
734 ;;;   1. It would then be available for compiling the entire system,
735 ;;;      not only parts of the system which are defined after DEFUN MAPHASH.
736 ;;;   2. It could be conditional on compilation policy, so that
737 ;;;      it could be compiled as a full call instead of an inline
738 ;;;      expansion when SPACE>SPEED.
739 (declaim (inline maphash))
740 (defun maphash (function-designator hash-table)
741   #!+sb-doc
742   "For each entry in HASH-TABLE, call the designated two-argument function on
743 the key and value of the entry. Return NIL."
744   ;; This essentially duplicates WITH-HASH-TABLE-ITERATOR, so
745   ;; any changes here should be reflected there as well.
746   (let ((fun (%coerce-callable-to-fun function-designator))
747         (size (length (hash-table-next-vector hash-table))))
748     (declare (type function fun))
749     (do ((i 1 (1+ i)))
750         ((>= i size))
751       (declare (type index/2 i))
752       (let* ((kv-vector (hash-table-table hash-table))
753              (key (aref kv-vector (* 2 i)))
754              (value (aref kv-vector (1+ (* 2 i)))))
755         ;; We are running without locking or WITHOUT-GCING. For a weak
756         ;; :VALUE hash table it's possible that the GC hit after KEY
757         ;; was read and now the entry is gone. So check if either the
758         ;; key or the value is empty.
759         (unless (or (eq key +empty-ht-slot+)
760                     (eq value +empty-ht-slot+))
761           (funcall fun key value))))))
762 \f
763 ;;;; methods on HASH-TABLE
764
765 ;;; Return a list of keyword args and values to use for MAKE-HASH-TABLE
766 ;;; when reconstructing HASH-TABLE.
767 (defun %hash-table-ctor-args (hash-table)
768   `(:test             ',(hash-table-test             hash-table)
769     :size             ',(hash-table-size             hash-table)
770     :rehash-size      ',(hash-table-rehash-size      hash-table)
771     :rehash-threshold ',(hash-table-rehash-threshold hash-table)
772     :weakness         ',(hash-table-weakness         hash-table)))
773
774 ;;; Return an association list representing the same data as HASH-TABLE.
775 (defun %hash-table-alist (hash-table)
776   (let ((result nil))
777     (maphash (lambda (key value)
778                (push (cons key value) result))
779              hash-table)
780     result))
781
782 ;;; Stuff an association list into HASH-TABLE. Return the hash table,
783 ;;; so that we can use this for the *PRINT-READABLY* case in
784 ;;; PRINT-OBJECT (HASH-TABLE T) without having to worry about LET
785 ;;; forms and readable gensyms and stuff.
786 (defun %stuff-hash-table (hash-table alist)
787   (dolist (x alist)
788     (setf (gethash (car x) hash-table) (cdr x)))
789   hash-table)
790
791 (def!method print-object ((hash-table hash-table) stream)
792   (declare (type stream stream))
793   (cond ((or (not *print-readably*) (not *read-eval*))
794          (print-unreadable-object (hash-table stream :type t :identity t)
795            (format stream
796                    ":TEST ~S :COUNT ~S"
797                    (hash-table-test hash-table)
798                    (hash-table-count hash-table))))
799         (t
800          (with-standard-io-syntax
801           (format stream
802                   "#.~W"
803                   `(%stuff-hash-table (make-hash-table ,@(%hash-table-ctor-args
804                                                           hash-table))
805                                      ',(%hash-table-alist hash-table)))))))
806
807 (def!method make-load-form ((hash-table hash-table) &optional environment)
808   (declare (ignore environment))
809   (values `(make-hash-table ,@(%hash-table-ctor-args hash-table))
810           `(%stuff-hash-table ,hash-table ',(%hash-table-alist hash-table))))