go-brainfuck-yourself

A Brainfuck interpreter that I wrote myself in Go
git clone git@abtrout.com:go-brainfuck-yourself.git
Log | Files | Refs | README

commit 9f20ae3252a41835203ebd2ff83f1aac4d4f86ea
parent ed31b64ea0479f9fcf59a55b7e404998f238352d
Author: david cochran <about.trout@gmail.com>
Date:   Thu, 23 Jul 2026 20:39:38 -0700

remove unnecessary slice allocation from . and ,

we can Read from and Write to bf.cells directly -- no need to allocate
a 1-byte slice every time. the benchmark win here is seen in allocs/op.

```
$ go test -bench=. -benchmem
goos: darwin
goarch: arm64
pkg: github.com/abtrout/gbfy
cpu: Apple M3
BenchmarkLong-8                        1        10477888417 ns/op          37896 B/op         23 allocs/op
BenchmarkMandelbrot-8                  1        12086849000 ns/op         329912 B/op         39 allocs/op
BenchmarkFactor-8                      1        1320697583 ns/op          128120 B/op         30 allocs/op
PASS
ok      github.com/abtrout/gbfy 24.177s
```

Diffstat:
Mbf.go | 10++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/bf.go b/bf.go @@ -66,15 +66,17 @@ func (bf *Brainfuck) eval() error { case '-': bf.cells[bf.d]-- case '.': - if _, err := bf.out.Write([]byte{bf.cells[bf.d]}); err != nil { + if n, err := bf.out.Write(bf.cells[bf.d : bf.d+1]); n == 0 { + return fmt.Errorf("failed to Write ouput: no bytes written") + } else if err != nil { return fmt.Errorf("failed to Write output: %v", err) } case ',': - input := make([]byte, 1) - if _, err := bf.in.Read(input); err != nil { + if n, err := bf.in.Read(bf.cells[bf.d : bf.d+1]); n == 0 { + return fmt.Errorf("failed to Read input: no bytes read") + } else if err != nil { return fmt.Errorf("failed to Read input: %v", err) } - bf.cells[bf.d] = input[0] case '[': if bf.cells[bf.d] == 0 { bf.i = bf.loops[bf.i]