Skip to content

Optimizations #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions include/aws/lambda-runtime/outcome.h
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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