Reading HTTP headers (was Re: cfsockets vs OT)
Reading HTTP headers (was Re: cfsockets vs OT)
- Subject: Reading HTTP headers (was Re: cfsockets vs OT)
- From: Larry Gerndt <email@hidden>
- Date: Mon, 05 Jan 2004 22:31:01 -0800
>
From: Becky Willrich <email@hidden>
>
Subject: Re: cfsockets vs OT
>
Date: Mon, 5 Jan 2004 11:20:08 -0800
>
To: tuviah snyder <email@hidden>
>
>
> I've read that performance should be better with BSD sockets than OT; however
>
> our little test app which fetches data from a URL using the HTTP protocol
>
> 20-30 times async using CFSockets..takes about twice the time than it does
>
> with OT async. Oddly enough performance improves when I move the cursor
>
> around the screen rapidly, or install multiple timers, ect (could be a
>
> runloop thing I guess). Any suggestions to improve performance? I'm thinking
>
> that the callbacks are not being fired fast enough, but would like to avoid
>
> polling.
>
>
Actually, more likely is that the callback is being called too often,
>
producing thrash. A sample would confirm that (as would a simple
>
counter that counted the number of times the callback fired, and the
>
number of bytes read each time). If the sample shows a lot of time
>
being spent in your callback (or you are reading a fairly small number
>
of bytes with each call), you're thrashing - you want a sample that
>
sits fairly quietly in CFRunLoopRun most of the time.
>
>
To get best performance, you actually want to let the kernel build up a
>
decent buffer of bytes from the socket, then read all of them at once.
>
For specifically HTTP transactions, we've found we get the best
>
performance when our callback is only called once for the header and
>
once for the body of the response (barring particularly large
>
responses, which you want to read in chunks of at least 16K at a time -
>
larger is probably better). You can do this by making sure you only
>
read from the socket in fairly large chunks (if you read with a buffer
>
size that matches the socket's buffer size, you can be guaranteed to
>
exhaust the socket's buffer with a single read), and that you do not
>
read too often. Not reading too often is hard to accomplish - you can
>
tune it somewhat by playing around with the low water mark on the
>
socket, but that's no guarantee....
>
>
I don't know, but I'm guessing OT was doing some of this socket
>
management for you, perhaps allowing you to read in fairly small chunks
>
from data that was pulled from the socket in one large chunk, and
>
that's why it performs better. CFSocket sits pretty directly on top of
>
BSD sockets, with almost no intermediate layering done on your behalf.
>
>
Oh, and for HTTP, you can always use CFNetwork (look at
>
CFNetwork/CFHTTPStream.h); it's the engine behind Safari, and yields
>
fairly good performance without exposing you to the details of managing
>
the socket. You will still need to read from the stream in fairly
>
large chunks, though - in general, reading just a few bytes many times
>
is a recipe for poor performance.
>
>
Hope that helps,
>
REW
This raises a question I've long had: in HTTP, what is the best way to read
in the HTTP header? The only way that makes sense to me is to read one byte
at a time until I see the header terminator, 0x0D0A0D0A, because there is no
way of knowing how big the header is. Am I missing something?
--
Larry Gerndt
AIM Handle: SonOfTheSonOfMan
Let the truth be told though the heavens fall -- James Garrison
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.