Provide APIs for the PlatformDispatcher to set the performance mode (flutter/engine#35614)

This commit is contained in:
Kaushik Iska
2022-08-29 09:26:21 -04:00
committed by GitHub
parent bcd7d80b9b
commit c30114ae65
7 changed files with 106 additions and 0 deletions

View File

@@ -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) \

View File

@@ -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();

View File

@@ -627,6 +627,19 @@ class PlatformDispatcher {
@FfiNative<Void Function(Handle)>('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<Int Function(Int)>('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,
}

View File

@@ -340,6 +340,11 @@ void PlatformConfigurationNativeApi::SetIsolateDebugName(
UIDartState::Current()->SetDebugName(name);
}
int PlatformConfigurationNativeApi::RequestDartPerformanceMode(int mode) {
UIDartState::ThrowIfUIOperationsProhibited();
return Dart_SetPerformanceMode(static_cast<Dart_PerformanceMode>(mode));
}
Dart_Handle PlatformConfigurationNativeApi::GetPersistentIsolateData() {
UIDartState::ThrowIfUIOperationsProhibited();

View File

@@ -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

View File

@@ -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<fml::AutoResetWaitableEvent>();
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> 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

View File

@@ -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,
}