4 ms·
A choice is the often _worst_ possible solution, assuming it is encoded into the format (which it may not be in this case). With a choice, when reading every p
by codys 9y ago
A choice is the often _worst_ possible solution, assuming it is encoded into the format (which it may not be in this case).
With a choice, when reading every platform has a conditional swap rather than some platforms having an unconditional swap (and the others having no swap at all).
A choice will only be beneficial if writes happen more often than reads (as providing a choice means that platforms can do no swapping on write). This can only be the case if some written data is never read.
Also, conditionals in general are not good from a performance perspective, so even more writes than reads doesn't make it clearly better to have a choice.
- mikeash 9y agoThis coder doesn't really have a format, it's more a way of defining a format. It should be flexible enough to work with many existing binary formats, and endianness configurability would need to be a part of that. Performance is not really a concern for this implementation. I would guess that the indirection from calling through all the Swift protocol methods will completely swap any cost of having extra branches (which will be predicted correctly about 99.99% of the time if there's a lot of them).