-
Notifications
You must be signed in to change notification settings - Fork 6k
Made flutter startup faster by allowing initialization to be parallelized #10182
Changes from 6 commits
c0ce97f
13d3875
7edae58
b51dc21
e8a6e40
5f842b9
9d6b813
1b62816
4541f9a
799d314
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,58 +67,57 @@ std::unique_ptr<Shell> Shell::CreateShellOnPlatformThread( | |
| // first because it has state that the other subsystems depend on. It must | ||
| // first be booted and the necessary references obtained to initialize the | ||
| // other subsystems. | ||
| fml::AutoResetWaitableEvent io_latch; | ||
| std::unique_ptr<ShellIOManager> io_manager; | ||
| std::promise<std::unique_ptr<ShellIOManager>> io_manager_promise; | ||
| auto io_manager = io_manager_promise.get_future(); | ||
| std::promise<fml::WeakPtr<ShellIOManager>> weak_io_manager_promise; | ||
| auto weak_io_manager = weak_io_manager_promise.get_future(); | ||
| auto io_task_runner = shell->GetTaskRunners().GetIOTaskRunner(); | ||
| fml::TaskRunner::RunNowOrPostTask( | ||
| io_task_runner, | ||
| [&io_latch, // | ||
| &io_manager, // | ||
| &platform_view, // | ||
| io_task_runner // | ||
| [&io_manager_promise, // | ||
| &weak_io_manager_promise, // | ||
| &platform_view, // | ||
| io_task_runner // | ||
| ]() { | ||
| TRACE_EVENT0("flutter", "ShellSetupIOSubsystem"); | ||
| io_manager = std::make_unique<ShellIOManager>( | ||
| auto io_manager = std::make_unique<ShellIOManager>( | ||
| platform_view->CreateResourceContext(), io_task_runner); | ||
| io_latch.Signal(); | ||
| weak_io_manager_promise.set_value(io_manager->GetWeakPtr()); | ||
| io_manager_promise.set_value(std::move(io_manager)); | ||
| }); | ||
| io_latch.Wait(); | ||
|
|
||
| // Create the rasterizer on the GPU thread. | ||
| fml::AutoResetWaitableEvent gpu_latch; | ||
| std::unique_ptr<Rasterizer> rasterizer; | ||
| std::promise<std::unique_ptr<Rasterizer>> rasterizer_promise; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move this before the platform view creation call. You don't have to wait for the platform view to be created before this can be kicked off as there is no dependency. This can be kicked off eagerly so that platform view creation can be concurrent with rasterizer creation as well.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| auto rasterizer = rasterizer_promise.get_future(); | ||
| fml::TaskRunner::RunNowOrPostTask( | ||
| task_runners.GetGPUTaskRunner(), [&gpu_latch, // | ||
| &rasterizer, // | ||
| task_runners.GetGPUTaskRunner(), [&rasterizer_promise, // | ||
| on_create_rasterizer, // | ||
| shell = shell.get() // | ||
| ]() { | ||
| TRACE_EVENT0("flutter", "ShellSetupGPUSubsystem"); | ||
| if (auto new_rasterizer = on_create_rasterizer(*shell)) { | ||
| rasterizer = std::move(new_rasterizer); | ||
| rasterizer_promise.set_value(std::move(new_rasterizer)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can just be condensed to one line now:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| } else { | ||
| rasterizer_promise.set_value(std::unique_ptr<Rasterizer>()); | ||
| } | ||
| gpu_latch.Signal(); | ||
| }); | ||
|
|
||
| gpu_latch.Wait(); | ||
|
|
||
| // Send dispatcher_maker to the engine constructor because shell won't have | ||
| // platform_view set until Shell::Setup is called later. | ||
| auto dispatcher_maker = platform_view->GetDispatcherMaker(); | ||
|
|
||
| // Create the engine on the UI thread. | ||
| fml::AutoResetWaitableEvent ui_latch; | ||
| std::unique_ptr<Engine> engine; | ||
| std::promise<std::unique_ptr<Engine>> engine_promise; | ||
| auto engine = engine_promise.get_future(); | ||
| fml::TaskRunner::RunNowOrPostTask( | ||
| shell->GetTaskRunners().GetUITaskRunner(), | ||
| fml::MakeCopyable([&ui_latch, // | ||
| &engine, // | ||
| fml::MakeCopyable([&engine_promise, // | ||
| shell = shell.get(), // | ||
| &dispatcher_maker, // | ||
| isolate_snapshot = std::move(isolate_snapshot), // | ||
| shared_snapshot = std::move(shared_snapshot), // | ||
| vsync_waiter = std::move(vsync_waiter), // | ||
| io_manager = io_manager->GetWeakPtr() // | ||
| &weak_io_manager // | ||
| ]() mutable { | ||
| TRACE_EVENT0("flutter", "ShellSetupUISubsystem"); | ||
| const auto& task_runners = shell->GetTaskRunners(); | ||
|
|
@@ -128,27 +127,23 @@ std::unique_ptr<Shell> Shell::CreateShellOnPlatformThread( | |
| auto animator = std::make_unique<Animator>(*shell, task_runners, | ||
| std::move(vsync_waiter)); | ||
|
|
||
| engine = std::make_unique<Engine>(*shell, // | ||
| dispatcher_maker, // | ||
| *shell->GetDartVM(), // | ||
| std::move(isolate_snapshot), // | ||
| std::move(shared_snapshot), // | ||
| task_runners, // | ||
| shell->GetSettings(), // | ||
| std::move(animator), // | ||
| std::move(io_manager) // | ||
| ); | ||
| ui_latch.Signal(); | ||
| engine_promise.set_value(std::make_unique<Engine>( | ||
| *shell, // | ||
| dispatcher_maker, // | ||
| *shell->GetDartVM(), // | ||
| std::move(isolate_snapshot), // | ||
| std::move(shared_snapshot), // | ||
| task_runners, // | ||
| shell->GetSettings(), // | ||
| std::move(animator), // | ||
| weak_io_manager.get() // | ||
| )); | ||
| })); | ||
|
|
||
| ui_latch.Wait(); | ||
| // We are already on the platform thread. So there is no platform latch to | ||
| // wait on. | ||
|
|
||
| if (!shell->Setup(std::move(platform_view), // | ||
| std::move(engine), // | ||
| std::move(rasterizer), // | ||
| std::move(io_manager)) // | ||
| engine.get(), // | ||
| rasterizer.get(), // | ||
| io_manager.get()) // | ||
| ) { | ||
| return nullptr; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and elsewhere, please append
_futureto the ivars or locals that are now futures.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done