Skip to content

Commit

Permalink
Avoid copying the result of a successful outcome (#100)
Browse files Browse the repository at this point in the history
Adding rvalue ref-qualifiers to the 'outcome' class getters enables
moving out the result instead of copying it. In this case, we can avoid
copying the request payload.

* Remove static modifier from inline header functions

static inline makes no sense in a header file.
  • Loading branch information
marcomagdy committed Aug 1, 2020
1 parent 17473f8 commit 57d9f29
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
48 changes: 36 additions & 12 deletions include/aws/lambda-runtime/outcome.h
Expand Up @@ -23,48 +23,72 @@ namespace lambda_runtime {
template <typename TResult, typename TFailure>
class outcome {
public:
outcome(TResult const& s) : s(s), m_success(true) {}
outcome(TResult const& s) : m_s(s), m_success(true) {}
outcome(TResult&& s) : m_s(std::move(s)), m_success(true) {}

outcome(TFailure const& f) : f(f), m_success(false) {}
outcome(TFailure const& f) : m_f(f), m_success(false) {}
outcome(TFailure&& f) : m_f(std::move(f)), m_success(false) {}

outcome(outcome const& other) : m_success(other.m_success)
{
if (m_success) {
new (&m_s) TResult(other.m_s);
}
else {
new (&m_f) TFailure(other.m_f);
}
}

outcome(outcome&& other) noexcept : m_success(other.m_success)
{
if (m_success) {
s = std::move(other.s);
new (&m_s) TResult(std::move(other.m_s));
}
else {
f = std::move(other.f);
new (&m_f) TFailure(std::move(other.m_f));
}
}

~outcome()
{
if (m_success) {
s.~TResult();
m_s.~TResult();
}
else {
f.~TFailure();
m_f.~TFailure();
}
}

TResult const& get_result() const
TResult const& get_result() const&
{
assert(m_success);
return s;
return m_s;
}

TResult&& get_result() &&
{
assert(m_success);
return std::move(m_s);
}

TFailure const& get_failure() const&
{
assert(!m_success);
return m_f;
}

TFailure const& get_failure() const
TFailure&& get_failure() &&
{
assert(!m_success);
return f;
return std::move(m_f);
}

bool is_success() const { return m_success; }

private:
union {
TResult s;
TFailure f;
TResult m_s;
TFailure m_f;
};
bool m_success;
};
Expand Down
6 changes: 3 additions & 3 deletions include/aws/logging/logging.h
Expand Up @@ -27,7 +27,7 @@ enum class verbosity {

void log(verbosity v, char const* tag, char const* msg, va_list args);

[[gnu::format(printf, 2, 3)]] static inline void log_error(char const* tag, char const* msg, ...)
[[gnu::format(printf, 2, 3)]] inline void log_error(char const* tag, char const* msg, ...)
{
va_list args;
va_start(args, msg);
Expand All @@ -37,7 +37,7 @@ void log(verbosity v, char const* tag, char const* msg, va_list args);
(void)msg;
}

[[gnu::format(printf, 2, 3)]] static inline void log_info(char const* tag, char const* msg, ...)
[[gnu::format(printf, 2, 3)]] inline void log_info(char const* tag, char const* msg, ...)
{
#if AWS_LAMBDA_LOG >= 1
va_list args;
Expand All @@ -50,7 +50,7 @@ void log(verbosity v, char const* tag, char const* msg, va_list args);
#endif
}

[[gnu::format(printf, 2, 3)]] static inline void log_debug(char const* tag, char const* msg, ...)
[[gnu::format(printf, 2, 3)]] inline void log_debug(char const* tag, char const* msg, ...)
{
#if AWS_LAMBDA_LOG >= 2
va_list args;
Expand Down
4 changes: 2 additions & 2 deletions src/runtime.cpp
Expand Up @@ -412,7 +412,7 @@ void run_handler(std::function<invocation_response(invocation_request const&)> c
size_t const max_retries = 3;

while (retries < max_retries) {
const auto next_outcome = rt.get_next();
auto next_outcome = rt.get_next();
if (!next_outcome.is_success()) {
if (next_outcome.get_failure() == aws::http::response_code::REQUEST_NOT_MADE) {
++retries;
Expand All @@ -429,7 +429,7 @@ void run_handler(std::function<invocation_response(invocation_request const&)> c

retries = 0;

auto const& req = next_outcome.get_result();
auto const req = std::move(next_outcome).get_result();
logging::log_info(LOG_TAG, "Invoking user handler");
invocation_response res = handler(req);
logging::log_info(LOG_TAG, "Invoking user handler completed.");
Expand Down

0 comments on commit 57d9f29

Please sign in to comment.