-
Notifications
You must be signed in to change notification settings - Fork 267
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
Extend collect any #371
Extend collect any #371
Conversation
async_simple/coro/Collect.h
Outdated
async_simple::coro::Lazy<size_t> operator()(auto&&... lazy) { | ||
auto result = co_await collectAny(std::move(lazy)...); | ||
|
||
tupleSwitch(result.index(), _callback_tuple, result); | ||
co_return result.index(); |
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.
Let's try to avoid adding layers in implementing fundamental functions by new co_await. It may results new allocations/deallocations, which may be a performance concern.
async_simple/coro/test/LazyTest.cpp
Outdated
index = syncAwait(collectAny(std::pair{test3().via(&e1), | ||
[&](auto val) { | ||
test_value = val.value(); | ||
EXPECT_EQ(42, test_value); | ||
}}, | ||
std::pair{test4(41).via(&e1), [&](auto val) { | ||
test_value = val.value(); | ||
EXPECT_EQ(41, test_value); | ||
}})); |
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.
It will be better if we can avoid writing std::pair
and try to use {, }
. It may be possible to construct a pair from the initializer list.
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.
I think maybe it's impossible: https://stackoverflow.com/questions/65071836/template-argument-deduction-for-implicit-pair
can't use std::initializer_list to avoid std::pair with brace initialize.
another reason, can't use std::initializer_list because it requires types are the same, not variadic template.
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.
Got it.
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.
Also we need to add documents for this.
async_simple/coro/Collect.h
Outdated
std::make_tuple(std::move(std::get<1>(std::get<I>(tuple)))...)); | ||
}(std::make_index_sequence<sizeof...(Ts)>()); | ||
|
||
auto result = co_await std::move(lazy_pair.first); |
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.
This still introduces new coroutines, which may be a performance concern. Let's try to avoid the new creation of coroutines as much as possible.
async_simple/coro/test/LazyTest.cpp
Outdated
index = syncAwait(collectAny(std::pair{test3().via(&e1), | ||
[&](auto val) { | ||
test_value = val.value(); | ||
EXPECT_EQ(42, test_value); | ||
}}, | ||
std::pair{test4(41).via(&e1), [&](auto val) { | ||
test_value = val.value(); | ||
EXPECT_EQ(41, test_value); | ||
}})); |
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.
Got it.
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.
We also need to update the document for such new features.
.clang-format
Outdated
Language: ObjC | ||
DisableFormat: true |
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.
This should be related.
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.
Please remove the unrelated change.
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.
to solve this error:https://github.com/alibaba/async_simple/actions/runs/8200305198/job/22426819367
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.
This must not be correct. There is nothing related to ObjC.
async_simple/coro/Collect.h
Outdated
return detail::collectAnyImpl(std::move(input)); | ||
typename IAlloc = std::allocator<LazyType<T>>, typename... Function> | ||
inline auto collectAny(std::vector<LazyType<T>, IAlloc>&& input, | ||
Function... func) { |
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.
Why the Function is variadic? There should be only one function with the vector version.
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.
to reuse code, the original vector version no callback function, if user don't pass a callback function, the code go back to original version, otherwise go to callback version
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.
I feel better with offering an overload here to make things explciitly.
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.
OK, remove the variadic template, use a template parameter: Callback, the default Callback type is Unit, and the
default Callback member variable can be optimized by compiler via [[no_unique_address]].
async_simple/coro/Collect.h
Outdated
@@ -83,18 +83,22 @@ struct CollectAnyResult { | |||
#endif | |||
}; | |||
|
|||
template <typename LazyType, typename InAlloc> | |||
template <typename LazyType, typename InAlloc, typename... Function> |
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.
template <typename LazyType, typename InAlloc, typename... Function> | |
template <typename LazyType, typename InAlloc, typename... Callback> |
Let's rename Function
to Callback
. Ditto for the following patch.
|
||
auto await_resume() { | ||
assert(_result != nullptr); | ||
return std::move(_result->value()); |
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.
Why we don't return value with callbacks for vectors but returning here?
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.
variadic version no need index, because the sequence is the index. the index for vector version.
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.
vector version return index is good, unify the return type of vector and variadic version is better.
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.
We need to document this
.clang-format
Outdated
Language: ObjC | ||
DisableFormat: true |
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.
Please remove the unrelated change.
async_simple/coro/test/LazyTest.cpp
Outdated
EXPECT_STREQ("testCollectAllVariadic", v_try_string.value().c_str()); | ||
|
||
CHECK_EXECUTOR(&e1); | ||
// TEST_F(LazyTest, testCollectAllVariadic) { |
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.
Why the tests got commented out?
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.
just for test, will revert.
@@ -1,4 +1,5 @@ | |||
file(GLOB coro_test_src "*.cpp") | |||
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/bigobj>") |
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.
Why do we need this?
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.
windows ci failed before,
https://github.com/alibaba/async_simple/actions/runs/8200305183/job/22426819820#step:4:304
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.
This shouldn't be related to this patch. Please remove the unrelated change.
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.
OK, will create a new PR to add this compile option for msvc.
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 in this PR: #375
.github/workflows/conan.yml
Outdated
- name: Install newer Clang | ||
run: | | ||
wget https://apt.llvm.org/llvm.sh | ||
chmod +x ./llvm.sh | ||
sudo ./llvm.sh 17 |
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.
We should do one thing in one patch. Please move this to other PR.
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.
OK, when test passed will create a new pr to upgrade clang for conan.
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.
This PR upgrade clang version: #373
b29aaf0
to
5aaeab6
Compare
5aaeab6
to
3de9c8e
Compare
Why
extend collectAny with callback, similar with go/kotlin select coroutine function.
What is changing
add two new collectAny methods
Example
collectAny with variadic callback functions:
collectAny with vector callback function: