summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-10-09 03:08:38 -0700
committerGitHub <noreply@github.com>2022-10-09 15:38:38 +0530
commita622c5df27e908bff152ce7374c47dabfdba0bba (patch)
tree0c836839a6d359db2afa8baf640860a31decf714 /ext
parentcc3e2b9b1a7ccd6c671bc0b3813cf7e2b0d88b5c (diff)
perf(ext/crypto): optimize `getRandomValues` (#16212)
Diffstat (limited to 'ext')
-rw-r--r--ext/crypto/00_crypto.js5
-rw-r--r--ext/crypto/lib.rs12
2 files changed, 11 insertions, 6 deletions
diff --git a/ext/crypto/00_crypto.js b/ext/crypto/00_crypto.js
index 907308325..7b21c9287 100644
--- a/ext/crypto/00_crypto.js
+++ b/ext/crypto/00_crypto.js
@@ -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",
diff --git a/ext/crypto/lib.rs b/ext/crypto/lib.rs
index d949135e7..f09982920 100644
--- a/ext/crypto/lib.rs
+++ b/ext/crypto/lib.rs
@@ -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(())