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 31, 2022
1 parent a4a3d01 commit 1833d73
Show file tree
Hide file tree
Showing 49 changed files with 547 additions and 15 deletions.
5 changes: 5 additions & 0 deletions eventuals/catch.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "eventuals/terminal.h"
#include "eventuals/then.h"
#include "eventuals/type-traits.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -174,6 +175,10 @@ struct _Catch final {
// the handler.
}

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

std::tuple<CatchHandlers_...> catch_handlers_;

Interrupt* interrupt_ = nullptr;
Expand Down
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 StaticHeapSize() {
return Bytes(0) + k_.StaticHeapSize();
}

F_ f_;

Interrupt* interrupt_ = nullptr;
Expand Down
9 changes: 9 additions & 0 deletions eventuals/concurrent-ordered.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "eventuals/map.h"
#include "eventuals/stream.h"
#include "eventuals/terminal.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -94,6 +95,10 @@ struct _ReorderAdaptor final {
upstream_->Done();
}

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

TypeErasedStream* upstream_ = nullptr;

std::map<int, std::deque<Value_>> buffer_;
Expand Down Expand Up @@ -208,6 +213,10 @@ struct _ConcurrentOrderedAdaptor final {
upstream_->Done();
}

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

bool ended_ = false;

std::optional<int> index_;
Expand Down
5 changes: 5 additions & 0 deletions eventuals/concurrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "eventuals/terminal.h"
#include "eventuals/then.h"
#include "eventuals/until.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -705,6 +706,10 @@ struct _Concurrent final {
handler_->Install();
}

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

Adaptor<F_, Arg_> adaptor_;

TypeErasedStream* stream_ = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions eventuals/conditional.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "eventuals/then.h" // For '_Then::Adaptor'.
#include "eventuals/type-traits.h" // For 'type_identity'.
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -78,6 +79,10 @@ struct _Conditional {
k_.Register(interrupt);
}

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

Condition_ condition_;
Then_ then_;
Else_ else_;
Expand Down
5 changes: 5 additions & 0 deletions eventuals/do-all.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "eventuals/compose.h"
#include "eventuals/scheduler.h"
#include "eventuals/terminal.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -285,6 +286,10 @@ struct _DoAll final {
handler_->Install();
}

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

// NOTE: need to destruct the fibers LAST since they have a
// Scheduler::Context which may get borrowed in 'adaptor_' and
// it's continuations so those need to be destructed first.
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 StaticHeapSize() {
return Bytes(0) + k_.StaticHeapSize();
}

Context_ context_;
Start_ start_;
Fail_ fail_;
Expand Down
5 changes: 5 additions & 0 deletions eventuals/expected.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "eventuals/compose.h"
#include "eventuals/eventual.h"
#include "stout/bytes.h"
#include "tl/expected.hpp"

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -102,6 +103,10 @@ class expected : public tl::expected<Value_, Error_> {
.template k<Value_>(std::move(k));
}

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

using tl::expected<Value_, Error_>::expected;

template <typename Downstream>
Expand Down
5 changes: 5 additions & 0 deletions eventuals/filter.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "eventuals/stream.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -46,6 +47,10 @@ struct _Filter final {
k_.Register(interrupt);
}

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

F_ f_;

TypeErasedStream* stream_ = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions eventuals/finally.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "eventuals/expected.h"
#include "eventuals/terminal.h" // For 'StoppedException'.
#include "eventuals/then.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -41,6 +42,10 @@ struct _Finally final {
k_.Register(interrupt);
}

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

K_ k_;
};

Expand Down
5 changes: 5 additions & 0 deletions eventuals/flat-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "eventuals/stream.h"
#include "eventuals/terminal.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -129,6 +130,10 @@ struct _FlatMap final {
});
}

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

F_ f_;

