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

[Improve]Optimize the generation speed of UUIDv4 for Golang SDK #11597

Closed
2 tasks done
davendu opened this issue Dec 13, 2024 · 1 comment · Fixed by #11598
Closed
2 tasks done

[Improve]Optimize the generation speed of UUIDv4 for Golang SDK #11597

davendu opened this issue Dec 13, 2024 · 1 comment · Fixed by #11598
Assignees
Milestone

Comments

@davendu
Copy link
Contributor

davendu commented Dec 13, 2024

Description

Currently the Golang SDK use github.com/gofrs/uuid for UUIDv4 generation. Due to randomness requirement of UUIDv4, the performance is decided by the entropy pool of runtime, which usually means a IO bottleneck.

Another package github.com/google/uuid could provide better performance by optionally enable the random pool. Here is the benchmark on my server running Linux :

> go test -bench=. -test.count=16 ./uuid_benchmark_test.go | summary
goos: linux
goarch: amd64
cpu: Intel(R) Xeon(R) Gold 6133 CPU @ 2.50GHz
Name                               Count-Avg (N)   AvgTime - Avg,Max,Min,Stdeva (ns/op)
BenchmarkGofrsUUID-8               1408388.188	  845.35	854	840.6	3.63
BenchmarkGoogleUUIDEnablePool-8    9252526.375	  129.15	133.2	128.2	1.35
BenchmarkGoogleUUIDDisablePool-8   1408379.063	  853.21	865.5	845	6.29

Here is the test code:

package uuid_benchmark

import (
	"testing"

	gofrs "github.com/gofrs/uuid"     // using v4.4.0+incompatible
	google "github.com/google/uuid"   // using v1.3.0
)

func BenchmarkGofrsUUID(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_, err := gofrs.NewV4()
		if err != nil {
			b.Error(err)
		}
	}
}

func BenchmarkGoogleUUIDEnablePool(b *testing.B) {
	google.EnableRandPool()
	for i := 0; i < b.N; i++ {
		_ = google.New()
	}
}

func BenchmarkGoogleUUIDDisablePool(b *testing.B) {
	google.DisableRandPool()
	for i := 0; i < b.N; i++ {
		_ = google.New()
	}
}

Note that users should enable the feature by calling google.EnableRandPool() manually, which does not provides concurrent safety.

InLong Component

InLong SDK

Are you willing to submit PR?

  • Yes, I am willing to submit a PR!

Code of Conduct

Copy link

Hello @davendu, thank you for opening your first issue in InLong 🧡 We will respond as soon as possible ⏳
If this is a bug report, please provide screenshots or error logs for us to reproduce your issue, so we can do our best to fix it.
If you have any questions in the meantime, you can also ask us on the InLong Discussions 🔍

@davendu davendu changed the title [Improve]Speed up UUID generate for Golang SDK [Improve]Optimize the generation speed of UUIDv4 for Golang SDK Dec 13, 2024
@dockerzhang dockerzhang added this to the 2.1.0 milestone Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants