Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

@gaaclarke
Copy link
Member

@gaaclarke gaaclarke commented Jan 8, 2021

Description

This should be a noop refactor that plumbs spawning all the way down to the dart isolate so that it can use Dart_CreateIsolateInGroup if necessary.

Related Issues

flutter/flutter#72025

Tests

I added the following tests:

This is just a refactor, no new or changed functionality, existing tests should cover it.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the contributor guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the C++, Objective-C, Java style guides for the engine.
  • I read the tree hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation.
  • All existing and new tests are passing.
  • I am willing to follow-up on review comments in a timely manner.

Reviewer Checklist

Breaking Change

Did any tests fail when you ran them? Please read handling breaking changes.

@gaaclarke gaaclarke force-pushed the lfe-isolate-groups branch 2 times, most recently from d255c92 to 5a280fe Compare January 8, 2021 23:19
@gaaclarke gaaclarke changed the title Started creating spawned isolates in isolate groups Did plumbing refactor to allow the usage of Dart_CreateIsolateInGroup Jan 8, 2021
@gaaclarke gaaclarke changed the title Did plumbing refactor to allow the usage of Dart_CreateIsolateInGroup Plumbing refactor to allow the usage of Dart_CreateIsolateInGroup Jan 8, 2021
@gaaclarke gaaclarke marked this pull request as ready for review January 9, 2021 00:19
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

Copy link
Member

@chinmaygarde chinmaygarde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the existing behavior that terminating the root isolate also terminates any isolates spawned by it. It should be possible to create distinct isolate groups with shared resources. Also, what resources are being duplicated during isolate creation?

/// possible with the caller DartIsolate. This allows them to
/// occupy less memory together.
/// @attention Only certain setups can take advantage of the most savings
/// currently, release builds currently.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkustermann where do you estimate you guys will land by the next Flutter stable?

Copy link
Member

@xster xster left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Please wait for @chinmaygarde to review.

//----------------------------------------------------------------------------
/// @brief Creates a running DartIsolate who shares as many resources as
/// possible with the caller DartIsolate. This allows them to
/// occupy less memory together.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Describe their and their components' lifecycles. Describe restrictions (i.e. they must be from the same aot).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

//----------------------------------------------------------------------------
/// @brief Create a Engine that shares as many resources as
/// possible with the calling Engine such that together
/// they occupy less memory.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment for doc. Can use more details.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, mostly linked to other documentation.

public:
template <class T>
using CreateCallback = std::function<std::unique_ptr<T>(Shell&)>;
typedef std::function<std::unique_ptr<Engine>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda does the same thing as the above. Can we make the names sound similar like EngineCreateCallback or something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const CreateCallback<PlatformView>& on_create_platform_view,
const CreateCallback<Rasterizer>& on_create_rasterizer,
DartVMRef vm,
const EngineMaker& engine_maker);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Could this be something like on_create_engine for consistency?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea, done

@gaaclarke
Copy link
Member Author

gaaclarke commented Jan 11, 2021

what resources are being duplicated during isolate creation?

The isolate group, so the heap and GC and the loaded executable.

This PR isn't making any functional changes. The top level APIs behaves the same. A spawned flutter engine and its isolate should live beyond its spawning engine, this matches the semantics of isolate groups and matches how engine groups will work.

I've added a comment to where the functional change will be made, conditionally we will be calling Dart_CreateIsolateInGroup instead of Dart_CreateIsolateGroup. I avoided turning it fully on yet until I can finish all the phase 1 work. I brought this over because this refactor makes other phase 1 work possible. You can see it in action in the prototype https://github.com/gaaclarke/engine/tree/spawn-engine

edit:

This breaks the existing behavior that terminating the root isolate also terminates any isolates spawned by it

No existing APIs have have had their semantics changed by this PR or any of the others. This is a whole different API so it's not a breaking change.

@chinmaygarde
Copy link
Member

No existing APIs have have had their semantics changed by this PR or any of the others.

