Skip to content

Commit

Permalink
Introduce HeapSize()
Browse files Browse the repository at this point in the history
This PR adds the possibility to determine for a chain of
eventuals the maximum amount of heap memory that needs to
get allocated.
  • Loading branch information
ArthurBandaryk committed Aug 26, 2022
1 parent a4a3d01 commit 680206d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions eventuals/closure.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "eventuals/compose.h"
#include "eventuals/interrupt.h"
#include "eventuals/type-erased-stream.h"
#include "stout/bytes.h"

////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -63,6 +64,10 @@ struct _Closure final {
return *continuation_;
}

Bytes HeapSize() {
return Bytes{0} + k_.HeapSize();
}

F_ f_;

Interrupt* interrupt_ = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions eventuals/eventual.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "eventuals/interrupt.h"
#include "eventuals/scheduler.h"
#include "eventuals/undefined.h"
#include "stout/bytes.h"

// TODO(benh): catch exceptions from 'start', 'fail', 'stop', etc.

Expand Down Expand Up @@ -170,6 +171,10 @@ struct _Eventual {
return adaptor_;
}

Bytes HeapSize() {
return Bytes{0} + k_.HeapSize();
}

Context_ context_;
Start_ start_;
Fail_ fail_;
Expand Down
9 changes: 9 additions & 0 deletions eventuals/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "eventuals/terminal.h"
#include "eventuals/undefined.h"
#include "stout/borrowable.h"
#include "stout/bytes.h"
#include "stout/stringify.h"

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -392,6 +393,10 @@ struct Reschedulable final {
interrupt_ = &interrupt;
}

Bytes HeapSize() {
return Bytes{0} + k_.HeapSize();
}

Interrupt* interrupt_ = nullptr;

using Continuation_ =
Expand Down Expand Up @@ -471,6 +476,10 @@ struct _Preempt final {
}
}

Bytes HeapSize() {
return Bytes{0} + k_.HeapSize();
}

Scheduler::Context context_;

E_ e_;
Expand Down
5 changes: 5 additions & 0 deletions eventuals/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "eventuals/compose.h"
#include "eventuals/interrupt.h"
#include "eventuals/undefined.h"
#include "stout/bytes.h"

////////////////////////////////////////////////////////////////////////

Expand Down Expand Up @@ -57,6 +58,10 @@ struct _Terminal final {

void Register(Interrupt&) {}

Bytes HeapSize() {
return Bytes{0};
}

Context_ context_;
Start_ start_;
Fail_ fail_;
Expand Down
16 changes: 16 additions & 0 deletions test/eventual.cc
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,21 @@ TEST(EventualTest, Ref) {
EXPECT_EQ(110, x);
}

TEST(EventualTest, HeapSize) {
auto e = []() {
return Eventual<int>([](auto& k) {
k.Start(1);
});
};

auto [future, k] = PromisifyForTest(e());

k.Start();

EXPECT_EQ(0, k.HeapSize().bytes());

EXPECT_EQ(1, future.get());
}

} // namespace
} // namespace eventuals::test

0 comments on commit 680206d

Please sign in to comment.