Slow asynchronous dart:io call with a faster synchronous form
ID |
dart.performance.avoid_slow_async_io |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Io |
Language |
Dart |
Description
Reports the dart:io metadata calls whose asynchronous form is slower than its synchronous counterpart - exists, stat, lastModified on File, Directory and Link, and the FileSystemEntity.isDirectory, isFile, isLink and type statics.
Rationale
These operations do not block: they read an inode the operating system answers from cache, and the work is over long before a thread would have yielded. Running them asynchronously therefore buys nothing and costs the overhead of the async machinery - marshalling the request to the I/O thread pool, allocating the Future, and scheduling the continuation - which measurably exceeds the call itself and forces every caller up the chain to become async as well. The synchronous form does the same work in one step.
The following code illustrates the pattern detected by this rule:
class Assets {
Future<bool> present(String path) async {
// FLAGGED: Slow asynchronous dart:io call with a faster synchronous form
return await File(path).exists();
}
}