Xcode is compiling all Swift files one at a time?

I have two apps; one is a subset of functionality of the other. The smaller app has about 170 Swift files. The larger app has these files plus about 120 more. So I would expect the larger app to take around twice as long to build.

Instead, the smaller app takes less than a minute to build while the larger app takes over 13 minutes to build; to be exact, it takes 15.3 times longer.

While reviewing the build report, I noticed that the smaller app compiles the Swift files in batches, with each batch taking around 10 seconds, but with up to 17 batches running at once. In contrast, the larger app compiles all 290 Swift files in one giant batch, so apparently there is no multithreading of the Swift compilation (see screen shots below). The difference in the number of batches the smaller app compiles at once roughly corresponds to the difference in overall build time.

Is there a build setting I can change to make the larger app compile Swift files in multiple smaller batches as the smaller app is doing? I checked SWIFT_COMPILATION_MODE and it's the same in both apps (Incremental for debugging and Whole Module for release) but I don't know what other settings I should check.

Answered by arlomedia in 877158022

I found it! I searched the build settings for "build" in both apps and just went down the list, comparing every setting. The only difference was User Script Sandboxing, which was Yes in the smaller app and No in the larger app. When I turned that on in the larger app, it built files in batches like the smaller app, and finished in 38 seconds -- that's 21 times faster than before. This is going to make release days so much easier.

Accepted Answer

I found it! I searched the build settings for "build" in both apps and just went down the list, comparing every setting. The only difference was User Script Sandboxing, which was Yes in the smaller app and No in the larger app. When I turned that on in the larger app, it built files in batches like the smaller app, and finished in 38 seconds -- that's 21 times faster than before. This is going to make release days so much easier.

Sigh ... now my builds are taking even longer than before, about 15 minutes. The problem was fixed immediately after changing that build setting, but when I had to prepare my next release, the exact problem was back, with all the Swift files compiling in sequence rather than in simultaneous batches. Does anyone have another explanation for this behavior?

I just spent a couple hours analyzing this with Claude Agent and found another solution. One of the Swift files contains a function getStaticData(table) that contains a switch statement based on the table name and returns an array of data for the requested table. I use this for static data like a list of the names of countries for the registration page. The function is about 5000 lines long and by looking at the build data, Claude figured out that was taking 8 or 9 minutes to optimize. We added @_optimize(none) to the function header and now the total build time is comparable to other projects of this size. Hopefully this fix will stick.

Xcode is compiling all Swift files one at a time?
 
 
Q