Remix.run Logo
Replacing Protobuf with Rust to go 5 times faster(pgdog.dev)
50 points by whiteros_e 3 hours ago | 34 comments
cranx 22 minutes ago | parent | next [-]

I find the title a bit misleading. I think it should be titled It’s Faster to Copy Memory Directly than Send a Protobuf. Which then seems rather obvious that removing a serialization and deserialization step reduces runtime.

nottorp 2 hours ago | parent | prev | next [-]

Are they sure it's because Rust? Perhaps if they rewrite Protobuf in Rust it will be as slow as the current implementation.

They changed the persistence system completely. Looks like from a generic solution to something specific to what they're carrying across the wire.

They could have done it in Lua and it would have been 3x faster.

consp 2 hours ago | parent | next [-]

If they made the headline something on the line of "replacing protobuf with a native, optimized implementation" would not get the same attention as putting rust in the title to attract the everything-in-rust-is-better crowd.

desiderantes 2 hours ago | parent [-]

That never happens. Instead, it always attracts the opposite group, the Rust complainers, where they go and complain about how "the everything-in-rust-is-better crowd created yet another fake headline to pretend that Rust is the panacea". Which results in a lot of engagement. Old ragebait trick.

embedding-shape 2 hours ago | parent | prev | next [-]

It's devbait, not many of us can resist bikeshedding about the title which obviously doesn't accurately reflect the article contents. And the article contents are self-aware enough to admit this to itself too, yet the title remains.

alias_neo 2 hours ago | parent | prev | next [-]

I was equally confused by the headline.

I wonder if it's just poorly worded and they meant to say something like "Replacing Protobuf with some native calls [in Rust]".

win311fwg 2 hours ago | parent | prev | next [-]

The title would suggest that it was already written in Rust; that it was the rewrite in Go that brought five times faster.

misja111 2 hours ago | parent | prev | next [-]

Correct, this has very little to do with Rust. But it wouldn't have made the front page without it.

locknitpicker 2 hours ago | parent | prev [-]

Yes you are absolutely right. The article even outright admits that Rust had nothing to do with it. From the article:

> Protobuf is fast, but not using Protobuf is faster.

The blog post reads like an unserious attempt to repeat a Rust meme.

gxt 13 minutes ago | parent | prev | next [-]

I've been working on a random-access SQL db and yeah, any format that's more than just a copy to disk/memory is going to be slower than the alternative. I tried several "buffer" formats when I started a few years ago thinking its not that big of a cost, only to realize plain copies are really fast.

rozenmd 2 hours ago | parent | prev | next [-]

"5 times faster" reminds me of Cap'n Proto's claim: in benchmarks, Cap’n Proto is INFINITY TIMES faster than Protocol Buffers: https://capnproto.org/

7777332215 an hour ago | parent | next [-]

In my experience capn proto is much less ergonomic.

gf000 an hour ago | parent | prev [-]

I mean, cap'n'proto is written by the same person who created protobuf, so they are legit (and that somewhat jokish claim is simply that it requires no parsing).

Sesse__ 26 minutes ago | parent [-]

> I mean, cap'n'proto is written by the same person who created protobuf

Notably, Protobuf 2, a rewrite of Protobuf 1. Protobuf 1 was created by Sanjay Ghemawat, I believe.

sylware 10 minutes ago | parent | prev | next [-]

I don't understand, I used protobuf for map data, but it is a hardcore simple format, this is the whole purpose of it.

I wrote assembly, memory mapping oriented protobuf software... in assembly, then what? I am allowed to say I am going 1000 times faster than rust now???

yodacola 2 hours ago | parent | prev | next [-]

FlatBuffers are already faster than that. But that's not why we choose Protobuf. It's because a megacorp maintains it.

nindalf 2 hours ago | parent | next [-]

You're saying we choose Protobufs [1] because Google maintains it but not FlatBuffers [2]?

[1] - https://github.com/protocolbuffers/protobuf: Google's data interchange format

[2] - https://github.com/google/flatbuffers: Also maintained by Google

rafaelmn 2 hours ago | parent [-]

I get the OP is off base with his remark - but at the same time maintained by Google means shit in practice.

AFAIK they have a bunch of production infra on protobuff/gRPC - not so sure about flatbufferrs which came out of the game dev side - that's the difference maker to me - which project is actually rooted in.

dewey an hour ago | parent [-]

> but at the same time maintained by Google means shit in practice.

