Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The length should be known in advance in order to be written, so the message cannot be incrementally written. You need more complex framing scheme like Consistent Overhead Byte Stuffing for that. And many applications do want a variable number of length bytes because i) 4 bytes is actually too long for short messages and ii) some message can exceed 2^32 bytes. Not to say the generic varint encoding is good for this purpose, though [1].

[1] If you ever have to design one, make sure that reading the first byte is enough to determine the number of subsequent length bytes.



> 4 bytes is actually too long for short messages

Would it ever be an actual bottleneck though? If it's not actually impeding throughput, I feel like this is more of an aesthetic argument than a technical one, and one where I'd happily sacrifice aesthetics to make the code simpler.

> some message can exceed 2^32 bytes

Fair enough, but that just makes the question "would 8 bytes per message ever actually be a bottleneck", which I'm still not convinced would ever be the case


I agree that 4 byte overhead in a single item is not a big deal even when the item itself is short. But if you have multiple heterogeneous items, it becomes worthwhile to set an upper bound for the relative ratio of overhead over data. (SQLite, for example, makes extensive use of varints for this reason [1].) I personally think a single byte overhead is worthy for sub-100-byte data, while larger ones should use a fixed size prefix because the overhead is no longer significant.

[1] https://www.sqlite.org/fileformat.html#record_format


I work on an embedded OS where our IPC buffer is only about 140 bytes.

Anything more requires multiple IPCs, with lots of expensive context switches.

Wasting even one precious byte on a pointless header would absolutely be an issue in this environment.


That makes sense. I've probably been thinking more about remote network protocols, where the time it takes for a message to reach its destination will be so large that the overhead for a few extra bytes per message is negligible; for a protocol used solely within a given local system, there would be a much lower threshold of acceptable overhead.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: