Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from a socket 1 byte a time vs reading in large chunk

Tags:

c++

c

sockets

cgi

What's the difference - performance-wise - between reading from a socket 1 byte a time vs reading in large chunk?

I have a C++ application that needs to pull pages from a web server and parse the received page line by line. Currently, I'm reading 1 byte at a time until I encounter a CRLF or the max of 1024 bytes is reached.

If reading in large chunk(e.g. 1024 bytes at a time) is a lot better performance-wise, any idea on how to achieve the same behavior I currently have (i.e. being able to store and process 1 html line at a time - until the CRLF without consuming the succeeding bytes yet)?

EDIT:

I can't afford too big buffers. I'm in a very tight code budget as the application is used in an embedded device. I prefer keeping only one fixed-size buffer, preferrably to hold one html line at a time. This makes my parsing and other processing easy as I am by anytime I try to access the buffer for parsing, I can assume that I'm processing one complete html line.

Thanks.

like image 307
green_t Avatar asked May 31 '09 09:05

green_t


1 Answers

I can't comment on C++, but from other platforms - yes, this can make a big difference; particularly in the amount of switches the code needs to do, and the number of times it needs to worry about the async nature of streams etc.

But the real test is, of course, to profile it. Why not write a basic app that churns through an arbitrary file using both approaches, and test it for some typical files... the effect is usually startling, if the code is IO bound. If the files are small and most of your app runtime is spent processing the data once it is in memory, you aren't likely to notice any difference.

like image 134
Marc Gravell Avatar answered Sep 28 '22 02:09

Marc Gravell