open static method
Opens path and spawns up to maxWorkers worker isolates.
Default concurrency is min(32, max(2, cpus - 1)); each worker is
synchronous, so more workers are how a caller gets more outstanding I/O.
The 32 cap keeps isolate/mmap overhead bounded on very large machines.
direct uses the platform direct-I/O path in every worker (Windows
FILE_FLAG_NO_BUFFERING, Linux/Android O_DIRECT, macOS/iOS
F_NOCACHE) with 4 KiB-aligned requests; use it for aligned streaming,
not random access.
Bounds queued + in-flight jobs; 0 keeps the original unbounded queue.
Backpressure makes _submit wait for a completion instead of growing the
queue without limit. Worker count is still CPU-derived or explicit.
With readOnly and a file no larger than mmapReadCacheLimit, each
worker maps the whole file read-only once and serves reads from the map
(hot random reads avoid a ReadFile/pread syscall per block). Set the
limit to 0 to disable, or raise it for hot files that fit in the address
space; mapping is virtual and does not eagerly read the file.
Implementation
static Future<ParallelBytesFile> open(
String path, {
bool create = true,
bool readOnly = false,
int? maxWorkers,
bool useIocp = false,
bool direct = false,
int mmapReadCacheLimit = 1 << 30,
int maxQueue = 0,
}) async {
if (useIocp && direct) {
throw ArgumentError('useIocp and direct are mutually exclusive');
}
final workers =
maxWorkers ??
math.min(32, math.max(2, Platform.numberOfProcessors - 1));
if (workers < 1) throw RangeError.value(workers, 'maxWorkers');
if (mmapReadCacheLimit < 0) {
throw RangeError.value(mmapReadCacheLimit, 'mmapReadCacheLimit');
}
if (maxQueue < 0) throw RangeError.value(maxQueue, 'maxQueue');
final coordinator = openMmapFileSync(
path,
create: create,
readOnly: readOnly,
mmapCache: false,
);
final pool = ParallelBytesFile._(
path,
readOnly,
workers,
useIocp,
direct,
mmapReadCacheLimit,
maxQueue,
coordinator,
);
try {
await pool._spawnWorkers();
return pool;
} catch (_) {
await pool.close();
rethrow;
}
}