On May 2, 12:41 pm, k...@pink-frog.com wrote:
> I set TCP_NODELAY. That helped a lot. We're down to:
>
> 0m0.051s
>
> versus:
>
> 0m4.078s
>
> Which is, roughly, a factor of eighty. Which is good, but still a
> huge factor. Do you have any more ideas ?
You're still making a complex and expensive system call for every
20-30 bytes you need to send. TCP_NODELAY minimized the symptoms of
the problem but didn't fix the problem. You still have a fundamental
difference -- TCP is a bytestream protocol and UDP is a datagram
protocol. Your code does not sensibly use a bytestream protocol.
Accumulate your data into larger chunks. Do not call 'send' or 'write'
unless one of two things are the case:
1) You have at least 1Kb to send.
2) You do not expect to need to send any more data in the immediate
future, until either some event occurs or you receive data from the
other side.
You may also be interacting badly with the scheduler, switching
between sending and receiving more often than is needed. It's hard to
know without seeing your test code, but this can be a source of poor
performance.
Imagine if your UDP code was doing the following:
1) Send 20 datagrams, each 20-30 bytes
2) Receive the 20 datagrams, each 20-30 bytes
3) Repeat
and your TCP code was doing the following:
1) Send one small chunk of 10-20 bytes.
2) Receive one small chunk of 10-20 bytes.
3) Repeat
The TCP code would require 20 times as many loops as the first. If the
loop overhead is a significant fraction of the time, then the first
bit of code will be much faster than the second. Sending larger chunks
is one way to remove this cost.
Try to take advantage of TCP's transmit pacing by calling 'send' in a
tight loop until it blocks.
DS
|