Reputation: 2478
I see that simple filesystem actions in System.IO are not async (move file, delete file). Following that principle - should small queries be async? If so what is boundary of small query? <5 ms? <50 ms?
Upvotes: 2
Views: 81
Reputation: 63732
There's little reason to use synchronous calls when asynchronous APIs are available. The tricky part is that many operations don't have asynchronous APIs.
The fundamentals of how System.IO
works are rooted in design decisions made a long time in the past, when things like network drives (much less "cloud storage") were relatively rare, and drives were small. Operations like "open file" have no asynchronous APIs, and some others that do have asynchronous APIs don't necessarily have them available in .NET.
You rarely need to consider the cost of an asynchronous call - in comparison to any I/O operation, it's going to be negligible. So the trick is to think about the cases where actual I/O isn't involved - for example, when reading from a file byte-by-byte, you actually spend most of the time reading from an in-memory buffer, making the async overhead significant. But the alternative isn't using synchronous APIs, it's ensuring the buffering proceeds at reasonable chunks of data, rather than individual bytes.
There's nothing that makes asynchronous APIs inherently slower. For example, the synchronous file APIs in Windows still call the asynchronous APIs - and then wait on the result (there's a few differences in flags, but nothing important). There hasn't been a real synchronous I/O operation in the desktop Windows world since about Windows 2000 (IIRC the support in the 95-era Windows was a bit... trickier).
If the filesystem APIs were designed today, File.Move
would certainly be an asynchronous operation. And indeed, in Windows API, there are two more modern variants - MoveFileEx
and MoveFileWithProgress
, which are both asynchronous. The only problem is that .NET doesn't expose those directly, so you'd need to use P/Invoke to call them. Needless to say, they are very useful on network shares.
Upvotes: 2