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 Sep 6, 2022
1 parent aed49a5 commit 4a2d85a
Show file tree
Hide file tree
Showing 53 changed files with 596 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
17 changes: 17 additions & 0 deletions eventuals/event-loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "eventuals/then.h"
#include "eventuals/type-traits.h"
#include "stout/borrowed_ptr.h"
#include "stout/bytes.h"
#include "uv.h"

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -398,6 +399,10 @@ class EventLoop final : public Scheduler {
handler_->Install();
}

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

private:
EventLoop& loop() {
return clock_->loop();
Expand Down Expand Up @@ -738,6 +743,10 @@ class EventLoop final : public Scheduler {
handler_->Install();
}

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

private:
// Adaptors to libuv functions.
uv_signal_t* signal() {
Expand Down Expand Up @@ -1039,6 +1048,10 @@ class EventLoop final : public Scheduler {
handler_->Install();
}

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

private:
// Adaptors to libuv functions.
uv_poll_t* poll() {
Expand Down Expand Up @@ -1259,6 +1272,10 @@ struct _EventLoopSchedule final {
}
}

Bytes StaticHeapSize() {
return Bytes(sizeof(Adapted_)) + k_.StaticHeapSize();
}

E_ e_;

std::optional<
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/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
18 changes: 16 additions & 2 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,9 +203,11 @@ struct _Generator final {
Continuation(
K_ k,
std::tuple<Args_...>&& args,
DispatchCallback<From_, To_, Args_...>&& dispatch)
DispatchCallback<From_, To_, Args_...>&& dispatch,
Bytes&& static_heap_size)
: args_(std::move(args)),
dispatch_(std::move(dispatch)),
static_heap_size_(std::move(static_heap_size)),
k_(std::move(k)) {}

// All Continuation functions just trigger dispatch Callback,
Expand Down Expand Up @@ -291,13 +294,19 @@ struct _Generator final {
args_);
}

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

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

DispatchCallback<From_, To_, Args_...> dispatch_;

std::unique_ptr<void, Callback<void(void*)>> e_;
Interrupt* interrupt_ = nullptr;

Bytes static_heap_size_ = 0;

// NOTE: we store 'k_' as the _last_ member so it will be
// destructed _first_ and thus we won't have any use-after-delete
// issues during destruction of 'k_' if it holds any references or
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'");

static_heap_size_ = Bytes(sizeof(HeapGenerator<E, From_, To_>));

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(static_heap_size_));
}

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

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

Bytes static_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/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "eventuals/event-loop.h"
#include "eventuals/scheduler.h"
#include "eventuals/x509.h"
#include "stout/bytes.h"

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

Expand Down Expand Up @@ -1141,6 +1142,10 @@ struct _HTTP final {
handler_->Install();
}

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

private:
EventLoop& loop_;

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
Loading

0 comments on commit 4a2d85a

Please sign in to comment.