Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SharedFuture #183

Merged
merged 7 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
build --cxxopt=-std=c++17 --host_cxxopt=-std=c++17 --incompatible_java_common_parameters=false --define=android_dexmerger_tool=d8_dexmerger --define=android_incremental_dexing_tool=d8_dexbuilder --nouse_workers_with_dexbuilder
build --cxxopt=-std=c++17 --cxxopt=-fcoroutines-ts --host_cxxopt=-std=c++17 --host_cxxopt=-fcoroutines-ts --incompatible_java_common_parameters=false --define=android_dexmerger_tool=d8_dexmerger --define=android_incremental_dexing_tool=d8_dexbuilder --nouse_workers_with_dexbuilder
3 changes: 2 additions & 1 deletion support-lib/cpp/Future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ class Future {
constexpr bool await_ready() const noexcept {
return false;
}
bool await_suspend(detail::CoroutineHandle<ConcretePromise> finished) const noexcept {
template <typename P>
bool await_suspend(detail::CoroutineHandle<P> finished) const noexcept {
li-feng-sc marked this conversation as resolved.
Show resolved Hide resolved
auto& promise_type = finished.promise();
if (*promise_type._result) {
if constexpr (std::is_void_v<T>) {
Expand Down
138 changes: 138 additions & 0 deletions support-lib/cpp/SharedFuture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* Copyright 2021 Snap, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Future.hpp"

#include <memory>
#include <optional>
#include <type_traits>
#include <variant>
#include <vector>

namespace djinni {

// SharedFuture is a wrapper around djinni::Future to allow multiple consumers (i.e. like std::shared_future)
// The API is designed to be similar to djinni::Future.
template<typename T>
class SharedFuture {
public:
// Create SharedFuture from Future. Runtime error if the future is already consumed.
explicit SharedFuture(Future<T>&& future);

// Transform into Future<T>.
Future<T> toFuture() const {
if (await_ready()) {
co_return await_resume(); // return stored value directly
li-feng-sc marked this conversation as resolved.
Show resolved Hide resolved
}
co_return co_await SharedFuture(*this); // retain copy during coroutine suspension
}

void wait() const {
return [this]() -> Future<void> { co_await *this; }().wait();
}

decltype(auto) get() const {
wait();
return await_resume();
}

// Transform the result of this future into a new future. The behavior is same as Future::then except that
// it doesn't consume the future, and can be called multiple times.
template<typename Func>
SharedFuture<std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<Func, T>>>> then(Func transform) const {

This comment was marked as resolved.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes this is a difference from Future, where you transform a ready future instead of the result. This was intentional so that the transform function gets to handle exceptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point regarding exception handling. Changed to handle similarly to Future.

Question: is it valid to have this return a Future? Then it costs less resources by default and the user can decide whether to make it into a SharedFuture or not.

Since this class really is just for convenience, I would opt to keep this chaining to SharedFutures. I think it makes more sense semantically and is cleaner to produce more SharedFutures. Otherwise, this could have just been a simple helper method cloneFuture(Future<T>& f) -> Future<T> instead of a full blown class.

Finally, regarding remove_cvref_t, that's what I used to have, but that is only available in C++20, and we are still configured as c++ 17 for now.

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the advantage of returning a regular Future is that it a) uses less resources and b) can deal with move-only returnvalues. For example: a transform functor returning a unique_ptr will be extremely awkward if then returns a SharedFuture.

I agree about resources.
But it turns out djnni::Future actually doesn't work with move-only types, anyway. At least that was the case last time I tried; is it different now?

You can always make a Future into a SharedFuture, but never the other way around.

Well you can just call toFuture() on the result. it's just a tradeoff of resource vs. convenience.

I'd be very interested in an example or explanation of when it's inconvenient to return Future instead of SharedFuture.

Sure. I added this class because it proves to be challenging to implement a djinni interface that is based on Futures. Specifically, you have a bunch of intermediate results that are Futures, and you need to apply some processing on them when the caller invokes methods returning other Futures. I practice, I always have a bunch of Futures that internally depend on other Futures, in the form of a DAG. This means I needed additional resolved result storage solution for each Future. SharedFuture solved this issue, and I needed all these intermediate results to be SharedFutures, as well. I only ever produce regular Futures again when I finally export them out of the djinni boundary to another language.

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So the code is usually something like

SharedFuture<Something> inputA;
SharedFuture<SomethingElse> inputB;

void someInit() {
  SharedFuture<C> derived = something(inputA);
  _someMember = someLogic(inputA, derived);
  _otherMember = otherLogic(derived);
}

Future<Foo> someAccessor() {
  return someLogic(_someMember);
}

Future<Bar> otherAccessor() {
  return someLogic(_someMember, _otherMember);
}

So it's just cleaner if then produces SharedFuture by default, so I don't have to wrap each result into SharedFuture explicitly 90% of the time.

In any case, I updated the code to include both flavors.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have a strong opinion on this. It kind of makes sense that once someone starts to use SharedFuture they'd want to keep using it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for updating the code. I've got no more complaints, looks great 🎉

co_return transform(co_await SharedFuture(*this)); // retain copy during coroutine suspension
}

// Overload for T = void or `transform` takes no arugment.
template<typename Func, typename = std::enable_if_t<!std::is_invocable_v<Func, T>>>
SharedFuture<std::remove_cv_t<std::remove_reference_t<std::invoke_result_t<Func>>>> then(Func transform) const {

This comment was marked as resolved.

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's fair. Originally, the lambdas took resolved values instead of the futures, so the overload was kind of needed for SharedFuture. Removed for now.

co_await SharedFuture(*this); // retain copy during coroutine suspension
co_return transform();
}

// -- coroutine support implementation only; not intended externally --

This comment was marked as resolved.


bool await_ready() const {
std::scoped_lock lock(_sharedStates->mutex);
return _sharedStates->storedValue.has_value();
}

decltype(auto) await_resume() const {
if constexpr (!std::is_void_v<T>) {
return *_sharedStates->storedValue;
}
}

This comment was marked as resolved.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. The future class was mainly created for bridging with other languages so this is not a super important limitation as we can't have a reference to a C++ object in other languages anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call, done.


bool await_suspend(detail::CoroutineHandle<> h) const;

struct Promise : public Future<T>::promise_type {
SharedFuture<T> get_return_object() noexcept {
return SharedFuture(Future<T>::promise_type::get_return_object());
}
};
using promise_type = Promise;

private:
struct SharedStates {
std::recursive_mutex mutex;
std::optional<std::conditional_t<std::is_void_v<T>, std::monostate, T>> storedValue = std::nullopt;
std::vector<detail::CoroutineHandle<>> coroutineHandles;
};
// Use a shared_ptr to allow copying SharedFuture.
std::shared_ptr<SharedStates> _sharedStates = std::make_shared<SharedStates>();
};

// CTAD deduction guide to construct from Future directly.
template<typename T>
SharedFuture(Future<T>&&) -> SharedFuture<T>;

// ------------------ Implementation ------------------

template<typename T>
SharedFuture<T>::SharedFuture(Future<T>&& future) {
// `future` will invoke all continuations when it is ready.
future.then([sharedStates = _sharedStates](auto futureResult) {
std::vector toCall = [&] {
std::scoped_lock lock(sharedStates->mutex);
if constexpr (std::is_void_v<T>) {
sharedStates->storedValue.emplace();
} else {
sharedStates->storedValue = futureResult.get();

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call

}
return std::move(sharedStates->coroutineHandles);
}();
for (auto& handle : toCall) {
handle();
}
});
}

template<typename T>
bool SharedFuture<T>::await_suspend(detail::CoroutineHandle<> h) const {
{
std::unique_lock lock(_sharedStates->mutex);
if (!_sharedStates->storedValue) {
_sharedStates->coroutineHandles.push_back(std::move(h));
return true;
}
}
h();
return true;
}

} // namespace djinni
techleeksnap marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions test-suite/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ objc_library(
copts = [
"-ObjC++",
"-std=c++17",
"-fcoroutines-ts"
],
srcs = glob([
"generated-src/objc/**/*.mm",
Expand Down
61 changes: 61 additions & 0 deletions test-suite/handwritten-src/objc/tests/DBSharedFutureTest.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#import <Foundation/Foundation.h>
#import <XCTest/XCTest.h>

#include "../../../support-lib/cpp/SharedFuture.hpp"

@interface DBSharedFutureTest : XCTestCase
@end

@implementation DBSharedFutureTest

- (void)setUp
{
[super setUp];
}

- (void)tearDown
{
[super tearDown];
}

- (void)testCreateFuture
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@LiFengSC These are translated from the existing cpp tests

{
djinni::SharedFuture<int> resolvedInt(djinni::Promise<int>::resolve(42));
XCTAssertEqual(resolvedInt.get(), 42);

djinni::Promise<NSString*> strPromise;
djinni::SharedFuture futureString(strPromise.getFuture());

strPromise.setValue(@"foo");
XCTAssertEqualObjects(futureString.get(), @"foo");
}

- (void)testThen
{
djinni::Promise<int> intPromise;
djinni::SharedFuture<int> futureInt(intPromise.getFuture());

auto transformedInt = futureInt.then([](int i) { return 2 * i; });

intPromise.setValue(42);
XCTAssertEqual(transformedInt.get(), 84);

// Also verify multiple consumers and chaining.
auto transformedString = futureInt.then([](int i) { return std::to_string(i); });
auto futurePlusOneTimesTwo = futureInt.then([](int i) { return i + 1; }).then([](int i) { return 2 * i; });
auto futureStringLen = transformedString.then([](const std::string& s) { return s.length(); }).toFuture();

XCTAssertEqual(transformedString.get(), std::string("42"));
XCTAssertEqual(futurePlusOneTimesTwo.get(), (42 + 1) * 2);
XCTAssertEqual(futureStringLen.get(), 2);

XCTAssertEqual(futureInt.get(), 42);

auto voidFuture = transformedString.then([]() {});
voidFuture.wait();

auto intFuture2 = voidFuture.then([]() { return 43; });
XCTAssertEqual(intFuture2.get(), 43);
}

@end