From 4d0017eed007518a5f4948888b2d070da5d5f494 Mon Sep 17 00:00:00 2001 From: Marco Magdy Date: Tue, 28 Nov 2023 20:23:49 -0800 Subject: [PATCH 1/2] Reduce copying for large payloads Starting with aws-lambda-cpp v0.2.7 we can move the payload from the outcome of the API. This should help performance especially for larger payloads. Another small optimization is to check if string is empty rather than compare it with a literal empty string. --- src/rapid-client.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/rapid-client.cc b/src/rapid-client.cc index d02f55e..46ead75 100644 --- a/src/rapid-client.cc +++ b/src/rapid-client.cc @@ -41,8 +41,7 @@ class RuntimeApiNextPromiseWorker : public Napi::AsyncWorker { return; } - // TODO: See if json parsing works on V8:Buffer objects, which might be a way to reduce copying large payloads - response = outcome.get_result(); + response = std::move(outcome).get_result(); } Napi::Value Promise() { @@ -72,12 +71,12 @@ class RuntimeApiNextPromiseWorker : public Napi::AsyncWorker { headers.Set( Napi::String::New(env, "lambda-runtime-invoked-function-arn"), Napi::String::New(env, response.function_arn.c_str())); - if (response.client_context != "") { + if (!response.client_context.empty()) { headers.Set( Napi::String::New(env, "lambda-runtime-client-context"), Napi::String::New(env, response.client_context.c_str())); } - if (response.cognito_identity != "") { + if (!response.cognito_identity.empty()) { headers.Set( Napi::String::New(env, "lambda-runtime-cognito-identity"), Napi::String::New(env, response.cognito_identity.c_str())); From 5afa0e4f60fec0bda5ad1cc44aa44de95e5cece6 Mon Sep 17 00:00:00 2001 From: Marco Magdy Date: Tue, 28 Nov 2023 20:58:52 -0800 Subject: [PATCH 2/2] Constructing JS strings from C strings is more expensive than passing std::strings --- src/rapid-client.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/rapid-client.cc b/src/rapid-client.cc index 46ead75..d6cb420 100644 --- a/src/rapid-client.cc +++ b/src/rapid-client.cc @@ -50,7 +50,7 @@ class RuntimeApiNextPromiseWorker : public Napi::AsyncWorker { void OnOK() override { Napi::Env env = Env(); - auto response_data = Napi::String::New(env, response.payload.c_str()); + auto response_data = Napi::String::New(env, response.payload); // TODO: The current javascript code (InvokeContext.js) to handle the header values itself. // These type conversions might be replaced by returning the final context object. @@ -64,22 +64,22 @@ class RuntimeApiNextPromiseWorker : public Napi::AsyncWorker { )); headers.Set( Napi::String::New(env, "lambda-runtime-aws-request-id"), - Napi::String::New(env, response.request_id.c_str())); + Napi::String::New(env, response.request_id)); headers.Set( Napi::String::New(env, "lambda-runtime-trace-id"), - Napi::String::New(env, response.xray_trace_id.c_str())); + Napi::String::New(env, response.xray_trace_id)); headers.Set( Napi::String::New(env, "lambda-runtime-invoked-function-arn"), - Napi::String::New(env, response.function_arn.c_str())); + Napi::String::New(env, response.function_arn)); if (!response.client_context.empty()) { headers.Set( Napi::String::New(env, "lambda-runtime-client-context"), - Napi::String::New(env, response.client_context.c_str())); + Napi::String::New(env, response.client_context)); } if (!response.cognito_identity.empty()) { headers.Set( Napi::String::New(env, "lambda-runtime-cognito-identity"), - Napi::String::New(env, response.cognito_identity.c_str())); + Napi::String::New(env, response.cognito_identity)); } auto ret = Napi::Object::New(env);