レッスン3

Gas Optimization Techniques with Remix

It's critical to practise now that you've mastered some fundamental storage optimisation approaches. Engage with the examples offered and experiment with creating your own. In the following session, we'll go deeper into optimising function visibility and reusing code with libraries. Keep in mind that optimisation is both an art and a science. Continue to experiment!

Optimizing Storage

The majority of Ethereum’s gas expenditures concentrate around storage. Each operation that changes the state (storage) of the Ethereum network is typically costly. As a result, concentrating on how data is kept and accessible can result in significant cost reductions. In this session, we’ll look at a few ways for optimising storage with Remix.

Why is Storage Expensive?

The Ethereum blockchain provides permanent storage. Everything saved on the blockchain will be available for as long as the blockchain survives, but this permanence comes at a cost. Optimizing storage not only lowers costs but also ensures that Ethereum’s global state is used efficiently.

Packing Storage Variables

Introduction

Solidity stores variables in slots. Each slot is 32 bytes. When variables can fit within a single slot, they can be read or updated with a single SLOAD or SSTORE operation, respectively.

Example

Consider two contracts:

Solidity
// Without Optimization
pragma solidity ^0.8.9;

contract UnoptimizedStorage {
    uint256 public value1;
    uint256 public value2;
}
Solidity
// With Optimization
pragma solidity ^0.8.9;

contract OptimizedStorage {
    uint128 public value1;
    uint128 public value2;
}
  1. Deploy both contracts in Remix and note the gas differences when interacting with their variables.

  2. In the optimized version, both value1 and value2 share a single 32-byte slot.

Reusing Storage Slots

Introduction

When you’re done with a storage slot, especially temporary data storage, you can delete or zero-out the slot to get a gas refund.

Example

Solidity
pragma solidity ^0.8.9;

contract RefundExample {
    uint256 public temporaryData;

    function storeTemporaryData(uint256 data) public {
        temporaryData = data;
    }

    function clearTemporaryData() public {
        delete temporaryData;
    }
}
  1. Deploy this contract in Remix.

  2. Store some temporary data and then clear it.

  3. Check the gas cost. Notice the gas refund you get for the delete operation.

Using Events Instead of Storage for Historical Data

Introduction

If data doesn’t need to be accessed on-chain, consider logging it as an event rather than storing it. Events are much cheaper than storage operations.

Example

Solidity
pragma solidity ^0.8.9;

contract EventExample {
    event DataStored(uint256 data);

    function storeData(uint256 data) public {
        emit DataStored(data);
    }
}
  1. Deploy and interact with the contract in Remix.

  2. Note the cheaper gas costs when compared to storage.

Looking Ahead

It’s critical to practise now that you’ve mastered some fundamental storage optimisation approaches. Engage with the examples offered and experiment with creating your own. In the following session, we’ll go deeper into optimising function visibility and reusing code with libraries. Keep in mind that optimisation is both an art and a science. Continue to experiment!

免責事項
* 暗号資産投資には重大なリスクが伴います。注意して進めてください。このコースは投資アドバイスを目的としたものではありません。
※ このコースはGate Learnに参加しているメンバーが作成したものです。作成者が共有した意見はGate Learnを代表するものではありません。
カタログ
レッスン3

Gas Optimization Techniques with Remix

It's critical to practise now that you've mastered some fundamental storage optimisation approaches. Engage with the examples offered and experiment with creating your own. In the following session, we'll go deeper into optimising function visibility and reusing code with libraries. Keep in mind that optimisation is both an art and a science. Continue to experiment!

Optimizing Storage

The majority of Ethereum’s gas expenditures concentrate around storage. Each operation that changes the state (storage) of the Ethereum network is typically costly. As a result, concentrating on how data is kept and accessible can result in significant cost reductions. In this session, we’ll look at a few ways for optimising storage with Remix.

Why is Storage Expensive?

The Ethereum blockchain provides permanent storage. Everything saved on the blockchain will be available for as long as the blockchain survives, but this permanence comes at a cost. Optimizing storage not only lowers costs but also ensures that Ethereum’s global state is used efficiently.

Packing Storage Variables

Introduction

Solidity stores variables in slots. Each slot is 32 bytes. When variables can fit within a single slot, they can be read or updated with a single SLOAD or SSTORE operation, respectively.

Example

Consider two contracts:

Solidity
// Without Optimization
pragma solidity ^0.8.9;

contract UnoptimizedStorage {
    uint256 public value1;
    uint256 public value2;
}
Solidity
// With Optimization
pragma solidity ^0.8.9;

contract OptimizedStorage {
    uint128 public value1;
    uint128 public value2;
}
  1. Deploy both contracts in Remix and note the gas differences when interacting with their variables.

  2. In the optimized version, both value1 and value2 share a single 32-byte slot.

Reusing Storage Slots

Introduction

When you’re done with a storage slot, especially temporary data storage, you can delete or zero-out the slot to get a gas refund.

Example

Solidity
pragma solidity ^0.8.9;

contract RefundExample {
    uint256 public temporaryData;

    function storeTemporaryData(uint256 data) public {
        temporaryData = data;
    }

    function clearTemporaryData() public {
        delete temporaryData;
    }
}
  1. Deploy this contract in Remix.

  2. Store some temporary data and then clear it.

  3. Check the gas cost. Notice the gas refund you get for the delete operation.

Using Events Instead of Storage for Historical Data

Introduction

If data doesn’t need to be accessed on-chain, consider logging it as an event rather than storing it. Events are much cheaper than storage operations.

Example

Solidity
pragma solidity ^0.8.9;

contract EventExample {
    event DataStored(uint256 data);

    function storeData(uint256 data) public {
        emit DataStored(data);
    }
}
  1. Deploy and interact with the contract in Remix.

  2. Note the cheaper gas costs when compared to storage.

Looking Ahead

It’s critical to practise now that you’ve mastered some fundamental storage optimisation approaches. Engage with the examples offered and experiment with creating your own. In the following session, we’ll go deeper into optimising function visibility and reusing code with libraries. Keep in mind that optimisation is both an art and a science. Continue to experiment!

免責事項
* 暗号資産投資には重大なリスクが伴います。注意して進めてください。このコースは投資アドバイスを目的としたものではありません。
※ このコースはGate Learnに参加しているメンバーが作成したものです。作成者が共有した意見はGate Learnを代表するものではありません。