News for hackers. Stuff that matters.
Re:Don't Use Protobuf for Telemetry by thisiscorrect on Wednesday December 30, 2020 @03:50PM (#25585934)
Mind elaborating on how the "standard" C++ and Java protobuf implementations are bloated?

[edit] I'm genuinely asking. I'm guessing you mean in the generated APIs, i.e. the code "weight", but maybe you meant something else?

Re:Don't Use Protobuf for Telemetry by tijsvd on Wednesday December 30, 2020 @04:38PM (#25586455)
Things may have improved since, but the implementations are somehow very large and slow.

Things may have changed since, but AFAIK the C++ implementation would always allocate on the heap for nested messages, and perhaps even for optional scalars. This may be optimal for larger documents, but not for smallish messages (my use case was market data and trading instructions). I measured certain small messages, where an encode/decode pair would take over a microsecond with Google's implementation, but about 50 ns with a simpler one (versus 15 ns for a memcpy).

For Java my experience is mostly with the API itself, which felt very heavy.

Edit: I think a lot depends on your use case. I use protobuf mostly as a 'trusted' protocol. If someone didn't set a required field, I don't care. Some bloat may have to do with verifications that I've never needed.

Re:Don't Use Protobuf for Telemetry by haberman on Wednesday December 30, 2020 @04:57PM (#25586630)
> Things may have changed since, but AFAIK the C++ implementation would always allocate on the heap for nested messages

This is no longer the case if you use arenas: https://developers.google.com/protocol-buffers/docs/referenc...

> and perhaps even for optional scalars

This has never been the case, except for string fields where std::string forces us to allocate.

Ideally we will eventually use std::string_view for string accessors instead of std::string, so that even string data can be allocated on an arena instead of the heap.

Re:Don't Use Protobuf for Telemetry by cbsmith on Thursday December 31, 2020 @02:06AM (#25590325)
It was never really the case. It's just before arenas it depended on your underlying heap allocator to do all the hard work.

Arenas have been around for a long while now though...

Re:Don't Use Protobuf for Telemetry by seppel on Wednesday December 30, 2020 @05:45PM (#25587151)
> This has never been the case, except for string fields where std::string forces us to allocate.

I'm was quite surprised you didnt offer your own stringview implementation (or something similar) the last time I looked at protobuf. I'd naively assume that inside Google this could be quite a low-efford high-reward optimization.

Re:Don't Use Protobuf for Telemetry by haberman on Wednesday December 30, 2020 @06:05PM (#25587374)
> I'm was quite surprised you didnt offer your own stringview implementation (or something similar) the last time I looked at protobuf.

We sort of do actually: https://github.com/protocolbuffers/protobuf/blob/master/src/...

The internal version of protobuf lets you switch individual string fields to string_view using [ctype=STRING_PIECE], but migrating the default away from std::string is mainly just an enormous migration challenge.

Internally we also do something slightly nuts: we break the encapsulation of std::string so that we can point it to arena-allocated memory (we then "steal" the memory back before the destructor runs). We can only afford to do this internally, where the implementation of std::string is known. The real long-term solution is to move to string_view.

