Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Copying optimization through multiple threads

Can you make file copying faster through multiple threading?

Edit: To clarify, suppose you were implementing CopyFile(src, tgt). It seems logical that under certain circumstances you could use multiple threads to make it go faster.

Edit Some more thoughts:

Naturally, it depends on the HW/storage in question.

If you're copying from one disk to another, for example, it's pretty clear that you can read/write at the same time using two threads, thus saving the performance cost of the fastest of the two (usually reading). But you don't really need multiple threads for reading/writing in parallel, just async-IO.

But if async-IO can really speed things up (up to 2x) when reading/writing from different disks, why isn't this the default implementation of CopyFile? (or is it?)

like image 834
Assaf Lavie Avatar asked Feb 11 '09 18:02

Assaf Lavie


2 Answers

If you are not careful you can make it slower. Disks are good at serialized access, if you have multiple threads the disk heads will be all over the place. Now if you are dealing with a high performance SAN maybe you have an improvement in performance, and the SAN will deal with optimizing the disk access.

like image 127
Otávio Décio Avatar answered Nov 10 '22 17:11

Otávio Décio


Here's a blog post about file copy performance improvements in Vista SP1:

http://blogs.technet.com/markrussinovich/archive/2008/02/04/2826167.aspx

Doing high performance file copy is crazy and you have to take into account things like the cache behavior and network drivers limitations.

So always use the OS file copy function (under Windows it's FileCopyEx) and don't write your own.

like image 38
Nir Avatar answered Nov 10 '22 15:11

Nir