[racket] TR performance bug: removing variable ref slows things down substantially?

From: Neil Toronto (neil.toronto at gmail.com)
Date: Sun Sep 16 00:15:21 EDT 2012

On 09/14/2012 05:19 PM, John Clements wrote:
> Okay, I think I have to start this e-mail with a big wow and thanks. This program runs about 4-5x faster in TR:
>
> #lang typed/racket
>
> (require racket/flonum)
>
> (define f (make-flvector (* 10 44100)))
>
> (define k (* 440.0 1/44100 2 pi))
> (time
>   (for ([j 100])
>     (for ([i (* 10 44100)])
>       (define i#i (exact->inexact i))
>       (void)
>       (flvector-set! f i
>                      (sin (fl* k i#i))))))
>
> This performs 44 million sine computations in about 4 seconds. So.. it's pretty quick.
>
> Here's the weird thing, though. When I comment out the whole flvector-set!, including the computation of the sine, it gets… slower. And performs lots of gc. Here's that program:
>
> #lang typed/racket
>
> (require racket/flonum)
>
> (define f (make-flvector (* 10 44100)))
>
> (define k (* 440.0 1/44100 2 pi))
> (time
>   (for ([j 100])
>     (for ([i (* 10 44100)])
>       (define i#i (exact->inexact i))
>       (void)
>       #;(flvector-set! f i
>                      (sin (fl* k i#i))))))
>
> This one runs in about 4.2 seconds, including almost 2 seconds of GC. And it doesn't compute a single sine!
>
> I'm guessing that the first one somehow triggers an unboxing optimization that eliminates the allocation, but the size of this effect still flabbergasts me. I suppose at the end of the day I may just be asking for some dead code elimination, but it also seems problematic to me that eliminating uses of a variable would ever make the code run more slowly[*].
>
> [*] Okay, I can think of one corner case: if the commented-out use of the variable could be shown to eliminate (through exceptional control flow) some of the possible values taken on by the variable. That can't be happening here, because there's no use of the variable after the commented-out one.

I'm seeing the same thing you are. I get 2500ms with the original 
program with no GC time, and 2100ms with 1800ms GC when I comment out 
the `flvector-set!'.

It gets weirder, though. If I add

   (flvector-set! f i i#i)

after the (void) and comment out the other flvector-set!, it runs really 
fast (250ms). If I add

   (flvector-set! f 0 i#i)

instead, I get 2100ms with 1800ms GC again.

Neil ⊥


Posted on the users mailing list.