Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use faster hash algorithm when loading and saving a module #4372

Merged
merged 4 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions lib/wasix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ repository.workspace = true
rust-version.workspace = true

[dependencies]
xxhash-rust = { version = "0.8.8", features = ["xxh64"] }
rusty_pool = { version = "0.7.0", optional = true }
cfg-if = "1.0"
thiserror = "1"
Expand Down
2 changes: 1 addition & 1 deletion lib/wasix/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub async fn load_module(
module_cache: &(dyn ModuleCache + Send + Sync),
wasm: &[u8],
) -> Result<Module, anyhow::Error> {
let hash = ModuleHash::sha256(wasm);
let hash = ModuleHash::xxhash(wasm);
let result = module_cache.load(hash, engine).await;

match result {
Expand Down
31 changes: 26 additions & 5 deletions lib/wasix/src/runtime/module_cache/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::hash::Hash;
use std::{
fmt::{self, Debug, Display, Formatter},
ops::Deref,
Expand Down Expand Up @@ -123,19 +124,22 @@ impl CacheError {

/// The SHA-256 hash of a WebAssembly module.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ModuleHash([u8; 32]);
pub enum ModuleHash {
Sha256([u8; 32]),
Xxhash([u8; 8]),
}

impl ModuleHash {
/// Create a new [`ModuleHash`] from the raw SHA-256 hash.
pub fn from_bytes(key: [u8; 32]) -> Self {
ModuleHash(key)
ModuleHash::Sha256(key)
}
syrusakbary marked this conversation as resolved.
Show resolved Hide resolved

/// Parse a sha256 hash from a hex-encoded string.
pub fn parse_hex(hex_str: &str) -> Result<Self, hex::FromHexError> {
let mut hash = [0_u8; 32];
hex::decode_to_slice(hex_str, &mut hash)?;
Ok(Self(hash))
Ok(Self::Sha256(hash))
}

/// Generate a new [`ModuleCache`] based on the SHA-256 hash of some bytes.
Expand All @@ -147,15 +151,32 @@ impl ModuleHash {
ModuleHash::from_bytes(hasher.finalize().into())
}

pub fn xxhash(wasm: impl AsRef<[u8]>) -> Self {
let wasm = wasm.as_ref();

let hash = xxhash_rust::xxh64::xxh64(wasm, 0);

Self::Xxhash(hash.to_ne_bytes())
}

/// Get the raw SHA-256 hash.
pub fn as_bytes(self) -> [u8; 32] {
self.0
if let ModuleHash::Sha256(inner) = self {
inner
} else {
unreachable!("this should be unreachable")
}
}
}

impl Display for ModuleHash {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
for byte in self.0 {
let bytes = match self {
ModuleHash::Sha256(bytes) => bytes.as_slice(),
ModuleHash::Xxhash(bytes) => bytes.as_slice(),
};

for byte in bytes {
write!(f, "{byte:02X}")?;
}

Expand Down
Loading