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: GetAllCharsets and GetCharsetInfoByID #247

Merged
merged 1 commit into from
Mar 19, 2019

Conversation

hawkingrei
Copy link
Member

What problem does this PR solve?

pingcap/tidb#9635
some of the functions may just return constants. There is no need to do compute every time if we save a map/slice and initialize them. Such as: GetAllCharsets and GetCharsetInfoByID

What is changed and how it works?

  1. GetAllCharsets is inited at the init function, and GetAllCharsets can return the inited array directly.
  2. GetCharsetInfoByID use a map to fetch Collation effectively, init the map in the init function too.

Check List

  • Unit test

Code changes

  • improve GetAllCharsets and GetCharsetInfoByID

Related changes

  • Need to update go.mod in tidb

@shenli
Copy link
Member

shenli commented Mar 18, 2019

@hawkingrei Thanks!
Is there any benchmark result to show the improvement?

@hawkingrei
Copy link
Member Author

hawkingrei commented Mar 18, 2019

Benchmark GetAllCharsets

Before improve:

go test -bench Benchmark -run GetAllCharsets ./... -count 5

goos: darwin
goarch: amd64
pkg: github.com/pingcap/parser/charset
BenchmarkGetAllCharsets-4   	 3000000	       405 ns/op
BenchmarkGetAllCharsets-4   	 3000000	       405 ns/op
BenchmarkGetAllCharsets-4   	 3000000	       397 ns/op
BenchmarkGetAllCharsets-4   	 3000000	       447 ns/op
BenchmarkGetAllCharsets-4   	 3000000	       443 ns/op
PASS
ok  	github.com/pingcap/parser/charset	8.616s
PASS

After improve

go test -bench Benchmark -run GetAllCharsets ./... -count 5

goos: darwin
goarch: amd64
pkg: github.com/pingcap/parser/charset
BenchmarkGetAllCharsets-4   	2000000000	         0.38 ns/op
BenchmarkGetAllCharsets-4   	2000000000	         0.37 ns/op
BenchmarkGetAllCharsets-4   	2000000000	         0.37 ns/op
BenchmarkGetAllCharsets-4   	2000000000	         0.36 ns/op
BenchmarkGetAllCharsets-4   	2000000000	         0.36 ns/op
PASS

Here is benchmark code.

func BenchmarkGetAllCharsets(b *testing.B) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		GetAllCharsets()
	}
}

@hawkingrei
Copy link
Member Author

Benchmark GetAllCharsets

Before improve:

go test -bench Benchmark -run GetGetCharsetInfoByID ./... -count 2

goos: darwin
goarch: amd64
pkg: github.com/pingcap/parser/charset
BenchmarkGetCharsetInfoByID-4   	30000000	        64.4 ns/op
BenchmarkGetCharsetInfoByID-4   	30000000	        72.4 ns/op
PASS
ok  	github.com/pingcap/parser/charset	4.208s

After improve

go test -bench Benchmark -run GetGetCharsetInfoByID ./... -count 2

goos: darwin
goarch: amd64
pkg: github.com/pingcap/parser/charset
BenchmarkGetCharsetInfoByID-4   	100000000	        20.6 ns/op
BenchmarkGetCharsetInfoByID-4   	100000000	        18.5 ns/op
PASS

Here is benchmark code.

func BenchmarkGetCharsetInfoByID(b *testing.B) {
	index := rand.Intn(248)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		GetCharsetInfoByID(index)
	}
}

@hawkingrei
Copy link
Member Author

@shenli it looks good

@tiancaiamao
Copy link
Collaborator

LGTM

Copy link
Contributor

@zimulala zimulala left a comment

Choose a reason for hiding this comment

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

LGTM

@zimulala zimulala added status/LGT2 LGT2 and removed status/LGT1 LGT1 labels Mar 19, 2019
@ngaut ngaut merged commit 551b517 into pingcap:master Mar 19, 2019
tiancaiamao pushed a commit to tiancaiamao/parser that referenced this pull request Apr 27, 2021
lyonzhi pushed a commit to lyonzhi/parser that referenced this pull request Apr 25, 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 this pull request may close these issues.

6 participants