Skip to content

Commit

Permalink
Add SIMD flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Jul 11, 2024
1 parent 06893af commit c79fdaa
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
14 changes: 11 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions llrt_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ patches = ["patches/promise-poll.patch"]
rquickjs-core = { path = "target/patch/rquickjs-core-0.6.2" }

[dependencies]
llrt_modules = { path = "../llrt_modules", features = ["all"] }
llrt_utils = { path = "../llrt_utils", features = ["all"] }
llrt_modules = { path = "../llrt_modules", features = ["all-simd"], default-features = false }
llrt_utils = { path = "../llrt_utils", features = ["all-simd"], default-features = false }
chrono = { version = "0.4.38", default-features = false, features = ["std"] }
quick-xml = "0.35.0"
crc32c = { version = "0.6.8" }
Expand Down
2 changes: 2 additions & 0 deletions llrt_modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ license-file = "LICENSE"
[features]
default = ["all"]
all = ["buffer", "fs", "path"]
all-simd = ["buffer-simd", "fs", "path"]

buffer = ["llrt_utils/encoding"]
buffer-simd = ["llrt_utils/encoding-simd"]
fs = ["tokio/fs", "llrt_utils/fs", "ring", "buffer", "path"]
path = []

Expand Down
2 changes: 1 addition & 1 deletion llrt_modules/src/modules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#[cfg(feature = "buffer")]
#[cfg(any(feature = "buffer", feature = "buffer-simd"))]
pub mod buffer;
#[cfg(feature = "fs")]
pub mod fs;
Expand Down
6 changes: 5 additions & 1 deletion llrt_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ license-file = "LICENSE"
[features]
default = ["all"]
all = ["fs", "encoding"]
all-simd = ["fs", "encoding-simd"]

fs = ["tokio/fs"]
encoding = ["base64-simd", "hex-simd"]
encoding = ["base64", "hex"]
encoding-simd = ["base64-simd", "hex-simd"]

[dependencies]
base64 = { version = "0.22", optional = true }
base64-simd = { version = "0.8.0", optional = true }
hex = { version = "0.4", optional = true }
hex-simd = { version = "0.8.0", optional = true }
rquickjs = { version = "0.6.2", features = [
"array-buffer",
Expand Down
48 changes: 48 additions & 0 deletions llrt_utils/src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#[cfg(not(feature = "encoding-simd"))]
use base64::{
alphabet,
engine::general_purpose::STANDARD,
engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig},
Engine,
};
#[cfg(feature = "encoding-simd")]
use hex_simd::AsciiCase;

macro_rules! encoder_enum {
Expand Down Expand Up @@ -86,30 +94,70 @@ impl Encoder {
}
}

#[cfg(feature = "encoding-simd")]
pub fn bytes_to_hex(bytes: &[u8]) -> Vec<u8> {
hex_simd::encode_type(bytes, AsciiCase::Lower)
}

#[cfg(not(feature = "encoding-simd"))]
pub fn bytes_to_hex(bytes: &[u8]) -> Vec<u8> {
hex::encode(bytes).into_bytes()
}

#[cfg(feature = "encoding-simd")]
pub fn bytes_from_hex(hex_bytes: &[u8]) -> Result<Vec<u8>, String> {
hex_simd::decode_to_vec(hex_bytes).map_err(|err| err.to_string())
}

#[cfg(not(feature = "encoding-simd"))]
pub fn bytes_from_hex(hex_bytes: &[u8]) -> Result<Vec<u8>, String> {
hex::decode(hex_bytes).map_err(|err| err.to_string())
}

#[cfg(feature = "encoding-simd")]
pub fn bytes_to_b64_string(bytes: &[u8]) -> String {
base64_simd::STANDARD.encode_to_string(bytes)
}

#[cfg(not(feature = "encoding-simd"))]
pub fn bytes_to_b64_string(bytes: &[u8]) -> String {
STANDARD.encode(bytes)
}

#[cfg(feature = "encoding-simd")]
pub fn bytes_from_b64(bytes: &[u8]) -> Result<Vec<u8>, String> {
base64_simd::forgiving_decode_to_vec(bytes).map_err(|e| e.to_string())
}

#[cfg(not(feature = "encoding-simd"))]
pub fn bytes_from_b64(bytes: &[u8]) -> Result<Vec<u8>, String> {
const STANDARD_FORGIVING: GeneralPurpose = GeneralPurpose::new(
&alphabet::STANDARD,
GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent),
);
STANDARD_FORGIVING.decode(bytes).map_err(|e| e.to_string())
}

#[cfg(feature = "encoding-simd")]
pub fn bytes_to_b64(bytes: &[u8]) -> Vec<u8> {
base64_simd::STANDARD.encode_type(bytes)
}

#[cfg(not(feature = "encoding-simd"))]
pub fn bytes_to_b64(bytes: &[u8]) -> Vec<u8> {
STANDARD.encode(bytes).into_bytes()
}

#[cfg(feature = "encoding-simd")]
pub fn bytes_to_hex_string(bytes: &[u8]) -> String {
hex_simd::encode_to_string(bytes, AsciiCase::Lower)
}

#[cfg(not(feature = "encoding-simd"))]
pub fn bytes_to_hex_string(bytes: &[u8]) -> String {
hex::encode(bytes)
}

pub fn bytes_to_string(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes).to_string()
}
2 changes: 1 addition & 1 deletion llrt_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
pub mod bytes;
#[cfg(feature = "encoding")]
#[cfg(any(feature = "encoding", feature = "encoding-simd"))]
pub mod encoding;
#[cfg(feature = "fs")]
pub mod fs;
Expand Down

0 comments on commit c79fdaa

Please sign in to comment.