Skip to content

Commit

Permalink
cs: fix iteration on copy of a mutable hash table
Browse files Browse the repository at this point in the history
Incorrect initialization of the copy's iteration state for a hash
table meant that table sizes larger than 32 looked like tables of only
32 elements when iterating (including when printing).

Closes #3344
  • Loading branch information
mflatt committed Aug 9, 2020
1 parent 4f02901 commit 543dab5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 21 additions & 0 deletions pkgs/racket-test-core/tests/racket/hash.rktl
Expand Up @@ -577,6 +577,27 @@
(test 1 hash-count ht2)
(test #t eq? (car (hash-keys ht2)) g))

;; ----------------------------------------
;; Regression test to make sure all elements are still
;; reachable by iteration in a copy of a mutable hash table:

(let ([ht (make-hash)])
(for ([i 113])
(hash-set! ht i 1))

(define new-ht (hash-copy ht))

(test (hash-count ht) hash-count new-ht)
(test (for/sum ([k (in-hash-keys ht)])
k)
'sum
(for/sum ([k (in-hash-keys new-ht)])
k))
(test (hash-count ht)
'count
(for/sum ([v (in-hash-values new-ht)])
v)))

;; ----------------------------------------

(report-errs)
6 changes: 3 additions & 3 deletions racket/src/cs/rumble/hash.ss
Expand Up @@ -14,8 +14,8 @@
(define-record eq-mutable-hash mutable-hash
())

(define (create-mutable-hash ht kind) (make-mutable-hash (make-lock kind) #f #f ht))
(define (create-eq-mutable-hash ht) (make-eq-mutable-hash (make-lock 'eq?) #f #f ht))
(define (create-mutable-hash ht kind) (make-mutable-hash (make-lock kind) #f #t ht))
(define (create-eq-mutable-hash ht) (make-eq-mutable-hash (make-lock 'eq?) #f #t ht))

(define (mutable-hash-lock ht) (locked-iterable-hash-lock ht))
(define (mutable-hash-cells ht) (locked-iterable-hash-cells ht))
Expand Down Expand Up @@ -654,7 +654,7 @@
0)
32))])
(let ([len (#%vector-length new-vec)])
(when (fx= len (hash-count ht))<
(when (fx= len (hash-count ht))
(set-locked-iterable-hash-retry?! ht #f)))
(let ([vec (cells-merge vec new-vec)])
(set-locked-iterable-hash-cells! ht vec)
Expand Down

1 comment on commit 543dab5

@lojic
Copy link
Contributor

@lojic lojic commented on 543dab5 Aug 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mflatt was the < character on hash.ss:657 just an extraneous typo ?

Please sign in to comment.