Browse Source

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 <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
v6
Ben Noordhuis 8 years ago
parent
commit
4803d0ae09
  1. 5
      src/node_crypto.cc

5
src/node_crypto.cc

@ -1804,11 +1804,10 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
int slen = i2d_SSL_SESSION(sess, nullptr); int slen = i2d_SSL_SESSION(sess, nullptr);
CHECK_GT(slen, 0); CHECK_GT(slen, 0);
char* sbuf = new char[slen]; char* sbuf = Malloc(slen);
unsigned char* p = reinterpret_cast<unsigned char*>(sbuf); unsigned char* p = reinterpret_cast<unsigned char*>(sbuf);
i2d_SSL_SESSION(sess, &p); i2d_SSL_SESSION(sess, &p);
args.GetReturnValue().Set(Encode(env->isolate(), sbuf, slen, BUFFER)); args.GetReturnValue().Set(Buffer::New(env, sbuf, slen).ToLocalChecked());
delete[] sbuf;
} }

Loading…
Cancel
Save