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 3e48ebc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion 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 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 3e48ebc

Please sign in to comment.