I took the code from kostyas benchmarks for the Rust Brainfuck interpreter and tried to optimize it. There is also a discussion on Reddit about the poor performance of Rust in the Benchmark.Before my improvements the code needed 16.81s to complete the benchmark and used 6.2Mb of memory, after that the code only needs 4.89s to run but the memory consumption is nearly unchanged.How can I improve the code and maybe reduce memory usage?The file with my changes is also on Github.use std::fs::File;use std::path::Path;use std::io::prelude::*;use std::vec::Vec;use std::io;use std::env;use std::collections::BTreeMap;struct Tape {  pos: usize,  tape: Vec<isize>}impl Tape {  fn new() -> Tape { Tape { pos: 0, tape: vec![0] } }  fn get(&self) -> isize { self.tape[self.pos] }  fn getc(&self) -> u8 { self.get() as u8 }  fn inc(&mut self) { self.tape[self.pos] += 1; }  fn dec(&mut self) { self.tape[self.pos] -= 1; }  fn advance(&mut self) { self.pos += 1; if self.tape.len() <= self.pos { self.tape.push(0) } }  fn devance(&mut self) { if self.pos > 0 { self.pos -= 1; } }}struct Program {  code: Vec<u8>,  bracket_map: BTreeMap<usize, usize>}impl Program {  fn new(content: Vec<u8>) -> Program {    let mut code = Vec::new();    let mut bracket_map = BTreeMap::new();    let mut leftstack = Vec::new();    for (pc, b) in content.iter().filter(|&&x| x == b'+' || x == b'-' || x == b'.' || x == b','            || x == b'<' || x == b'>' || x == b'[' || x == b']').map(|&x| x).enumerate() {      if b == b'[' {        leftstack.push(pc);      } else if b == b']' {        if let Some(left) = leftstack.pop() {          bracket_map.insert(left, pc);          bracket_map.insert(pc, left);        }      }      code.push(b);    }    Program{ code: code, bracket_map: bracket_map }  }  fn run(&self) {    let mut pc: usize = 0;    let mut tape = Tape::new();    let mut stdout = io::stdout();    while pc < self.code.len() {      match self.code[pc] {        b'+' => tape.inc(),        b'-' => tape.dec(),        b'>' => tape.advance(),        b'<' => tape.devance(),        b'[' => { if tape.get() == 0 { pc = self.bracket_map[&pc]; } },        b']' => { if tape.get() != 0 { pc = self.bracket_map[&pc]; } },        b'.' => { stdout.write(&[tape.getc()]).unwrap(); stdout.flush().unwrap() },        _ => unreachable!()      }      pc += 1;    }  }}fn main() {  let mut buf = Vec::new();  {    let arg1 = env::args().nth(1).unwrap();    let path = Path::new(&arg1);    let mut file = File::open(&path).unwrap();    file.read_to_end(&mut buf).unwrap();  }  Program::new(buf).run();}More information: The program is compiled with rustc -C opt-level=3 brainfuck/brainfuck.rs -o brainfuck_rs, the memory usage is determined by a ruby script. C++ performs a lot better regading memory usage it only needs 1.6Mb.Brainfuck rogram used for benchmark: Benchmark brainf*ck program>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++[>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++.I am using Rust nightly rust-nightly-bin-1.2.0_2015.06.06-1 on Arch Linux running on 4 Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz.
Rust Brainfuck interpreter
performance;interpreter;brainfuck;rust;memory optimization