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 store_slice function to reduce gas consumption #1300

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zqy-691001
Copy link

Certainly! Here's a detailed explanation of the optimization process for the store_slice function in the TON Blockchain's stdlib.fc file:


Overview:

The store_slice function within the stdlib.fc file of the TON Blockchain has been identified as an area for potential optimization. The current implementation leads to unnecessary gas consumption due to the order of operations in the TON Virtual Machine (TVM). This document outlines the proposed changes, the expected benefits, and the steps to contribute this optimization back to the project.

Current Issue:

The existing store_slice function is implemented as follows in C:

builder store_slice(builder b, slice s) "STSLICER";

This results in the following assembly instructions:

execute SWAP
execute STSLICER

The gas consumption for this sequence is 44 units (18 for SWAP + 26 for STSLICER).

Proposed Optimization:

By rewriting the function directly in assembly, we can achieve the desired operation order without the need for an additional SWAP operation:

builder store_slice(builder b, slice s) asm(s b) "STSLICE";

This change results in a more efficient single assembly instruction:

execute STSLICE

With this optimization, the gas consumption is reduced to just 18 units, which is a significant improvement over the original implementation.

Benefits of the Optimization:

  1. Reduced Gas Consumption: The most immediate benefit is the reduction in gas consumption from 44 units to 18 units. This can lead to cost savings for users interacting with smart contracts on the TON Blockchain, as they will require less gas to execute the store_slice operation.

  2. Improved Efficiency: The optimized function is more efficient in terms of both gas usage and execution speed, as it eliminates the unnecessary SWAP operation.

  3. Simplified Assembly: The assembly code is simplified, which can make it easier to understand and maintain.

Copy link

@hossen71 hossen71 left a comment

Choose a reason for hiding this comment

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

☺️good

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Copy link

@hossen71 hossen71 left a comment

Choose a reason for hiding this comment

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

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.

2 participants