mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Introduce parallel removal of items, instead of only doing a sequential removal (and discovery) one-by-one ### Motivation: #2933 mentioned this function call appears to be slow. If we attempt to delete a directory with lots of subdirectories, we end up discovering each subdirectory one by one on a single thread. ### Modifications: This PR refactors the logic to remove items. Instead of sequentially discovering and deleting items one by one, we traverse all directories up to a maximum concurrency, and issue file deletions in parallel within each directory. ### Result: The new concurrent implementation was measured to be ~1.4x faster than `FileManager`, ~1.5x faster than `rm -rf` and ~3x faster than the current sequential implementation on `main`. This new concurrent implementation does use the most CPU out of all the other approaches. Results are broken down into two sections. We delete a copy of the `swift-nio` repository, where the `.build/` folder has grown to a large size, providing lots of additional files and subdirectories. We produce a single CLI using `--configration=release` that can switch between using the `.sequential` mode (what is currently available on the `main` branch), `.parallel` (the new concurrent implementation in this PR) and `filemanager` (uses `Foundation.FileManager` as a comparison). Resolves https://github.com/apple/swift-nio/issues/2933 #### Normal size **Description:** The `swift-nio` library as found when building the CLI used for benchmarking. **Benchmark run with:** 3579 directories, 11657 files ``` $ hyperfine --runs 10 --warmup 2 --prepare "rm -rf /tmp/swift-nio && cp -r $(pwd) /tmp/swift-nio" ".build/release/NIOTestCLI --sequential /tmp/swift-nio" ".build/release/NIOTestCLI --parallel /tmp/swift-nio" ".build/release/NIOTestCLI --filemanager /tmp/swift-nio" "rm -rf /tmp/swift-nio" Benchmark 1: .build/release/NIOTestCLI --sequential /tmp/swift-nio Time (mean ± σ): 2.712 s ± 0.053 s [User: 0.526 s, System: 8.632 s] Range (min … max): 2.625 s … 2.781 s 10 runs Benchmark 2: .build/release/NIOTestCLI --parallel /tmp/swift-nio Time (mean ± σ): 896.7 ms ± 25.6 ms [User: 302.9 ms, System: 5313.5 ms] Range (min … max): 853.2 ms … 942.1 ms 10 runs Benchmark 3: .build/release/NIOTestCLI --filemanager /tmp/swift-nio Time (mean ± σ): 1.337 s ± 0.076 s [User: 0.022 s, System: 1.253 s] Range (min … max): 1.252 s … 1.508 s 10 runs Benchmark 4: rm -rf /tmp/swift-nio Time (mean ± σ): 1.483 s ± 0.037 s [User: 0.015 s, System: 1.416 s] Range (min … max): 1.413 s … 1.541 s 10 runs Summary .build/release/NIOTestCLI --parallel /tmp/swift-nio ran 1.49 ± 0.10 times faster than .build/release/NIOTestCLI --filemanager /tmp/swift-nio 1.65 ± 0.06 times faster than rm -rf /tmp/swift-nio 3.02 ± 0.10 times faster than .build/release/NIOTestCLI --sequential /tmp/swift-nio ``` #### Large size **Description:** The `swift-nio` library as found after re-running the `.github` workflow many times locally. I'm unable to reproduce this directory size, but have benchmarked against it. **Benchmark run with:** 15260 directories, 46842 files ``` $ hyperfine --runs 10 --warmup 2 --prepare "rm -rf /tmp/swift-nio && cp -r $(pwd) /tmp/swift-nio" ".build/release/NIOTestCLI --sequential /tmp/swift-nio" ".build/release/NIOTestCLI --parallel /tmp/swift-nio" ".build/release/NIOTestCLI --filemanager /tmp/swift-nio" "rm -rf /tmp/swift-nio" Benchmark 1: .build/release/NIOTestCLI --sequential /tmp/swift-nio Time (mean ± σ): 12.082 s ± 0.354 s [User: 2.180 s, System: 41.547 s] Range (min … max): 11.717 s … 12.923 s 10 runs Benchmark 2: .build/release/NIOTestCLI --parallel /tmp/swift-nio Time (mean ± σ): 4.770 s ± 0.102 s [User: 1.357 s, System: 25.785 s] Range (min … max): 4.570 s … 4.883 s 10 runs Benchmark 3: .build/release/NIOTestCLI --filemanager /tmp/swift-nio Time (mean ± σ): 6.503 s ± 0.115 s [User: 0.081 s, System: 5.427 s] Range (min … max): 6.262 s … 6.687 s 10 runs Benchmark 4: rm -rf /tmp/swift-nio Time (mean ± σ): 7.056 s ± 0.246 s [User: 0.064 s, System: 6.119 s] Range (min … max): 6.435 s … 7.233 s 10 runs Summary .build/release/NIOTestCLI --parallel /tmp/swift-nio ran 1.36 ± 0.04 times faster than .build/release/NIOTestCLI --filemanager /tmp/swift-nio 1.48 ± 0.06 times faster than rm -rf /tmp/swift-nio 2.53 ± 0.09 times faster than .build/release/NIOTestCLI --sequential /tmp/swift-nio ``` --------- Co-authored-by: George Barnett <gbarnett@apple.com>
107 lines
3.2 KiB
Plaintext
107 lines
3.2 KiB
Plaintext
|
|
The SwiftNIO Project
|
|
====================
|
|
|
|
Please visit the SwiftNIO web site for more information:
|
|
|
|
* https://github.com/apple/swift-nio
|
|
|
|
Copyright 2017, 2018 The SwiftNIO Project
|
|
|
|
The SwiftNIO Project licenses this file to you under the Apache License,
|
|
version 2.0 (the "License"); you may not use this file except in compliance
|
|
with the License. You may obtain a copy of the License at:
|
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
License for the specific language governing permissions and limitations
|
|
under the License.
|
|
|
|
Also, please refer to each LICENSE.<component>.txt file, which is located in
|
|
the 'license' directory of the distribution file, for the license terms of the
|
|
components that this product depends on.
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
This product is heavily influenced by Netty.
|
|
|
|
* LICENSE (Apache License 2.0):
|
|
* https://github.com/netty/netty/blob/4.1/LICENSE.txt
|
|
* HOMEPAGE:
|
|
* https://netty.io
|
|
|
|
---
|
|
|
|
This product contains NodeJS's llhttp.
|
|
|
|
* LICENSE (MIT):
|
|
* https://github.com/nodejs/llhttp/blob/1e1c5b43326494e97cf8244ff57475eb72a1b62c/LICENSE-MIT
|
|
* HOMEPAGE:
|
|
* https://github.com/nodejs/llhttp
|
|
|
|
---
|
|
|
|
This product contains "cpp_magic.h" from Thomas Nixon & Jonathan Heathcote's uSHET
|
|
|
|
* LICENSE (MIT):
|
|
* https://github.com/18sg/uSHET/blob/c09e0acafd86720efe42dc15c63e0cc228244c32/lib/cpp_magic.h
|
|
* HOMEPAGE:
|
|
* https://github.com/18sg/uSHET
|
|
|
|
---
|
|
|
|
This product contains "sha1.c" and "sha1.h" from FreeBSD (Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project)
|
|
|
|
* LICENSE (BSD-3):
|
|
* https://opensource.org/licenses/BSD-3-Clause
|
|
* HOMEPAGE:
|
|
* https://github.com/freebsd/freebsd-src
|
|
|
|
---
|
|
|
|
This product contains a derivation of Fabian Fett's 'Base64.swift'.
|
|
|
|
* LICENSE (Apache License 2.0):
|
|
* https://github.com/swift-extras/swift-extras-base64/blob/b8af49699d59ad065b801715a5009619100245ca/LICENSE
|
|
* HOMEPAGE:
|
|
* https://github.com/fabianfett/swift-base64-kit
|
|
|
|
---
|
|
|
|
This product contains a derivation of "XCTest+AsyncAwait.swift" from AsyncHTTPClient.
|
|
|
|
* LICENSE (Apache License 2.0):
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
* HOMEPAGE:
|
|
* https://github.com/swift-server/async-http-client
|
|
|
|
---
|
|
|
|
This product contains a derivation of "_TinyArray.swift" from SwiftCertificates.
|
|
|
|
* LICENSE (Apache License 2.0):
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
* HOMEPAGE:
|
|
* https://github.com/apple/swift-certificates
|
|
|
|
---
|
|
|
|
This product contains a derivation of the mocking infrastructure from Swift System.
|
|
|
|
* LICENSE (Apache License 2.0):
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
* HOMEPAGE:
|
|
* https://github.com/apple/swift-system
|
|
|
|
---
|
|
|
|
This product contains a derivation of "TokenBucket.swift" from Swift Package Manager.
|
|
|
|
* LICENSE (Apache License 2.0):
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
* HOMEPAGE:
|
|
* https://github.com/swiftlang/swift-package-manager
|