-
Notifications
You must be signed in to change notification settings - Fork 22
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
Hashing without memory allocations #69
Conversation
I got the idea that have the calldata variant is pointless but with the current implementation of |
Ah, you're right. It might be worth adding a calldata variant in that case. |
Actually, I think it's not necessary. You can slice the calldata buffer and it'll copy just the slice, not the whole buffer. Like this: function test_calldataSlice(bytes calldata data) public {
bytes calldata slice = data[5:data.length];
bytes32 hash = slice.keccak256Subarray(0, slice.length);
} |
Yeah, that's true but the user should be aware of that. The common way we usually use the |
I don't think we should add functions without good reason. Every new piece of code means more maintenance in the long run. Harder to read codebase, etc. |
b1edae2
to
63b9241
Compare
When reviewing this PR after the latest changes I realized something: it's actually useful to have a calldata variant because we can use the space after the free memory pointer to copy the data, hash it and then return the result. The good thing about this is that we don't consume a fragment of the memory while respecting the memory model. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it!
One thought: Otoh, since slicing Would have the small advantage that hashing a full |
Hm, makes sense. I'll do that then. |
This adds a few functions that allow the user to hash without extra memory allocations.
This doesn't include calldata variants because they would be pointless: the
KECCAK256
instruction only operates with data in memory so a copy to memory would be necessary anyway.