Skip to content

Commit 238457d

Browse files
committed
perf: make use of mongocrypt_binary_t fields instead of getter functions
1 parent d8bf5ce commit 238457d

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

addon/mongocrypt.cc

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,11 @@ std::unique_ptr<mongocrypt_binary_t, MongoCryptBinaryDeleter> Uint8ArrayToBinary
4545
}
4646

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

5351
Uint8Array BufferWithLengthOf(Env env, mongocrypt_binary_t* binary) {
54-
size_t len = mongocrypt_binary_len(binary);
55-
return Buffer<uint8_t>::New(env, len);
52+
return Buffer<uint8_t>::New(env, binary->len);
5653
}
5754

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

6663
void CopyBufferData(mongocrypt_binary_t* out, Uint8Array buffer, size_t count) {
67-
assert(count <= mongocrypt_binary_len(out));
64+
assert(count <= out->len);
6865
assert(count <= buffer.ByteLength());
69-
memcpy(mongocrypt_binary_data(out), buffer.Data(), count);
66+
memcpy(out->data, buffer.Data(), count);
7067
}
7168

7269
void CopyBufferData(mongocrypt_binary_t* out, Uint8Array buffer) {
73-
CopyBufferData(out, buffer, mongocrypt_binary_len(out));
70+
CopyBufferData(out, buffer, out->len);
7471
}
7572

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

189186
Value result;
190187
try {
191188
result =
192-
hook.Call(std::initializer_list<napi_value>{keyBuffer, ivBuffer, inBuffer, outBuffer});
189+
hook.Call(std::initializer_list<napi_value>{keyBuffer, ivBuffer, inBuffer, outputBuffer});
193190
} catch (...) {
194191
return false;
195192
}
@@ -200,7 +197,7 @@ static bool aes_256_generic_hook(MongoCrypt* mongoCrypt,
200197
}
201198

202199
*bytes_written = result.ToNumber().Uint32Value();
203-
CopyBufferData(out, outBuffer, *bytes_written);
200+
CopyBufferData(out, outputBuffer, *bytes_written);
204201
return true;
205202
}
206203

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

265-
Uint8Array outBuffer = BufferWithLengthOf(env, out);
262+
Uint8Array outputBuffer = BufferWithLengthOf(env, out);
266263
Napi::Value result;
267264
try {
268265
result =
269-
hook.Call(std::initializer_list<napi_value>{outBuffer, Number::New(env, count)});
266+
hook.Call(std::initializer_list<napi_value>{outputBuffer, Number::New(env, count)});
270267
} catch (...) {
271268
return false;
272269
}
@@ -276,7 +273,7 @@ bool MongoCrypt::setupCryptoHooks() {
276273
return false;
277274
}
278275

279-
CopyBufferData(out, outBuffer);
276+
CopyBufferData(out, outputBuffer);
280277
return true;
281278
};
282279

test/benchmarks/bench.mjs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ const ERROR = 0;
1515

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

18-
let { CRYPT_SHARED_LIB_PATH = '' } = process.env;
19-
const cryptSharedLibPath = CRYPT_SHARED_LIB_PATH.length !== 0 ? CRYPT_SHARED_LIB_PATH : undefined;
18+
const { CRYPT_SHARED_LIB_PATH: cryptSharedLibPath = '' } = process.env;
2019

2120
const warmupSecs = 2;
2221
const testInSecs = 57;
@@ -121,10 +120,7 @@ function main() {
121120
`testInSecs=${testInSecs}`
122121
);
123122

124-
const mongoCryptOptions = {
125-
kmsProviders: BSON.serialize(kmsProviders),
126-
cryptoCallbacks
127-
};
123+
const mongoCryptOptions = { kmsProviders: BSON.serialize(kmsProviders), cryptoCallbacks };
128124
if (cryptSharedLibPath) mongoCryptOptions.cryptSharedLibPath = cryptSharedLibPath;
129125

130126
const mongoCrypt = new MongoCrypt(mongoCryptOptions);

0 commit comments

Comments
 (0)