mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Motivation:
Using some limited parallelism can improved the speed of the copy for very limited increase in cpu use.
In circumstances where the source and/or destination are on a high latency device, such as a network file system,
the resulting speed up can be even more significant.
Providing conservative defaults, while allowing users the ability to control it directly if desired, provides for a faster default and access to user targeted gains in performance.
The use of parallel by default is considered in keeping with the swift-nio model of concurrent capabilities.
The parallelism used is restricted because the underlying limited resource for this is highly likely to be limited system file descriptors, rather than the limits of the underlying executor itself, especially on embedded devices with significantly more limited concurrent descriptors.
Modifications:
All copyItem overloads except ```copyItem(sourcePath:destinationPath:shouldProceedAfterError:shouldCopyFile)```
can, and likely will, use concurrent copies when copying directories.
A Newly type ```CopyStrategy``` controls the behaviour, allowing:
* ```parallel(maxDescriptors: Int)```
* Parallel limited to a maximum number of concurrently open file descriptors.
* ```sequential```
* The previous behaviour, still asynchronous but only one operation at a time
* ```platformDefault```
* Whatever the API determines the default should be from the prior options
The cancellation semantics of copyItem were previously unclear/undocumented.
These are now stated and cancellation is tested.
There were some aspects of naming/types on this API which were inconsistent/unclear. Those have been altered to aid clarity.
* Less duplication with the protocol docs
* Consistent use of `item` not `file`
* Use DirectoryEntry for the source in both ```shouldXxx``` callbacks
NIOFileSystem package status remains unchanged, it is still _NIOFileSystem with the semantics that entails.
Result:
For any use of the following functions on FileSystemProtocol, when backed by FileSystem:
- ```copyItem(*)```:
The shouldCopyItem check happens before the internal FileSystem "do I know how to copy this sort of FileType" check. This makes the code a little simpler, but the desire is to allow consuming code to filter out anything unexpected if they like without needing to trap errors.
- ```copyItem(sourcePath:destinationPath)```:
Depending on the platform this will become parallel, users on MacOS, Linux and Windows will have up to 4 concurrent file system operations at once.
Empirical testing on a MacBook pro with a significant dense directory hierarchy of 12 GiB had performance improvements of ~50% reduction in elapsed time for CPU usage increases below the noise floor of the testing
- ```copyItem(sourcePath:destinationPath:shouldProceedAfterError:shouldCopyFile)```:
When migrating from this to ```copyItem(sourcePath:destinationPath:copyStrategy:shouldProceedAfterError:shouldCopyItem)```care should be taken to ensure the should callbacks are safe to use in different concurrency domains, or that CopyStrategy.sequential is explicitly stated.
The backward compatibility shim provided does this by default.
163 lines
7.0 KiB
Swift
163 lines
7.0 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2024 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) || os(Linux) || os(Android)
|
|
import NIOCore
|
|
|
|
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
|
|
extension FileSystem {
|
|
|
|
/// Iterative implementation of a recursive parallel copy of the directory from `sourcePath` to `destinationPath`.
|
|
///
|
|
/// The parallelism is solely at the level of individual items (so files, symbolic links and directories), a larger file
|
|
/// is not considered for being 'split' into concurrent reads or wites.
|
|
/// If any symbolic link is encountered then only the link is copied.
|
|
/// The copied items will preserve permissions and any extended attributes (if supported by the file system).
|
|
///
|
|
/// Note: `maxConcurrentOperations` is used as a hard (conservative) limit on the number of open file descriptors at any point.
|
|
/// Operations are assume to consume 2 descriptors so maximum open descriptors is `maxConcurrentOperations * 2`
|
|
@usableFromInline
|
|
func copyDirectoryParallel(
|
|
from sourcePath: FilePath,
|
|
to destinationPath: FilePath,
|
|
maxConcurrentOperations: Int,
|
|
shouldProceedAfterError: @escaping @Sendable (
|
|
_ entry: DirectoryEntry,
|
|
_ error: Error
|
|
) async throws -> Void,
|
|
shouldCopyItem: @escaping @Sendable (
|
|
_ source: DirectoryEntry,
|
|
_ destination: FilePath
|
|
) async -> Bool
|
|
) async throws {
|
|
// Implemented with NIOAsyncSequenceProducer rather than AsyncStream.
|
|
// It is approximately the same speed in the best case but has significantly less variance.
|
|
// NIOAsyncSequenceProducer also enforces a multi producer single consumer access pattern.
|
|
let copyRequiredQueue = NIOAsyncSequenceProducer.makeSequence(
|
|
elementType: DirCopyItem.self,
|
|
backPressureStrategy: NoBackPressureStrategy(),
|
|
finishOnDeinit: false,
|
|
delegate: DirCopyDelegate()
|
|
)
|
|
|
|
// We ignore the result of yield in all cases because we are not implementing back pressure
|
|
// and cancellation is dealt with separately.
|
|
@Sendable func yield(_ contentsOf: [DirCopyItem]) {
|
|
_ = copyRequiredQueue.source.yield(contentsOf: contentsOf)
|
|
}
|
|
|
|
// Kick start the procees by enqueuing the root entry,
|
|
// the calling function already validated the root needed copying.
|
|
_ = copyRequiredQueue.source.yield(
|
|
.toCopy(from: .init(path: sourcePath, type: .directory)!, to: destinationPath)
|
|
)
|
|
|
|
// The processing of the very first item (the root) will increment this,
|
|
// after then when it hits zero we've finished.
|
|
// This does not need to be a ManagedAtomic or similar because:
|
|
// - All maintenance of state is done in the withThrowingTaskGroup callback
|
|
// - All actual file system work is done by tasks created on the `taskGroup`
|
|
var activeDirCount = 0
|
|
|
|
// Despite there being no 'result' of each operation we cannot use a discarding task group
|
|
// because we use the 'drain results' queue as a concurrency limiting side effect.
|
|
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
|
|
|
|
// Code handling each item to process on the current task
|
|
// Side Effects:
|
|
// - Updates activeDirCount and finishes the stream if required.
|
|
// - Either adds a single task on `taskGroup` or none.
|
|
// Returns true if it added a task, false otherwise.
|
|
func onNextItem(_ item: DirCopyItem) -> Bool {
|
|
switch item {
|
|
case .endOfDir:
|
|
activeDirCount -= 1
|
|
if activeDirCount == 0 {
|
|
copyRequiredQueue.source.finish()
|
|
}
|
|
return false
|
|
case let .toCopy(from: from, to: to):
|
|
if from.type == .directory {
|
|
activeDirCount += 1
|
|
}
|
|
taskGroup.addTask {
|
|
try await copySelfAndEnqueueChildren(
|
|
from: from,
|
|
to: to,
|
|
yield: yield,
|
|
shouldProceedAfterError: shouldProceedAfterError,
|
|
shouldCopyItem: shouldCopyItem
|
|
)
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
let iter = copyRequiredQueue.sequence.makeAsyncIterator()
|
|
|
|
// inProgress counts the number of tasks we have added to the task group
|
|
// Get up to the maximum concurrency first.
|
|
// We haven't started monitoring for task completion, so inProgress is 'worst case'.
|
|
var inProgress = 0
|
|
while inProgress <= maxConcurrentOperations {
|
|
let item = await iter.next()
|
|
if let item = item {
|
|
if onNextItem(item) {
|
|
inProgress += 1
|
|
}
|
|
} else {
|
|
// Either we completed things before we hit the limit or we were cancelled.
|
|
// In the latter case we choose to propagate the cancel clearly.
|
|
// This makes testing for the cancellation more reliable.
|
|
try Task.checkCancellation()
|
|
return
|
|
}
|
|
}
|
|
|
|
// Then operate one in (finish) -> one out (start another),
|
|
// but only for items that trigger a task.
|
|
while let _ = try await taskGroup.next() {
|
|
var keepConsuming = true
|
|
while keepConsuming {
|
|
let item = await iter.next()
|
|
if let item = item {
|
|
keepConsuming = !onNextItem(item)
|
|
} else {
|
|
keepConsuming = false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// An 'always ask for more' no back-pressure strategy for a ``NIOAsyncSequenceProducer``.
|
|
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
|
private struct NoBackPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategy {
|
|
mutating func didYield(bufferDepth: Int) -> Bool { true }
|
|
|
|
mutating func didConsume(bufferDepth: Int) -> Bool { true }
|
|
}
|
|
|
|
/// We ignore back pressure, the inherent handle limiting in copyDirectoryParallel means it is unnecessary.
|
|
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
|
|
private struct DirCopyDelegate: NIOAsyncSequenceProducerDelegate, Sendable {
|
|
@inlinable
|
|
func produceMore() {}
|
|
|
|
@inlinable
|
|
func didTerminate() {}
|
|
}
|
|
#endif
|