asaskevich / EventBus

[Go] Lightweight eventbus with async compatibility for Go
MIT License
1.74k stars 220 forks source link

use copy() instead of append() in publish() #40

Closed lookfirst closed 4 years ago

lookfirst commented 5 years ago

Thanks for some very useful code.

In Publish(), it does an append in order to prevent issues with removal.

I found a test suite that shows that copy is faster. The similar code would be BenchmarkAppendAlloc (645ns/op) vs. BenchmarkCopy (521ns/op).

https://gist.github.com/xogeny/b819af6a0cf8ba1caaef

go test -bench=.
goos: darwin
goarch: amd64
BenchmarkAppend-12                   1000000          1087 ns/op
BenchmarkAppendAlloc-12              3000000           645 ns/op
BenchmarkAppendAllocInline-12        3000000           508 ns/op
BenchmarkCopy-12                     5000000           521 ns/op
PASS
ok      _/tmp/append_vs_copy    8.642s
asaskevich commented 4 years ago

Thank you!

lookfirst commented 4 years ago

Wow! I forgot I even wrote this code. lol