Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost asio socket: fastest way to read file from hard drive?

So I have tried:

int buff_length = 8192;
ifstream stream;
char* buffer = new char[buff_length];

stream.open( path.string().c_str(), ios::binary );

boost::system::error_code ignored_error;

while (stream)
{
    stream.read(buffer, buff_length);
    boost::asio::write(*socket, boost::asio::buffer(buffer, stream.gcount()),
                            boost::asio::transfer_all(), ignored_error);  
}

I wonder how you do it - how to do it faster?

My app shall work across Windows, linux and mac os. That is why I use boost alot. I use ab for testing. I want to get 2 or at least 1.5 times faster on reading and sending files. May be Boost::Iostream can help me any how?

like image 624
Rella Avatar asked Nov 05 '22 16:11

Rella


1 Answers

Not entirely sure what you're after, but to answer your question about fast reading of files you can map the file into memory.

This way you read from the memory instead of from disk. Depending on the file size and such there are different approaches that might be interesting, e.g. map whole file if small or map regions of file throughout the file as you process it if it's a large file.

In Boost.Interprocess you can read more about this here.

like image 180
murrekatt Avatar answered Nov 09 '22 04:11

murrekatt