Skip to content

Commit

Permalink
fix(decrypt): prevent extra clones while decrypting chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Dec 18, 2023
1 parent 377c072 commit a557f96
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 36 deletions.
27 changes: 3 additions & 24 deletions src/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::io::Cursor;
use std::sync::Arc;
use xor_name::XorName;

pub fn decrypt(src_hashes: Vec<XorName>, encrypted_chunks: Vec<EncryptedChunk>) -> Result<Bytes> {
pub fn decrypt(src_hashes: Vec<XorName>, encrypted_chunks: &[&EncryptedChunk]) -> Result<Bytes> {
let src_hashes = Arc::new(src_hashes);
let num_chunks = encrypted_chunks.len();
let cpus = num_cpus::get();
Expand All @@ -23,30 +23,19 @@ pub fn decrypt(src_hashes: Vec<XorName>, encrypted_chunks: Vec<EncryptedChunk>)
let raw_chunks: Vec<(usize, Bytes)> = encrypted_chunks
.chunks(batch_size)
.par_bridge()
.map(|batch| DecryptionBatch {
jobs: batch
.iter()
.map(|c| DecryptionJob {
index: c.index,
encrypted_content: c.content.clone(),
src_hashes: src_hashes.clone(),
})
.collect_vec(),
})
.map(|batch| {
batch
.jobs
.par_iter()
.map(|c| {
Ok::<(usize, Bytes), Error>((
c.index,
decrypt_chunk(c.index, c.encrypted_content.clone(), c.src_hashes.as_ref())?,
decrypt_chunk(c.index, c.content.clone(), src_hashes.as_ref())?,
))
})
.flatten()
.collect::<Vec<_>>()
})
.flatten()
.flatten()
.collect();

if num_chunks > raw_chunks.len() {
Expand All @@ -66,16 +55,6 @@ pub fn decrypt(src_hashes: Vec<XorName>, encrypted_chunks: Vec<EncryptedChunk>)
Ok(raw_data)
}

struct DecryptionBatch {
jobs: Vec<DecryptionJob>,
}

struct DecryptionJob {
index: usize,
encrypted_content: Bytes,
src_hashes: Arc<Vec<XorName>>,
}

pub(crate) fn decrypt_chunk(
chunk_number: usize,
content: Bytes,
Expand Down
19 changes: 7 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,12 +426,9 @@ pub fn encrypt(bytes: Bytes) -> Result<(DataMap, Vec<EncryptedChunk>)> {
/// Decrypts what is expected to be the full set of chunks covered by the data map.
pub fn decrypt_full_set(data_map: &DataMap, chunks: &[EncryptedChunk]) -> Result<Bytes> {
let src_hashes = extract_hashes(data_map);
let sorted_chunks = chunks
.iter()
.sorted_by_key(|c| c.index)
.cloned() // should not be needed, something is wrong here, the docs for sorted_by_key says it will return owned items...!
.collect_vec();
decrypt::decrypt(src_hashes, sorted_chunks)
let mut sorted_chunks = Vec::with_capacity(chunks.len());
sorted_chunks.extend(chunks.iter().sorted_by_key(|c| c.index));
decrypt::decrypt(src_hashes, &sorted_chunks)
}

/// Decrypts a range, used when seeking.
Expand All @@ -444,12 +441,10 @@ pub fn decrypt_range(
len: usize,
) -> Result<Bytes> {
let src_hashes = extract_hashes(data_map);
let encrypted_chunks = chunks
.iter()
.sorted_by_key(|c| c.index)
.cloned()
.collect_vec();
let mut bytes = decrypt::decrypt(src_hashes, encrypted_chunks)?;
let mut sorted_chunks = Vec::with_capacity(chunks.len());
sorted_chunks.extend(chunks.iter().sorted_by_key(|c| c.index));

let mut bytes = decrypt::decrypt(src_hashes, &sorted_chunks)?;

if relative_pos >= bytes.len() {
return Ok(Bytes::new());
Expand Down

0 comments on commit a557f96

Please sign in to comment.