Boost logo

Boost :

From: Martin Bonner (martin.bonner_at_[hidden])
Date: 2005-10-18 05:20:15


From: Christopher Kohlhoff [mailto:chris_at_[hidden]]
>
>>> struct message {
>>> int a;
>>> double b;
>>> int c_size;
>>> char c[0];
>>> };
>>> message* m = ...;
>>> sock.write(buffers(m, sizeof(message) + m->c_size));
>>

> --- Martin Bonner <martin.bonner_at_[hidden]> wrote:
>> Oh no! Please! Don't let us encourage bad code like that.

>
> One person's bad code is another person's necessary performance
> optimisation :)
>
>> It will work if
>> the receiving socket belongs to the same program on the same
>> machine. Otherwise it depends on:
>> - sizeof(int) (16,32,64 bits)
>> - endianness
>> - representation of double (IEEE or IBM)
>>
>> ... and that's just the obvious, real-world, differences that can be
>> encountered TODAY.
>
> It will in fact work across different machines that have the same
> architecture, where the programs use the same alignment etc. This may
> seem picky, but it is a vital point.
>
> The optimisation shown in the example, ugly though it looks, is in
> principle what is used in receiver-makes-right protocols, i.e. the
> cost associated with dealing with architecture differences is left to
> the receiver. That way if the data is sent between two machines of
> the same type, then those costs are eliminated.
>
> There's a large body of research and experience in these sorts of
> optimisations, e.g. MPI, and they are critical to high performance
> computing.
>
> I agree that code like that should not appear in typical applications
> and should not be generally encouraged. However there is a use case
> for it in domains that would also often use C++ for performance.
> Therefore I believe that a C++ networking library must not preclude
> such a usage.

That seems fair enough (a programmer's gotta do what the profiler says a
programmer's gotta do).

However, can I put in a plea that we arrange things so that this is not the
simplest thing to do? Compare the C file API. The simplest way to read and
write a struct to a file, is with a simple call to fread/fwrite. That means
too many people just read and writes structs to file as binary, and then hit
problems when the next compiler release changes the struct layout. Doing it
the right way (with a series of unsigned chars) is hard work (and often
doesn't get done).

-- 
Martin Bonner
Martin.Bonner_at_[hidden]
Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ,
ENGLAND Tel: +44 (0)1223 441434

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk