Replacing Protobuf with Rust to go 5 times faster

pgdog.dev

81 points by whiteros_e 5 hours ago


GuB-42 - 6 minutes ago

What I find particularly ironic is that the title make it feel like Rust gives a 5x performance improvement when it actually slows thing down.

The problem they have software written in Rust, and they need to use the libpg_query library, that is written in C. Because they can't use the C library directly, they had to use a Rust-to-C binding library, that uses Protobuf for portability reasons. Problem is that it is slow.

So what they did is that they wrote their own non-portable but much more optimized Rust-to-C bindings, with the help of a LLM.

But had they written their software in C, they wouldn't have needed to do any conversion at all. It means they could have titled the article "How we lowered the performance penalty of using Rust".

I don't know much about Rust or libpg_query, but they probably could have gone even faster by getting rid of the conversion entirely. It would most likely have involved major adaptations and some unsafe Rust though. Writing a converter has many advantages: portability, convenience, security, etc... but it has a cost, and ultimately, I think it is a big reason why computers are so fast and apps are so slow. Our machines keep copying, converting, serializing and deserializing things.

Note: I have nothing against what they did, quite the opposite, I always appreciate those who care about performance, and what they did is reasonable and effective, good job!

cranx - 2 hours ago

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 - 4 hours ago

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.

rozenmd - 3 hours ago

"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/

yodacola - 4 hours ago

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

- 2 hours ago
[deleted]
linuxftw - 40 minutes ago

Many people are exclaiming that the title is baity, but I disagree. It seems like a perfectly fine title in the context of this blog, which is about a specific product. It's unlikely they wrote the blog with a HN submission in mind. They're not a news publication, either.

t-writescode - 3 hours ago

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?

lowdownbutter - 3 hours ago

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

spwa4 - an hour ago

You should be terrified of the instability you're introducing to achieve this. Memory sharing between processes is very difficult to keep stable, it is half the reason kernels exist.

sylware - 2 hours ago

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???

IshKebab - 4 hours ago

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.

steeve - 3 hours ago

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