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

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Sat Jun 28 09:56:05 EDT 2014

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


Posted on the dev mailing list.