Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[pigeon] Fix C++ generator's handling of non-class host APIs#2270

Merged
auto-submit[bot] merged 15 commits intoflutter:mainfrom
stuartmorgan-g:windows-host-api-return-and-arg-fixes
Jul 20, 2022
Merged

[pigeon] Fix C++ generator's handling of non-class host APIs#2270
auto-submit[bot] merged 15 commits intoflutter:mainfrom
stuartmorgan-g:windows-host-api-return-and-arg-fixes

Conversation

@stuartmorgan-g
Copy link
Collaborator

@stuartmorgan-g stuartmorgan-g commented Jun 22, 2022

Overhauls the way arguments and return types are handled for host APIs:

  • Fixes handling of non-class types
  • Changes the way optional types are represented
  • Switches from unique_ptr for return types, which are awkward to use and still had a copy at the encoding step, to an approach that relies on move semantics and RVO for efficiency. Copies are possible (e.g., if someone defeats RVO in their host API implementation), but it should hopefully give us a good balance of efficiency and ease of use.

This ports several of the missing native tests from iOS to Windows in order to give meaningful coverage of these changes. It also adds significant Dart unit testing of the type handling.

To limit PR scope, this does not address:

  • Enums as arguments, which are almost certainly still broken.
  • Flutter APIs, which still assumes data classes.

Those can be fixed in follow-ups.

Fixes flutter/flutter#105164
Fixes flutter/flutter#105057

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

@azchohfi
Copy link
Contributor

RVO is confusing. Still reviewing this.

@azchohfi
Copy link
Contributor

I'm thinking here that since all the custom messages are also wrapped with an ErrorOr<X> wrapper, it means that there is a runtime decision for which object is created (either an Error or X), which means (from my understanding) that it will not use RVO. We still benefit from move ctors, but not RVO, right? Did I get it wrong?

@stuartmorgan-g
Copy link
Collaborator Author

stuartmorgan-g commented Jun 22, 2022

No, the cases where RVO doesn't work are more subtle than that. It's something more like (I don't trust that I understand it well enough to explain it fully correctly) if the return statement itself has runtime switching RVO doesn't work. To make sure my understanding of the basic case is correct, I did an instrumented test.

This does RVO (Foo is an object that wraps an int):

ErrorOr<Foo> GetFoo(bool is_error, int value) {
  if (is_error) {
    return FlutterError("an error");
  }
  return Foo(value);
}
...
Foo foo = GetFoo(7);

But this doesn't:

ErrorOr<Foo> GetFoo(bool is_error, int value) {
  return is_error ? ErrorOr<Foo>(FlutterError("an error")) : ErrorOr<Foo>(value);
}
...
Foo foo = GetFoo(7);

The latter appears to call the move constructor if possible, or the copy constructor if there isn't one, for the return. (I'll add more move handling in flutter/flutter#106438 so the fallback will still be efficient. I missed that we don't define a destructor or copy/assign, so adding non-default versions isn't necessary.)

Given how cumbersome the latter is to write, I would expect RVO to be the common case.

@stuartmorgan-g
Copy link
Collaborator Author

(flutter/plugins#6035 is an example of this PR in action if it's helpful for reviewing)

@stuartmorgan-g
Copy link
Collaborator Author

@azchohfi Do you think you'll be able to review this in the next couple of weeks?

If not, @gaaclarke could you take a look?

@azchohfi
Copy link
Contributor

I completely forgot about this. I had half-reviewed it already, but thanks for the ping. Will do!

Copy link
Contributor

@azchohfi azchohfi left a comment

Choose a reason for hiding this comment

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

So much cleaner... This is great!
Also loved that we now have a much better test coverage!

@stuartmorgan-g
Copy link
Collaborator Author

Thanks! I'll try to get to the other follow-ups (style fixes, Flutter API tests and type changes) relatively soon now that this is complete.

@stuartmorgan-g stuartmorgan-g added the autosubmit Merge PR when tree becomes green via auto submit App label Jul 20, 2022
@auto-submit auto-submit bot merged commit d85506b into flutter:main Jul 20, 2022
@stuartmorgan-g stuartmorgan-g deleted the windows-host-api-return-and-arg-fixes branch July 20, 2022 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: pigeon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[pigeon] Windows C++ generator doesn't handle optional arguments correctly [pigeon] C++ generator makes uncompilable code for returning a list

2 participants