Skip to content

Commit 3573d30

Browse files
committed
iOS,macOS: Compile Swift with opt flags in opt builds
We never passed optimisation flags to `swiftc`, and the compiler's default is `-Onone`, so all of the Darwin embedders' Swift shipped unoptimised in profile and release, and `assert()`s triggered in shipping binaries, as seen in flutter#190909. This updates our optimisation gn configs to set equivalent `swiftflags` alongside the `cflags` we already set. For iOS that means `-Osize` (to match `-Os` for Obj-C/C++). For macOS, that means `-O` (to match `-O2` for Obj-C/C++, Swift just has -O, no -O2, -O3). For both, we set and `-Onone` for `:no_optimize`. When `is_debug` is true, we default to unopt. Swift `assert()` behaves like `FML_DCHECK` (active in unopt, compiled out in opt builds) and we should use `precondition()` where we'd use `FML_CHECK` -- i.e. cases we want to assert even in release builds. The following dev-specific asserts are now compiled out from shipped engines: * `DisplayLinkManager.shared` main-thread check * `KeyboardInsetManager` vsync client internal-state check * `FlutterRunLoop.mainRunLoop` the guard check already triggers * `TraceScope.deinit` ensure `end()` was called This also moves the main-thread check in `FlutterRunLoop.ensureMainLoopInitialized` to a `precondition`. Binding the main run loop to the wrong thread silently leaves everything later scheduled on it running on the wrong thread so we should continue to blow up in all modes. This change chops 130272 bytes (0.79%) off `Flutter.framework` on `ios_release`. Issue: flutter#191616 Related: flutter#190909 No new tests, since this is a compile config change: the build *is* the test, along with not breaking the existing tests.
1 parent 64e1cfe commit 3573d30

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

‎engine/src/build/config/compiler/BUILD.gn‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ config("optimize") {
10161016
cflags = [ "/O1" ] + common_optimize_on_cflags + [ "/Oi" ]
10171017
} else if (is_ios) {
10181018
cflags = [ "-Os" ] + common_optimize_on_cflags # Favor size over speed.
1019+
swiftflags = [ "-Osize" ]
10191020
} else if (is_android || is_fuchsia) {
10201021
cflags = [ "-Oz" ] + common_optimize_on_cflags # Favor size over speed.
10211022
} else if (is_wasm) {
@@ -1027,8 +1028,10 @@ config("optimize") {
10271028
} else {
10281029
if (optimize_for_size) {
10291030
cflags = [ "-Os" ] + common_optimize_on_cflags
1031+
swiftflags = [ "-Osize" ]
10301032
} else {
10311033
cflags = [ "-O2" ] + common_optimize_on_cflags
1034+
swiftflags = [ "-O" ]
10321035
}
10331036
}
10341037

@@ -1070,6 +1073,7 @@ config("no_optimize") {
10701073
ldflags = common_optimize_on_ldflags
10711074
} else {
10721075
cflags = [ "-O0" ]
1076+
swiftflags = [ "-Onone" ]
10731077
}
10741078
}
10751079

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterRunLoop.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ import Foundation
9292
// Ensures that the `FlutterRunLoop` for main thread is initialized. Only
9393
// needs to be called once and must be called on the main thread.
9494
@objc static func ensureMainLoopInitialized() {
95-
assert(Thread.isMainThread, "Must be called on the main thread.")
95+
precondition(Thread.isMainThread, "Must be called on the main thread.")
9696
if _mainRunLoop == nil {
9797
_mainRunLoop = FlutterRunLoop()
9898
}

0 commit comments

Comments
 (0)