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

all: bump golangci-lint to v1.51.1, test against Go v1.20, drop support for Go v1.18 #52

Merged
merged 2 commits into from
Feb 13, 2023

Conversation

rolinh
Copy link
Member

@rolinh rolinh commented Feb 9, 2023

No description provided.

@rolinh rolinh added module/main Affects the main module module/flow Affects the flow module module/cmd Affects the CLI module enhancement New feature or request labels Feb 9, 2023
@rolinh rolinh force-pushed the pr/rolinh/go-v1.20 branch from 419f285 to ec825aa Compare February 9, 2023 07:23
Copy link
Member

@kaworu kaworu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is unhappy:

Error: network.go:19:9: SA1019: rand.Read has been deprecated since Go 1.20 because it shouldn't be used: For almost all use cases, crypto/rand.Read is more appropriate. (staticcheck)
_, _ = rand.Read(hw)

Let's use crypto/rand.Read.

@rolinh
Copy link
Member Author

rolinh commented Feb 13, 2023

Let's use crypto/rand.Read.

The crypto/rand package is not a drop-in replacement for math/rand. The API is far less comprehensive than math/rand. We mostly use rand.Intn() but also rand.Uint32(), rand.Float64() and rand.Read(). Given the work required is comprehensive, let's tackle it as a follow-up PR.

@rolinh rolinh force-pushed the pr/rolinh/go-v1.20 branch from ec825aa to d73226c Compare February 13, 2023 11:02
@kaworu
Copy link
Member

kaworu commented Feb 13, 2023

The crypto/rand package is not a drop-in replacement for math/rand. The API is far less comprehensive than math/rand. We mostly use rand.Intn() but also rand.Uint32(), rand.Float64() and rand.Read().

That's fair, and furthermore our usage doesn't fall under "almost all use cases" either. To be clear, I'm only concerned about rand.Read() (not the others like rand.Intn() etc.) and only because deprecation means it will be removed eventually.

[…] Given the work required is comprehensive, let's tackle it as a follow-up PR.

FWIW I don't think we need a CSPRNG, non-crypto will do just fine as long as it is fast. Last time I checked the PRNG was the bottleneck when generating random flows from multiple goroutines (because the current implementation uses math/rand's default locked source). It would be great to tackle this issue on the way.

@rolinh
Copy link
Member Author

rolinh commented Feb 13, 2023

Right, only rand.Read() and rand.Seed() have been deprecated.

deprecation means it will be remove eventually

It actually means their use is discouraged but they won't be removed because this would break the Go compatibility promise (actual removal would require a Go 2 release) so I'm not too concerned about it.

FWIW I don't think we need a CSPRNG, non-crypto will do just fine as long as it is fast.

We use rand.Read() to generated random MAC and IP addresses. Out of curiosity, I wrote a simple bench:

func Benchmark_MAC(b *testing.B) {
       for i := 0; i < b.N; i++ {
               MAC()
       }
}

func Benchmark_IP(b *testing.B) {
       for i := 0; i < b.N; i++ {
               IP()
       }
}

Here's the result with math/rand's Read():

goos: linux
goarch: amd64
pkg: github.com/cilium/fake
cpu: AMD Ryzen 9 3950X 16-Core Processor            
Benchmark_MAC-32    	12133753	      153.5 ns/op
Benchmark_IP-32     	4572577	      341.4 ns/op
PASS
ok  	github.com/cilium/fake	4.809s

And here's the result with crypto/rand's Read():

goos: linux
goarch: amd64
pkg: github.com/cilium/fake
cpu: AMD Ryzen 9 3950X 16-Core Processor            
Benchmark_MAC-32    	1767763	      747.1 ns/op
Benchmark_IP-32     	1410813	      822.6 ns/op
PASS
ok  	github.com/cilium/fake	4.033s

EDIT: I added parallel test and the results indeed confirms the behavior you mention:

func Benchmark_MAC_Parallel(b *testing.B) {
       b.RunParallel(func(pb *testing.PB) {
               for pb.Next() {
                       MAC()
               }
       })
}

func Benchmark_IP_Parallel(b *testing.B) {
       b.RunParallel(func(pb *testing.PB) {
               for pb.Next() {
                       IP()
               }
       })
}

And the results with math/rand:

goos: linux
goarch: amd64
pkg: github.com/cilium/fake
cpu: AMD Ryzen 9 3950X 16-Core Processor            
Benchmark_MAC-32             	9568294	      134.0 ns/op
Benchmark_MAC_Parallel-32    	2759085	      441.8 ns/op
Benchmark_IP-32              	3405540	      349.6 ns/op
Benchmark_IP_Parallel-32     	1333165	      909.0 ns/op

Versus crypto/rand:

goos: linux
goarch: amd64
pkg: github.com/cilium/fake
cpu: AMD Ryzen 9 3950X 16-Core Processor            
Benchmark_MAC-32             	1799186	      583.9 ns/op
Benchmark_MAC_Parallel-32    	30073413      40.19 ns/op
Benchmark_IP-32              	1533070	      911.6 ns/op
Benchmark_IP_Parallel-32     	2222691	      540.7 ns/op

@kaworu kaworu merged commit e9d7933 into master Feb 13, 2023
@kaworu kaworu deleted the pr/rolinh/go-v1.20 branch February 13, 2023 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request module/cmd Affects the CLI module module/flow Affects the flow module module/main Affects the main module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants