deckarep / golang-set

A simple, battle-tested and generic set type for the Go language. Trusted by Docker, 1Password, Ethereum and Hashicorp.
Other
4.15k stars 274 forks source link

[Feature Request]Add Each method for elements iteration #56

Closed bluele closed 6 years ago

bluele commented 6 years ago

Hi. Currently golang-set has Iter and Iteration method, but it is not necessary to return the channel in many cases that iterate elements.(Of course, sometimes it is necessary.) So I propose an iteration method that does not use channels or goroutines.

Each method is faster than Iter and Iterator. Benchmark is here:

BenchmarkEach1Safe-4                            30000000                54.0 ns/op             0 B/op          0 allocs/op
BenchmarkEach1Unsafe-4                          30000000                45.1 ns/op             0 B/op          0 allocs/op
BenchmarkEach10Safe-4                           10000000               160 ns/op               0 B/op          0 allocs/op
BenchmarkEach10Unsafe-4                         10000000               155 ns/op               0 B/op          0 allocs/op
BenchmarkEach100Safe-4                           1000000              1449 ns/op               0 B/op          0 allocs/op
BenchmarkEach100Unsafe-4                         1000000              1500 ns/op               0 B/op          0 allocs/op
BenchmarkIter1Safe-4                             3000000               573 ns/op              96 B/op          1 allocs/op
BenchmarkIter1Unsafe-4                           3000000               552 ns/op              96 B/op          1 allocs/op
BenchmarkIter10Safe-4                             500000              2828 ns/op              96 B/op          1 allocs/op
BenchmarkIter10Unsafe-4                           500000              2779 ns/op              96 B/op          1 allocs/op
BenchmarkIter100Safe-4                            100000             25204 ns/op              96 B/op          1 allocs/op
BenchmarkIter100Unsafe-4                           50000             25880 ns/op              96 B/op          1 allocs/op
BenchmarkIterator1Safe-4                         2000000               779 ns/op             208 B/op          3 allocs/op
BenchmarkIterator1Unsafe-4                       2000000               797 ns/op             208 B/op          3 allocs/op
BenchmarkIterator10Safe-4                         300000              4483 ns/op             208 B/op          3 allocs/op
BenchmarkIterator10Unsafe-4                       300000              4486 ns/op             208 B/op          3 allocs/op
BenchmarkIterator100Safe-4                         30000             38857 ns/op             208 B/op          3 allocs/op
BenchmarkIterator100Unsafe-4                       30000             45187 ns/op             208 B/op          3 allocs/op
deckarep commented 6 years ago

Hi @bluele - thanks for your contribution looks like a nice addition to the API. I’ll reciew this I detail and get back to you...if everything looks good I’ll be merging it!