From e82b14b09a2e0f64708b4b8cdc621ee339084c5b Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Tue, 12 Apr 2022 21:20:16 +0200 Subject: [PATCH] bench setup that works --- rust2/Cargo.toml | 4 + rust2/benches/bench.bf | 2 + rust2/{ => benches}/fizzbuzz.bf | 0 rust2/{ => benches}/hello.bf | 0 rust2/benches/loopremove.bf | 4 + rust2/benches/opts.rs | 57 +++++++-- rust2/mandelbrot.bf | 212 ++++++++++++++++++++++++++++++++ rust2/src/ir_interpreter.rs | 27 ++-- rust2/src/lib.rs | 3 + rust2/src/main.rs | 9 +- rust2/src/opts.rs | 3 +- 11 files changed, 302 insertions(+), 19 deletions(-) create mode 100644 rust2/benches/bench.bf rename rust2/{ => benches}/fizzbuzz.bf (100%) rename rust2/{ => benches}/hello.bf (100%) create mode 100644 rust2/benches/loopremove.bf create mode 100644 rust2/mandelbrot.bf diff --git a/rust2/Cargo.toml b/rust2/Cargo.toml index b754480..0826c31 100644 --- a/rust2/Cargo.toml +++ b/rust2/Cargo.toml @@ -11,3 +11,7 @@ bumpalo = { version = "3.9.1", features = ["allocator_api"] } [dev-dependencies] criterion = "0.3.5" insta = "1.14.0" + +[[bench]] +name = "opts" +harness = false \ No newline at end of file diff --git a/rust2/benches/bench.bf b/rust2/benches/bench.bf new file mode 100644 index 0000000..52582c6 --- /dev/null +++ b/rust2/benches/bench.bf @@ -0,0 +1,2 @@ +++++++++[->-[->-[->-[-]<]<]<] +>++++++++[<++++++++++>-]<[>+>+<<-]>-.>-----.> \ No newline at end of file diff --git a/rust2/fizzbuzz.bf b/rust2/benches/fizzbuzz.bf similarity index 100% rename from rust2/fizzbuzz.bf rename to rust2/benches/fizzbuzz.bf diff --git a/rust2/hello.bf b/rust2/benches/hello.bf similarity index 100% rename from rust2/hello.bf rename to rust2/benches/hello.bf diff --git a/rust2/benches/loopremove.bf b/rust2/benches/loopremove.bf new file mode 100644 index 0000000..8c81277 --- /dev/null +++ b/rust2/benches/loopremove.bf @@ -0,0 +1,4 @@ +>+++++[-]++++++++++[[-->+++++++++>+++++++++>+++++++++<<<<->]]>.>.>. +<[-]<[-]<[-] +>>>>+ >>>>> ++++++++++ <<<< <<<<< ++[[>>>>]]+[[++--[+++>>]]>.][-] \ No newline at end of file diff --git a/rust2/benches/opts.rs b/rust2/benches/opts.rs index 2f614ac..806d857 100644 --- a/rust2/benches/opts.rs +++ b/rust2/benches/opts.rs @@ -1,16 +1,55 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use bumpalo::Bump; +use criterion::{criterion_group, criterion_main, Criterion}; +use std::fs; +use std::io::{Read, Write}; +use std::path::PathBuf; -fn fibonacci(n: u64) -> u64 { - match n { - 0 => 1, - 1 => 1, - n => fibonacci(n - 1) + fibonacci(n - 2), +struct MockReadWrite; + +impl Read for MockReadWrite { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + buf.fill(b'A'); + Ok(buf.len()) } } -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("fib 20", |b| b.iter(|| fibonacci(black_box(20)))); +impl Write for MockReadWrite { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + Ok(buf.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } } -criterion_group!(benches, criterion_benchmark); +fn get_bf(path_from_bench: impl AsRef) -> String { + let file = PathBuf::from(file!()) + .parent() + .unwrap() + .join(path_from_bench.as_ref()); + + fs::read_to_string(file).unwrap() +} + +fn run_bf_bench(bf: &str) { + let bump = Bump::new(); + let parsed = brainfuck::parse::parse(&bump, bf.bytes()).unwrap(); + let optimized = brainfuck::opts::optimize(&bump, &parsed); + brainfuck::ir_interpreter::run(&optimized, MockReadWrite, MockReadWrite); +} + +fn optimized(c: &mut Criterion) { + let fizzbuzz = get_bf("fizzbuzz.bf"); + let hello_world = get_bf("hello.bf"); + let bench = get_bf("bench.bf"); + let loopremove = get_bf("loopremove.bf"); + + c.bench_function("fizzbuzz", |b| b.iter(|| run_bf_bench(&fizzbuzz))); + c.bench_function("hello_world", |b| b.iter(|| run_bf_bench(&hello_world))); + c.bench_function("bench", |b| b.iter(|| run_bf_bench(&bench))); + c.bench_function("loopremove", |b| b.iter(|| run_bf_bench(&loopremove))); +} + +criterion_group!(benches, optimized); criterion_main!(benches); diff --git a/rust2/mandelbrot.bf b/rust2/mandelbrot.bf new file mode 100644 index 0000000..3c8c46b --- /dev/null +++ b/rust2/mandelbrot.bf @@ -0,0 +1,212 @@ + A mandelbrot set fractal viewer in brainf*** written by Erik Bosman ++++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++ ++++++++-[[>>>>>>>>>]+[<<<<<<<<<]>>>>>>>>>-]+<<<<<<<<<>>>>>>>>>[>>>>>>>>[ +-]<<<<<<<<>>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>>[-]+<<<<<<<<>+++++<>[<>- +<>[<>-<>>>>>>>>>>+<<<<<<<<<<>]<>>>>>>>>>>]<>>>>>>>>+<<<<<<<<>>>>>>>>>>>> +>>>>>>>>>>>>>>>>>>>>>>>+<<<<<<<<<<<<<<<<<[<<<<<<<<<]>>>[-]+<<<>>>[<<<>>> +>>>>>>[>>>>>>>[-]<<<<<<<>>>>>>>>>]<<<<<<<<<[<<<<<<<<<]>>>>>>>[-]+<<<<<<< +>++++<>[<>-<>[<>-<>>>>>>>>>>+<<<<<<<<<<>]<>>>>>>>>>>]<>>>>>>>+<<<<<<<>++ ++++++<>[<>-<>[<>-<>>>>>>>>>>+<<<<<<<<<<>]<>>>>>>>>>>]<>>>>>>>+<<<<<<<<<< +<<<<<<[<<<<<<<<<]>>>[<<<>>>[-]<<<>>>>>>>>>[>>>>>>>[-<<<<<<<>+<>>>>>>>]<< +<<<<<>[-<>>>>>>>+<<<<<<<>>>>>+<<<<<>>+<<>]<>>>>>>>>>]<<<<<<<<<[<<<<<<<<< +]>>>>>>>>>[>>>>>>>>[-<<<<<<<<>+<>>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<<[<<<<<<<<<]]>>>]<<< \ No newline at end of file diff --git a/rust2/src/ir_interpreter.rs b/rust2/src/ir_interpreter.rs index 043ff40..b8ee88e 100644 --- a/rust2/src/ir_interpreter.rs +++ b/rust2/src/ir_interpreter.rs @@ -6,14 +6,27 @@ const MEM_SIZE: usize = 32_000; type Memory = [Wrapping; MEM_SIZE]; -pub fn run(instrs: &[Stmt<'_>]) { +pub fn run(instrs: &[Stmt<'_>], mut stdout: W, mut stdin: R) +where + W: Write, + R: Read, +{ let mut mem = [Wrapping(0u8); MEM_SIZE]; let mut ptr = 0; - execute(&mut mem, &mut ptr, instrs); + execute(&mut mem, &mut ptr, instrs, &mut stdout, &mut stdin); } -fn execute(mem: &mut Memory, ptr: &mut usize, instrs: &[Stmt<'_>]) { +fn execute( + mem: &mut Memory, + ptr: &mut usize, + instrs: &[Stmt<'_>], + stdout: &mut W, + stdin: &mut R, +) where + W: Write, + R: Read, +{ for instr in instrs { match instr { Stmt::Add(n) => { @@ -38,17 +51,17 @@ fn execute(mem: &mut Memory, ptr: &mut usize, instrs: &[Stmt<'_>]) { } Stmt::Out => { let char = mem[*ptr].0 as char; - print!("{char}"); - std::io::stdout().flush().unwrap(); + write!(stdout, "{char}").unwrap(); + stdout.flush().unwrap(); } Stmt::In => { let mut buf = [0; 1]; - std::io::stdin().read_exact(&mut buf).unwrap(); + stdin.read_exact(&mut buf).unwrap(); mem[*ptr] = Wrapping(buf[0]); } Stmt::Loop(body) => { while mem[*ptr] != Wrapping(0) { - execute(mem, ptr, body); + execute(mem, ptr, body, stdout, stdin); } } Stmt::SetNull => { diff --git a/rust2/src/lib.rs b/rust2/src/lib.rs index d1c6f81..4b53eec 100644 --- a/rust2/src/lib.rs +++ b/rust2/src/lib.rs @@ -1,3 +1,6 @@ +#![feature(allocator_api, let_else)] +#![warn(rust_2018_idioms)] + pub mod ir_interpreter; pub mod opts; pub mod parse; diff --git a/rust2/src/main.rs b/rust2/src/main.rs index dc07926..a3fae2a 100644 --- a/rust2/src/main.rs +++ b/rust2/src/main.rs @@ -2,7 +2,7 @@ #![warn(rust_2018_idioms)] use bumpalo::Bump; -use std::{env, fs, process}; +use std::{env, fs, io, process}; use brainfuck::{ir_interpreter, opts, parse}; @@ -31,5 +31,10 @@ fn main() { drop(parsed); drop(ast_alloc); - ir_interpreter::run(&optimized_ir); + let stdout = io::stdout(); + let stdout = stdout.lock(); + let stdin = io::stdin(); + let stdin = stdin.lock(); + + ir_interpreter::run(&optimized_ir, stdout, stdin); } diff --git a/rust2/src/opts.rs b/rust2/src/opts.rs index b6bf0b9..a41ae95 100644 --- a/rust2/src/opts.rs +++ b/rust2/src/opts.rs @@ -72,8 +72,9 @@ fn pass_find_set_null(ir: &mut Ir<'_>) { for stmt in ir { if let Stmt::Loop(body) = stmt { if let [Stmt::Sub(_)] = body.as_slice() { - println!("REPLACE"); *stmt = Stmt::SetNull; + } else { + pass_find_set_null(body); } } }