Skip to main content

CompressMultiScoped

Function CompressMultiScoped 

Source
pub fn CompressMultiScoped<Alloc: BrotliAlloc + Send + 'static, Scope: ThreadScope>(
    params: &BrotliEncoderParams,
    input: &[u8],
    output: &mut [u8],
    alloc_per_thread: &mut [Option<Alloc>],
    thread_scope: &Scope,
) -> Result<usize, BrotliEncoderThreadError>
where <Alloc as Allocator<u8>>::AllocatedMemory: Send, <Alloc as Allocator<u16>>::AllocatedMemory: Send, <Alloc as Allocator<u32>>::AllocatedMemory: Send,
Expand description

Compresses input in alloc_per_thread.len() catable chunks inside a caller-provided thread scope, concatenating them into output and returning the compressed size.

This is CompressMulti without the ownership dance: because the scope guarantees the workers finish before it returns, the input is borrowed rather than moved behind an Arc<RwLock<..>>, so the workers read it with no locking and no refcount traffic. Each worker writes its result straight into its own slot instead of being joined one at a time, and — as in CompressMulti — the last chunk is compressed on the calling thread while the others run.

Every slot of alloc_per_thread must be Some on entry; on return each slot holds its allocator back, except for a slot whose worker panicked.

use simd_brotli::enc::BrotliEncoderParams;
use simd_brotli::enc::threading::{CompressMultiScoped, StdThreadScope};
use alloc_stdlib::StandardAlloc;

let input = vec![42u8; 1 << 16];
let mut output = vec![0u8; 1 << 16];
let mut alloc_per_thread = [Some(StandardAlloc::default()); 4];
let size = CompressMultiScoped(
    &BrotliEncoderParams::default(),
    &input[..],
    &mut output[..],
    &mut alloc_per_thread[..],
    &StdThreadScope,
).unwrap();
assert!(size < input.len());