-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
A faster s2b function #637
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good find, just one question.
I also think so before, but from the point of view of Go compiler ASM, keep sh as *(*reflect.StringHeader)(unsafe.Pointer(&s)) costing lower than keep it as a pointer. There may some compiler optimizations in this case. Let's see it in ASM.
(In ASM, without GC code)
We can see the version one can use more registers and the version two can use only one, so V1 has fewer instructions in ASM, actually V1 use 5 instructions, V2 use 6 instructions. (Size of V1 is 43, and size of V2 is 48, and this is the only difference.) |
Interesting. Thanks! |
A faster s2b function
The new function just use the return stack space to store the final value, without the allocation of a temporary struct. s2bFast is 100% faster if the Go compiler doesn't use deeper optimization in some code, if there is deeper optimization (inline for example), s2bFast is 5%~15% faster. The s2bFast is always same as s2b in any situations, both functions identically from the perspective of caller. You can see this in ASM, the new one has a smaller stack space and without locals.
Environment: go1.12.7 darwin/amd64
(The go code)
(In ASM)
Benchmark result ( Enable all optimizations)
Benchmark result ( Disable inline for benchmark, simulate no optimization situation )