5 ms·
It depends. It was common case for years in games to just cast your networked data to an array and send over the network. Nowadays it's much more in fashion to
by a_t48 8y ago
It depends. It was common case for years in games to just cast your networked data to an array and send over the network. Nowadays it's much more in fashion to pick a serialization library to sit in between, as PCs are faster. But one might choose not to depending on the problem space. Embedded devices might not have the spare cycles or the space for a full serialization library.
So...you're not wrong, but it's basically the same reason people use C over something with more ergonomics.
Edit: the last time I wrote a networking library, I also just cast to raw structures, mainly because it was less work (didn't want to integrate a serialization library on both sides of the link). This was fine for a while (and very fast), but also hid some bugs in the protocol, and made sending arrays of data very ugly. Eventually we put protobuf on top, and things ended up much nicer.
- DaiPlusPlus 8y agoHow does sending raw memory to the socket work if you have a struct filled with pointers?
- mehrdadn 8y agoI think they simply don't do that with structs that contain pointers?
- a_t48 8y agoIndeed, this. If you have data with pointers, you either need to flatten (read: copy) your structure into something appropriate for structuring, or just get a proper serialization layer.
- repsilat 8y agoI guess there could be some fun workarounds with ptrdiff_t instead of pointers. "My child is 1000 bytes that way in memory" etc.
- a_t48 8y agoThat's a pretty classical file serialization trick to pack a bunch of heterogeneous data in one file. A header/manifest starting the file, with an array of names, offsets, and sizes.
- blattimwind 8y agoActually works somewhat well for mmap'ed access, too, by using a macro or function to "dereference" "pointers" (offsets).
- a_t48 8y agoOr even an OffsetPtr class with overloaded arrow and dereference operator, if you swing that way. :)
- DaiPlusPlus 8y agoWhat’s the runtime performance of that in-practice? Does x86, x64 and ARM have support for relative offset pointers without any performance impact? And do compilers use them?
- blattimwind 8y agoYes, address generation can at the very least do [reg + constant], and on x86, much much more. These are bread and butter optimizations constantly used by compilers all over the place, because this is how field access works.