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

changed the String() method for UUID's #174

Merged
merged 3 commits into from
May 27, 2014

Conversation

cwndrws
Copy link
Contributor

@cwndrws cwndrws commented May 21, 2014

I was profiling some code for work, that uses the UUID.String() method a lot and saw it was using fmt.Sprintf(), which is a bit overkill, if you ask me. I changed it to use a switch statement. It is now between 4-6 times faster. Here is a benchmark.

@0x6e6562
Copy link
Contributor

Very interesting. After having optimized this, what effect did you see on the performance of the app as a whole?

@cwndrws
Copy link
Contributor Author

cwndrws commented May 22, 2014

On Wed, May 21, 2014 at 02:50:51PM -0700, Ben Hood wrote:

Very interesting. After having optimized this, what effect did you see on the performance of the app as a whole?
Well the application is a server that handles many different kinds of
loads. Under a few specific test cases, where we are returning to an end
user and are turning all UUID's into strings, it was about twice as
fast as before.

@0x6e6562
Copy link
Contributor

I've just run your benchmark myself:

go test -test.bench Benchmark* -test.run XXX
PASS
BenchmarkOldWay  1000000          2039 ns/op
BenchmarkNewWay 10000000           160 ns/op
ok      github.com/relops/gocql 3.848s

It does look like it has a significant performance improvement without affecting the functionality.

So I would be in favor of merging this - but I would like one of the other maintainers opinion on this, since this the first performance related patch we have received.

@nemosupremo
Copy link
Contributor

Looking over the pr I have to ask:

  1. hexString should be a const as it should never be changed
  2. b1 and b2 shouldn't be global. This will cause corruptions if two goroutines run this function at the same time

@cwndrws
Copy link
Contributor Author

cwndrws commented May 22, 2014

good look @nemothekid I just pushed another commit addressing these issues.

@tux21b
Copy link
Contributor

tux21b commented May 22, 2014

There is no need to export HexString or make it global. What do you think of this, sightly modified version?

const hexString = "0123456789abcdef"
var offsets = [...]int{0, 2, 4, 6, 9, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34}
r := make([]byte, 36)
for i, b := range uuid {
    r[offsets[i]] = hexString[b>>4]
    r[offsets[i]+1] = hexString[b&0xF]
}
r[8] = '-'
r[13] = '-'
r[18] = '-'
r[23] = '-'
return string(r)

It seems to perform sightly faster here. Oh, and it would be nice if you could also add your benchmark as BenchmarkUUIDString or something like that to the uuid_test.go file, so that we can track performance regressions in the future.

@cwndrws
Copy link
Contributor Author

cwndrws commented May 27, 2014

I have changed it to use the offsets array.

tux21b added a commit that referenced this pull request May 27, 2014
changed the String() method for UUID's
@tux21b tux21b merged commit f6fb3e6 into apache:master May 27, 2014
@tux21b
Copy link
Contributor

tux21b commented May 27, 2014

LGTM. Many thanks for your contribution! :)

@cwndrws cwndrws deleted the OptimizeUUIDString branch May 27, 2014 15:46
dkropachev pushed a commit to go-auxiliaries/gocql that referenced this pull request May 31, 2024
…ions

When LWT optimization was implemented in gocql
(scylladb#49), all `newFramer`
calls were replaced with `newFramerWithExts` calls.

Previously, during the upstream merging the code using `newFramer`
was added (scylladb#109) and it was
forgotten to replace it with `newFramerWithExts`. That way, the
`flagLWT` field was set to `0` by default, causing the driver
to consider every query executed as a LWT query.

The issue was not immediately noticed because the only difference
in behavior occurs when we want to use `ShuffleReplicas()` in
`TokenAwareHostPolicy`.

This commit fixes the issue by replacing `newFramer` with `newFramerWithExts`.

Fixes: apache#174
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants