-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Optimize tx
module for bytecode size and gas usage
#7087
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
Merged
Merged
+55
−102
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xunilrj
reviewed
Apr 14, 2025
xunilrj
reviewed
Apr 14, 2025
xunilrj
approved these changes
Apr 14, 2025
IGI-111
approved these changes
Apr 14, 2025
8 tasks
ironcev
added a commit
that referenced
this pull request
Apr 15, 2025
…ge (#7092) ## Description This PR optimizes the `std::inputs` and `std::outputs` modules for bytcode size and gas cost using the same optimization techinques like in #7087. The optimized public functions are listed in the below code snippet. **The bytcode size of that code got reduced from 5688 bytes to 2544 bytes.** ```sway fn main() { let _ = std::tx::tx_witnesses_count(); let _ = std::inputs::input_count(); let _ = std::inputs::input_amount(0); let _ = std::inputs::input_coin_owner(0); let _ = std::inputs::input_predicate_data::<u64>(0); let _ = std::inputs::input_asset_id(0); let _ = std::inputs::input_witness_index(0); let _ = std::inputs::input_predicate_length(0); let _ = std::inputs::input_predicate(0); let _ = std::inputs::input_predicate_data_length(0); let _ = std::inputs::input_message_sender(0); let _ = std::inputs::input_message_recipient(0); let _ = std::inputs::input_message_nonce(0); let _ = std::inputs::input_message_data_length(0); let _ = std::inputs::input_message_data(0, 0); let _ = std::outputs::output_count(); let _ = std::outputs::output_amount(0); let _ = std::outputs::output_asset_id(0); let _ = std::outputs::output_asset_to(0); } ``` Additionally, the PR adds a new function `std::outputs::output_asset_id_and_to` to efficiently retrieve the id and the receiver of the output asset in a single call. Calling this function is more performant that calling `output_asset_id` and `output_asset_to` separately. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
8 tasks
ironcev
added a commit
that referenced
this pull request
Apr 19, 2025
## Description This PR optimizes the `std::auth` module for bytecode size and gas cost by using optimization techniques similar to those used in #7087 and #7092. The optimized public functions are listed in the below code snippet. **The bytecode size of that code got reduced from 2480 bytes to 1200 bytes.** ```sway fn main() { let _ = std::auth::predicate_address(); let _ = std::auth::caller_addresses(); let _ = std::auth::caller_address(); } ``` ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
8 tasks
ironcev
added a commit
that referenced
this pull request
May 7, 2025
## Description This PR optimizes the `std::crypto` module for bytecode size and gas cost by using optimization techniques similar to those used in #7087, #7092, and #7096. The optimized public functions are listed in the below code snippet. **The bytecode size of that code got reduced from 46104 bytes to 44288 bytes.** We actually expect a bigger gain here. At one point in the optimization, the bytecode size went down to 33896 bytes. Bouncing back to ~44600 is related to the issue described in #7150. Once that issue is solved, we expect even smaller bytcode sizes. ```sway script; use std::crypto::ed25519::*; use std::crypto::point2d::*; use std::crypto::public_key::*; use std::crypto::scalar::*; use std::crypto::secp256k1::*; use std::crypto::secp256r1::*; use std::crypto::signature::*; use std::vm::evm::evm_address::EvmAddress; fn main() { let ed25519 = std::crypto::ed25519::Ed25519::new(); let _ = ed25519 == ed25519; let point2d = std::crypto::point2d::Point2D::new(); let _ = point2d == point2d; let public_key = std::crypto::public_key::PublicKey::new(); let _ = public_key == public_key; let _ = public_key.is_zero(); let scalar = std::crypto::scalar::Scalar::new(); let _ = scalar == scalar; let message = std::crypto::message::Message::new(); let secp256k1 = std::crypto::secp256k1::Secp256k1::new(); let _ = secp256k1 == secp256k1; let _ = secp256k1.address(message); let _ = secp256k1.evm_address(message); let _ = secp256k1.verify(public_key, message); let _ = secp256k1.verify_address(Address::zero(), message); let _ = secp256k1.verify_evm_address(EvmAddress::zero(), message); let secp256r1 = std::crypto::secp256r1::Secp256r1::new(); let _ = secp256r1 == secp256r1; let _ = secp256r1.address(message); let _ = secp256r1.evm_address(message); let _ = secp256r1.verify(public_key, message); let _ = secp256r1.verify_address(Address::zero(), message); let _ = secp256r1.verify_evm_address(EvmAddress::zero(), message); } ``` ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR optimizes functions in the
tx
module for bytecode size and gas usage. E.g., the bytecode size of the following script that calls all of the optimized functions got reduced from 1792 bytes to 648 bytes:The optimization:
tx_type()
and where necessary, replaces calls totx_type()
to a singleif
.tx_script_start_pointer
. The function was re-checking thetx_type()
to be a script, although always being called only if thetx_type
was already checked for being a script. Additionally, it was wrapping the result inOption
requiring an additional unnecessary indirection.tx_script_data_start_pointer
for similar reasons.tx_witness_data
by removing unnecessary length calculation and duplicated code.match
expressions by replacing duplicated match arms with a single arm.The PR introduces a slight change in the semantics of functions like, e.g.,
tx_script_length
that returnOption
. Before, if thetx_type
wasMint
they would panic instead of returningNone
. Now they returnNone
. The new behavior actually looks like the proper one to me. A function returningOption
shouldn't panic.Checklist
Breaking*
orNew Feature
labels where relevant.