Skip to content

Commit

Permalink
refactor uthread::Launch
Browse files Browse the repository at this point in the history
  • Loading branch information
4kangjc committed Oct 28, 2022
1 parent 8d4fafb commit 941e6a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
34 changes: 14 additions & 20 deletions async_simple/uthread/Async.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +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>
inline Uthread async(F&& f, Executor* ex) {
template <Launch T, class F>
inline std::enable_if_t<T == Launch::Prompt, 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>
inline void async(F&& f, Executor* ex) {
template <Launch T, class F>
inline std::enable_if_t<T == Launch::Schedule> async(F&& f, Executor* ex) {
if (!ex)
AS_UNLIKELY { return; }
ex->schedule([f = std::move(f), ex]() {
Expand All @@ -67,10 +64,9 @@ 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>
inline void async(F&& f, C&& c, Executor* ex) {
template <Launch T, class F, class C>
inline std::enable_if_t<T == Launch::Schedule> async(F&& f, C&& c,
Executor* ex) {
if (!ex)
AS_UNLIKELY { return; }
ex->schedule([f = std::move(f), c = std::move(c), ex]() {
Expand All @@ -79,10 +75,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>
inline void async(F&& f, Executor* ex) {
template <Launch T, class F>
inline std::enable_if_t<T == Launch::Current> 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 941e6a0

@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: 941e6a0 Previous: 06f321a Ratio
Uthread_read_diff_size_bench/2048/iterations:3 334183415.33334225 ns/iter 195089381.66668105 ns/iter 1.71
Uthread_same_read_bench/32/iterations:3 2330741.9999791062 ns/iter 940548.6666764772 ns/iter 2.48
Uthread_same_read_bench/128/iterations:3 5674403.333330247 ns/iter 2609408.6666716975 ns/iter 2.17
Uthread_same_read_bench/512/iterations:3 21053583.000006407 ns/iter 9736690.333340904 ns/iter 2.16
Generator_pure_switch_bench/1000000/iterations:3 728146.666669242 ns/iter 452605.0000019192 ns/iter 1.61
Lazy_read_diff_size_bench/32/iterations:3 12146687.666662121 ns/iter 6923408.666674883 ns/iter 1.75
Lazy_read_diff_size_bench/128/iterations:3 42492573.333333895 ns/iter 28197273.000008255 ns/iter 1.51
Lazy_same_read_bench/32/iterations:3 1038451.9999983391 ns/iter 646210.6666541937 ns/iter 1.61
Lazy_same_read_bench/128/iterations:3 2524278.9999992964 ns/iter 1217386.666667153 ns/iter 2.07
Uthread_call_depth_bench/64/iterations:3 6433.333330354192 ns/iter 1599.9999997499497 ns/iter 4.02
Uthread_call_depth_bench/512/iterations:3 3199.9999994998993 ns/iter 1966.6666730699944 ns/iter 1.63
Uthread_call_depth_bench/1024/iterations:3 3500.0000006372525 ns/iter 1866.6666695329088 ns/iter 1.87
Uthread_call_depth_bench/2048/iterations:3 3600.0000041743383 ns/iter 1633.3333405782469 ns/iter 2.20
Lazy_call_depth_bench/4/iterations:3 2399.9999996249244 ns/iter 1499.999996212864 ns/iter 1.60
Lazy_call_depth_bench/8/iterations:3 3533.6666712737497 ns/iter 2299.9999866139356 ns/iter 1.54
Lazy_call_depth_bench/1024/iterations:3 119868.66667257347 ns/iter 65701.33335041344 ns/iter 1.82
Lazy_call_depth_bench/2048/iterations:3 231970.9999956861 ns/iter 126768.66667031088 ns/iter 1.83

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

Please sign in to comment.