Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What about buffered channels? #1

Closed
im-kulikov opened this issue Feb 11, 2018 · 17 comments
Closed

What about buffered channels? #1

im-kulikov opened this issue Feb 11, 2018 · 17 comments

Comments

@im-kulikov
Copy link

im-kulikov commented Feb 11, 2018

Mb I do something wrong, but... what about buffered channels?

about this mac 2018-02-11 14-47-46

# MacBook Pro (15-inch, 2017)
# go version go1.9.4 darwin/amd64
goos: darwin
goarch: amd64
pkg: github.com/tidwall/fastlane
# ch := make(chan int, 5)
BenchmarkFastlaneChan-8         20000000                66.7 ns/op            17 B/op          1 allocs/op
BenchmarkGoChan-8               20000000               102 ns/op               0 B/op          0 allocs/op
# ch := make(chan int, 10)
BenchmarkFastlaneChan-8         20000000                66.1 ns/op            17 B/op          1 allocs/op
BenchmarkGoChan-8               20000000                86.0 ns/op             0 B/op          0 allocs/op
# ch := make(chan int, 50)
BenchmarkFastlaneChan-8         20000000                67.3 ns/op            17 B/op          1 allocs/op
BenchmarkGoChan-8               20000000                68.0 ns/op             0 B/op          0 allocs/op
# ch := make(chan int, 100)
BenchmarkFastlaneChan-8         20000000                65.3 ns/op            17 B/op          1 allocs/op
BenchmarkGoChan-8               20000000                66.6 ns/op             0 B/op          0 allocs/op
# ch := make(chan int, 500)
BenchmarkFastlaneChan-8         20000000                63.9 ns/op            17 B/op          1 allocs/op
BenchmarkGoChan-8               20000000                66.6 ns/op             0 B/op          0 allocs/op
# go 1.10rc2
# ch := make(chan int, 100)
BenchmarkFastlaneChan-8         20000000                71.1 ns/op            16 B/op          1 allocs/op
BenchmarkGoChan-8               20000000                70.9 ns/op             0 B/op          0 allocs/op

Looks like not so quick, like in your benchmarks...

@tidwall
Copy link
Owner

tidwall commented Feb 11, 2018

Thanks for the input.
Buffered Go channels are faster than non buffered in general.

@tidwall tidwall closed this as completed Feb 11, 2018
@tidwall
Copy link
Owner

tidwall commented Feb 11, 2018

Also I'll explore extending the benchmarks.

@im-kulikov
Copy link
Author

I'll do this issue only for inform 🙂
I apologize for the possible confusion

@tidwall
Copy link
Owner

tidwall commented Feb 11, 2018

No worries at all. I really appreciate your feedback!!

This is a brand new project and there's still a lot to improve upon.

One thing to consider is that a fastlane channel uses an interface{} for it's value and in the benchmarks the Go channel uses an make(chan int). When changed to make(chan interface{}) the Go channels slow down quite a bit. And when I change the base value type for fastlane channels from interface{} to int, they speed up a lot.

I'm considering creating a generator instead of requiring interface{}.

@tidwall
Copy link
Owner

tidwall commented Feb 11, 2018

So it’s much faster now. Check out the new benchmarks.

@im-kulikov
Copy link
Author

@tidwall good job!

fastlane-channel: 1000000 ops in 54.11713ms (18478437/sec)
go-channel(100):  1000000 ops in 66.659071ms (15001709/sec)
go-channel(10):   1000000 ops in 90.502487ms (11049420/sec)
go-channel(0):    1000000 ops in 184.488879ms (5420380/sec)
goos: darwin
goarch: amd64
pkg: github.com/tidwall/fastlane
BenchmarkFastlaneChan-8         30000000                51.0 ns/op            10 B/op          0 allocs/op
BenchmarkGoChan100-8            20000000                66.5 ns/op             0 B/op          0 allocs/op
BenchmarkGoChan10-8             20000000                85.6 ns/op             0 B/op          0 allocs/op
BenchmarkGoChanUnbuffered-8     10000000               185 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/tidwall/fastlane     7.236s

but.. I would add ReportAlloc()

like this:

func BenchmarkFastlaneChan(b *testing.B) {
	b.ResetTimer()
	b.ReportAllocs()
	benchmarkFastlaneChan(b.N, false)
}

func BenchmarkGoChan100(b *testing.B) {
	b.ResetTimer()
	b.ReportAllocs()
	benchmarkGoChan(b.N, 100, false)
}

func BenchmarkGoChan10(b *testing.B) {
	b.ResetTimer()
	b.ReportAllocs()
	benchmarkGoChan(b.N, 10, false)
}

