smallnest / chanx

unbounded chan
MIT License
432 stars 63 forks source link

chan: reduce the memory allocation #19

Closed nolouch closed 6 months ago

nolouch commented 6 months ago

In our scenario, the Len function may frequently invoked.

before:

go test -benchmem -run=^$ -bench ^BenchmarkUnbounded github.com/smallnest/chanx  
goos: darwin
goarch: arm64
pkg: github.com/smallnest/chanx
BenchmarkUnboundedChanLen-12            65669220                18.17 ns/op           32 B/op          1 allocs/op
BenchmarkUnboundedBufLen-12             69060109                18.27 ns/op           32 B/op          1 allocs/op

after

goos: darwin
goarch: arm64
pkg: github.com/smallnest/chanx
BenchmarkUnboundedChanLen-12            1000000000               0.2879 ns/op          0 B/op          0 allocs/op
BenchmarkUnboundedBufLen-12             1000000000               0.2846 ns/op          0 B/op          0 allocs/op
nolouch commented 6 months ago

PTAL @smallnest