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

From: Sam Tobin-Hochstadt (samth at ccs.neu.edu)
Date: Wed Jun 22 08:31:47 EDT 2011

On Wed, Jun 22, 2011 at 12:03 AM, Danny Yoo <dyoo at cs.wpi.edu> wrote:
> So it looks like a multiple of 4.  It would be nice if that constant
> were smaller, but I'm still impressed that Racket gets even that
> close, given how little time Racket takes to do the actual
> compilation.

Here are some more vague thoughts on things that might improve performance:

First, Matthew would know better, but I worry that `set!' is a
(relatively) slow operation in Racket, compared to PyPy.  In
particular, it's really important for Brainfuck to keep the pointer in
a register, but Racket's compilation strategy will always put it in
the heap.  To fix this, I think you'd need to rearchitect the whole
system to pass the pointer around explicitly.  Given the drastic
change, I'm not sure it will improve performance, but you might try.

Second, your `#%module-begin' doesn't put everything in a function.
I'm suspicious that means that your code outside of loops isn't
jitted, so you might try changing that.

Third, at least one runtime check is generated because `new-state'
isn't inlined.

Fourth, if you put everything in a function, and give it a
distinguished name, then you can use my disassembler [1] to look at
the generated output, and see if there are inefficiencies.

Fifth, try redirecting to /dev/null, or removing output altogether, to
see how much time is being spent there vs in the computation.

[1] https://github.com/samth/disassemble
sam th
samth at ccs.neu.edu

Posted on the users mailing list.