Skip to content

Comments

perf(precompile): use Bytes in precompile functions#1085

Merged
rakita merged 3 commits intobluealloy:mainfrom
DaniPopes:dani/precompiles-bytes
Feb 13, 2024
Merged

perf(precompile): use Bytes in precompile functions#1085
rakita merged 3 commits intobluealloy:mainfrom
DaniPopes:dani/precompiles-bytes

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.

@DaniPopes DaniPopes force-pushed the dani/precompiles-bytes branch from 4acdf40 to 1ac744d Compare February 13, 2024 03:18
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.

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
@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
This was referenced Mar 10, 2025
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