Skip to content

Commit 30b8eb6

Browse files
Change the default optimization level to -O2 for wasm in release mode. (flutter#162917)
As per the investigation in flutter#162620, we determined that the soundness we gain from using `-O2` outweighs the perf benefits of `-O4`.
1 parent c5fbe83 commit 30b8eb6

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

packages/flutter_tools/lib/src/web/compiler_config.dart

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,9 @@ sealed class WebCompilerConfig {
2121
/// Build environment flag for [sourceMaps].
2222
static const String kSourceMapsEnabled = 'SourceMaps';
2323

24-
/// Calculates the optimization level for dart2js/dart2wasm for the given
24+
/// Calculates the optimization level for the compiler for the given
2525
/// build mode.
26-
int optimizationLevelForBuildMode(BuildMode mode) =>
27-
optimizationLevel ??
28-
switch (mode) {
29-
BuildMode.debug => 0,
30-
BuildMode.profile || BuildMode.release => 4,
31-
BuildMode.jitRelease => throw ArgumentError('Invalid build mode for web'),
32-
};
26+
int optimizationLevelForBuildMode(BuildMode mode);
3327

3428
/// The compiler optimization level specified by the user.
3529
///
@@ -111,13 +105,15 @@ class JsCompilerConfig extends WebCompilerConfig {
111105
];
112106

113107
@override
114-
int optimizationLevelForBuildMode(BuildMode mode) {
115-
final int level = super.optimizationLevelForBuildMode(mode);
116-
117-
// dart2js optimization level 0 is not well supported. Use
118-
// 1 instead.
119-
return level == 0 ? 1 : level;
120-
}
108+
int optimizationLevelForBuildMode(BuildMode mode) =>
109+
optimizationLevel ??
110+
switch (mode) {
111+
// dart2js optimization level 0 is not well supported. Use
112+
// 1 instead.
113+
BuildMode.debug => 1,
114+
BuildMode.profile || BuildMode.release => 4,
115+
BuildMode.jitRelease => throw ArgumentError('Invalid build mode for web'),
116+
};
121117

122118
/// Arguments to use in the full JS compile, but not CFE-only.
123119
///
@@ -163,6 +159,19 @@ class WasmCompilerConfig extends WebCompilerConfig {
163159
@override
164160
CompileTarget get compileTarget => CompileTarget.wasm;
165161

162+
@override
163+
int optimizationLevelForBuildMode(BuildMode mode) =>
164+
optimizationLevel ??
165+
switch (mode) {
166+
BuildMode.debug => 0,
167+
168+
// The optimization level of O2 uses only sound optimizations. We default
169+
// to this level because our web benchmarks have shown that the difference
170+
// between O2 and O4 is marginal enough that we would prefer soundness here.
171+
BuildMode.profile || BuildMode.release => 2,
172+
BuildMode.jitRelease => throw ArgumentError('Invalid build mode for web'),
173+
};
174+
166175
List<String> toCommandOptions(BuildMode buildMode) {
167176
final bool stripSymbols = buildMode == BuildMode.release && stripWasm;
168177
return <String>[

packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ void main() {
11401140
WebRendererMode.canvaskit,
11411141
WebRendererMode.skwasm,
11421142
]) {
1143-
for (int level = 1; level <= 4; level++) {
1143+
for (final int? level in <int?>[null, 0, 1, 2, 3, 4]) {
11441144
for (final bool strip in <bool>[true, false]) {
11451145
for (final List<String> defines in const <List<String>>[
11461146
<String>[],
@@ -1151,6 +1151,13 @@ void main() {
11511151
test(
11521152
'Dart2WasmTarget invokes dart2wasm with renderer=$renderer, -O$level, stripping=$strip, defines=$defines, modeMode=$buildMode sourceMaps=$sourceMaps',
11531153
() => testbed.run(() async {
1154+
final int expectedLevel =
1155+
level ??
1156+
switch (buildMode) {
1157+
'debug' => 0,
1158+
'profile' || 'release' => 2,
1159+
_ => throw UnimplementedError(),
1160+
};
11541161
environment.defines[kBuildMode] = buildMode;
11551162
environment.defines[kDartDefines] = encodeDartDefines(defines);
11561163

@@ -1182,7 +1189,7 @@ void main() {
11821189
],
11831190
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
11841191
'--extra-compiler-option=--depfile=${depFile.absolute.path}',
1185-
'-O$level',
1192+
'-O$expectedLevel',
11861193
if (strip && buildMode == 'release') '--strip-wasm' else '--no-strip-wasm',
11871194
if (!sourceMaps) '--no-source-maps',
11881195
if (buildMode == 'debug') '--extra-compiler-option=--enable-asserts',

0 commit comments

Comments
 (0)