Skip to content

Commit

Permalink
chore: upgrade base64-simd to 0.8.0 (denoland#17463)
Browse files Browse the repository at this point in the history
This PR upgrades the `base64-simd` dependency of `deno_web`.

base64-simd v0.8 supports `forgiving_decode` in ["copy"
mode](https://docs.rs/base64-simd/0.8.0/base64_simd/fn.forgiving_decode.html),
["inplace"
mode](https://docs.rs/base64-simd/0.8.0/base64_simd/fn.forgiving_decode_inplace.html)
or ["alloc"
mode](https://docs.rs/base64-simd/0.8.0/base64_simd/fn.forgiving_decode_to_vec.html).
When denoland#17159 resolves, they can be used to reduce unnecessary allocations
and copies.

base64-simd v0.8 also supports AArch64 SIMD out of box.
  • Loading branch information
Nugine committed Jan 18, 2023
1 parent f1b275e commit 9686a00
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
26 changes: 12 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ext/web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ path = "lib.rs"

[dependencies]
async-trait.workspace = true
base64-simd = "0.7"
base64-simd = "0.8"
deno_core.workspace = true
encoding_rs.workspace = true
flate2.workspace = true
Expand Down
15 changes: 8 additions & 7 deletions ext/web/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,27 @@ pub fn init<P: TimersPermission + 'static>(
#[op]
fn op_base64_decode(input: String) -> Result<ZeroCopyBuf, AnyError> {
let mut s = input.into_bytes();
let decoded_len = forgiving_base64_decode(&mut s)?;
let decoded_len = forgiving_base64_decode_inplace(&mut s)?;
s.truncate(decoded_len);
Ok(s.into())
}

#[op]
fn op_base64_atob(mut s: ByteString) -> Result<ByteString, AnyError> {
let decoded_len = forgiving_base64_decode(&mut s)?;
let decoded_len = forgiving_base64_decode_inplace(&mut s)?;
s.truncate(decoded_len);
Ok(s)
}

/// See <https://infra.spec.whatwg.org/#forgiving-base64>
#[inline]
fn forgiving_base64_decode(input: &mut [u8]) -> Result<usize, AnyError> {
fn forgiving_base64_decode_inplace(
input: &mut [u8],
) -> Result<usize, AnyError> {
let error: _ =
|| DomExceptionInvalidCharacterError::new("Failed to decode base64");
let decoded = base64_simd::Base64::forgiving_decode_inplace(input)
.map_err(|_| error())?;
let decoded =
base64_simd::forgiving_decode_inplace(input).map_err(|_| error())?;
Ok(decoded.len())
}

Expand All @@ -165,8 +167,7 @@ fn op_base64_btoa(s: ByteString) -> String {
/// See <https://infra.spec.whatwg.org/#forgiving-base64>
#[inline]
fn forgiving_base64_encode(s: &[u8]) -> String {
const BASE64_STANDARD: base64_simd::Base64 = base64_simd::Base64::STANDARD;
BASE64_STANDARD.encode_to_boxed_str(s).into_string()
base64_simd::STANDARD.encode_to_string(s)
}

#[op]
Expand Down

0 comments on commit 9686a00

Please sign in to comment.