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(precompile): use Bytes in precompile functions #1085

Merged
merged 3 commits into from
Feb 13, 2024

Conversation

DaniPopes
Copy link
Collaborator

Closes #1072.

let input = right_pad_vec(input, base_len + exp_len + mod_len);
let (base, input) = input.split_at(base_len);
let (exponent, modulus) = input.split_at(exp_len);
debug_assert_eq!(modulus.len(), mod_len);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Made this allocate only once in case input is too short.

modulus.len() must be mod_len due to the padding above, this is the same as slicing the input 3 times.

@@ -26,8 +25,7 @@ pub fn right_pad<const LEN: usize>(data: &[u8]) -> Cow<'_, [u8; LEN]> {
Cow::Borrowed(data.try_into().unwrap())
} else {
let mut padded = [0; LEN];
let end = min(LEN, data.len());
padded[..end].copy_from_slice(&data[..end]);
padded[..data.len()].copy_from_slice(data);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I realized these min calls were unnecessary since data.len() is always less than the input length by definition.

let gas_used = calc_linear_cost_u32(input.len(), IDENTITY_BASE, IDENTITY_PER_WORD);
if gas_used > gas_limit {
return Err(Error::OutOfGas);
}
Ok((gas_used, input.to_vec()))
Ok((gas_used, input.clone()))
Copy link
Collaborator Author

@DaniPopes DaniPopes Feb 13, 2024

Choose a reason for hiding this comment

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

This is the biggest win IMO, avoids malloc+memcpy basically as if we're using Arc. identity precompile is common in code generated by Vyper.

Copy link
Member

@rakita rakita left a comment

Choose a reason for hiding this comment

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

lgtm!

@rakita
Copy link
Member

rakita commented Feb 13, 2024

nit clippy

@rakita rakita merged commit 0651044 into bluealloy:main Feb 13, 2024
25 checks passed
@github-actions github-actions bot mentioned this pull request Feb 13, 2024
@DaniPopes DaniPopes deleted the dani/precompiles-bytes branch February 13, 2024 15:46
This was referenced Feb 17, 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.

perf: make precompiles accept and return Bytes to reduce allocations
2 participants