[racket] racket 5.4 syntax tweak wishlist

From: Neil Toronto (neil.toronto at gmail.com)
Date: Mon Sep 10 11:57:18 EDT 2012

On 09/10/2012 03:58 AM, Erich Rast wrote:
> I agree with that one. Regarding this I also have a question. Sometimes
> I just need one of multiple values, but this always bothers me somewhat.
> Take for example
> (let-values (([a b c] (values 1 2 3)))
>    (computation-with a))
> Are the spurious bindings to b and c optimized away if they aren't used?
> Is this even possible in general?

Matthew pushed a change to the optimizer a little while ago that 
transforms your example into

   (let-values ([(a) 1] [(b) 2] [(c) 3]))
     (computation-with a))

If `b' and `c' are unused and the optimizer can prove their values are 
side-effect-free, they'll be optimized away. They are in your example, 
according to the timings output by this program:

#lang racket

(require racket/unsafe/ops)

(define (make-x) 4)
(define x (make-x))   ; keeps the optimizer from inlining x
(define bx (box #f))  ; mutated to make sure optimizer leaves `+' alone

(for ([_  (in-range 5)])
   (time (for ([_  (in-range 10000000)])
           (let-values ([(a b c)  (values 1 2 3)])
             (unsafe-set-box! bx (+ a x))))))

(for ([_  (in-range 5)])
   (time (for ([_  (in-range 10000000)])
           (let ([a 1])
             (unsafe-set-box! bx (+ a x))))))

I don't know how smart the optimizer is about proving computations are 
side-effect-free. It's probably limited to arithmetic and a few other 
basic primitives.

Neil ⊥

Posted on the users mailing list.