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

perf(client): prevent unneeded memory copy in BlobWitnessProvider #5

Closed
wants to merge 1 commit into from

Conversation

samlaf
Copy link
Contributor

@samlaf samlaf commented Dec 20, 2024

from_bytes creates a copy of the passed slice. We can instead just use new directly which takes ownership of the blob, and prevents the copy, since we don't need it afterwards.

Copy link
Collaborator

@hashcashier hashcashier left a comment

Choose a reason for hiding this comment

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

I think the PR title should change because this doesn't actually remove the host-side memcpy it refers to, but uses the Copy trait instead.

Would be great if all host-side occurrences of from_bytes this could be replaced with this simpler variant though!

@@ -44,7 +44,7 @@ impl<T: BlobProvider + Send> BlobProvider for BlobWitnessProvider<T> {
) -> Result<Vec<Box<Blob>>, Self::Error> {
let blobs = self.provider.get_blobs(block_ref, blob_hashes).await?;
for blob in &blobs {
let c_kzg_blob = c_kzg::Blob::from_bytes(blob.as_slice()).unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't the only host-side occurrence of using from_bytes.

@@ -44,7 +44,7 @@ impl<T: BlobProvider + Send> BlobProvider for BlobWitnessProvider<T> {
) -> Result<Vec<Box<Blob>>, Self::Error> {
let blobs = self.provider.get_blobs(block_ref, blob_hashes).await?;
for blob in &blobs {
let c_kzg_blob = c_kzg::Blob::from_bytes(blob.as_slice()).unwrap();
let c_kzg_blob = c_kzg::Blob::new(***blob);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe this change makes the copy implicit (via Copy trait) racer than explicit via from slice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah you might be right!

#[repr(C)]
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct Blob {
    bytes: [u8; 131072usize],
}

Could the fact that its C representation mean its actually moved and not copied? If not, then you're right and I will just close this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Feel like c-kzg should have a move constructor though...!

Copy link
Collaborator

Choose a reason for hiding this comment

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

The memory representation doesn't affect the borrow checker afaik

Copy link
Contributor Author

@samlaf samlaf Dec 24, 2024

Choose a reason for hiding this comment

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

After reading a bit more, pretty convinced also this is true. But also don't think the borrow checker and the fact that a struct implements the Copy trait actually means it will be copied when passed into a function that doesn't modify it, because those are lower level concerns. Rust passes arguments by value, but I think it is also able to perform https://en.wikipedia.org/wiki/Copy_elision like C++. Hence I do think the change in this PR could help (probably only when building with --release to have compiler optimizations). Nonetheless, this change would make a lot more sense if I implemented it uniformly across the codebase, and wrote some benchmark to actually show that it helps. So I'll close for now. Enjoy your holiday break! :)

@samlaf samlaf closed this Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants