Skip to content

Commit 1a2c72b

Browse files
committed
Further small optimizations
1 parent fd1e24b commit 1a2c72b

File tree

2 files changed

+55
-39
lines changed

2 files changed

+55
-39
lines changed

srfi/250/hash-tables.scm

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,21 @@
116116
((hash-table-type-test-function ht) obj))
117117

118118
(define (%hash-table-bucket-for ht hash key)
119-
(let loop ((hash hash))
120-
;;(display hash) (newline)
121-
(let* ((bucket
122-
(modulo hash
123-
(compact-array-length (hash-table-compact-index ht))))
124-
(entry-idx
125-
(compact-array-ref (hash-table-compact-index ht) bucket)))
126-
(if entry-idx
127-
(let ((found-key
128-
(vector-ref (hash-table-keys-vector ht) entry-idx)))
129-
(if (and (not (deletion? found-key))
130-
(or (unfilled? found-key)
131-
(hash-table-same? ht key found-key)))
132-
bucket
133-
(loop (+ hash 1))))
134-
bucket))))
119+
(let ((n-buckets (compact-array-length (hash-table-compact-index ht))))
120+
(let loop ((hash hash))
121+
;;(display hash) (newline)
122+
(let* ((bucket (modulo hash n-buckets))
123+
(entry-idx
124+
(compact-array-ref (hash-table-compact-index ht) bucket)))
125+
(if entry-idx
126+
(let ((found-key
127+
(vector-ref (hash-table-keys-vector ht) entry-idx)))
128+
(if (and (not (deletion? found-key))
129+
(or (unfilled? found-key)
130+
(hash-table-same? ht key found-key)))
131+
bucket
132+
(loop (+ hash 1))))
133+
bucket)))))
135134

136135
(define (hash-table-bucket-for-key ht key)
137136
(%hash-table-bucket-for ht (hash-table-hash ht key) key))
@@ -158,7 +157,7 @@
158157
(define (hash-table-prune-dead-entries! ht fast?)
159158
;; NB only set fast? to #t if you are going to be rehashing all
160159
;; entries anyway!
161-
(unless (= (hash-table-size ht) (hash-table-next-entry ht))
160+
(unless (eqv? (hash-table-size ht) (hash-table-next-entry ht))
162161
(let loop ((from-idx 0)
163162
(to-idx 0))
164163
;;(display from-idx) (newline) (display to-idx) (newline) (newline)
@@ -178,7 +177,7 @@
178177
(compact-array-delete! (hash-table-compact-index ht)
179178
(vector-ref (hash-table-values-vector ht) from-idx)))
180179
(loop (+ from-idx 1) to-idx))
181-
((= from-idx to-idx) (loop (+ from-idx 1) (+ to-idx 1)))
180+
((eqv? from-idx to-idx) (loop (+ from-idx 1) (+ to-idx 1)))
182181
(else
183182
(vector-set! (hash-table-keys-vector ht)
184183
to-idx
@@ -409,7 +408,7 @@
409408
entry-idx *deletion*)
410409
(vector-set! (hash-table-values-vector ht)
411410
entry-idx bucket)
412-
(when (= entry-idx (- (hash-table-next-entry ht) 1))
411+
(when (eqv? entry-idx (- (hash-table-next-entry ht) 1))
413412
(hash-table-prune-dead-entries-at-end! ht))
414413
#t)
415414
#f)))
Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
(define (make-compact-array size)
22
(cond ((< size #xFF)
3-
(let ((bv (make-bytevector (+ size 1) #xFF)))
3+
(let ((bv (make-bytevector (+ size 2) #xFF)))
44
(bytevector-u8-set! bv 0 1)
5+
(bytevector-u8-set! bv 1 size)
56
bv))
67
((< size #xFFFF)
7-
(let ((bv (make-bytevector (* 2 (+ size 1)) #xFF)))
8+
(let ((bv (make-bytevector (* 2 (+ size 2)) #xFF)))
89
(bytevector-u8-set! bv 0 2)
10+
(bytevector-u16-native-set! bv 2 size)
911
bv))
1012
((< size #xFFFFFFFF)
11-
(let ((bv (make-bytevector (* 4 (+ size 1)) #xFF)))
13+
(let ((bv (make-bytevector (* 4 (+ size 2)) #xFF)))
1214
(bytevector-u8-set! bv 0 4)
15+
(bytevector-u32-native-set! bv 4 size)
1316
bv))
1417
(else
15-
(let ((bv (make-bytevector (* 8 (+ size 1)) #xFF)))
18+
(let ((bv (make-bytevector (* 8 (+ size 2)) #xFF)))
1619
(bytevector-u8-set! bv 0 8)
20+
(bytevector-u64-native-set! bv 8 size)
1721
bv))))
1822

1923
(define (compact-array-ref sa idx)
20-
(define (max-to x n) (if (= x n) #f x))
24+
(define (max-to x n) (if (eqv? x n) #f x))
2125
(assert (bytevector? sa))
2226
(case (bytevector-u8-ref sa 0)
23-
((1) (max-to (bytevector-u8-ref sa (+ idx 1)) #xFF))
24-
((2) (max-to (bytevector-u16-native-ref sa (* (+ idx 1) 2)) #xFFFF))
25-
((4) (max-to (bytevector-u32-native-ref sa (* (+ idx 1) 4)) #xFFFFFFFF))
26-
((8) (max-to (bytevector-u64-native-ref sa (* (+ idx 1) 8)) #xFFFFFFFFFFFFFFFF))
27+
((1) (max-to (bytevector-u8-ref sa (+ idx 2)) #xFF))
28+
((2) (max-to (bytevector-u16-native-ref sa (* (+ idx 2) 2)) #xFFFF))
29+
((4) (max-to (bytevector-u32-native-ref sa (* (+ idx 2) 4)) #xFFFFFFFF))
30+
((8) (max-to (bytevector-u64-native-ref sa (* (+ idx 2) 8)) #xFFFFFFFFFFFFFFFF))
2731
(else (assertion-violation 'compact-array-ref "not a compact array" sa))))
2832

2933
(define (compact-array-set? sa idx)
@@ -34,29 +38,42 @@
3438
(if (>= val n) (assertion-violation 'compact-array-set! "can't store value in compact array, try upgrading it")))
3539
(assert (bytevector? sa))
3640
(case (bytevector-u8-ref sa 0)
37-
((1) (check-size #xFF) (bytevector-u8-set! sa (+ idx 1) val))
38-
((2) (check-size #xFFFF) (bytevector-u16-native-set! sa (* (+ idx 1) 2) val))
39-
((4) (check-size #xFFFFFFFF) (bytevector-u32-native-set! sa (* (+ idx 1) 4) val))
40-
((8) (check-size #xFFFFFFFFFFFFFFFF) (bytevector-u64-native-set! sa (* (+ idx 1) 8) val))
41+
((1) (check-size #xFF) (bytevector-u8-set! sa (+ idx 2) val))
42+
((2) (check-size #xFFFF) (bytevector-u16-native-set! sa (* (+ idx 2) 2) val))
43+
((4) (check-size #xFFFFFFFF) (bytevector-u32-native-set! sa (* (+ idx 2) 4) val))
44+
((8) (check-size #xFFFFFFFFFFFFFFFF) (bytevector-u64-native-set! sa (* (+ idx 2) 8) val))
4145
(else (assertion-violation 'compact-array-set! "not a compact array" sa))))
4246

4347
(define (compact-array-delete! sa idx)
4448
(assert (bytevector? sa))
4549
(case (bytevector-u8-ref sa 0)
46-
((1) (bytevector-u8-set! sa (+ idx 1) #xFF))
47-
((2) (bytevector-u16-native-set! sa (* (+ idx 1) 2) #xFFFF))
48-
((4) (bytevector-u32-native-set! sa (* (+ idx 1) 4) #xFFFFFFFF))
49-
((8) (bytevector-u64-native-set! sa (* (+ idx 1) 8) #xFFFFFFFFFFFFFFFF))
50+
((1) (bytevector-u8-set! sa (+ idx 2) #xFF))
51+
((2) (bytevector-u16-native-set! sa (* (+ idx 2) 2) #xFFFF))
52+
((4) (bytevector-u32-native-set! sa (* (+ idx 2) 4) #xFFFFFFFF))
53+
((8) (bytevector-u64-native-set! sa (* (+ idx 2) 8) #xFFFFFFFFFFFFFFFF))
5054
(else (assertion-violation 'compact-array-delete! "not a compact array" sa))))
5155

5256
(define (compact-array-clear! sa)
5357
(assert (bytevector? sa))
54-
(let ((size (bytevector-u8-ref sa 0)))
58+
(let ((elt-size (bytevector-u8-ref sa 0)))
5559
(bytevector-fill! sa #xFF)
56-
(bytevector-u8-set! sa 0 size)))
60+
(bytevector-u8-set! sa 0 elt-size)
61+
(case elt-size
62+
((1) (bytevector-u8-set! sa 1 (- 2 (bytevector-length sa))))
63+
((2) (bytevector-u16-native-set! sa 2
64+
(- 2 (div (bytevector-length sa) 2))))
65+
((4) (bytevector-u32-native-set! sa 4
66+
(- 2 (div (bytevector-length sa) 4))))
67+
((8) (bytevector-u32-native-set! sa 8
68+
(- 2 (div (bytevector-length sa) 8)))))))
5769

5870
(define (compact-array-copy sa) (bytevector-copy sa))
5971

6072
(define (compact-array-length sa)
6173
(assert (bytevector? sa))
62-
(- (div (bytevector-length sa) (bytevector-u8-ref sa 0)) 1))
74+
(case (bytevector-u8-ref sa 0)
75+
((1) (bytevector-u8-ref sa 1))
76+
((2) (bytevector-u16-native-ref sa 2))
77+
((4) (bytevector-u32-native-ref sa 4))
78+
((8) (bytevector-u64-native-ref sa 8))
79+
(else (assertion-violation 'compact-array-ref "not a compact array" sa))))

0 commit comments

Comments
 (0)