Better buffer management

enricodias4654

Member
YetiShare User
Jan 13, 2015
411
1
16
Hello.

I notice that the current file.class.php reads the file using $buffer = fgets($handle, 4096) and calculate a usleep time in each loop to control the download speed. With this setup I'm consuming 10% processing power for each 100mbps on a xeon E5-1650v2. It's too much for such a simple loop.

I've ran some tests and found some interesting things:

fgets will return content on each new line, many times returning less than 4096 bytes in each loop. fread is better.
4096 bytes is quite small. Using a higher buffer reduces the number of loops and cpu usage.
Even knowing that a single I/O will return the maximum of 8192 bytes, executing fread with a value greater than 8192 will result in several operations of 8192 bytes but it will use less cpu time than reading 8192 per loop.
Using multiples of 4096 is better since 4k is the default block size in most servers.

Using a 128kb buffer I'm experiencing a reduction of 60% in cpu usage so far.


Obs.: Note that the buffer size must be <= $speed to ensure 1 loop on every second.