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
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.
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.
@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
diff for me to see if it should rebuild
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.
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