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

Compare strings with strings.EqualFold #8356

Merged
merged 1 commit into from
Sep 18, 2024
Merged

Compare strings with strings.EqualFold #8356

merged 1 commit into from
Sep 18, 2024

Conversation

Juneezee
Copy link
Contributor

2024-09-13_23-00

This PR fixes a Staticcheck warning (https://staticcheck.dev/docs/checks/#SA6005). Comparing two strings to the same case with strings.ToLower and strings.ToUpper is more computational expensive than strings.EqualFold.

Sample benchmark:

func BenchmarkToUpperSingle(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if strings.ToUpper("foobar") != "FOOBAR" {
			b.Fail()
		}
	}
}

func BenchmarkToUpperDouble(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if strings.ToUpper("foobar") != strings.ToUpper("FOOBAR") {
			b.Fail()
		}
	}
}

func BenchmarkToLowerSingle(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if strings.ToLower("FOOBAR") != "foobar" {
			b.Fail()
		}
	}
}

func BenchmarkToLowerDouble(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if strings.ToLower("FOOBAR") != strings.ToLower("foobar") {
			b.Fail()
		}
	}
}

func BenchmarkEqualFold(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if !strings.EqualFold("FOOBAR", "foobar") {
			b.Fail()
		}
	}
}

Result:

goos: linux
goarch: amd64
pkg: github.com/dolthub/dolt/go
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkToUpperSingle-16    	 9589896	       113.8 ns/op	       8 B/op	       1 allocs/op
BenchmarkToUpperDouble-16    	 7097330	       155.9 ns/op	       8 B/op	       1 allocs/op
BenchmarkToLowerSingle-16    	12346219	       106.5 ns/op	       8 B/op	       1 allocs/op
BenchmarkToLowerDouble-16    	 8084006	       143.6 ns/op	       8 B/op	       1 allocs/op
BenchmarkEqualFold-16        	63650023	        18.76 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/dolthub/dolt/go	6.456s

Comparing two strings to the same case with `strings.ToLower` and
`strings.ToUpper` is more computational expensive than
`strings.EqualFold` [1].

Sample benchmark:

func BenchmarkToUpperSingle(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if strings.ToUpper("foobar") != "FOOBAR" {
			b.Fail()
		}
	}
}

func BenchmarkToUpperDouble(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if strings.ToUpper("foobar") != strings.ToUpper("FOOBAR") {
			b.Fail()
		}
	}
}

func BenchmarkToLowerSingle(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if strings.ToLower("FOOBAR") != "foobar" {
			b.Fail()
		}
	}
}

func BenchmarkToLowerDouble(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if strings.ToLower("FOOBAR") != strings.ToLower("foobar") {
			b.Fail()
		}
	}
}

func BenchmarkEqualFold(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if !strings.EqualFold("FOOBAR", "foobar") {
			b.Fail()
		}
	}
}

goos: linux
goarch: amd64
pkg: github.com/dolthub/dolt/go
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkToUpperSingle-16    	 9589896	       113.8 ns/op	       8 B/op	       1 allocs/op
BenchmarkToUpperDouble-16    	 7097330	       155.9 ns/op	       8 B/op	       1 allocs/op
BenchmarkToLowerSingle-16    	12346219	       106.5 ns/op	       8 B/op	       1 allocs/op
BenchmarkToLowerDouble-16    	 8084006	       143.6 ns/op	       8 B/op	       1 allocs/op
BenchmarkEqualFold-16        	63650023	        18.76 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/dolthub/dolt/go	6.456s

[1]: https://staticcheck.dev/docs/checks/#SA6005

Signed-off-by: Eng Zer Jun <[email protected]>
Copy link
Contributor

@jycor jycor left a comment

Choose a reason for hiding this comment

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

Thanks for the changes.
Everything looks great!

@jycor jycor merged commit 964ee23 into dolthub:main Sep 18, 2024
19 of 21 checks passed
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.

3 participants