If you worked on Go projects that import Google protobuf / grpc / Kubernetes client libraries you are often reminded of that fact.

secondcoming an hour ago | parent | prev [-]

Yet they've yet to release their internal optimisation that allows zero-copying string-type fields.

t-writescode 2 hours ago | parent | prev | next [-]

Just for fun, how often do regular-sized companies that deal in regular-sized traffic need Protobuf to accomplish their goals in the first place, compared to JSON or even XML with basic string marshalling?

tuetuopay 15 minutes ago | parent | next [-]

Type safety. The contract is the law instead of a suggestion like JSON.

Having a way to describe your whole API and generate bindings is a godsend. Yes, it can be done with JSON and OpenApi, yet it’s not mandatory.

tcfhgj an hour ago | parent | prev | next [-]

Well, protobuf allows to generate easy to use code for parsing defined data and service stubs for many languages and is one of the faster and less bandwidth wasting options

Chiron1991 31 minutes ago | parent | prev | next [-]

It's not just about traffic. IoT devices (or any other low-powered devices for that matter) also like protobuf because of its comparatively high efficiency.

bluGill an hour ago | parent | prev | next [-]

In most languages protobuf is eaiser because it generates the boilerplate. And protobuf is cross language so even if you are working in javascript where json is native protobuf is still faster because the other side can be whatever and you are not spending their time parsing.

jonathanstrange 41 minutes ago | parent | prev [-]

Protobuf is fantastic because it separates the definition from the language. When you make changes, you recompile your definitions to native code and you can be sure it will stay compatible with other languages and implementations.

lowdownbutter an hour ago | parent | prev | next [-]

Don't read clickbaity headlines and scan hacker news five times faster.

IshKebab 2 hours ago | parent | prev | next [-]

I vaguely recall that there's a Rust macro to automatically convert recursive functions to iterative.

But I would just increase the stack size limit if it ever becomes a problem. As far as I know the only reason it is so small is because of address space exhaustion which only affects 32-bit systems.

jeroenhd 2 hours ago | parent | next [-]

Explicit tail call optimization is in the works but I don't think it's available in stable jut yet.

The `become` keyword has already been reserved and work continues to happen (https://github.com/rust-lang/rust/issues/112788). If you enable #![feature(explicit_tail_calls)] you can already use the feature in the nightly compiler: https://play.rust-lang.org/?version=nightly&mode=debug&editi...

(Note that enabling release mode on that link will have the compiler pre-calculate the result so you need to put it to debug mode if you want to see the assembly this generates)

embedding-shape 2 hours ago | parent | prev [-]

> I vaguely recall that there's a Rust macro to automatically convert recursive functions to iterative.

Isn't that just TCO or similar? Usually a part of the compiler/core of the language itself, AFAIK.

koverstreet an hour ago | parent [-]

I haven't been following become/TCO in Rust - but what I've usually seen is TCO getting flipped off because it interferes with backtraces and debugging.

So I think there's value in providing it as an explicit opt-in; that way when you're reading the code, you know to account for it when you're looking at backtraces.

Additionally, if you're relying on TCO it might be a major bug if the compiler isn't able to apply it - and optimizations that aren't applied are normally invisible. This might mean you could get an error if you're expecting TCO and you or the compiler screwed something up.

tialaramex 6 minutes ago | parent [-]

In a language like Rust where local variables are explicitly destroyed when scope ends a naive TCO is very annoying and `become` also helps fix that.

Suppose I have a recursive function f(n: u8) where f(0) is 0 and otherwise f(n) is n * bar(n) + f(n-1)

I might well write that with a local temporary to calculate bar(n) and then we do the sum, but this would inhibit TCO because that temporary should exist after we did the recursive calculation, even though it doesn't matter in practice.

A compiler could try to cleverly figure out whether it matters and destroy that local temporary earlier then apply TCO, but now your TCO is fragile because a seemingly minor code change might fool that "clever" logic, by ensuring it isn't correct to make this change and breaking your optimisation.

The `become` keyword is a claim by the programmer that we can drop all these locals and do TCO. So because the programmer claimed this should work they're giving the compiler permission to attempt the early drop and if it doesn't work and can't be TCO then complain that the program is wrong.

steeve an hour ago | parent | prev [-]

tldr: they replaced using protobuf as the type system across language boundaries for FFI with true FFI

Xunjin an hour ago | parent [-]

I loved, every clickbait title should come with a tldr just like this one.