Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make add_piece taking less time #1707

Merged
merged 1 commit into from
May 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions filecoin-proofs/src/commitment_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use rayon::prelude::{ParallelIterator, ParallelSlice};

use crate::{constants::DefaultPieceHasher, pieces::piece_hash};

const BUFFER_SIZE: usize = 4096;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried bumping it even further, for example, up to 1MiB-8MiB?
Even with the 4KiB, it is just 64 SHA invocations, the branch predictor is probably just warming up at that point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried 8MiB it didn't make a difference.

I think yet another difference would make it if the hashing would be done in parallel. But a simple "par chunker" didn't really help, as then the pieces are to small. One need to implement it manually. I decided that it's not worth it and that this improvement is already good enough for how simple it is.


/// Calculates comm-d of the data piped through to it.
/// Data must be bit padded and power of 2 bytes.
pub struct CommitmentReader<R> {
source: R,
buffer: [u8; 64],
buffer: Vec<u8>,
buffer_pos: usize,
current_tree: Vec<<DefaultPieceHasher as Hasher>::Domain>,
}
Expand All @@ -20,21 +22,24 @@ impl<R: Read> CommitmentReader<R> {
pub fn new(source: R) -> Self {
CommitmentReader {
source,
buffer: [0u8; 64],
buffer: vec![0u8; BUFFER_SIZE],
buffer_pos: 0,
current_tree: Vec::new(),
}
}

/// Attempt to generate the next hash, but only if the buffers are full.
fn try_hash(&mut self) {
if self.buffer_pos < 63 {
// Get more bytes in case we cannot iterate cleanly in 64 byte chunks.
if self.buffer_pos % 64 != 0 {
return;
}

// WARNING: keep in sync with DefaultPieceHasher and its .node impl
let hash = <DefaultPieceHasher as Hasher>::Function::hash(&self.buffer);
self.current_tree.push(hash);
for chunk in self.buffer[..self.buffer_pos].chunks_exact(64) {
// WARNING: keep in sync with DefaultPieceHasher and its .node impl
let hash = <DefaultPieceHasher as Hasher>::Function::hash(chunk);
self.current_tree.push(hash);
}
self.buffer_pos = 0;

// TODO: reduce hashes when possible, instead of keeping them around.
Expand Down Expand Up @@ -67,7 +72,7 @@ impl<R: Read> CommitmentReader<R> {
impl<R: Read> Read for CommitmentReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let start = self.buffer_pos;
let left = 64 - self.buffer_pos;
let left = BUFFER_SIZE - self.buffer_pos;
let end = start + min(left, buf.len());

// fill the buffer as much as possible
Expand Down