Skip to content

Commit 08d2a47

Browse files
authored
iOS,macOS: Compile Swift with opt flags in opt builds (flutter#191622)
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 (demonstrates existing behaviour) No new tests, since this is a compile config change: the build *is* the test, along with not breaking the existing tests. <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent cc0c10e commit 08d2a47

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)