Skip to content

Commit

Permalink
src: remove extra heap allocation in GetSession()
Browse files Browse the repository at this point in the history
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: #14122
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
bnoordhuis authored and Fishrock123 committed Jul 19, 2017
1 parent fc7d8c0 commit 23d9e7e
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1804,11 +1804,10 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& 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<unsigned char*>(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());
}


Expand Down

0 comments on commit 23d9e7e

Please sign in to comment.