Re:Don't Use Protobuf for Telemetry by jasonzemos on Wednesday December 30, 2020 @05:14PM (#25586812)
Since all other comments appear to contradict you and make apologies from authority (after all, Google can't do anything wrong, right?) I'd like to just reassure you with my 20 years of experience developing C (and the last 10 with C++) in network and system software: that any library -- any library -- that forces internal dynamic memory upon its user smells bad. It's not a universal condemnation, but it begs the question, or rather the skepticism to ask why?

In the case of protobufs, there is no real answer. Protobufs is one of the few network libraries that doesn't operate with zero-copy. That steps over a tangible line in the sand.

No zero-copy for networking? Forced internal heap allocations with only this arena feature after a decade? Sorry no. Protobufs isn't useful for serious network applications.

Re:Don't Use Protobuf for Telemetry by kentonv on Wednesday December 30, 2020 @07:07PM (#25587925)
Unless you are targeting a constrained embedded system, this kind of anti-memory-allocation thinking is counter-productive. Avoiding memory allocation significantly increases the complexity of an API -- or worse, leads to shortcuts like using (thread-unsafe) globals or (overrun-prone) fixed buffers.

In C, dealing with memory allocation is such a pain that C programmers still tend to avoid it. But C++, especially post-C++11, makes dynamic memory allocation much, much easier to deal with. (And GC'd languages, obviously, are easier still.) There is still a performance cost, obviously, but that cost almost never matters in application programming use cases, and is even negligible in many systems programming cases.

I do think Protobuf does too much allocation, but saying that libraries should not allocate memory at all is an outdated view.

Re:Don't Use Protobuf for Telemetry by jasonzemos on Wednesday December 30, 2020 @08:49PM (#25588664)
> Avoiding memory allocation significantly increases the complexity of an API -- or worse, leads to shortcuts like using (thread-unsafe) globals or (overrun-prone) fixed buffers.

Allow me to unpack this, because the object interface that all of these serialization schemes present is what increases complexity. It makes sense that the simple implementation would tend toward dynamic memory and not further compound such complexity on their interface. This is a flawed architectural premise taken by these library implementations.

On the receiving side, there is little reason to ever deviate from zero-copy/zero-allocate. These libraries are far too pro-active in deserializing incoming data into native object structures. It's not necessary to present the user with this; they should pique through the results lazily -- query into the data at their own discretion. This is important because it bounds the minimum requisite cost of receiving messages at ... zero. It costs nothing to discard trash. Whereas with these proactive serialization layers, every single incoming attack becomes an exercise against the deserializer. In a zero-copy/zero-allocate -- let's even say, zero-parse mere lexing of the data, software has more flexibility. In practice, that means passing pointers around to parts of messages straight off the wire to application functionality further up the stack.

On the sending side, there is little reason to ever force whole native object representations to conduct a serialization. In other words, don't force the user to build an std::map first and serialize it later. All input is streaming input. Properties do not have to be pre-buffered, they can be streamed. One can model any network serialization this way, sans perhaps canonical representations (sorted JSON keys, etc), and far more efficiently than with requiring arbitrary native structures.

This is not really a rebuttal about the merits for or against dynamic memory in and of itself. I know the research, thread-aware allocators can be pretty good -- even darn good, and the state of the art in GC is nothing to shake a stick at. The problem is that with better design it's just not necessary, and in the end it does have a cost that one should want to eliminate if possible. I'm certain it's quite usually possible, at least more than I see in a list like https://en.wikipedia.org/wiki/Comparison_of_data-serializati... etc

Re:Don't Use Protobuf for Telemetry by kentonv on Wednesday December 30, 2020 @09:17PM (#25588853)
What you describe sounds exactly like how Cap'n Proto works. Obviously I like that design since I wrote it.

But it does have drawbacks. The encoded size is necessarily a bit larger to allow random access traversal of the raw bytes (though it compresses well). The API to manipulate structures in-place is a little awkward, particularly on the writing side. And, you can't really use the generated types as mutable in-memory state, as people commonly like to do with Protobuf types -- as a result, a common feature request for Cap'n Proto is to support generating "native" structs with the ability to convert between those and the zero-copy types as desired.

Everything is full of trade-offs. I don't disagree with your design preferences but I do object to the extreme line you are taking on them. You said: "Protobufs isn't useful for serious network applications." That is plainly contradicted by the existence of a trillion-dollar company built on said technology.

Re:Don't Use Protobuf for Telemetry by a_t48 on Wednesday December 30, 2020 @09:43PM (#25589022)
> These libraries are far too pro-active in deserializing incoming data into native object structures.

Sometimes a native object structure is what you want.

Re:Don't Use Protobuf for Telemetry by vvanders on Wednesday December 30, 2020 @07:29PM (#25588089)
Constrained embedded systems cover a broad range of things up to and including your phone. There's few things I hate to see more than a flat profile from memory allocation or cache misses.

The good news is that flatbuffers[1] is a reasonable replacement for most of my use cases. In particular being able to mmap() them directly is a wonderful thing that you can't do with protobufs in addition to being very allocation sparse.

[1] https://google.github.io/flatbuffers/

Re:Don't Use Protobuf for Telemetry by kentonv on Wednesday December 30, 2020 @08:04PM (#25588354)
> Constrained embedded systems cover a broad range of things up to and including your phone.

No, modern phones are certainly not constrained in the way I meant, and I don't think you could call them "embedded" either. The common programming languages used on phones are very memory-allocation-friendly.

> There's few things I hate to see more than a flat profile from memory allocation or cache misses.

I think you may be arguing a different point, or a different level of extremity of the point. Reducing memory allocation to optimize performance is a fine thing that everyone does. The person I was replying to, though, seemed to be asserting that libraries should completely avoid allocating memory for themselves.

> The good news is that flatbuffers[1] is a reasonable replacement for most of my use cases. In particular being able to mmap() them directly is a wonderful thing that you can't do with protobufs in addition to being very allocation sparse.

Yeah... I'm the author of Cap'n Proto, which has the same property, and predates Flatbuffers.

Re:Don't Use Protobuf for Telemetry by vvanders on Wednesday December 30, 2020 @09:46PM (#25589040)
> The common programming languages used on phones are very memory-allocation-friendly.

I would hardly classify Dalvik or ART as "allocation friendly", they don't perform escape analysis and if you do it constantly you'll be in a world of constant hard GC pauses. Multiple times over the years I've had to build free-lists in Java to avoid this specific problem.

Same for C++ if you use one of the built-in generic memory allocators. The fastest new/delete are the ones that you don't call.

For what it's worth I tend to agree with the grand-parent thread. The lack of awareness of allocations, cache-invalidation via indirection are a significant contributor to why we see software clawing back hardware wins across the years on these platforms.

Re:Don't Use Protobuf for Telemetry by cbsmith on Thursday December 31, 2020 @02:15AM (#25590363)
> I would hardly classify Dalvik or ART as "allocation friendly", they don't perform escape analysis and if you do it constantly you'll be in a world of constant hard GC pauses. Multiple times over the years I've had to build free-lists in Java to avoid this specific problem.

I don't know what you're doing, but I've generally found free-lists to be a net performance negative in Java libraries. Time and again, I've been called in to "optimize" Java code that uses them, and usually by simply removing them I can get rid of the performance problems entirely.

Re:Don't Use Protobuf for Telemetry by vvanders on Thursday December 31, 2020 @02:14PM (#25596022)
Dalvik and ART have different behaviors than traditional Java VMs. Dalvik is aimed at low memory devices, ART trades some memory for speed but still is significantly different(i.e. it doesn't do escape analysis). As always benchmarking on hardware is the way to confirm this but I've had numerous cases where free lists or pre-allocated arrays has gained 10-30x performance improvements.

Flatbuffers in particular excels here since once you have the ByteBuffer in memory you can immediately start accessing data without needing to do any extensive parsing.

Even the official Android docs are very explicit[1] on the allocation point. Allocations are not cheap and even with generational collection you still will blow the 16.6ms frame window for 60 FPS if any of your operations allocate excessively.

[1] https://developer.android.com/training/custom-views/optimizi...

Re:Don't Use Protobuf for Telemetry by cbsmith on Friday January 01, 2021 @09:16PM (#25609614)
Sure, with flatbuffers, I grok the case (though where possible I just use an off-heap mmap buffer for them anyway), but that's a very specific case.
Re:Don't Use Protobuf for Telemetry by musicale on Thursday December 31, 2020 @02:16AM (#25590373)
You may be right - presumably iPhones have fewer GC pauses (though you can still have VM, loading, compression/decompression, network, and other pauses.)

Be that as it may, lots of people still manage to use Android devices.

Re:Don't Use Protobuf for Telemetry by mcguire on Wednesday December 30, 2020 @11:21PM (#25589557)
Once upon a time, we were concerned with handling network traffic at wire speed. I can remember when the AIX folks were happy they could implement the TCP fast path entirely in the interrupt handler.
Re:Don't Use Protobuf for Telemetry by kentonv on Thursday December 31, 2020 @10:33AM (#25593539)
Certainly you wouldn't use protobuf to encode TCP packets. It's meant for encoding application-layer messages, typically ones that aren't especially large but contain a lot of complex structure.
Re:Don't Use Protobuf for Telemetry by mcguire on Thursday December 31, 2020 @11:29AM (#25594101)
Where efficiency doesn't matter because they don't need to be processed quickly and cpu cycles and memory are free.
Re:Don't Use Protobuf for Telemetry by haberman on Wednesday December 30, 2020 @05:43PM (#25587138)
> make apologies from authority (after all, Google can't do anything wrong, right?)

That's not my position at all. In my other comment (https://news.ycombinator.com/item?id=25586447) I explain how I've spent 10 years trying to improve on protobuf C++ precisely because I agree that some of these limitations are unnecessary.

> No zero-copy for networking? Forced internal heap allocations with only this arena feature after a decade? Sorry no. Protobufs isn't useful for serious network applications.

I suppose it depends what you are comparing it to. Almost every JSON library has the same limitations you mentioned, and yet many people find JSON useful for network applications. But I agree that giving users full control over allocations makes a library useful in many more situations.

I think arena allocation is a pretty reasonable solution to the problem. You can use whatever memory you want for the arena (stack, heap, static buffer) and you can constrain it so that no heap allocations are allowed.

Unfortunately protobuf C++ can't live fully within this arena model while it uses std::string for accessors. Hopefully this can be fixed at some point.

Re:Don't Use Protobuf for Telemetry by jasonzemos on Wednesday December 30, 2020 @07:24PM (#25588051)
> I suppose it depends what you are comparing it to. Almost every JSON library has the same limitations you mentioned, and yet many people find JSON useful for network applications.

JSON is a human-readable format which is hugely advantageous to develop and operate in many settings. Protobufs doesn't have that advantage. Yet we're paying all of the same costs to structure the data with both. That's enough to posit JSON as a net winner over protobufs.

> Unfortunately protobuf C++ can't live fully within this arena model while it uses std::string for accessors.

Forcing std::string as a container for core components of a networking API (specifically for an accessor) is an exemplary demonstration of a lack of seriousness in a library.

Re:Don't Use Protobuf for Telemetry by kentonv on Wednesday December 30, 2020 @08:53PM (#25588690)
You can debate the design decisions all you want (and I agree `std::string` was a poor choice here)... but it's kind of absurd to say that a technology underpinning products used by billions of people every day is not "serious".
Re:Don't Use Protobuf for Telemetry by jasonzemos on Wednesday December 30, 2020 @09:07PM (#25588780)
Why is that absurd? Products used by billions of people may just as well not have serious requirements. I beg not to debate semantics, indeed my own fault, but in the world of network software requirements can start to get very serious, straight through to userspace protocol stacks on DMA'ed device buffers.

Would you go through all of the effort to rig an application with userspace networking only to return its results inside an `std::string` -- one of the few std containers where one can't even control the allocator if they wanted to? That's absurd, if anything.

Re:Don't Use Protobuf for Telemetry by kentonv on Wednesday December 30, 2020 @09:35PM (#25588967)
If you are arguing that there exist use cases for which Protobuf is ill-suited, then fine, I obviously agree.

But you seem to be arguing that Protobuf is bad because it is not well suited to certain use cases, dismissing all other use cases as "non-serious". That is offensive.

Re:Don't Use Protobuf for Telemetry by jasonzemos on Wednesday December 30, 2020 @10:09PM (#25589170)
I asked you not to argue semantics, but you're doing it anyway. I didn't respond here to argue protobufs actually is bad, since all software is suited to something for somebody somewhere.

In fact, I can just do that now: Protobufs is bad. You seem to agree, because you haven't even advocated for it -- just flatbuffers and Cap'n Proto and the phonebook of alternatives. Listen, I don't care who you've worked for or what you've done. As a scientist and engineer I'm totally ready and able to entertain critiques of my prior works without being offended. I'm offended that this is the quality of your participation here.

Re:Don't Use Protobuf for Telemetry by dekhn on Thursday December 31, 2020 @10:21AM (#25593425)
This is a really unprofessional and unreasonable approach (and I can see you're being downvoted for it). Since you say you're a scientist and engineer, can you share your prior work so we can evaluate it?
Re:Don't Use Protobuf for Telemetry by xyzzyz on Wednesday December 30, 2020 @09:30PM (#25588945)
> but in the world of network software requirements can start to get very serious, straight through to userspace protocol stacks on DMA'ed device buffers.

Google does all of that when it makes sense, and yet uses it to push protobufs. That’s the entire point: you are calling it absurd and non-serious, but this only shifts my opinion on you, not on Google.

Re:Don't Use Protobuf for Telemetry by andrekandre on Wednesday December 30, 2020 @08:15PM (#25588424)
> Forcing std::string as a container for core components of a networking API (specifically for an accessor) is an exemplary demonstration of a lack of seriousness in a library.

im not super experienced with c++, so please forgive the maybe obvious question, but why is that?

(im guessing it has to do with allowing control over where allocations come from?)

Re:Don't Use Protobuf for Telemetry by arcticbull on Wednesday December 30, 2020 @08:18PM (#25588443)
In general I agree although I think it's fair to separate the protobuf wire format from the proto3 schema format which had a canonical json representation.
Re:Don't Use Protobuf for Telemetry by tijsvd on Wednesday December 30, 2020 @05:41PM (#25587114)
> No zero-copy for networking? Forced internal heap allocations with only this arena feature after a decade? Sorry no. Protobufs isn't useful for serious network applications.

That's a bit harsh. Protobufs deliver smaller wire size than any of the newer "zero-copy" formats. And many receivers of zero-copy formats will... copy the data into some internal representation. If your protobuf implementation delivers classes that are good enough to work with internally (store in maps, forward, etc) then you don't really lose something; instead you gain, due to no manual conversion layer.

Re:Don't Use Protobuf for Telemetry by jasonzemos on Wednesday December 30, 2020 @05:50PM (#25587211)
CBOR also delivers optimal wire sizes with its variable length encoding. Ceteris paribus, there's no technical advantage to using protobufs. The choice is almost always non-technical (business, platform, partnership, etc) or simple naivete. It's not harsh, it's just engineering.
Re:Don't Use Protobuf for Telemetry by cbsmith on Thursday December 31, 2020 @02:13AM (#25590357)
"that any library -- any library -- that forces internal dynamic memory upon its user smells bad"

Cough.

While with embedded systems there definitely is a big thing about dynamic memory allocation, much as I don't like it, it's not like very popular and successful libraries don't do this. It's a pretty common and accepted practice, and there are standard idioms for how it is done.

Re:Don't Use Protobuf for Telemetry by kentonv on Wednesday December 30, 2020 @05:01PM (#25586681)
> AFAIK the C++ implementation would always allocate on the heap for nested messages

FWIW if you reuse the same message object for multiple parsings, it will re-use the sub-objects as well, thus amortizing away the allocation cost. Parsing the same message into the same object twice should do zero allocations on the second parse. This is the intended way to use Protobuf for small-size messages.

Apparently the C++ implementation has also grown support for arena allocation more recently. (After my time, so I don't know much about it.)

Re:Don't Use Protobuf for Telemetry by tijsvd on Wednesday December 30, 2020 @05:28PM (#25586968)
But then all that must come with bookkeeping, which brings its own cost.

Take a look at an implementation like Prost, for Rust. It's very similar to what I did (10 years ago by now). Everything is just inline, except when messages can be recursive (which should be rare for most protocols).

Re:Don't Use Protobuf for Telemetry by haberman on Wednesday December 30, 2020 @06:22PM (#25587528)
> Everything is just inline, except when messages can be recursive (which should be rare for most protocols).

Many messages are have lots of optional sub-message fields, and set only a few of them in any given message. These messages would be huge if everything is inline (especially if the same thing happens in those sub-messages).

I agree that inlining all sub-messages works great for dense schemas, but it assumes too much about the schema to be a good design for a general-purpose proto library I think. Also maps and repeated fields can never be inline.

Re:Don't Use Protobuf for Telemetry by tijsvd on Wednesday December 30, 2020 @06:36PM (#25587651)
Yes, this comes back to use case.

So it's great that there are different implementations for different use cases. It helps that the wire format is simple and well-documented.

Re:Don't Use Protobuf for Telemetry by kentonv on Wednesday December 30, 2020 @06:15PM (#25587463)
The bookkeeping is not that hard... the pointer is null until it is first allocated, then it remains non-null, while a separate boolean indicates whether the sub-message is actually present in the parent.

The big problem is bloat in memory usage if you parse many differently-shaped messages, requiring the app to implement hacks like only reusing a particular object a certain number of times.

Re:Don't Use Protobuf for Telemetry by a_t48 on Wednesday December 30, 2020 @09:46PM (#25589041)
I wish I had known this a few years ago, I could have saved some allocations. :)
Re:Don't Use Protobuf for Telemetry by gabereiser on Wednesday December 30, 2020 @03:57PM (#25586006)
Probably a complaint of the codegen and it’s wacky name mangling.
Re:Don't Use Protobuf for Telemetry by notretarded on Wednesday December 30, 2020 @04:01PM (#25586051)
Probably all this "security" nonsense. What could possibly go wrong with implementing a binary serialisation protocol your self...
Re:Don't Use Protobuf for Telemetry by inetknght on Wednesday December 30, 2020 @04:06PM (#25586110)
Oh look sarcasm! On Hacker News!