-
Notifications
You must be signed in to change notification settings - Fork 556
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
Conversation
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); |
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.
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.
4acdf40
to
1ac744d
Compare
@@ -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); |
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.
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())) |
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.
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.
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.
lgtm!
nit clippy |
Closes #1072.