Skip to content

Commit 7ebe451

Browse files
committed
Cache the number of entries at which the compact index has to grow
Avoiding doing this fractional calculation on every entry addition makes addition about twice as fast
1 parent 16b277f commit 7ebe451

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

srfi/250.sld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
(size hash-table-size hash-table-size-set!)
8989
(next-entry hash-table-next-entry hash-table-next-entry-set!)
9090
(compact-index hash-table-compact-index hash-table-compact-index-set!)
91+
(compact-index-max-fill hash-table-compact-index-max-fill hash-table-compact-index-max-fill-set!)
9192
(keys-vector hash-table-keys-vector hash-table-keys-vector-set!)
9293
(values-vector hash-table-values-vector hash-table-values-vector-set!)
9394
(mutable? hash-table-mutable? hash-table-mutable?-set!))

srfi/250/hash-tables.scm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@
6666
(assertion-violation 'make-hash-table
6767
"size is not an exact nonnegative integer"
6868
k))
69-
(let ((k* (max k 1)))
69+
(let ((k* (max k 1))
70+
(n-buckets (find-nice-n-buckets k)))
7071
(%make-hash-table (comparator-type-test-predicate comparator)
7172
(comparator-hash-function comparator)
7273
(comparator-equality-predicate comparator)
7374
0
7475
0
75-
(make-compact-array (find-nice-n-buckets k))
76+
(make-compact-array n-buckets)
77+
(floor (* n-buckets 2/3))
7678
(make-vector k* *unfilled*)
7779
(make-vector k* *unfilled*)
7880
#t)))))
@@ -214,6 +216,7 @@
214216
;; iterate all of them anyway
215217
(hash-table-prune-dead-entries! ht #t)
216218
(hash-table-compact-index-set! ht new-compact-index)
219+
(hash-table-compact-index-max-fill-set! ht (floor (* new-size 2/3)))
217220
(let loop ((idx 0))
218221
(unless (>= idx (vector-length (hash-table-keys-vector ht)))
219222
(let ((key (vector-ref (hash-table-keys-vector ht) idx)))
@@ -226,8 +229,7 @@
226229
;; #t if the hash table’s compact index has to grow to accommodate the
227230
;; next association added
228231
(define (hash-table-compact-index-must-grow? ht)
229-
(>= (hash-table-next-entry ht)
230-
(floor (* (compact-array-length (hash-table-compact-index ht)) 2/3))))
232+
(>= (hash-table-next-entry ht) (hash-table-compact-index-max-fill ht)))
231233

232234
;; add to the entries arrays, setting the bucket in the compact index
233235
(define (hash-table-add-entry! ht bucket key value)
@@ -630,6 +632,7 @@
630632
(hash-table-size ht)
631633
(hash-table-next-entry ht)
632634
(compact-array-copy (hash-table-compact-index ht))
635+
(hash-table-compact-index-max-fill ht)
633636
(vector-copy (hash-table-keys-vector ht))
634637
(vector-copy (hash-table-values-vector ht))
635638
#t)

srfi/:250/hash-tables.sls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
(mutable size)
8282
(mutable next-entry)
8383
(mutable compact-index)
84+
(mutable compact-index-max-fill)
8485
(mutable keys-vector)
8586
(mutable values-vector)
8687
(mutable mutable?))

srfi/srfi-250.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
(mutable size)
8989
(mutable next-entry)
9090
(mutable compact-index)
91+
(mutable compact-index-max-fill)
9192
(mutable keys-vector)
9293
(mutable values-vector)
9394
(mutable mutable?))

0 commit comments

Comments
 (0)