Skip to content

Commit

Permalink
Avoid calloc for encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersTrier committed Aug 15, 2024
1 parent 566fde7 commit c1f11ee
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
27 changes: 26 additions & 1 deletion src/engine/shards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,32 @@ impl Shards {
self.shard_count = shard_count;
self.shard_bytes = shard_bytes;

self.data.resize(shard_count * (shard_bytes / 64), [0; 64]);
self.data.clear();
self.data.reserve(shard_count * (shard_bytes / 64));
}

pub(crate) fn zero_alloc(&mut self, shard_count: usize, shard_bytes: usize) {
assert!(shard_bytes > 0 && shard_bytes & 63 == 0);

self.shard_count = shard_count;
self.shard_bytes = shard_bytes;
self.data.resize(self.shard_count * (self.shard_bytes / 64), [0; 64]);
}

pub(crate) fn push(&mut self, shard: &[u8]) {
assert_eq!(shard.len(), self.shard_bytes);
assert!(shard.len() > 0 && shard.len() & 63 == 0);

for chunk in shard.chunks_exact(64) {
match chunk.try_into() {
Ok(c) => self.data.push(c),
_ => unreachable!()
}
}
}

pub(crate) fn clear_recovery(&mut self) {
self.data.resize(self.shard_count * (self.shard_bytes / 64), [0; 64]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rate/decoder_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl DecoderWork {
self.received.grow(max_received_pos);
}

self.shards.resize(work_count, shard_bytes);
self.shards.zero_alloc(work_count, shard_bytes);
}

pub(crate) fn reset_received(&mut self) {
Expand Down
5 changes: 2 additions & 3 deletions src/rate/encoder_work.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ impl EncoderWork {
got: original_shard.len(),
})
} else {
self.shards[self.original_received_count]
.as_flattened_mut()
.copy_from_slice(original_shard);
self.shards.push(original_shard);
self.original_received_count += 1;
Ok(())
}
Expand All @@ -77,6 +75,7 @@ impl EncoderWork {
original_received_count: self.original_received_count,
})
} else {
self.shards.clear_recovery();
Ok((
self.shards.as_ref_mut(),
self.original_count,
Expand Down

0 comments on commit c1f11ee

Please sign in to comment.