You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2013/11/04 17:44:56 UTC
[2/2] git commit: Create Latch in Future Lazily.
Create Latch in Future Lazily.
No need to create a latch if we don't call Future.await(). Given that
Future.await() is rare in the code base, we should have a big
performance improvement. I am attaching the experiment results.
From: Jie Yu <yu...@gmail.com>
Review: https://reviews.apache.org/r/15190
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/7bc7acc9
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/7bc7acc9
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/7bc7acc9
Branch: refs/heads/master
Commit: 7bc7acc9f3a31359cc2b0130d26acd20e7cb96af
Parents: ae85e39
Author: Benjamin Hindman <be...@gmail.com>
Authored: Mon Nov 4 06:43:46 2013 -1000
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Mon Nov 4 06:44:26 2013 -1000
----------------------------------------------------------------------
3rdparty/libprocess/include/process/future.hpp | 37 ++++++++++++++++-----
1 file changed, 29 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/7bc7acc9/3rdparty/libprocess/include/process/future.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/future.hpp b/3rdparty/libprocess/include/process/future.hpp
index f08fdf3..1625d1d 100644
--- a/3rdparty/libprocess/include/process/future.hpp
+++ b/3rdparty/libprocess/include/process/future.hpp
@@ -240,7 +240,7 @@ private:
~Data();
int lock;
- Latch latch;
+ Latch* latch;
State state;
T* t;
std::string* message; // Message associated with failure.
@@ -492,6 +492,7 @@ Future<T> Future<T>::failed(const std::string& message)
template <typename T>
Future<T>::Data::Data()
: lock(0),
+ latch(NULL),
state(PENDING),
t(NULL),
message(NULL) {}
@@ -500,6 +501,7 @@ Future<T>::Data::Data()
template <typename T>
Future<T>::Data::~Data()
{
+ delete latch;
delete t;
delete message;
}
@@ -568,7 +570,9 @@ bool Future<T>::discard()
{
if (data->state == PENDING) {
data->state = DISCARDED;
- data->latch.trigger();
+ if (data->latch != NULL) {
+ data->latch->trigger();
+ }
result = true;
}
}
@@ -626,11 +630,24 @@ bool Future<T>::isFailed() const
template <typename T>
bool Future<T>::await(const Duration& duration) const
{
- if (!isReady() && !isDiscarded() && !isFailed()) {
- return data->latch.await(duration);
- } else {
- return true;
+ bool await = false;
+
+ internal::acquire(&data->lock);
+ {
+ if (data->state == PENDING) {
+ if (data->latch == NULL) {
+ data->latch = new Latch();
+ }
+ await = true;
+ }
}
+ internal::release(&data->lock);
+
+ if (await) {
+ return data->latch->await(duration);
+ }
+
+ return true;
}
@@ -898,7 +915,9 @@ bool Future<T>::set(const T& _t)
if (data->state == PENDING) {
data->t = new T(_t);
data->state = READY;
- data->latch.trigger();
+ if (data->latch != NULL) {
+ data->latch->trigger();
+ }
result = true;
}
}
@@ -935,7 +954,9 @@ bool Future<T>::fail(const std::string& _message)
if (data->state == PENDING) {
data->message = new std::string(_message);
data->state = FAILED;
- data->latch.trigger();
+ if (data->latch != NULL) {
+ data->latch->trigger();
+ }
result = true;
}
}