News for hackers. Stuff that matters.
Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by wokwokwok on Friday April 12, 2024 @05:00AM (#40010652)
While it’s a nice bed time story if you like go, the reality is no, it’s not typical.

Go has a good perf story, but typically rust or c++ would be faster after heavy optimisation; and should be more or less on par with typical applications. This isn’t a critique of go, and shouldn’t surprise anyone.

Typically go also has unexpected optimisation hoops to jump through and problems related to the heavy use of channels (see the well documented answer here: https://stackoverflow.com/questions/47312029/when-should-you...), so you would generally expect it to be slower…

…but, naive implementations are always slower, and really, it’s probably much of a muchness out the box for most day to day uses.

In almost all situations (even python or Java) you can get great performance if you invest time and effort in it.

But idiomatic code typically faster than rust? No, not really.

Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by pjmlp on Friday April 12, 2024 @07:02AM (#40011243)
While I am not a big Go fan, most people validating Go's performance mix language and implementation, and forget gccgo also exists, sharing many optimizations offered by GCC's backend.

Unfortunely, it seems stuck in Go 1.18, the last pre-generics version, with no roadmap for moving forward.

Given Go's folks stance on generics, still a lot of Go code is compilable with gccgo.

Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by riku_iki on Friday April 12, 2024 @05:15AM (#40010757)
> but typically rust or c++ would be faster after heavy optimisation;

it is concerning how much digging was required to optimize rust code in this case.

Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by sapiogram on Friday April 12, 2024 @06:53AM (#40011199)
The Go code is already hyper-optimized by experts, just not by the blog authors, so you don't read about it here. As someone who has tried to write high-performance Go code on occasion, I can assure you that a ton of digging would have been required on that side as well.
Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by kjksf on Friday April 12, 2024 @08:11AM (#40011656)
To bring this discussion back to earth, the Go code in question is: https://github.com/VictoriaMetrics/easyproto/blob/master/rea...

I don't see why did you label Aliaksandr Valialkin, the author, an "expert". I mean, he's no dummy but what exactly makes him an expert on optimizing Go code?

As someone who also writes Go, I don't see any "hyper optimizations" in the code. It just decodes the bytes of Protocol Buffer using straightforward code that I would expect a competent developer to write.

It really is just: read bytes from memory ([]byte) and interpret them according to PB spec.

There's only one trick there: unsafeBytesToString() that does no-allocation conversion of []byte to string. This is unsafe in general but safe in their specific case. And I've seen this trick before so it's not some secret, expert-only knowledge.

Most comments here are like bad LLMs: hallucinating opinions without bothering to spent even few minutes acquiring the data to base those opinions on.

Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by kasey_junk on Friday April 12, 2024 @08:27AM (#40011801)
He’s probably most well known in the golang community for fasthttp which is a widely used and highly optimized golang replacement for the golang stdlib. I’m a long term golang developer and I think calling him a golang optimization “expert” is fair.

That said, I agree with your assessment about this particular code. It’s fairly straightforward idiomatic go.

Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by sgift on Friday April 12, 2024 @08:25AM (#40011788)
> I don't see why did you label Aliaksandr Valialkin, the author, an "expert". I mean, he's no dummy but what exactly makes him an expert on optimizing Go code?

https://victoriametrics.com/team/ - Let's see. Author of multiple performance-optimized libraries with a masters degree in computer software engineering and a background in highly scalable systems (as needed for adtech). Sounds pretty much like an expert for optimizing code to me.

> And I've seen this trick before so it's not some secret, expert-only knowledge.

So, if you know it it's not export knowledge or what's your argument here?

Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by sapiogram on Friday April 12, 2024 @08:44AM (#40011948)
> I don't see why did you label Aliaksandr Valialkin, the author, an "expert". I mean, he's no dummy but what exactly makes him an expert on optimizing Go code?

I was trying to convey the meaning of "far more experienced than the blog post authors", but without having to insult the authors. It's a good writeup after all, and I'm glad they took the time.

We must have some different interpretations of what "optimized" means. This is the very first piece of code in the file you linked:

  func (fc *FieldContext) NextField(src []byte) ([]byte, error) {
      if len(src) >= 2 {
        n := uint16(src[0])<<8 | uint16(src[1])
        if (n&0x8080 == 0) && (n&0x0700 == (uint16(wireTypeLen) << 8)) {
            // Fast path - read message with the length smaller than 0x80 bytes.
            msgLen := int(n & 0xff)
            src = src[2:]
            if len(src) < msgLen {
                return src, fmt.Errorf("cannot read field for from %d bytes; need at least %d bytes", len(src), msgLen)
            }
            fc.FieldNum = uint32(n >> (8 + 3))
            fc.wireType = wireTypeLen
            fc.data = src[:msgLen]
            src = src[msgLen:]
            return src, nil
        }
    }
    // ... function continues beyond this point

As far as I can tell, this entire codepath exists solely as an optimization. I spent many years working on a chess engine for fun, so I'm pretty well versed in bit twiddling, but I'm seriously struggling with this. Like, is it doing `(n&0x8080 == 0)` to check to whether length is less than 0x80? Is that even correct?

I think "hyper optimized" is a completely fair characterization. But we clearly work in different industries.

Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by tedunangst on Friday April 12, 2024 @12:52PM (#40015013)
I'm not sure the presence of bit unpacking code in a decoder for a bit packed protocol is sufficient to call it hyper optimized. That seems like the nature of the problem.
Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by ndriscoll on Friday April 12, 2024 @10:59PM (#40020107)
Protobuf uses a bunch of variable length encodings. Here it's decoding a TLV format, but the length is itself a variable length integer (and seems to be a kind of tag-value encoding?) where you basically get 7 bits per byte telling you the value, and the leftmost bit tells you whether there's another byte. So if you mask with 0x8080 and get zero, then it was a 1 byte (7 bit) integer.

If 0x8080 is not set, then the tag-value record is 2 bytes. Left byte has tag. Right is value. Then they're masking with 0x0700 to get the type of record, which should be LEN.

So if it's a single byte LEN record, they can take that single byte as the length (they mask with 0x00ff, but really it's 0x007f. They already know the 0x80 bit is zero, and the value is contained in the least significant 7 bits). Otherwise they have to do some fiddly logic to decode the variable length integer to figure out the length (length here being the L in TLV).

Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by fredrikholm on Friday April 12, 2024 @06:30AM (#40011082)
Whilst still being 1/3rd slower than the Go version.
Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by Filligree on Friday April 12, 2024 @06:52AM (#40011192)
They could have used lifetimes. Instead they used elaborate workarounds, which turned out to be slower.
Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by pigpang on Friday April 12, 2024 @08:11AM (#40011660)
Rust-protobuf is much slower than quick-protobuf or prost.
Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by v0y4g3r on Friday April 12, 2024 @09:01AM (#40012119)
The Go version is not a naive implementation, it's also the well optimized version from VictoriaMetrics.
Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by kosherhurricane on Saturday April 13, 2024 @12:19AM (#40020464)
It's really easy to improve the performance of Go implementation as compared to python or Java. There are lots of built-in tools to help you (like the profiler), and the resulting code is really very legible even to fresh college grads.

This is based on my first hand experience, but YMMV.