refactor(compress): move MT input hint dispatch to Rust
Project the scalar single-thread hint inputs and keep the multithreaded context behind a C callback so Rust owns the worker-count dispatch. Preserve the existing buffer-mode assertion and both hint arithmetic leaves, with focused tests covering MT selection and single-thread fallback. All heavy verification was run serially with a 40 GiB virtual-memory cap and one build job. Test Plan: - git diff --cached --check - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/Cargo.toml --all-targets -- -D warnings - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/Cargo.toml --all-targets (790 passed) - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo clippy --manifest-path rust/cli/Cargo.toml --all-targets -- -D warnings - ulimit -v 41943040; CARGO_BUILD_JOBS=1 cargo test --manifest-path rust/cli/Cargo.toml --all-targets (184 passed) - ulimit -v 41943040; cargo +nightly fmt --manifest-path rust/Cargo.toml --all -- --check - ulimit -v 41943040; make -j1 - ulimit -v 41943040; make -j1 -C tests test
This commit is contained in:
@@ -6831,6 +6831,88 @@ pub extern "C" fn ZSTD_rust_nextInputSizeHint(
|
||||
)
|
||||
}
|
||||
|
||||
type NextInputSizeHintMTorSTFn = unsafe extern "C" fn(*mut c_void) -> usize;
|
||||
|
||||
/// Projection for the MT-versus-single-thread input-hint dispatch.
|
||||
///
|
||||
/// Rust owns only the worker-count branch. The single-thread hint inputs are
|
||||
/// scalar, while the MT context remains opaque behind its C callback.
|
||||
#[repr(C)]
|
||||
pub struct ZSTD_rust_nextInputSizeHintMTorSTState {
|
||||
block_size_max: usize,
|
||||
stable_in_not_consumed: usize,
|
||||
in_buff_target: usize,
|
||||
in_buff_pos: usize,
|
||||
nb_workers: c_int,
|
||||
in_buffer_mode: c_int,
|
||||
mt_context: *mut c_void,
|
||||
mt_next_input_size_hint: Option<NextInputSizeHintMTorSTFn>,
|
||||
}
|
||||
|
||||
const _: () = {
|
||||
assert!(size_of::<NextInputSizeHintMTorSTFn>() == size_of::<usize>());
|
||||
assert!(size_of::<Option<NextInputSizeHintMTorSTFn>>() == size_of::<usize>());
|
||||
assert!(offset_of!(ZSTD_rust_nextInputSizeHintMTorSTState, block_size_max) == 0);
|
||||
assert!(
|
||||
offset_of!(
|
||||
ZSTD_rust_nextInputSizeHintMTorSTState,
|
||||
stable_in_not_consumed
|
||||
) == size_of::<usize>()
|
||||
);
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_nextInputSizeHintMTorSTState, in_buff_target)
|
||||
== 2 * size_of::<usize>()
|
||||
);
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_nextInputSizeHintMTorSTState, in_buff_pos) == 3 * size_of::<usize>()
|
||||
);
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_nextInputSizeHintMTorSTState, nb_workers) == 4 * size_of::<usize>()
|
||||
);
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_nextInputSizeHintMTorSTState, in_buffer_mode)
|
||||
== 4 * size_of::<usize>() + size_of::<c_int>()
|
||||
);
|
||||
assert!(
|
||||
offset_of!(ZSTD_rust_nextInputSizeHintMTorSTState, mt_context)
|
||||
== 4 * size_of::<usize>() + 2 * size_of::<c_int>()
|
||||
);
|
||||
assert!(
|
||||
offset_of!(
|
||||
ZSTD_rust_nextInputSizeHintMTorSTState,
|
||||
mt_next_input_size_hint
|
||||
) == 4 * size_of::<usize>() + 2 * size_of::<c_int>() + size_of::<usize>()
|
||||
);
|
||||
assert!(
|
||||
size_of::<ZSTD_rust_nextInputSizeHintMTorSTState>()
|
||||
== 4 * size_of::<usize>() + 2 * size_of::<c_int>() + 2 * size_of::<usize>()
|
||||
);
|
||||
};
|
||||
|
||||
/// Select the MT or single-thread input-size hint without exposing the MT
|
||||
/// context representation to Rust.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn ZSTD_rust_nextInputSizeHintMTorST(
|
||||
state: *const ZSTD_rust_nextInputSizeHintMTorSTState,
|
||||
) -> usize {
|
||||
let Some(state) = (unsafe { state.as_ref() }) else {
|
||||
return 0;
|
||||
};
|
||||
if state.nb_workers >= 1 {
|
||||
let Some(mt_next_input_size_hint) = state.mt_next_input_size_hint else {
|
||||
return 0;
|
||||
};
|
||||
return unsafe { mt_next_input_size_hint(state.mt_context) };
|
||||
}
|
||||
next_input_size_hint(
|
||||
state.in_buffer_mode,
|
||||
state.block_size_max,
|
||||
state.stable_in_not_consumed,
|
||||
state.in_buff_target,
|
||||
state.in_buff_pos,
|
||||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn mt_next_input_size_hint(target_section_size: usize, in_buff_filled: usize) -> usize {
|
||||
let hint_in_size = target_section_size.wrapping_sub(in_buff_filled);
|
||||
@@ -14731,6 +14813,48 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
unsafe extern "C" fn next_input_size_hint_mt_or_st_test_callback(
|
||||
context: *mut c_void,
|
||||
) -> usize {
|
||||
let calls = unsafe { &mut *context.cast::<usize>() };
|
||||
*calls += 1;
|
||||
777
|
||||
}
|
||||
|
||||
fn next_input_size_hint_mt_or_st_test_state(
|
||||
nb_workers: c_int,
|
||||
calls: &mut usize,
|
||||
) -> ZSTD_rust_nextInputSizeHintMTorSTState {
|
||||
ZSTD_rust_nextInputSizeHintMTorSTState {
|
||||
block_size_max: 256,
|
||||
stable_in_not_consumed: 37,
|
||||
in_buff_target: 128,
|
||||
in_buff_pos: 32,
|
||||
nb_workers,
|
||||
in_buffer_mode: ZSTD_BM_BUFFERED,
|
||||
mt_context: calls as *mut usize as *mut c_void,
|
||||
mt_next_input_size_hint: Some(next_input_size_hint_mt_or_st_test_callback),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn next_input_size_hint_mt_or_st_dispatches_to_mt_callback_for_workers() {
|
||||
let mut calls = 0;
|
||||
let state = next_input_size_hint_mt_or_st_test_state(1, &mut calls);
|
||||
|
||||
assert_eq!(unsafe { ZSTD_rust_nextInputSizeHintMTorST(&state) }, 777);
|
||||
assert_eq!(calls, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn next_input_size_hint_mt_or_st_uses_single_thread_leaf_without_workers() {
|
||||
let mut calls = 0;
|
||||
let state = next_input_size_hint_mt_or_st_test_state(0, &mut calls);
|
||||
|
||||
assert_eq!(unsafe { ZSTD_rust_nextInputSizeHintMTorST(&state) }, 96);
|
||||
assert_eq!(calls, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mt_next_input_size_hint_handles_empty_input_buffer() {
|
||||
assert_eq!(mt_next_input_size_hint(128, 0), 128);
|
||||
|
||||
Reference in New Issue
Block a user