From 2c9beed40486259e914d274e9015c15d169889cc Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 25 Oct 2023 09:45:25 +0200 Subject: [PATCH 1/2] Optimize gas for AssetId new() and default() functions --- sway-lib-std/src/contract_id.sw | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/sway-lib-std/src/contract_id.sw b/sway-lib-std/src/contract_id.sw index bdec134cbce..0ebcbb0f283 100644 --- a/sway-lib-std/src/contract_id.sw +++ b/sway-lib-std/src/contract_id.sw @@ -170,7 +170,11 @@ impl AssetId { /// } /// ``` pub fn new(contract_id: ContractId, sub_id: SubId) -> Self { - let value = sha256((contract_id, sub_id)); + let value = 0x0000000000000000000000000000000000000000000000000000000000000000; + asm(token_id: value, ptr: (contract_id, sub_id), bytes: 64) { + s256 token_id ptr bytes; + }; + Self { value } } @@ -199,7 +203,11 @@ impl AssetId { /// } /// ``` pub fn default(contract_id: ContractId) -> Self { - let value = sha256((contract_id, 0x0000000000000000000000000000000000000000000000000000000000000000)); + let value = 0x0000000000000000000000000000000000000000000000000000000000000000; + asm(token_id: value, ptr: (contract_id, 0x0000000000000000000000000000000000000000000000000000000000000000), bytes: 64) { + s256 token_id ptr bytes; + }; + Self { value } } From 94f9cbb9bed1377537c20f65d43d88104bb50ffb Mon Sep 17 00:00:00 2001 From: bitzoic Date: Wed, 25 Oct 2023 09:52:59 +0200 Subject: [PATCH 2/2] Rename variable for clarity --- sway-lib-std/src/contract_id.sw | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sway-lib-std/src/contract_id.sw b/sway-lib-std/src/contract_id.sw index 0ebcbb0f283..7b7b65d8be1 100644 --- a/sway-lib-std/src/contract_id.sw +++ b/sway-lib-std/src/contract_id.sw @@ -170,12 +170,12 @@ impl AssetId { /// } /// ``` pub fn new(contract_id: ContractId, sub_id: SubId) -> Self { - let value = 0x0000000000000000000000000000000000000000000000000000000000000000; - asm(token_id: value, ptr: (contract_id, sub_id), bytes: 64) { - s256 token_id ptr bytes; + let result_buffer = 0x0000000000000000000000000000000000000000000000000000000000000000; + asm(asset_id: result_buffer, ptr: (contract_id, sub_id), bytes: 64) { + s256 asset_id ptr bytes; }; - Self { value } + Self { value: result_buffer } } /// Creates a new AssetId from a ContractId and the zero SubId. @@ -203,12 +203,12 @@ impl AssetId { /// } /// ``` pub fn default(contract_id: ContractId) -> Self { - let value = 0x0000000000000000000000000000000000000000000000000000000000000000; - asm(token_id: value, ptr: (contract_id, 0x0000000000000000000000000000000000000000000000000000000000000000), bytes: 64) { - s256 token_id ptr bytes; + let result_buffer = 0x0000000000000000000000000000000000000000000000000000000000000000; + asm(asset_id: result_buffer, ptr: (contract_id, 0x0000000000000000000000000000000000000000000000000000000000000000), bytes: 64) { + s256 asset_id ptr bytes; }; - - Self { value } + + Self { value: result_buffer } } /// The base_asset_id represents the base asset of a chain.