iCharlesHu / swift-experimental-subprocess

A prototype to the new Swift Subprocess
Apache License 2.0
9 stars 2 forks source link

performance issue #9

Open weissi opened 1 week ago

weissi commented 1 week ago

For a simple PerfTester written like this

import SwiftExperimentalSubprocess
import Foundation

@main
struct Main {
    static func main() async throws {
        let r = try await Subprocess.run(
            .at("/bin/bash"),
            arguments: ["-c", "cat"],
            input: .readFrom(.standardInput, closeAfterProcessSpawned: false)
        ) { p in
            var buffer: [UInt8] = []
            buffer.reserveCapacity(1024 * 1024)
            for try await b in p.standardOutput {
                buffer.append(b)
                if buffer.count >= 1024 * 1024 {
                    write(STDOUT_FILENO, buffer, buffer.count)
                    buffer.removeAll(keepingCapacity: true)
                }
            }
            write(STDOUT_FILENO, buffer, buffer.count)
        }
        print(r)
    }
}

compiled in release mode, that's the result

$ head -c "$((1 * 1024 * 1024))" /dev/zero | time .build/release/PerfTester  > /dev/null
        8.33 real         2.94 user         5.69 sys

which is 122 kB/s on my M2 Max machine. I would expect something between 1 and 3 GiB/s.

weissi commented 1 week ago

Apple folks: rdar://139888114