mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
perf(ext/crypto): optimize getRandomValues
(#16212)
This commit is contained in:
parent
cc3e2b9b1a
commit
a622c5df27
3 changed files with 32 additions and 6 deletions
21
cli/bench/getrandom.js
Normal file
21
cli/bench/getrandom.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
let [total, count] = typeof Deno !== "undefined"
|
||||
? Deno.args
|
||||
: [process.argv[2], process.argv[3]];
|
||||
|
||||
total = total ? parseInt(total, 0) : 50;
|
||||
count = count ? parseInt(count, 10) : 100000;
|
||||
|
||||
async function bench(fun) {
|
||||
const start = Date.now();
|
||||
for (let i = 0; i < count; i++) await fun();
|
||||
const elapsed = Date.now() - start;
|
||||
const rate = Math.floor(count / (elapsed / 1000));
|
||||
console.log(`time ${elapsed} ms rate ${rate}`);
|
||||
if (--total) await bench(fun);
|
||||
}
|
||||
|
||||
const c = typeof crypto !== "undefined" ? crypto : require("crypto").webcrypto;
|
||||
|
||||
const ui8 = new Uint8Array(1024);
|
||||
bench(() => c.getRandomValues(ui8));
|
|
@ -4652,6 +4652,11 @@
|
|||
webidl.assertBranded(this, CryptoPrototype);
|
||||
const prefix = "Failed to execute 'getRandomValues' on 'Crypto'";
|
||||
webidl.requiredArguments(arguments.length, 1, { prefix });
|
||||
// Fast path for Uint8Array
|
||||
if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, arrayBufferView)) {
|
||||
ops.op_crypto_get_random_values(arrayBufferView);
|
||||
return arrayBufferView;
|
||||
}
|
||||
arrayBufferView = webidl.converters.ArrayBufferView(arrayBufferView, {
|
||||
prefix,
|
||||
context: "Argument 1",
|
||||
|
|
|
@ -132,24 +132,24 @@ pub fn op_crypto_base64url_encode(data: ZeroCopyBuf) -> String {
|
|||
data
|
||||
}
|
||||
|
||||
#[op]
|
||||
#[op(fast)]
|
||||
pub fn op_crypto_get_random_values(
|
||||
state: &mut OpState,
|
||||
mut zero_copy: ZeroCopyBuf,
|
||||
out: &mut [u8],
|
||||
) -> Result<(), AnyError> {
|
||||
if zero_copy.len() > 65536 {
|
||||
if out.len() > 65536 {
|
||||
return Err(
|
||||
deno_web::DomExceptionQuotaExceededError::new(&format!("The ArrayBufferView's byte length ({}) exceeds the number of bytes of entropy available via this API (65536)", zero_copy.len()))
|
||||
deno_web::DomExceptionQuotaExceededError::new(&format!("The ArrayBufferView's byte length ({}) exceeds the number of bytes of entropy available via this API (65536)", out.len()))
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
|
||||
let maybe_seeded_rng = state.try_borrow_mut::<StdRng>();
|
||||
if let Some(seeded_rng) = maybe_seeded_rng {
|
||||
seeded_rng.fill(&mut *zero_copy);
|
||||
seeded_rng.fill(out);
|
||||
} else {
|
||||
let mut rng = thread_rng();
|
||||
rng.fill(&mut *zero_copy);
|
||||
rng.fill(out);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
Loading…
Reference in a new issue