From c30114ae65c2d9b4d7f3a0426055ae824e1ed6fc Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Mon, 29 Aug 2022 09:26:21 -0400 Subject: [PATCH] Provide APIs for the PlatformDispatcher to set the performance mode (flutter/engine#35614) --- engine/src/flutter/lib/ui/dart_ui.cc | 1 + .../src/flutter/lib/ui/fixtures/ui_test.dart | 6 ++++ .../flutter/lib/ui/platform_dispatcher.dart | 36 +++++++++++++++++++ .../lib/ui/window/platform_configuration.cc | 5 +++ .../lib/ui/window/platform_configuration.h | 17 +++++++++ .../platform_configuration_unittests.cc | 35 ++++++++++++++++++ .../lib/web_ui/lib/platform_dispatcher.dart | 6 ++++ 7 files changed, 106 insertions(+) diff --git a/engine/src/flutter/lib/ui/dart_ui.cc b/engine/src/flutter/lib/ui/dart_ui.cc index c8c85972a7..20f7345fcd 100644 --- a/engine/src/flutter/lib/ui/dart_ui.cc +++ b/engine/src/flutter/lib/ui/dart_ui.cc @@ -93,6 +93,7 @@ typedef CanvasPath Path; V(PlatformConfigurationNativeApi::UpdateSemantics, 1) \ V(PlatformConfigurationNativeApi::SetNeedsReportTimings, 1) \ V(PlatformConfigurationNativeApi::SetIsolateDebugName, 1) \ + V(PlatformConfigurationNativeApi::RequestDartPerformanceMode, 1) \ V(PlatformConfigurationNativeApi::GetPersistentIsolateData, 0) \ V(PlatformConfigurationNativeApi::ComputePlatformResolvedLocale, 1) \ V(PlatformConfigurationNativeApi::SendPlatformMessage, 3) \ diff --git a/engine/src/flutter/lib/ui/fixtures/ui_test.dart b/engine/src/flutter/lib/ui/fixtures/ui_test.dart index 5528172119..b04ef7be99 100644 --- a/engine/src/flutter/lib/ui/fixtures/ui_test.dart +++ b/engine/src/flutter/lib/ui/fixtures/ui_test.dart @@ -39,6 +39,12 @@ void customOnErrorThrow() { throw Exception('throw1'); } +@pragma('vm:entry-point') +void setLatencyPerformanceMode() { + PlatformDispatcher.instance.requestDartPerformanceMode(DartPerformanceMode.latency); + _finish(); +} + @pragma('vm:entry-point') void validateSceneBuilderAndScene() { final SceneBuilder builder = SceneBuilder(); diff --git a/engine/src/flutter/lib/ui/platform_dispatcher.dart b/engine/src/flutter/lib/ui/platform_dispatcher.dart index 9a3d7e9f6b..7c0af5dec2 100644 --- a/engine/src/flutter/lib/ui/platform_dispatcher.dart +++ b/engine/src/flutter/lib/ui/platform_dispatcher.dart @@ -627,6 +627,19 @@ class PlatformDispatcher { @FfiNative('PlatformConfigurationNativeApi::SetIsolateDebugName') external static void _setIsolateDebugName(String name); + /// Requests the Dart VM to adjusts the GC heuristics based on the requested `performance_mode`. + /// + /// This operation is a no-op of web. The request to change a performance may be ignored by the + /// engine or not resolve in a predictable way. + /// + /// See [DartPerformanceMode] for more information on individual performance modes. + void requestDartPerformanceMode(DartPerformanceMode mode) { + _requestDartPerformanceMode(mode.index); + } + + @FfiNative('PlatformConfigurationNativeApi::RequestDartPerformanceMode') + external static int _requestDartPerformanceMode(int mode); + /// The embedder can specify data that the isolate can request synchronously /// on launch. This accessor fetches that data. /// @@ -2087,3 +2100,26 @@ class Locale { return out.toString(); } } + +/// Various performance modes for tuning the Dart VM's GC performance. +/// +/// For the editor of this enum, please keep the order in sync with `Dart_PerformanceMode` +/// in [dart_api.h](https://github.com/dart-lang/sdk/blob/main/runtime/include/dart_api.h#L1302). +enum DartPerformanceMode { + /// This is the default mode that the Dart VM is in. + balanced, + + /// Optimize for low latency, at the expense of throughput and memory overhead + /// by performing work in smaller batches (requiring more overhead) or by + /// delaying work (requiring more memory). An embedder should not remain in + /// this mode indefinitely. + latency, + + /// Optimize for high throughput, at the expense of latency and memory overhead + /// by performing work in larger batches with more intervening growth. + throughput, + + /// Optimize for low memory, at the expensive of throughput and latency by more + /// frequently performing work. + memory, +} diff --git a/engine/src/flutter/lib/ui/window/platform_configuration.cc b/engine/src/flutter/lib/ui/window/platform_configuration.cc index 4bee227549..1a98b204a1 100644 --- a/engine/src/flutter/lib/ui/window/platform_configuration.cc +++ b/engine/src/flutter/lib/ui/window/platform_configuration.cc @@ -340,6 +340,11 @@ void PlatformConfigurationNativeApi::SetIsolateDebugName( UIDartState::Current()->SetDebugName(name); } +int PlatformConfigurationNativeApi::RequestDartPerformanceMode(int mode) { + UIDartState::ThrowIfUIOperationsProhibited(); + return Dart_SetPerformanceMode(static_cast(mode)); +} + Dart_Handle PlatformConfigurationNativeApi::GetPersistentIsolateData() { UIDartState::ThrowIfUIOperationsProhibited(); diff --git a/engine/src/flutter/lib/ui/window/platform_configuration.h b/engine/src/flutter/lib/ui/window/platform_configuration.h index ee90789866..632bac5c9d 100644 --- a/engine/src/flutter/lib/ui/window/platform_configuration.h +++ b/engine/src/flutter/lib/ui/window/platform_configuration.h @@ -477,6 +477,23 @@ class PlatformConfigurationNativeApi { static void RespondToPlatformMessage(int response_id, const tonic::DartByteData& data); + + //-------------------------------------------------------------------------- + /// @brief Requests the Dart VM to adjusts the GC heuristics based on + /// the requested `performance_mode`. Returns the old performance + /// mode. + /// + /// Requesting a performance mode doesn't guarantee any + /// performance characteristics. This is best effort, and should + /// be used after careful consideration of the various GC + /// trade-offs. + /// + /// @param[in] performance_mode The requested performance mode. Please refer + /// to documentation of `Dart_PerformanceMode` + /// for more details about what each performance + /// mode does. + /// + static int RequestDartPerformanceMode(int mode); }; } // namespace flutter diff --git a/engine/src/flutter/lib/ui/window/platform_configuration_unittests.cc b/engine/src/flutter/lib/ui/window/platform_configuration_unittests.cc index 530a092d79..a00fb6faa1 100644 --- a/engine/src/flutter/lib/ui/window/platform_configuration_unittests.cc +++ b/engine/src/flutter/lib/ui/window/platform_configuration_unittests.cc @@ -271,5 +271,40 @@ TEST_F(ShellTest, PlatformConfigurationOnErrorThrows) { DestroyShell(std::move(shell), std::move(task_runners)); } +TEST_F(ShellTest, PlatformConfigurationSetDartPerformanceMode) { + auto message_latch = std::make_shared(); + auto finish = [message_latch](Dart_NativeArguments args) { + // call needs to happen on the UI thread. + Dart_PerformanceMode prev = + Dart_SetPerformanceMode(Dart_PerformanceMode_Default); + ASSERT_EQ(Dart_PerformanceMode_Latency, prev); + message_latch->Signal(); + }; + AddNativeCallback("Finish", CREATE_NATIVE_ENTRY(finish)); + + Settings settings = CreateSettingsForFixture(); + + TaskRunners task_runners("test", // label + GetCurrentTaskRunner(), // platform + CreateNewThread(), // raster + CreateNewThread(), // ui + CreateNewThread() // io + ); + + std::unique_ptr shell = + CreateShell(std::move(settings), std::move(task_runners)); + + ASSERT_TRUE(shell->IsSetup()); + auto run_configuration = RunConfiguration::InferFromSettings(settings); + run_configuration.SetEntrypoint("setLatencyPerformanceMode"); + + shell->RunEngine(std::move(run_configuration), [&](auto result) { + ASSERT_EQ(result, Engine::RunStatus::Success); + }); + + message_latch->Wait(); + DestroyShell(std::move(shell), std::move(task_runners)); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/lib/web_ui/lib/platform_dispatcher.dart b/engine/src/flutter/lib/web_ui/lib/platform_dispatcher.dart index defcc6df15..ebc42b43df 100644 --- a/engine/src/flutter/lib/web_ui/lib/platform_dispatcher.dart +++ b/engine/src/flutter/lib/web_ui/lib/platform_dispatcher.dart @@ -54,6 +54,8 @@ abstract class PlatformDispatcher { void setIsolateDebugName(String name) {} + void requestDartPerformanceMode(DartPerformanceMode mode) {} + ByteData? getPersistentIsolateData() => null; void scheduleFrame(); @@ -544,3 +546,7 @@ class Locale { return out.toString(); } } + +enum DartPerformanceMode { + balanced, +}