Learning rust and converting my brainfuck interpreter Aug 09, 2015

brainfuck  rust  interpreter 

Rust brainfuck

What is Rust?

Rust is a low level programming language developed by Mozilla with the goal of being fast, thread safe and able to prevent almost all segfaults. It is a very young language, the first release was in may 2015, and contains some interesting concepts like borrowing. and disabled by default mutability (changing values). It was developed as a research project at the same time as servo, a multithreaded browser renderer also in development at Mozilla.

Why did I learn Rust

I made the decision to learn a low level language after reading a book about the design of CPU's. The idea of writing software that gets run directly on the processor, instead of going through a interpreter or compiler/JIT, was something I’d never spent a lot of time on. I had never managed my own memory, designed my own structs, or learned how to use low level threads. The performance was also an incentive, my PHP brainfuck implementation was awfully slow, so I wanted to improve it and see how fast I could make it.

Also because most of the code I wrote at work was PHP, I wanted to diversify my knowlegde a bit. Being a webdeveloper all my life was never a goal. Rust seemed like a good option because it was new, seemed easy enough and had a nice integrated package manager. Downside was that I didn’t find a lot of up to date examples.

Brainfuck

Taking the advice of Kevin Cheers (How to Learn to Code) I wanted to start with a problem I could solve, because the interpreter I wrote in PHP was so slow, I decided to use that as my first project. I spent a couple of days hacking together a prototype, improved it by looking at other peoples code and then ran some benchmarks to see if I could speed up something. I got stuck a while with memory errors, because my data was on stack on not on heap. (hence I learned how to manage my stack and heap).

In the end I am pretty happy with the result, you can find the code here. The difference in time between running PHP vs RUST is about 29min, so my initial goal was made. I will come back later with a more complete benchmark, where I will also compare it with PHP7.


Comments