TypeErasedStream* outer_ = nullptr;
Expand Down
20 changes: 17 additions & 3 deletions eventuals/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "eventuals/terminal.h"
#include "eventuals/then.h"
#include "eventuals/type-traits.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -202,10 +203,12 @@ struct _Generator final {
Continuation(
K_ k,
std::tuple<Args_...>&& args,
DispatchCallback<From_, To_, Args_...>&& dispatch)
DispatchCallback<From_, To_, Args_...>&& dispatch,
Bytes&& heap_size)
: args_(std::move(args)),
dispatch_(std::move(dispatch)),
k_(std::move(k)) {}
k_(std::move(k)),
heap_size_(std::move(heap_size)) {}

// All Continuation functions just trigger dispatch Callback,
// that stores all callbacks for different events
Expand Down Expand Up @@ -291,6 +294,10 @@ struct _Generator final {
args_);
}

Bytes StaticHeapSize() {
return heap_size_ + k_.StaticHeapSize();
}

std::tuple<Args_...> args_;

DispatchCallback<From_, To_, Args_...> dispatch_;
Expand All @@ -303,6 +310,8 @@ struct _Generator final {
// issues during destruction of 'k_' if it holds any references or
// pointers to any (or within any) of the above members.
K_ k_;

Bytes heap_size_ = 0;
};

template <typename From_, typename To_, typename Errors_, typename... Args_>
Expand Down Expand Up @@ -399,6 +408,8 @@ struct _Generator final {
std::is_convertible_v<Value, To_>,
"eventual result type can not be converted into type of 'Generator'");

heap_size_ = Bytes(sizeof(HeapGenerator<E, From_, To_>(f(args...))));

dispatch_ = [f = std::move(f)](
Action action,
std::optional<std::exception_ptr>&& exception,
Expand Down Expand Up @@ -474,7 +485,8 @@ struct _Generator final {
return Continuation<K, From_, To_, Errors_, Args_...>(
std::move(k),
std::move(args_),
std::move(dispatch_));
std::move(dispatch_),
std::move(heap_size_));
}

std::conditional_t<
Expand All @@ -484,6 +496,8 @@ struct _Generator final {
dispatch_;

std::tuple<Args_...> args_;

Bytes heap_size_ = 0;
};
};

Expand Down
5 changes: 5 additions & 0 deletions eventuals/head.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "eventuals/stream.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -45,6 +46,10 @@ struct _Head final {
k_.Register(interrupt);
}

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

std::optional<Arg_> arg_;

TypeErasedStream* stream_ = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions eventuals/if.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "eventuals/eventual.h"
#include "eventuals/then.h" // For '_Then::Adaptor'.
#include "eventuals/type-traits.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -57,6 +58,10 @@ struct _If final {
k_.Register(interrupt);
}

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

bool condition_;
YesE_ yes_;
NoE_ no_;
Expand Down
13 changes: 13 additions & 0 deletions eventuals/lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "eventuals/stream.h"
#include "eventuals/then.h"
#include "eventuals/undefined.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -456,6 +457,10 @@ struct _Acquire final {
k_.Register(interrupt);
}

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

Lock* lock_;
Lock::Waiter waiter_;
std::optional<
Expand Down Expand Up @@ -548,6 +553,10 @@ struct _Release final {
k_.Register(interrupt);
}

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

Lock* lock_;

// NOTE: we store 'k_' as the _last_ member so it will be
Expand Down Expand Up @@ -776,6 +785,10 @@ struct _Wait final {
k_.Register(interrupt);
}

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

Lock* lock_;
F_ f_;

Expand Down
5 changes: 5 additions & 0 deletions eventuals/loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "eventuals/stream.h"
#include "eventuals/type-traits.h"
#include "eventuals/undefined.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -216,6 +217,10 @@ struct _Loop final {
return adaptor_;
}

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

Context_ context_;
Begin_ begin_;
Body_ body_;
Expand Down
Loading

0 comments on commit 1833d73

Please sign in to comment.