Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,13 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"

[[package]]
name = "fast_memcpy"
version = "0.0.0"
dependencies = [
"criterion",
]

[[package]]
name = "fast_select"
version = "0.0.0"
Expand Down Expand Up @@ -8086,6 +8093,7 @@ name = "underhill_entry"
version = "0.0.0"
dependencies = [
"anyhow",
"fast_memcpy",
"mimalloc",
"openssl_crypto_only",
"underhill_core",
Expand Down
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ console_relay = { path = "support/console_relay" }
safe_intrinsics = { path = "support/safe_intrinsics" }
debug_ptr = { path = "support/debug_ptr" }
user_driver_emulated_mock = { path = "vm/devices/user_driver_emulated_mock" }
fast_memcpy = { path = "support/fast_memcpy" }
fast_select = { path = "support/fast_select" }
fdt = { path = "support/fdt" }
guid = { path = "support/guid" }
Expand Down Expand Up @@ -615,6 +616,11 @@ opt-level = 3
[profile.dev.package.generator]
opt-level = 3

# memcpy needs to be optimized heavily regardless of the build profile
[profile.dev.package.fast_memcpy]
opt-level = 3
overflow-checks = false

[patch.crates-io]
# Pending <https://github.com/ferrilab/bitvec/pull/273>
bitvec = { git = "https://github.com/smalis-msft/bitvec", branch = "set-aliased-previous-val" }
Expand Down
1 change: 1 addition & 0 deletions openhcl/underhill_entry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ underhill_dump.workspace = true
openssl_crypto_only.workspace = true

anyhow.workspace = true
fast_memcpy = { workspace = true, features = ["replace_system_memcpy"] }
mimalloc.workspace = true

[lints]
Expand Down
7 changes: 7 additions & 0 deletions openhcl/underhill_entry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

// musl's memcpy implementation is slow on x86_64, so we use memcpy crate to
// provide an optimized implementation.
//
// xtask-fmt allow-target-arch sys-crate
#[cfg(target_arch = "x86_64")]
use fast_memcpy as _;

// OpenVMM-HCL only needs libcrypto from openssl, not libssl.
#[cfg(target_os = "linux")]
openssl_crypto_only::openssl_crypto_only!();
Expand Down
23 changes: 23 additions & 0 deletions support/fast_memcpy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

[package]
name = "fast_memcpy"
rust-version.workspace = true
edition.workspace = true

[features]
# Export `memcpy` and `memmove`, replacing the system-provided implementation.
replace_system_memcpy = []

[dependencies]

[dev-dependencies]
criterion.workspace = true

[[bench]]
name = "perf"
harness = false

[lints]
workspace = true
44 changes: 44 additions & 0 deletions support/fast_memcpy/benches/perf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Performance tests.

// UNSAFETY: testing unsafe code.
#![expect(unsafe_code)]
#![expect(missing_docs)]

use criterion::BenchmarkId;

criterion::criterion_main!(benches);

criterion::criterion_group!(benches, bench_memcpy);

fn bench_memcpy(c: &mut criterion::Criterion) {
unsafe extern "C" {
fn memcpy(dest: *mut u8, src: *const u8, len: usize) -> *mut u8;
}
do_bench_memcpy(c.benchmark_group("fast_memcpy"), fast_memcpy::memcpy);
do_bench_memcpy(c.benchmark_group("system_memcpy"), memcpy);
}

fn do_bench_memcpy(
mut group: criterion::BenchmarkGroup<'_, criterion::measurement::WallTime>,
memcpy_fn: unsafe extern "C" fn(*mut u8, *const u8, usize) -> *mut u8,
) {
for &len in &[
1usize, 2, 3, 4, 7, 8, 12, 24, 32, 48, 64, 256, 1024, 4096, 8000,
] {
group.bench_function(BenchmarkId::new("len", len), |b| {
let src = vec![0u8; len];
let mut dest = vec![0u8; len];
// SAFETY: operating correctly on src/dest.
b.iter(|| unsafe {
memcpy_fn(
core::hint::black_box(dest.as_mut_ptr()),
core::hint::black_box(src.as_ptr()),
core::hint::black_box(len),
)
});
});
}
}
Loading
Loading