Skip to content

Replace usage of unsafe with Vec::extend_from_within #14

Closed
@paolobarbolini

Description

@paolobarbolini

When this will eventually become stable it will allow unsafe to be removed from here:

/*
const BATCH_SIZE: usize = 32;
let full_copies = match_length / BATCH_SIZE;
let partial_copy_size = match_length % BATCH_SIZE;
let mut buf = [0u8; BATCH_SIZE];
for x in 0..full_copies {
let idx = start_idx + x * BATCH_SIZE;
let source = &self.buffer.as_slice()[idx..idx + BATCH_SIZE];
buf[0..BATCH_SIZE].copy_from_slice(source);
self.buffer.extend(&buf[0..BATCH_SIZE]);
}
let idx = start_idx + full_copies * BATCH_SIZE;
let source = &self.buffer.as_slice()[idx..idx + partial_copy_size];
buf[0..partial_copy_size].copy_from_slice(source);
self.buffer.extend(&buf[0..partial_copy_size]);
*/
// using this unsafe block instead of the above increases performance by ca 5% when decoding the enwik9 dataset
self.buffer.reserve(match_length);
unsafe {
self.buffer.set_len(self.buffer.len() + match_length);
let slice = &mut self.buffer[start_idx..];
let src = slice.as_mut_ptr();
let dst = src.add(slice.len() - match_length);
std::ptr::copy_nonoverlapping(src, dst, match_length);
}

I just saw the tracking issue and I immediately remembered your crate could use something like this 😃

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions