`swift run` really slow

Hi! I'm new to Swift development :slight_smile:

Using swift run is really slow for me whereas swift build and then running the binary isn't. Why is that?

Note that I'm using VSCode + the swift CLI, not xcode

swift run takes 3-ish seconds:

$ time swift run
Building for debugging...
Build complete! (0.09s)
Hello world
swift run  0.09s user 0.06s system 4% cpu 3.345 total

swift build and manually running takes 0.4s:

$ time (swift build && ./.build/debug/api)
Building for debugging...
Build complete! (0.08s)
Hello world
( swift build && ./.build/debug/api; )  0.00s user 0.00s system 0% cpu 0.427 total

Am I doing anything wrong? I'm on MacOS 14.0

2 Likes

I don't use the command myself, but it appears swift run is a wrapper which invokes lots of other commands to do real work. You can run swift run --vv to see the details and perhaps find which child process it spends most of the time waiting for on your system.

Thanks! It seems to be stuck on this for 3s:

debug: /usr/bin/xcrun --sdk xros --show-sdk-platform-path

Do you maybe have any idea what could be causing this?

I'm not familiar with it. The command runs fine on my system. The general approach to investigate issues like this is using process tracing tool. You can try dtruss if you're interested.

1 Like

Thank you :) I really appreciate your help

Hello, the specific reason seems to be unknown. I suggest that you ensure your versions are the latest to get optimized performance.

You may check the Swift version with: swift --version

I'm on 5.9:

$ swift --version
swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)
Target: arm64-apple-macosx14.0

Same for me, it's taking about 3 seconds and it's slow. Swift 5.9.

Sounds like it's looking for VisionOS or something.

1 Like

Yeah, it seems like all the SDK searches are buggy and error out, at least for me. It can't even find macOS.

~ % /usr/bin/xcrun --sdk macOS --show-sdk-platform-path
2023-10-16 10:33:35.909 xcodebuild[79763:19248010] Writing error result bundle to /var/folders/c8/jdfwl8sj2f15x8vlx441smjc0000gn/T/ResultBundle_2023-16-10_10-33-0035.xcresult
xcodebuild: error: SDK "macOS" cannot be located.
2023-10-16 10:33:37.424 xcodebuild[79765:19248029] Writing error result bundle to /var/folders/c8/jdfwl8sj2f15x8vlx441smjc0000gn/T/ResultBundle_2023-16-10_10-33-0037.xcresult
xcodebuild: error: SDK "macOS" cannot be located.
xcrun: error: Failed to open property list '/Users/user/macOS/SDKSettings.plist'
2023-10-16 10:33:38.932 xcodebuild[79766:19248044] Writing error result bundle to /var/folders/c8/jdfwl8sj2f15x8vlx441smjc0000gn/T/ResultBundle_2023-16-10_10-33-0038.xcresult
xcodebuild: error: SDK "macOS" cannot be located.
xcrun: error: unable to lookup item 'PlatformPath' in SDK 'macOS'
1 Like

@merlindru , Your Swift version is well-updated. The reason seems to be unknown to me. I would suggest that you may trace using dtruss tool on MacOS - this may help to trace and debug.

I have also noticed before 5.9 swift run was really slow. As I use it in a monorepo to do some jobs in all the packages frequently but the code does not change often I decided to diff the folder and on changes I run swift run normally and without changes swift run --skip-build this increases the speed but still nog compared to running the executable directly.

I would like swift run to do

  1. diff for me to see if it should rebuild
  2. run the executable

It seams to do a lot more and I cannot see why it does that. Thanks for investigating why that is! I will do the same.

Could we file a here? apple/swift-package-manager: The Package Manager for the Swift Programming Language

Perhaps I found the cause. In conclusion, using regular Xcode, which is not beta, the SDK for VisionOS does not exist, so I saw that the search was taking a long time.

I ran swift run on Xcode 15 beta (Xcode 15.1, Build version 15C5042i) and it took less than 0.7 seconds.

$ time (swift run)
Building for debugging...
Build complete! (0.13s)
Hello, world!
( swift run; )  0.04s user 0.04s system 12% cpu 0.651 total

I run /usr/bin/xcrun --sdk xros --show-sdk-platform-path, which took a long time to execute, with not beta, beta and compare the execution, the former fails and the latter succeeds.

# not beta
$ xcodebuild -version
Xcode 15.0.1
Build version 15A507
   
$ /usr/bin/xcrun --sdk xros --show-sdk-platform-path
2023-11-05 12:36:18.975 xcodebuild[23854:206385] Writing error result bundle to /var/folders/bw/c9dsq_vn3ld19lv5yf57n50m0000gn/T/ResultBundle_2023-05-11_12-36-0018.xcresult
xcodebuild: error: SDK "xros" cannot be located.
2023-11-05 12:36:20.426 xcodebuild[23855:206411] Writing error result bundle to /var/folders/bw/c9dsq_vn3ld19lv5yf57n50m0000gn/T/ResultBundle_2023-05-11_12-36-0020.xcresult
xcodebuild: error: SDK "xros" cannot be located.
xcrun: error: unable to lookup item 'PlatformPath' in SDK 'xros'

# beta
$ xcodebuild -version
Xcode 15.1
Build version 15C5042i
   
$ /usr/bin/xcrun --sdk xros --show-sdk-platform-path
/Applications/Xcode-beta2_15_1.app/Contents/Developer/Platforms/XROS.platform
1 Like