[racket-dev] Racket peephole opt in lieu of TR's generalized ListDots to usefully type hash

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Jun 29 02:08:06 EDT 2014

How about `(hash-set #hash() k v)`?

(I probably should have been more careful in defining `hash` to allow
it to return a constant when given 0 arguments.)

At Sat, 28 Jun 2014 09:56:05 -0400 (EDT), "J. Ian Johnson" wrote:
> I depend highly on creating singleton hashes in my program (one key maps). I 
> tried a few ways to create them, but alas directly using hash is the best. TR 
> can't type non-nullary applications hash though.
> The times of the following test are the same in TR as in normal Racket, so I'd 
> like to see peephole optimizations that turn the first two into the last one:
> 
> #lang racket
> 
> (define N 5000000)
> (define keys (build-list N (λ (d) (random))))
> (define values (build-list N (λ (d) (random))))
> 
> (define (clean!)
>   (collect-garbage)
>   (collect-garbage)
>   (collect-garbage))
> (clean!)
> (time (for ([k (in-list keys)]
>             [v (in-list values)])
>         (hash-set (hash) k v)))
> ;cpu time: 460 real time: 461 gc time: 13
> 
> (clean!)
> (time (for ([k (in-list keys)]
>             [v (in-list values)])
>         (make-immutable-hash (list (cons k v)))))
> ;cpu time: 507 real time: 506 gc time: 29
> 
> (clean!)
> (time (for ([k (in-list keys)]
>             [v (in-list values)])
>         (hash k v)))
> ;cpu time: 393 real time: 392 gc time: 17
> 
> ;;;;;;;;;;;
> 
> I'd provide a patch, but I don't know where this kind of thing lives in the 
> compiler if it exists at all.
> -Ian
> 
> _________________________
>   Racket Developers list:
>   http://lists.racket-lang.org/dev


Posted on the dev mailing list.