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

Conversation

bitzoic
Copy link
Member

@bitzoic bitzoic commented Oct 25, 2023

Description

The following changes remove the use of the std::hash::sha256 function in place of assembly to hash the ContractId and SubId of an asset. As the values hashed are of static length, we can perform the hashing in assembly avoiding the conversion to Bytes and concatenation of the two values done in sha256()

These changes result in a 93% reduction in gas usage.

Checklist

  • I have linked to any relevant issues.
  • 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).
  • 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.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@bitzoic bitzoic added enhancement New feature or request lib: std Standard library labels Oct 25, 2023
@bitzoic bitzoic requested a review from a team October 25, 2023 07:50
@bitzoic bitzoic self-assigned this Oct 25, 2023
@K1-R1 K1-R1 requested a review from a team October 25, 2023 17:07
Copy link
Contributor

@Braqzen Braqzen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I question the addition of asm back into the std. We want it to be readable but at the same time we want it to be cheap.
Consolidation via a single function wrapper is also better for future changes as we only need to change it in 1 place.

@bitzoic bitzoic enabled auto-merge (squash) October 26, 2023 08:17
@bitzoic bitzoic merged commit 7104b29 into master Oct 26, 2023
31 checks passed
@bitzoic bitzoic deleted the bitzoic-asset-id-optimization branch October 26, 2023 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lib: std Standard library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants