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