
Brainfuck Optimization Strategies - nirvanis
http://calmerthanyouare.org/2015/01/07/optimizing-brainfuck.html
======
Gurkenmaster
There's of course also a optimized brainfuck compiler written in sed:
[https://github.com/stedolan/bf.sed](https://github.com/stedolan/bf.sed)

~~~
Exuma
Ugh

~~~
musername
Is that the sound of your chin hitting the floor or air escaping as you tense
up, trying to decipher the code? :)

------
bquinlan
I while ago I wrote a simple Brainfuck interpreter, JIT and compiler
([https://github.com/brianquinlan/brainfuck-
jit](https://github.com/brianquinlan/brainfuck-jit)).

The compiler implements the "Operation offsets" optimization as described in
the article. It is actually pretty straightforward. This function converts a
map of offset => delta to x86-64 instructions:
[https://github.com/brianquinlan/brainfuck-
jit/blob/master/bf...](https://github.com/brianquinlan/brainfuck-
jit/blob/master/bf_compile_and_go.cpp#L175)

and the function following it generates that table.

~~~
icefox
A silly Brainfuck interpreter I wrote one evening converts BF to JavaScript
and runs eval() on it. The result was that I got for free all of the compiler
optimizations and JIT capabilities in the Javascript engines for a trivial
amount of effort. I found the resulting execution faster than the various hand
rolled C interpreters I found online. The best part was that I spent probably
less than an hour implementing and testing my "interpreter".

------
lifthrasiir
I have a (at the initial release, and possibly still) "state-of-the-art"
Brainfuck compiler to C [1], which can compile a "Hello, world" program into a
single `puts` call.

Some ProTip(tm): JITing Brainfuck doesn't fare a lot; for example, resetting
the cell is an O(n) operation in BF, so JIT alone doesn't reduce this into
O(1). Some kind of scalar evolution has to be implemented to reduce this O(n)
factor. More sophiscated liveness and range analysis might be required to go
further (esotope-bfc didn't get there, anyway).

[1] [https://bitbucket.org/lifthrasiir/esotope-
bfc](https://bitbucket.org/lifthrasiir/esotope-bfc)

------
JetSpiegel
[http://calmerthanyouare.org/2014/09/30/brainfuck-
java.html](http://calmerthanyouare.org/2014/09/30/brainfuck-java.html) some of
the issues encountered when writing a brainfuck-to-java compiler in brainfuck

Yo dawg, etc

~~~
JetSpiegel
It gets better

[http://awib.googlecode.com/svn/builds/awib-0.4.b](http://awib.googlecode.com/svn/builds/awib-0.4.b)

Awib is a brainfuck compiler written in brainfuck. It is also polyglot in
bash, Tcl and C

For instance, using gcc, the following will build an executable file called
awib from awib-0.2.b.

    
    
        $ cp awib-0.2.b awib-0.2.c
        $ gcc awib-0.2.c -o awib.tmp
        $ ./awib.tmp < awib-0.2.b > awib-0.2.c
        $ gcc -O2 awib-0.2.c -o awib
    

Using bash works fine, but is very very very slow:

    
    
        $ (echo "@386_linux"; cat awib.b) | bash awib.b > awib
        $ chmod +x awib
    

And tcl:

    
    
        $ (echo "@386_linux"; cat awib.b) | tclsh awib.b > awib
        $ chmod +x awib

------
sp332
As a comparison, what does it look like if you enable GCC optimization?

~~~
matslina
Here's the no-vs-all optimizations graph for gcc with -O3:
[http://imgur.com/Q6yycrT](http://imgur.com/Q6yycrT)

------
debacle
Brainfuck is a really beautiful language. It was the first language I wrote a
parser for, and it's incredibly beautiful in its simplicity.

~~~
SCHiM
What always strikes me (I also wrote a bf parser a while back) is the pay-off
in how simple it is to implement vs how hard it is to write in it. This seems
a very obvious and simple thing to notice: the less tokens/rules your language
uses the easier it is to write a parser for it, but the harder it is to write
a program in that language.

The implications seem more subtle and are more interesting. The very simple
phenomenon implies that application development time later can be saved by
compiler development time now. If so then what is the most optimal
language/compiler? What is the best distribution of time? Is it perhaps so
that there exists a language that is trivial to implement, yet also super
powerful and expressive to program in (don't say lisp here, though it does
almost satisfy the second constraint imo).

~~~
jnbiche
>Is it perhaps so that there exists a language that is trivial to implement,
yet also super powerful and expressive to program in (don't say lisp here,
though it does almost satisfy the second constraint imo).

Lisp. Satisfies both constraints.

But seriously, Forth is another interesting language that strikes a good
balance between ease of implementation vs ease of use and expressive power.
But even more than Lisp, it takes some serious getting used to.

------
primaryobjects
Is there an implementation of this that converts any BF code into C/C# code?
Might make a handy web-based tool.

It would be interesting to apply this to AI generated programs
([http://www.primaryobjects.com/CMS/Article163](http://www.primaryobjects.com/CMS/Article163))

~~~
matslina
Implementations are available in the repository linked to from the article.
Not quite "ready for production", but hopefully a useful starting point.
[https://github.com/matslina/bfoptimization](https://github.com/matslina/bfoptimization)

~~~
primaryobjects
Thanks, I see it. The translation code is inside ir.py. So you can call
ir_to_c(bf_to_ir("+++.")) to do the conversion.

Here is the code to convert BF to C, in case anyone else is interested
[https://gist.github.com/primaryobjects/0ba06f1f6f0d3e984d89](https://gist.github.com/primaryobjects/0ba06f1f6f0d3e984d89)

Here it is running the converted C code
[https://ideone.com/z16KPJ](https://ideone.com/z16KPJ)

Very cool.

------
abandonliberty
Though rather juvenile I appreciated this interpreter that maps instructions
to offensive four letter words.

NSFW text. ff/feckfeck/f* ckf* ck.

[http://web.archive.org/web/20050318095341/http://www.chilliw...](http://web.archive.org/web/20050318095341/http://www.chilliwilli.co.uk/ff/)

[http://web.archive.org/web/20050311055852/http://www.chilliw...](http://web.archive.org/web/20050311055852/http://www.chilliwilli.co.uk/ff/hwcommented.ff)

------
ninjakeyboard
I'm working on implementing the JVM in Brainfuck

------
huhtenberg
Now peg it against [http://mazonka.com/brainf/](http://mazonka.com/brainf/),
the bff4lnr version :)

~~~
matslina
Since bff4lnr is an interpreter, this really becomes apples and oranges. But
for fun, dbfi.b on bff4lnr (at -O3) vs all at -O0 and -O3:
[http://imgur.com/uEEgVr0](http://imgur.com/uEEgVr0) Conclusion: if you want
speed then compile.

~~~
huhtenberg
Ah, my bad. I didn't realize yours was a compiler.

------
Yadi
This sounds cool, but I was not sure till halfway through the post if it's
like an Onion article or not.

------
Fede_V
How long until someone makes a homoiconic version of brainfuck?

------
chucksmart
Who is hiring brainfuck developers these days?

~~~
shultays
I always keep it on my CV, hopefully one day.

------
blt
Beautiful insanity.

------
nemo
Kudos, I guess...

