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

Optimize gas for AssetId::new() and AssetId::default() functions #5226

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions sway-lib-std/src/contract_id.sw
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,12 @@ impl AssetId {
/// }
/// ```
pub fn new(contract_id: ContractId, sub_id: SubId) -> Self {
let value = sha256((contract_id, sub_id));
Self { value }
let result_buffer = 0x0000000000000000000000000000000000000000000000000000000000000000;
asm(asset_id: result_buffer, ptr: (contract_id, sub_id), bytes: 64) {
s256 asset_id ptr bytes;
};

Self { value: result_buffer }
}

/// Creates a new AssetId from a ContractId and the zero SubId.
Expand Down Expand Up @@ -199,8 +203,12 @@ impl AssetId {
/// }
/// ```
pub fn default(contract_id: ContractId) -> Self {
let value = sha256((contract_id, 0x0000000000000000000000000000000000000000000000000000000000000000));
Self { value }
let result_buffer = 0x0000000000000000000000000000000000000000000000000000000000000000;
asm(asset_id: result_buffer, ptr: (contract_id, 0x0000000000000000000000000000000000000000000000000000000000000000), bytes: 64) {
s256 asset_id ptr bytes;
};

Self { value: result_buffer }
}

/// The base_asset_id represents the base asset of a chain.
Expand Down
Loading