Skip to content

Commit

Permalink
perf: make use of mongocrypt_binary_t fields instead of getter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Jun 13, 2024
1 parent d8bf5ce commit 238457d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
25 changes: 11 additions & 14 deletions addon/mongocrypt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@ std::unique_ptr<mongocrypt_binary_t, MongoCryptBinaryDeleter> Uint8ArrayToBinary
}

Uint8Array BufferFromBinary(Env env, mongocrypt_binary_t* binary) {
const uint8_t* data = mongocrypt_binary_data(binary);
size_t len = mongocrypt_binary_len(binary);
return Buffer<uint8_t>::Copy(env, data, len);
return Buffer<uint8_t>::Copy(env, (uint8_t*)binary->data, binary->len);
}

Uint8Array BufferWithLengthOf(Env env, mongocrypt_binary_t* binary) {
size_t len = mongocrypt_binary_len(binary);
return Buffer<uint8_t>::New(env, len);
return Buffer<uint8_t>::New(env, binary->len);
}

Uint8Array Uint8ArrayFromValue(Napi::Value v, std::string argument_name) {
Expand All @@ -64,13 +61,13 @@ Uint8Array Uint8ArrayFromValue(Napi::Value v, std::string argument_name) {
}

void CopyBufferData(mongocrypt_binary_t* out, Uint8Array buffer, size_t count) {
assert(count <= mongocrypt_binary_len(out));
assert(count <= out->len);
assert(count <= buffer.ByteLength());
memcpy(mongocrypt_binary_data(out), buffer.Data(), count);
memcpy(out->data, buffer.Data(), count);
}

void CopyBufferData(mongocrypt_binary_t* out, Uint8Array buffer) {
CopyBufferData(out, buffer, mongocrypt_binary_len(out));
CopyBufferData(out, buffer, out->len);
}

std::string errorStringFromStatus(mongocrypt_t* crypt) {
Expand Down Expand Up @@ -184,12 +181,12 @@ static bool aes_256_generic_hook(MongoCrypt* mongoCrypt,
Uint8Array keyBuffer = BufferFromBinary(env, key);
Uint8Array ivBuffer = BufferFromBinary(env, iv);
Uint8Array inBuffer = BufferFromBinary(env, in);
Uint8Array outBuffer = BufferWithLengthOf(env, out);
Uint8Array outputBuffer = BufferWithLengthOf(env, out);

Value result;
try {
result =
hook.Call(std::initializer_list<napi_value>{keyBuffer, ivBuffer, inBuffer, outBuffer});
hook.Call(std::initializer_list<napi_value>{keyBuffer, ivBuffer, inBuffer, outputBuffer});
} catch (...) {
return false;
}
Expand All @@ -200,7 +197,7 @@ static bool aes_256_generic_hook(MongoCrypt* mongoCrypt,
}

*bytes_written = result.ToNumber().Uint32Value();
CopyBufferData(out, outBuffer, *bytes_written);
CopyBufferData(out, outputBuffer, *bytes_written);
return true;
}

Expand Down Expand Up @@ -262,11 +259,11 @@ bool MongoCrypt::setupCryptoHooks() {
HandleScope scope(env);
Function hook = mongoCrypt->GetCallback("randomHook");

Uint8Array outBuffer = BufferWithLengthOf(env, out);
Uint8Array outputBuffer = BufferWithLengthOf(env, out);
Napi::Value result;
try {
result =
hook.Call(std::initializer_list<napi_value>{outBuffer, Number::New(env, count)});
hook.Call(std::initializer_list<napi_value>{outputBuffer, Number::New(env, count)});
} catch (...) {
return false;
}
Expand All @@ -276,7 +273,7 @@ bool MongoCrypt::setupCryptoHooks() {
return false;
}

CopyBufferData(out, outBuffer);
CopyBufferData(out, outputBuffer);
return true;
};

Expand Down
8 changes: 2 additions & 6 deletions test/benchmarks/bench.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ const ERROR = 0;

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

let { CRYPT_SHARED_LIB_PATH = '' } = process.env;
const cryptSharedLibPath = CRYPT_SHARED_LIB_PATH.length !== 0 ? CRYPT_SHARED_LIB_PATH : undefined;
const { CRYPT_SHARED_LIB_PATH: cryptSharedLibPath = '' } = process.env;

const warmupSecs = 2;
const testInSecs = 57;
Expand Down Expand Up @@ -121,10 +120,7 @@ function main() {
`testInSecs=${testInSecs}`
);

const mongoCryptOptions = {
kmsProviders: BSON.serialize(kmsProviders),
cryptoCallbacks
};
const mongoCryptOptions = { kmsProviders: BSON.serialize(kmsProviders), cryptoCallbacks };
if (cryptSharedLibPath) mongoCryptOptions.cryptSharedLibPath = cryptSharedLibPath;

const mongoCrypt = new MongoCrypt(mongoCryptOptions);
Expand Down

0 comments on commit 238457d

Please sign in to comment.