[racket] Tutorial: writing a language (brainf*ck) in Racket

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Mon Jun 20 17:37:49 EDT 2011

>> You'd want to do the following:
>>  - add some bounds checking in `increment-ptr' and `decrement-ptr'.
>>  - change the `vector' operations to their `unsafe-vector' counterparts.
>>  - change `sub1' and `add1' to use `unsafe-fx+' and `unsafe-fx-'
>>  - change `=' to `unsafe-fx='
>>  - change the uses of `set-state-ptr!' and `state-ptr' and such to use
>> `unsafe-struct-set!' and `unsafe-struct-ref'.
>>
>> All of those will almost certainly improve performance.
>
> Yup, this is done now.


I'd want to report the effect of these optimizations.


Under the old compiler (<= 1.5), I was seeing the following:

    fermi ~/work/bf/examples $ raco make prime.rkt && (echo 100 | time
racket prime.rkt)
    Primes up to: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67
71 73 79 83 89 97
    37.65user 0.64system 0:38.54elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
    0inputs+0outputs (0major+10287minor)pagefaults 0swaps


Under the new compiler (1.7), I'm now seeing:

    fermi ~/work/bf/examples $ raco make prime.rkt && (echo 100 | time
racket prime.rkt)
    Primes up to: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67
71 73 79 83 89 97
    1.15user 0.06system 0:01.32elapsed 91%CPU (0avgtext+0avgdata 0maxresident)k
    0inputs+0outputs (0major+10366minor)pagefaults 0swaps


For comparison, here are the numbers I'm seeing from the PyPy tutorial
example1.py (using pypy b590cf6de419):

    fermi ~/local/pypy-tutorial $ echo 100 | time
~/local/pypy/bin/pypy example1.py prime.b
    Primes up to: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67
71 73 79 83 89 97
    16.72user 0.24system 0:17.18elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k
    0inputs+0outputs (0major+3554minor)pagefaults 0swaps


So, yes, my original implementation was a bit slower than the PyPy
one, but I can blame the slowdown to a few simply factors that are
purely accidental, not intrinsic.  The combination of  the simple
optimizations that Sam suggested, as well as replacing the runtime
parameterization with syntax-parameterize, results in about a 30x
speedup.



Posted on the users mailing list.