News for hackers. Stuff that matters.
Re:MessagePack: like JSON, but fast and small by lalaithion on Tuesday March 10, 2020 @01:54PM (#22537750)
It's also likely faster to parse; string and array sizes are statically known, and all parsing happens in order, so there's no backtracking. Want to parse a string in JSON? okay, this is a starting quote, so let's iterate down the rest of the document, looking for unescaped end quotes, and then copy that into a buffer. In msgpack, it's a single memcpy. Additionally, with number heavy messages, then it shines.

{"red": 127,"green": 28,"blue": 27,"x": -18,"y": 27,"z": 2008,"time": 1583862185,"dollars": 1298,"cents":12,"name":"tom",timeseries":[1,2,3,4,55,66,77,218,239,340,9009,80008,400004]}

gives 106 bytes with msgpack and 157 with gzip. Also, if you gzip the msgpack, for your message example, it's 248 bytes.

Re:MessagePack: like JSON, but fast and small by cptwunderlich on Tuesday March 10, 2020 @02:28PM (#22538080)
And you trust a message from an unknown source? You can't simply memcpy according to some length indicator, that's just not safe. You still have to parse and validate.
Re:MessagePack: like JSON, but fast and small by kentonv on Tuesday March 10, 2020 @02:47PM (#22538278)
Obviously you need to check that the length doesn't go past the end of the message, but that's a trivial O(1) check. You don't have to scan the bytes of the string first to decide if they are safe to memcpy.
Re:MessagePack: like JSON, but fast and small by laumars on Tuesday March 10, 2020 @03:50PM (#22538920)
You might want to validate those byte sequences are valid character encodings.
Re:MessagePack: like JSON, but fast and small by kelnos on Tuesday March 10, 2020 @03:55PM (#22538981)
You should be doing that with JSON as well, so this isn't a pro/con of either format.
Re:MessagePack: like JSON, but fast and small by laumars on Tuesday March 10, 2020 @04:19PM (#22539256)
That was obviously my point.

ie just because MessagePack is a binary format it doesn't mean you can skip the same string checks that JSON requires; which means parsing MessagePack strings is unlikely to be any faster than JSON strings (contrary to the suggestions others have implied with the "just memcpy" comments). It's just with JSON that validation is done as part of the parser (remember JSON only technically supports a subset of ASCII and any extended characters or unicode is encoded via escape codes) where as with MessagePack you'd need to do that validation as an additional step.

Integers, on the hand, might differ since JSON would need additional validation (again, backed into the parser) which MessagePack would not because MessagePack encodes the integers as binary integers where as JSON encodes them as ASCII values that would need converting back to binary integers.

(hint: read the message I'm replying to).

Re:MessagePack: like JSON, but fast and small by kentonv on Tuesday March 10, 2020 @07:41PM (#22541263)
Many (most?) applications do not actually care whether a byte blob of text is structurally valid UTF-8. They are either passing it around as an opaque byte blob, or already applying much stricter application-specific validation. Validating UTF-8 automatically at the serialization layer is a huge waste of cycles, especially in a big distributed system.
Re:MessagePack: like JSON, but fast and small by laumars on Wednesday March 11, 2020 @03:19AM (#22543795)
On closed systems where you control both the input and output, then sure (Though I’d still recommend against that particular short cut because it’s an easy way for bugs to go undetected).

However if you’re accepting MessagePack encoded data from insecure systems (such as end users) then you absolutely should be validating your input somewhere along the pipeline and it’s usually better to do that early on.

Also it’s not generally the distributed systems you worry about when it comes to this specific degree of micro-optimisation (which is basically what this is). It’s the monolithic ones. Distributed architecture is meant to solve various problems (for example but not limited to, high availability, reduced geographical latency, single site but running on cheaper commodity hardware, etc) but often at the cost of CPU cycles. Whereas your monolithic infrastructures where you have fewer servers (such as Stack Overflows set up) would be greatly more dependant on reducing computational overhead where corners could be cut. However they’d also be significantly less likely to need networked RPCs via MessagePack anyway (simply due to the monolithic design of their architecture).

Re:MessagePack: like JSON, but fast and small by lwf on Tuesday March 10, 2020 @02:43PM (#22538234)
As long as you know the length of the entire buffer, you just ensure that:

  current_addr + message_len - start_addr < buffer_len
Or am I missing something?
Re:MessagePack: like JSON, but fast and small by maxwindiff on Tuesday March 10, 2020 @03:23PM (#22538657)
Invalid unicode sequences?
Re:MessagePack: like JSON, but fast and small by diabeetusman on Tuesday March 10, 2020 @02:52PM (#22538329)
buffer_len could be larger than the message, copying some incorrect things into memory.

Similar to HeartBleed, where there wasn't validation on the heartbeat message, and the server would echo back buffer_len instead of just what was sent.

Re:MessagePack: like JSON, but fast and small by theamk on Tuesday March 10, 2020 @04:30PM (#22539354)
I believe author intended buffer_len to be the length of incoming buffer (size of HTTP payload, number of bytes read from file, length of the database entry, etc...). So the worst that can happen is that entire input message is consumed -- like a JS payload which missed closing quote.

I can think of a very contrived situation where this can be a problem, but in most cases this will be perfectly safe.

Re:MessagePack: like JSON, but fast and small by bmn__ on Tuesday March 10, 2020 @02:42PM (#22538226)
https://capnproto.org serialisation scheme skips the decoding. Does that make it not safe?
Re:MessagePack: like JSON, but fast and small by tebeka on Tuesday March 10, 2020 @02:52PM (#22538334)
Re:MessagePack: like JSON, but fast and small by strbean on Tuesday March 10, 2020 @03:27PM (#22538698)
None of those are serialization schemes. XML can be used for serialization, but if you look at the whole ecosystem it is a Turing-complete complexity monster, so of course it isn't safe.
Re:MessagePack: like JSON, but fast and small by DougBTX on Tuesday March 10, 2020 @03:32PM (#22538736)
It depends on what constraints apply to the data. Any bit pattern could be used for an int, but to guarantee a UTF-8 string it would need to be validated.
Re:MessagePack: like JSON, but fast and small by kingofpandora on Tuesday March 10, 2020 @02:32PM (#22538116)
Genuine question - is it dangerous to memcpy X bytes that we know must be interpreted as, say, an integer?
Re:MessagePack: like JSON, but fast and small by byte1918 on Tuesday March 10, 2020 @03:10PM (#22538522)
No. Everything is 0s and 1s after all. Take for eg. a byte. It has 8 bits and by permutating all the 0s and 1s you end up with all the possible values of a signed byte; all the numbers -128 to 127. So now, if you were to copy a byte from a random memory location that byte will just contain a permutation of 0s and 1s which when interpreted as a signed int, will simply contain a number between -128 and 127.
Re:MessagePack: like JSON, but fast and small by kingofpandora on Tuesday March 10, 2020 @04:02PM (#22539060)
That's what I thought ...
Re:MessagePack: like JSON, but fast and small by tangent128 on Tuesday March 10, 2020 @03:01PM (#22538451)
Potentially. Most network protocols are big-endian while x86 is little-endian.
Re:MessagePack: like JSON, but fast and small by hinkley on Tuesday March 10, 2020 @03:37PM (#22538784)
Keep fighting the good fight.
Re:MessagePack: like JSON, but fast and small by rumanator on Tuesday March 10, 2020 @02:42PM (#22538230)
> Want to parse a string in JSON? okay, this is a starting quote, so let's iterate down the rest of the document, looking for unescaped end quotes, and then copy that into a buffer.

Yes, so what's the problem?

Meanwhile keep in mind that the JSON document was already passed in a HTTP request body, or that its trivial to put string length checks on a parser, or even nesting limit checks.

Re:MessagePack: like JSON, but fast and small by hn_throwaway_99 on Tuesday March 10, 2020 @03:14PM (#22538569)
Delimited formats are, in general, slower to parse than formats where the record sizes are encoded in the message.

That said, not something I want to prematurely optimize for.

Re:MessagePack: like JSON, but fast and small by strbean on Tuesday March 10, 2020 @03:32PM (#22538734)
I don't think it's that premature. Making the (big) assumption that serialization format X has good stable support in your language, and has the same expressive capabilities as JSON, you should be able to use it as a drop in replacement (as long as you control both ends of the communication...). And doesn't a huge portion of what we do around here amount to "shuffle serialized data around as efficiently as we can"?
Re:MessagePack: like JSON, but fast and small by rumanator on Wednesday March 11, 2020 @02:57AM (#22543713)
> Delimited formats are, in general, slower to parse than formats where the record sizes are encoded in the message.

I don't see your point at all. The state machine required to parse a JSON string essentially has only 2 states: current token is either a string character or a string delimiter. That's it. Adding a record size increases the number of states because now you not only need to track if a token is a string character but also if the character makes sense to be there within that state.

Moreover, when you parse a JSON string, once you hit the string's end delimiter you already are able to calculate the string size. Thus, if the string is short enough to fit the buffer then not only is the lexer simpler but it also requires the same memory allocations than strig formats with record sizes. If however a string doesn't fit the buffer then we are already in the territory of a ropes data structure in both cases, thus the number of memory allocations tend to be equivalent.