Skip to content

Commit

Permalink
refactor uthread::Launch
Browse files Browse the repository at this point in the history
  • Loading branch information
4kangjc committed Nov 2, 2022
1 parent 78970ff commit a4a3e14
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
28 changes: 12 additions & 16 deletions async_simple/uthread/Async.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,20 @@
namespace async_simple {
namespace uthread {

struct Launch {
struct Prompt {};
struct Schedule {};
struct Current {};
enum class Launch {
Prompt,
Schedule,
Current,
};

template <class T, class F,
typename std::enable_if<std::is_same<T, Launch::Prompt>::value,
T>::type* = nullptr>
template <Launch policy, class F>
requires (policy == Launch::Prompt)
inline Uthread async(F&& f, Executor* ex) {
return Uthread(Attribute{ex}, std::forward<F>(f));
}

template <class T, class F,
typename std::enable_if<std::is_same<T, Launch::Schedule>::value,
T>::type* = nullptr>
template <Launch policy, class F>
requires (policy == Launch::Schedule)
inline void async(F&& f, Executor* ex) {
if (!ex)
AS_UNLIKELY { return; }
Expand All @@ -67,9 +65,8 @@ inline void async(F&& f, Executor* ex) {
}

// schedule async task, set a callback
template <class T, class F, class C,
typename std::enable_if<std::is_same<T, Launch::Schedule>::value,
T>::type* = nullptr>
template <Launch policy, class F, class C>
requires (policy == Launch::Schedule)
inline void async(F&& f, C&& c, Executor* ex) {
if (!ex)
AS_UNLIKELY { return; }
Expand All @@ -79,9 +76,8 @@ inline void async(F&& f, C&& c, Executor* ex) {
});
}

template <class T, class F,
typename std::enable_if<std::is_same<T, Launch::Current>::value,
T>::type* = nullptr>
template <Launch policy, class F>
requires (policy == Launch::Current)
inline void async(F&& f, Executor* ex) {
Uthread uth(Attribute{ex}, std::forward<F>(f));
uth.detach();
Expand Down
8 changes: 4 additions & 4 deletions async_simple/uthread/Collect.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ namespace uthread {
// TODO: Due to it is possible that the user of async_simple doesn't support
// c++17, we didn't merge this two implementation by if constexpr. Merge them
// once the codebases are ready to use c++17.
template <class Policy, class Iterator>
template <Launch Policy, class Iterator>
std::vector<typename std::enable_if<
!std::is_void<std::invoke_result_t<
typename std::iterator_traits<Iterator>::value_type>>::value,
std::invoke_result_t<typename std::iterator_traits<Iterator>::value_type>>::
type>
collectAll(Iterator first, Iterator last, Executor* ex) {
assert(std::distance(first, last) >= 0);
static_assert(!std::is_same<Launch::Prompt, Policy>::value,
static_assert(Policy != Launch::Prompt,
"collectAll not support Prompt launch policy");

using ResultType = std::invoke_result_t<
Expand Down Expand Up @@ -93,14 +93,14 @@ collectAll(Iterator first, Iterator last, Executor* ex) {
});
}

template <class Policy, class Iterator>
template <Launch Policy, class Iterator>
typename std::enable_if<
std::is_void<std::invoke_result_t<
typename std::iterator_traits<Iterator>::value_type>>::value,
void>::type
collectAll(Iterator first, Iterator last, Executor* ex) {
assert(std::distance(first, last) >= 0);
static_assert(!std::is_same<Launch::Prompt, Policy>::value,
static_assert(Launch::Prompt != Policy,
"collectN not support Prompt launch policy");

struct Context {
Expand Down

1 comment on commit a4a3e14

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'C++ Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: a4a3e14 Previous: 3f81e44 Ratio
Future_chain 38578.20718415332 ns/iter 24709.3810399997 ns/iter 1.56
async_simple_Lazy_chain 89608.95565552695 ns/iter 48881.436487995255 ns/iter 1.83
RescheduleLazy_chain 138171.49232637958 ns/iter 71172.51374488126 ns/iter 1.94
Uthread_read_diff_size_bench/32/iterations:3 26491196.66665456 ns/iter 12581259.333330764 ns/iter 2.11
Uthread_read_diff_size_bench/512/iterations:3 103775808.66666372 ns/iter 45679390.9999836 ns/iter 2.27
Uthread_read_diff_size_bench/1024/iterations:3 157664572.66666597 ns/iter 102121057.6666552 ns/iter 1.54
Uthread_same_read_bench/256/iterations:3 8781109.999991562 ns/iter 5396319.666658656 ns/iter 1.63
Uthread_same_read_bench/512/iterations:3 22055874.666657854 ns/iter 10190832.99996737 ns/iter 2.16
Generator_pure_switch_bench/100000/iterations:3 51200.33333128048 ns/iter 33800.333331631315 ns/iter 1.51

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.