That is not what I meant. Existing Flutter apps expect that isolates spawned by their root isolate will be terminated with the root isolate is collected. This will not be the case for isolates spawned using this new call. This deviation from expected behavior is the breaking change I meant.

The isolate group, so the heap and GC and the loaded executable.

The heap should be per isolate and not the group. The GC and other VM components are owned by the Dart VM instance and not the isolate group. The loaded executable code should be handled by libdl and reference counted already. So, in bytes, what are the resources you are saving by using this vs the old APIs.

@gaaclarke
Copy link
Member Author

@chinmaygarde This is a whole new API so there isn't any expectations. The behavior you are describing is definitive to the work we are doing.

The heap isn't per isolate, it is per group although there are safeguards that disallow them from directly accessing memory allocated to a different isolate (there is plans to change that). With respect to the loaded program it isn't a problem with occupied memory but the time to create a new isolate. Loading the executable takes a sizable chunk of the time to create a new isolate, in this case it is just wasted effort.

All this work is outlined in this issue: dart-lang/sdk#36097 I think there is a design doc floating around, too.

std::unique_ptr<IsolateConfiguration> isolate_configration,
std::shared_ptr<VolatilePathTracker> volatile_path_tracker);
std::shared_ptr<VolatilePathTracker> volatile_path_tracker,
const DartIsolate* spawning_isolate = nullptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you change the signature, update the method doc

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

/// @brief Creates a running DartIsolate who shares as many resources as
/// possible with the caller DartIsolate. This allows them to
/// occupy less memory together and to be created faster.
/// @details Shared components will be destroyed when the last DartIsolate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when the last DartIsolate among the group. Might be misunderstood as if you make 1, 2, 3, 4, shared components will be destroyed if 4 is destroyed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

const DartIsolate* spawning_isolate = nullptr);

//----------------------------------------------------------------------------
/// @brief Creates a running DartIsolate who shares as many resources as
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creates a new running DartIsolate from the current DartIsolate

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is much of an improvement. Hopefully the other edits cover the information you are trying to convey with that.

/// @details Shared components will be destroyed when the last DartIsolate
/// is destroyed. SpawnIsolate can only be used to create
/// DartIsolates whose executable code is shared with the calling
/// DartIsolate.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the current DartIsolate need to be in any particular states before calling this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure of the hard requirements of Dart_CreateIsolateInGroup. It isn't spelled out in its documentation.

/// DartIsolate.
/// @attention Only certain setups can take advantage of the most savings
/// currently, AOT specifically.
/// @return A running DartIsolate.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new, running DartIsolate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/// DartIsolates whose executable code is shared with the calling
/// DartIsolate.
/// @attention Only certain setups can take advantage of the most savings
/// currently, AOT specifically.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the same disclosure for why it's a weak pointer, who owns it, performance considerations etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/// possible with the calling Engine such that together
/// they occupy less memory and be created faster.
/// @details This method ultimately calls DartIsolate::SpawnIsolate to make
/// sure resources are shared.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same doc comments. Does the current engine need to be in any state before calling this? Mention that it's a new Engine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

/// @details This version of Create can take in a running DartVMRef and a
/// function that defines how the Shell's Engine should be
/// created.
/// @param[in] vm A running DartVMRef where this Shell's Dart
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to briefly describe how one typically gets a DartVMRef?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we should point to DartVMRef::Create, it's pretty straightforward

Copy link
Member

@chinmaygarde chinmaygarde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks good. Please add tests for Engine::Spawn to shell_unittests and for DartIsolate::SpawnIsolate to runtime_unittests.

@gaaclarke
Copy link
Member Author

Mostly looks good. Please add tests for Engine::Spawn to shell_unittests and for DartIsolate::SpawnIsolate to runtime_unittests.

A dependent PR has a unit test for Engine::Spawn #23603 SpawnIsolate has test coverage via the golden image test for spawning engines and the FlutterEngineTests of objective-c. I'll look into seeing if I can get a unit test directly on SpawnIsolate.

@chinmaygarde
Copy link
Member

I'll look into seeing if I can get a unit test directly on SpawnIsolate.

You should be able to base the test on DartIsolateTest::RootIoslateCreationAndShutdown. Take that isolate and spawn another one using that as the base.

SGTM about the test in the dependent PR.

engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jan 13, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jan 13, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jan 13, 2021
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jan 13, 2021
zanderso pushed a commit to flutter/flutter that referenced this pull request Jan 13, 2021
* 51dd6aa [web] Enable the new rich paragraph implementation (flutter/engine#23162)

* 0f9fc3d Roll Skia from 7cf3addb1bd8 to 93c2d81f199a (1 revision) (flutter/engine#23614)

* a1e7424 [dart-runner] Avoid calling Destroy on nullptr (flutter/engine#23608)

* 846d958 Windows textures: Add placeholder flutter_texture_registrar.h (flutter/engine#23623)

* 55afc18 Roll Dart SDK from 7fcbd388b620 to ef8bf7f0a667 (5 revisions) (flutter/engine#23628)

* d2b8154 [canvaskit] apply invser scale on the left (flutter/engine#23550)

* be1a3c0 Roll Fuchsia Linux SDK from UB6RsTbdU... to FfWbbB4r8... (flutter/engine#23633)

* f743c89 Roll Skia from 93c2d81f199a to 9fd75e96d712 (29 revisions) (flutter/engine#23635)

* cf42dbe Roll wuffs to google/wuffs@c86add2 (flutter/engine#23607)

* f1278d0 Link SkShaper/SkParagraph into the engine by default (flutter/engine#23626)

* fb56b4b Android deeplink sends "path + query" instead of just path (flutter/engine#23561)

* 22bb891 Plumbing refactor to allow the usage of Dart_CreateIsolateInGroup (flutter/engine#23549)

* 20991a5 Add accessibility suport to Linux shell. (flutter/engine#19634)

* 145922b Roll Dart SDK from ef8bf7f0a667 to 636ff0ec97e0 (1 revision) (flutter/engine#23639)

* 176ae6e Roll Fuchsia Mac SDK from oll0Dgp9o... to JSzm8D59u... (flutter/engine#23641)

* d2320a8 Roll Dart SDK from 636ff0ec97e0 to d3d7b77e8165 (1 revision) (flutter/engine#23642)

* 8bdd099 Roll Skia from 9fd75e96d712 to 38ca513408d1 (1 revision) (flutter/engine#23643)

* cc572e1 Roll Skia from 38ca513408d1 to be2a8614c5d6 (2 revisions) (flutter/engine#23644)

* e9383a0 Roll Dart SDK from d3d7b77e8165 to 010633edc631 (1 revision) (flutter/engine#23645)

* 9d6ed8b Roll Fuchsia Linux SDK from FfWbbB4r8... to BUsKF6z4t... (flutter/engine#23646)

* 6d55dd4 Roll Dart SDK from 010633edc631 to 724d9e5e7d71 (1 revision) (flutter/engine#23647)

* 299f081 Roll Skia from be2a8614c5d6 to 0d7de6bc9ac3 (1 revision) (flutter/engine#23648)

* 0ec99cf Roll Fuchsia Mac SDK from JSzm8D59u... to BsUY1yjWh... (flutter/engine#23650)

* 859494f Revert "[web] Enable the new rich paragraph implementation (#23162)" (flutter/engine#23651)

* 648dae9 Roll Skia from 0d7de6bc9ac3 to 92969f265686 (7 revisions) (flutter/engine#23652)
@xster xster mentioned this pull request Jan 15, 2021
13 tasks
gspencergoog pushed a commit to gspencergoog/engine that referenced this pull request Jan 20, 2021
…utter#23549)

Did the plumbing refactor that allows us to call Dart_CreateIsolateInGroup when applicable.
hjfreyer pushed a commit to hjfreyer/engine that referenced this pull request Mar 22, 2021
…utter#23549)

Did the plumbing refactor that allows us to call Dart_CreateIsolateInGroup when applicable.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants