From 8be9bd139f8ea77828b0a58a5936bb5cfe4c23ec Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 10 Jul 2017 12:56:37 +0200 Subject: [PATCH] src: remove extra heap allocation in GetSession() Don't allocate + copy + free; allocate and fill in place, then hand off the pointer to Buffer::New(). Avoids unnecessary heap allocations in the following methods: - crypto.CryptoStream#getSession() - tls.TLSSocket#getSession() PR-URL: https://github.com/nodejs/node/pull/14122 Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- src/node_crypto.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index fb5e8fd65b7a3b..a4f38595916281 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -1804,11 +1804,10 @@ void SSLWrap::GetSession(const FunctionCallbackInfo& args) { int slen = i2d_SSL_SESSION(sess, nullptr); CHECK_GT(slen, 0); - char* sbuf = new char[slen]; + char* sbuf = Malloc(slen); unsigned char* p = reinterpret_cast(sbuf); i2d_SSL_SESSION(sess, &p); - args.GetReturnValue().Set(Encode(env->isolate(), sbuf, slen, BUFFER)); - delete[] sbuf; + args.GetReturnValue().Set(Buffer::New(env, sbuf, slen).ToLocalChecked()); }