Skip to content

Commit 2638436

Browse files
committed
optimize performance.
1 parent 1b41d8a commit 2638436

File tree

6 files changed

+36
-19
lines changed

6 files changed

+36
-19
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ option(PROMISE_BUILD_SHARED "Build shared library" OFF)
1919

2020
set(my_headers
2121
include/promise-cpp/promise.hpp
22+
include/promise-cpp/promise_inl.hpp
2223
include/promise-cpp/any.hpp
2324
include/promise-cpp/add_ons.hpp
2425
include/promise-cpp/call_traits.hpp

example/mfc_timer/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ set(PROJECT_SOURCES
2323
res/mfctimer.rc2
2424

2525
../../include/promise-cpp/promise.hpp
26+
../../include/promise-cpp/promise_inl.hpp
2627
../../include/promise-cpp/any.hpp
2728
../../include/promise-cpp/add_ons.hpp
2829
../../include/promise-cpp/call_traits.hpp

example/qt_timer/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(PROJECT_SOURCES
3333
mainwindow.h
3434
mainwindow.ui
3535
../../include/promise-cpp/promise.hpp
36+
../../include/promise-cpp/promise_inl.hpp
3637
../../include/promise-cpp/any.hpp
3738
../../include/promise-cpp/add_ons.hpp
3839
../../include/promise-cpp/call_traits.hpp

example/test0.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
#include <stdio.h>
2828
#include <string>
29+
#include <vector>
2930
#include "promise-cpp/promise.hpp"
3031

3132
using namespace promise;
@@ -121,10 +122,11 @@ void test_promise_all() {
121122

122123
int main(int argc, char **argv) {
123124
handleUncaughtException([](Promise &d) {
124-
printf("UncaughtException\n");
125-
125+
126126
d.fail([](long n, int m) {
127127
printf("UncaughtException parameters = %d %d\n", (int)n, m);
128+
}).fail([]() {
129+
printf("UncaughtException\n");
128130
});
129131
});
130132

@@ -136,7 +138,9 @@ int main(int argc, char **argv) {
136138
printf("====== after call run ======\n");
137139

138140
//next.reject(123, 'a');
139-
next.reject((long)123, (int)345); //it will go to handleUncaughtException(...)
141+
if(next) {
142+
next.reject((long)123, (int)345); //it will go to handleUncaughtException(...)
143+
}
140144
//next.resolve('b');
141145
//next.reject(std::string("xhchen"));
142146
//next.reject(45);

include/promise-cpp/any.hpp

+19-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* THE SOFTWARE.
3030
*/
3131
#include <typeindex>
32+
#include <vector>
3233
#include "add_ons.hpp"
3334
#include "call_traits.hpp"
3435

@@ -55,7 +56,7 @@ class any {
5556

5657
template<typename ValueType>
5758
any(const ValueType &value)
58-
: content(new holder<ValueType>(value)) {
59+
: content(new holder<typename std::remove_cvref<ValueType>::type>(value)) {
5960
}
6061

6162
template<typename RET, typename ...ARG>
@@ -67,6 +68,20 @@ class any {
6768
: content(other.content ? other.content->clone() : 0) {
6869
}
6970

71+
// Move constructor
72+
any(any &&other)
73+
: content(other.content) {
74+
other.content = 0;
75+
}
76+
77+
// Perfect forwarding of ValueType
78+
template<typename ValueType>
79+
any(ValueType &&value
80+
, typename std::enable_if<!std::is_same<any &, ValueType>::value>::type* = nullptr // disable if value has type `any&`
81+
, typename std::enable_if<!std::is_const<ValueType>::value>::type* = nullptr) // disable if value has type `const ValueType&&`
82+
: content(new holder<typename std::remove_cvref<ValueType>::type>(static_cast<ValueType &&>(value))) {
83+
}
84+
7085
~any() {
7186
if (content != nullptr) {
7287
delete(content);
@@ -262,8 +277,8 @@ struct any_call_t<RET, std::tuple<NOCVR_ARG>, FUNC> {
262277
: any_cast<any_arguemnt_type &>(arg));
263278
if(args.size() < 1)
264279
throw bad_any_cast(arg.type(), typeid(nocvr_argument_type));
265-
//printf("[%s] [%s]\n", args[0].type().name(), typeid(NOCVR_ARG).name());
266-
return func(any_cast<NOCVR_ARG &>(args[0]));
280+
//printf("[%s] [%s]\n", args.front().type().name(), typeid(NOCVR_ARG).name());
281+
return func(any_cast<NOCVR_ARG &>(args.front()));
267282
}
268283
};
269284

@@ -281,7 +296,7 @@ struct any_call_t<RET, std::tuple<any>, FUNC> {
281296
return (func(empty));
282297
}
283298
else if(args.size() == 1)
284-
return (func(args[0]));
299+
return (func(args.front()));
285300
else
286301
return (func(const_cast<any &>(arg)));
287302
}

include/promise-cpp/promise_inl.hpp

+7-12
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ static inline void join(const std::shared_ptr<PromiseHolder> &left, const std::s
9595

9696
std::list<std::weak_ptr<SharedPromise>> owners;
9797
owners.splice(owners.end(), right->owners_);
98-
right->state_ = TaskState::kResolved;
9998

10099
if(owners.size() > 100) {
101100
fprintf(stderr, "Maybe memory leak, too many promise owners: %d", (int)owners.size());
@@ -389,10 +388,8 @@ Promise newPromise(const std::function<void(Defer &defer)> &run) {
389388
promise.sharedPromise_->promiseHolder_ = std::make_shared<PromiseHolder>();
390389
promise.sharedPromise_->promiseHolder_->owners_.push_back(promise.sharedPromise_);
391390

392-
auto returnAsIs = [](const any &arg) -> any {
393-
return arg;
394-
};
395-
promise.then(returnAsIs);
391+
// return as is
392+
promise.then(any(), any());
396393
std::shared_ptr<Task> &task = promise.sharedPromise_->promiseHolder_->pendingTasks_.front();
397394

398395
Defer defer(task);
@@ -412,10 +409,8 @@ Promise newPromise() {
412409
promise.sharedPromise_->promiseHolder_ = std::make_shared<PromiseHolder>();
413410
promise.sharedPromise_->promiseHolder_->owners_.push_back(promise.sharedPromise_);
414411

415-
auto returnAsIs = [](const any &arg) -> any {
416-
return arg;
417-
};
418-
promise.then(returnAsIs);
412+
// return as is
413+
promise.then(any(), any());
419414
return promise;
420415
}
421416

@@ -435,10 +430,10 @@ Promise doWhile(const std::function<void(DeferLoop &loop)> &run) {
435430
if (arg.type() == typeid(std::vector<any>)) {
436431
std::vector<any> &args = any_cast<std::vector<any> &>(arg);
437432
if (args.size() == 2
438-
&& args[0].type() == typeid(DoBreakTag)
439-
&& args[1].type() == typeid(std::vector<any>)) {
433+
&& args.front().type() == typeid(DoBreakTag)
434+
&& args.back().type() == typeid(std::vector<any>)) {
440435
isBreak = true;
441-
defer.resolve(args[1]);
436+
defer.resolve(args.back());
442437
}
443438
}
444439

0 commit comments

Comments
 (0)