func BenchmarkGoChanUnbuffered(b *testing.B) {
	b.ResetTimer()
	b.ReportAllocs()
	benchmarkGoChan(b.N, 0, false)
}

@im-kulikov
Copy link
Author

im-kulikov commented Feb 11, 2018

@tidwall If you do not mind, I would like to make PR

@tidwall
Copy link
Owner

tidwall commented Feb 11, 2018

Super sorry, but I didn't see you last comment, and I pushed a change that includes ReportAllocs along with an optimization to keep the memory down.

@im-kulikov
Copy link
Author

nothing to worry about.. the main thing that is done 🙂

@im-kulikov
Copy link
Author

goos: darwin
goarch: amd64
pkg: github.com/tidwall/fastlane
BenchmarkFastlaneChan-8         30000000                49.2 ns/op             0 B/op          0 allocs/op
BenchmarkGoChan100-8            20000000                66.7 ns/op             0 B/op          0 allocs/op
BenchmarkGoChan10-8             20000000                85.2 ns/op             0 B/op          0 allocs/op
BenchmarkGoChanUnbuffered-8     10000000               182 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/tidwall/fastlane     6.734s

Oh... it's cool! you fix B/op 👍🏻

@im-kulikov
Copy link
Author

and with go1.10rc2 it's ≈1.5 faster than go-chan 🙂

# go version
# go version go1.10rc2 darwin/amd64
fastlane-channel: 1000000 ops in 43.944193ms (22756135/sec)
go-channel(100):  1000000 ops in 71.747656ms (13937737/sec)
go-channel(10):   1000000 ops in 91.059684ms (10981808/sec)
go-channel(0):    1000000 ops in 191.414302ms (5224270/sec)
goos: darwin
goarch: amd64
BenchmarkFastlaneChan-8         30000000                48.7 ns/op             0 B/op          0 allocs/op
BenchmarkGoChan100-8            20000000                70.7 ns/op             0 B/op          0 allocs/op
BenchmarkGoChan10-8             20000000                91.8 ns/op             0 B/op          0 allocs/op
BenchmarkGoChanUnbuffered-8     10000000               189 ns/op               0 B/op          0 allocs/op
PASS
ok      _/Users/kulikov/.golang/src/github.com/tidwall/fastlane 7.422s

@im-kulikov
Copy link
Author

im-kulikov commented Feb 17, 2018

@tidwall something strange with Go 1.10 and latest changes (Run from GoLand):

# with -race -count 1
goos: darwin
goarch: amd64
pkg: github.com/tidwall/fastlane
1000000	      1724 ns/op	       0 B/op	       0 allocs/op
3000000	       506 ns/op	       0 B/op	       0 allocs/op
2000000	       643 ns/op	       0 B/op	       0 allocs/op
2000000	       820 ns/op	       0 B/op	       0 allocs/op
PASS
coverage: 33.3% of statements

Run from command-line

→ go test -test.bench .  -race -v -count 1 ./...
=== RUN   TestFastlaneChan
fastlane-channel: 1000000 ops in 531.007676ms (1883211/sec)
go-channel(100):  1000000 ops in 384.519695ms (2600647/sec)
go-channel(10):   1000000 ops in 432.52048ms (2312029/sec)
go-channel(0):    1000000 ops in 717.480591ms (1393765/sec)
--- PASS: TestFastlaneChan (2.07s)
goos: darwin
goarch: amd64
pkg: github.com/tidwall/fastlane
BenchmarkFastlaneChan-8          3000000               543 ns/op               0 B/op          0 allocs/op
BenchmarkGoChan100-8             3000000               557 ns/op               0 B/op          0 allocs/op
BenchmarkGoChan10-8              2000000               696 ns/op               0 B/op          0 allocs/op
BenchmarkGoChanUnbuffered-8      2000000               831 ns/op               0 B/op          0 allocs/op
PASS

@tidwall
Copy link
Owner

tidwall commented Feb 17, 2018

What’s strange?

@im-kulikov
Copy link
Author

im-kulikov commented Feb 17, 2018

this part

BenchmarkFastlaneChan-8  1000000	      1724 ns/op	       0 B/op	       0 allocs/op

it slower than go-channels three times

@tidwall
Copy link
Owner

tidwall commented Feb 17, 2018

This is because you are using the -race flag. Remove that flag for benchmarks.

@im-kulikov
Copy link
Author

oh.. thanks.. it my mistake

@tidwall
Copy link
Owner

tidwall commented Feb 17, 2018

No worries. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants