Boost logo

Boost :

From: Christopher Kohlhoff (chris_at_[hidden])
Date: 2005-08-18 20:07:38


Hi Thorsten,

--- Thorsten Ottosen <nesotto_at_[hidden]> wrote:
> please consider the other mails too. there are too many drawback
> in void* IMO.
>
> I have seen/used code where a statically allocated data was far
> slower than putting it on the heap. This wsa not stack-allocated, but
> static data. So I'm a bit concerned about performance claims.

The performance problems of requiring vector<char> or char[N] exist on
several levels:

- For vector<char>, there is the initialisation of the chars to 0 on
construction or when you do a resize. Note that this is proportional to
the size of the vector, not necessarily to the amount of data
transferred. I have seen this have a noticable cost in a CPU-bound
server handling thousands of connections.

- Requiring a copy from a native data structure into vector<char> or
char[N]. If I have an array of a doubles say, I should be able to send
it as-is to a peer that has identical architecture and compiler.
Avoiding unnecessary data copying is a vital part of implementing high
performance protocols.

I also believe that the argument that the cost is not significant
compared to network I/O does not hold when you are developing an
application using asynchronous I/O, since the time spent
sending/receiving no longer blocks the application.

> If the only performance sensible iterator types are T*,
> I would't mind simply
>
> template< unsigned N >
> void receive( char (&)[N] );
>
> template< unsigned N >
> void receive( char (&)[N], unsigned size );
>
> void recieve( vector<char>& );
>
> void recieve( vector<char>&, unsigned size );
>
> or something.

I just realised that you cannot have such functions on the socket class
due to the possibility of a short send or receive. Since you can send
or receive fewer bytes than requested, how would you implement
something like asio::send_n or asio::recv_n on top of the above
functions?

Again, I believe that adding safety is best done in layers, in
accordance with the don't pay for what you don't need principle:

asio::socket::send/recv etc taking void* + size_t. These functions can
result in a short send or receive.
  ^
  |
asio::send_n/recv_n etc taking void* + size_t. These functions attempt
to transfer the all of the requested data.
  ^
  |
asio::send_?/recv_? etc. New functions that take safe types like
vector<char> and char[N].

Cheers,
Chris


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