Skip to content

Commit

Permalink
crypto: use on-stack storage in HashUpdate
Browse files Browse the repository at this point in the history
Boosts speed up to 10% on primitive `createHash().update().digest()`
benchmark.

PR-URL: #664
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
indutny committed Jan 30, 2015
1 parent aca2011 commit 3d4e96f
Showing 1 changed file with 20 additions and 65 deletions.
85 changes: 20 additions & 65 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2750,19 +2750,10 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {

// Only copy the data if we have to, because it's a string
if (args[0]->IsString()) {
Local<String> string = args[0].As<String>();
enum encoding encoding = ParseEncoding(env->isolate(), args[1], BINARY);
if (!StringBytes::IsValidString(env->isolate(), string, encoding))
return env->ThrowTypeError("Bad input string");
size_t buflen = StringBytes::StorageSize(env->isolate(), string, encoding);
char* buf = new char[buflen];
size_t written = StringBytes::Write(env->isolate(),
buf,
buflen,
string,
encoding);
r = cipher->Update(buf, written, &out, &out_len);
delete[] buf;
StringBytes::InlineDecoder decoder;
if (!decoder.Decode(env, args[0].As<String>(), args[1], BINARY))
return;
r = cipher->Update(decoder.out(), decoder.size(), &out, &out_len);
} else {
char* buf = Buffer::Data(args[0]);
size_t buflen = Buffer::Length(args[0]);
Expand Down Expand Up @@ -2929,19 +2920,10 @@ void Hmac::HmacUpdate(const FunctionCallbackInfo<Value>& args) {
// Only copy the data if we have to, because it's a string
bool r;
if (args[0]->IsString()) {
Local<String> string = args[0].As<String>();
enum encoding encoding = ParseEncoding(env->isolate(), args[1], BINARY);
if (!StringBytes::IsValidString(env->isolate(), string, encoding))
return env->ThrowTypeError("Bad input string");
size_t buflen = StringBytes::StorageSize(env->isolate(), string, encoding);
char* buf = new char[buflen];
size_t written = StringBytes::Write(env->isolate(),
buf,
buflen,
string,
encoding);
r = hmac->HmacUpdate(buf, written);
delete[] buf;
StringBytes::InlineDecoder decoder;
if (!decoder.Decode(env, args[0].As<String>(), args[1], BINARY))
return;
r = hmac->HmacUpdate(decoder.out(), decoder.size());
} else {
char* buf = Buffer::Data(args[0]);
size_t buflen = Buffer::Length(args[0]);
Expand Down Expand Up @@ -3053,19 +3035,10 @@ void Hash::HashUpdate(const FunctionCallbackInfo<Value>& args) {
// Only copy the data if we have to, because it's a string
bool r;
if (args[0]->IsString()) {
Local<String> string = args[0].As<String>();
enum encoding encoding = ParseEncoding(env->isolate(), args[1], BINARY);
if (!StringBytes::IsValidString(env->isolate(), string, encoding))
return env->ThrowTypeError("Bad input string");
size_t buflen = StringBytes::StorageSize(env->isolate(), string, encoding);
char* buf = new char[buflen];
size_t written = StringBytes::Write(env->isolate(),
buf,
buflen,
string,
encoding);
r = hash->HashUpdate(buf, written);
delete[] buf;
StringBytes::InlineDecoder decoder;
if (!decoder.Decode(env, args[0].As<String>(), args[1], BINARY))
return;
r = hash->HashUpdate(decoder.out(), decoder.size());
} else {
char* buf = Buffer::Data(args[0]);
size_t buflen = Buffer::Length(args[0]);
Expand Down Expand Up @@ -3214,19 +3187,10 @@ void Sign::SignUpdate(const FunctionCallbackInfo<Value>& args) {
// Only copy the data if we have to, because it's a string
Error err;
if (args[0]->IsString()) {
Local<String> string = args[0].As<String>();
enum encoding encoding = ParseEncoding(env->isolate(), args[1], BINARY);
if (!StringBytes::IsValidString(env->isolate(), string, encoding))
return env->ThrowTypeError("Bad input string");
size_t buflen = StringBytes::StorageSize(env->isolate(), string, encoding);
char* buf = new char[buflen];
size_t written = StringBytes::Write(env->isolate(),
buf,
buflen,
string,
encoding);
err = sign->SignUpdate(buf, written);
delete[] buf;
StringBytes::InlineDecoder decoder;
if (!decoder.Decode(env, args[0].As<String>(), args[1], BINARY))
return;
err = sign->SignUpdate(decoder.out(), decoder.size());
} else {
char* buf = Buffer::Data(args[0]);
size_t buflen = Buffer::Length(args[0]);
Expand Down Expand Up @@ -3395,19 +3359,10 @@ void Verify::VerifyUpdate(const FunctionCallbackInfo<Value>& args) {
// Only copy the data if we have to, because it's a string
Error err;
if (args[0]->IsString()) {
Local<String> string = args[0].As<String>();
enum encoding encoding = ParseEncoding(env->isolate(), args[1], BINARY);
if (!StringBytes::IsValidString(env->isolate(), string, encoding))
return env->ThrowTypeError("Bad input string");
size_t buflen = StringBytes::StorageSize(env->isolate(), string, encoding);
char* buf = new char[buflen];
size_t written = StringBytes::Write(env->isolate(),
buf,
buflen,
string,
encoding);
err = verify->VerifyUpdate(buf, written);
delete[] buf;
StringBytes::InlineDecoder decoder;
if (!decoder.Decode(env, args[0].As<String>(), args[1], BINARY))
return;
err = verify->VerifyUpdate(decoder.out(), decoder.size());
} else {
char* buf = Buffer::Data(args[0]);
size_t buflen = Buffer::Length(args[0]);
Expand Down

0 comments on commit 3d4e96f

Please sign in to comment.