Using arrays instead of channels for lexer

We have heard many complaining that channels are slow. But when I tried to benchmark this, we get some surprising results. A queue seems slower than a channel?!

The results read:

BenchmarkChan-8         20000000            67.0 ns/op
BenchmarkQueue-8        10000000           172 ns/op
BenchmarkCQueue-8        5000000           307 ns/op
BenchmarkGringo-8       20000000            81.8 ns/op

Maybe someone can take a look at the code to see if I implement these queues optimally. Queue wraps around a Go array that keeps growing indefinitely. CQueue uses the Go array as a circular buffer.

The testing code looks like this:

func BenchmarkChan(b *testing.B) {
	c := make(chan struct{}, 10)
	go func() {
		for i := 0; i < b.N; i++ {
			c <- struct{}{}
	for _ = range c {

Wao! This is incredible. I looked at the code, looks alright. So, channels are not so slow after all. That’s really awesome.

Actually, this is worth a blog post, if you’re interested in writing one.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.