Comments (3)
Since Golang compilers don't allow us to unroll automatically, here's this method even more optimized. Most of the variables are not really necessary:
func ReadUvarint(r io.ByteReader) (uint64, error) {
// Modified from the go standard library. Copyright the Go Authors and
// released under the BSD License.
var x uint64
// byte index 0 (i = 0)
b, err := r.ReadByte()
if err != nil {
return 0, err
}
if b < 0x80 {
return x | uint64(b), nil
}
x |= uint64(b & 0x7f)
// byte index 1 (i = 1)
b, err = r.ReadByte()
if err != nil {
if err == io.EOF {
// "eof" will look like a success.
// If we've read part of a value, this is not a
// success.
err = io.ErrUnexpectedEOF
}
return 0, err
}
if b < 0x80 {
if b == 0 {
return 0, ErrNotMinimal
}
return x | uint64(b)<<7, nil
}
x |= uint64(b&0x7f) << 7
// byte index 2 (i = 2, s = 14)
b, err = r.ReadByte()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return 0, err
}
if b < 0x80 {
if b == 0 {
return 0, ErrNotMinimal
}
return x | uint64(b)<<14, nil
}
x |= uint64(b&0x7f) << 14
// byte index 3 (i = 3, s = 21)
b, err = r.ReadByte()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return 0, err
}
if b < 0x80 {
if b == 0 {
return 0, ErrNotMinimal
}
return x | uint64(b)<<21, nil
}
x |= uint64(b&0x7f) << 21
// byte index 4 (i = 4, s = 28)
b, err = r.ReadByte()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return 0, err
}
if b < 0x80 {
if b == 0 {
return 0, ErrNotMinimal
}
return x | uint64(b)<<28, nil
}
x |= uint64(b&0x7f) << 28
// byte index 5 (i = 5, s = 35)
b, err = r.ReadByte()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return 0, err
}
if b < 0x80 {
if b == 0 {
return 0, ErrNotMinimal
}
return x | uint64(b)<<35, nil
}
x |= uint64(b&0x7f) << 35
b, err = r.ReadByte()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return 0, err
}
if b < 0x80 {
if b == 0 {
return 0, ErrNotMinimal
}
return x | uint64(b)<<42, nil
}
x |= uint64(b&0x7f) << 42
// byte index 7 (i = 7, s = 49)
b, err = r.ReadByte()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return 0, err
}
if b < 0x80 {
if b == 0 {
return 0, ErrNotMinimal
}
return x | uint64(b)<<49, nil
}
x |= uint64(b&0x7f) << 49
// byte index 8 (i = 8, s = 56)
b, err = r.ReadByte()
if err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return 0, err
}
if b >= 0x80 {
// this is the 9th and last byte we're willing to read, but it
// signals there's more (1 in MSB).
// or this is the >= 10th byte, and for some reason we're still here.
return 0, ErrOverflow
} else {
if b == 0 {
return 0, ErrNotMinimal
}
return x | uint64(b)<<56, nil
}
}
from go-varint.
Testing shows that the unrolled version cut the time spent inside a mutex by 30-60%
from go-varint.
Your first version is faster (~20%) but the loop unrolling didn't seem to help. In general, loop unrolling only helps with tight loops. In this case, the indirect call to ReadByte
is likely removing the benefits from the unrolling.
from go-varint.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-varint.