Jan 10, 2023

Gas Optimization

1. Always try to minimize gas usage!


1.2) Smart Contract Interact
- Sending transactions
- Calling functions
- Transferring value between contracts

1.1) Block Limit limits interaction
- The block limit in Ethereum refers to the maximum amount of gas that can be consumed in a single block.
- This gas limit is set to ensure the network's stability and prevent abuse by excessively complex or resource-consuming transactions.

1.3) Expensive OpCodes
- Expensive opcodes in Ethereum refer to instructions that consume a significant amount of gas when executed.
- These operations often involve complex computations, large storage access, or cryptographic operations, resulting in higher gas costs.

1.3.1) SHA-256 Hashing:
- The SHA-256 opcode is computationally intensive and requires significant gas due to its cryptographic nature.

1.3.2) ECDSA Signature Verification:
- Verifying digital signatures using the ECDSA opcode involves complex elliptic curve cryptography, making it gas-intensive.

1.4) Cheap OpCodes
- Cheap opcodes, on the other hand, are instructions that consume relatively less gas when executed.
- These operations are less resource-intensive and include simple arithmetic operations, basic storage access, or logical operations, resulting in lower gas costs.

1.4.1) Addition (ADD):
- The ADD opcode performs simple addition of two numbers and consumes a moderate amount of gas.

1.4.2) Storage Access (SLOAD):
- Reading a value from storage using the SLOAD opcode is comparatively inexpensive than complex operations.

1.5) Free OpCodes
- Free opcodes are specific instructions that do not consume any gas when executed.
- These operations typically include certain no-op (no-operation) instructions or functionalities that are subsidized by the protocol, allowing them to execute without gas costs.

1.5.1) No-Operation (NOP):
- NOP opcode performs no operation and doesn't consume gas. It's used for various purposes like padding or placeholders.

1.5.2) Self-Destruct (SELFDESTRUCT):
- The SELFDESTRUCT opcode allows a contract to destroy itself and send remaining Ether to a specified address, with no gas cost for the destruction.

2. Have Nonpayable functions

Benefits:

Ether Processing Overhead Elimination: Non-payable functions in Ethereum smart contracts bypass Ether processing, reducing gas costs associated with Ether transfers or value manipulations during function calls.

Gas Savings for Etherless Operations: Transactions calling non-payable functions have lower gas costs since no Ether is sent or processed. This optimizes gas usage for operations not involving Ether transactions.

3. EIP 1559

Benefits:

Fee Market Change:
- EIP 1559 alters how transaction fees are calculated and managed within Ethereum, introducing a new transaction pricing mechanism.

Base Fee & Inclusion Priority:
- The proposal introduces a base fee, which dynamically adjusts based on network demand. Users specify an optional inclusion priority (tip) for faster transaction processing.

Predictable Fees:
- Users have more predictability regarding transaction fees due to the base fee's predictable adjustment based on network congestion.

3. Storage slots



How To Optimize gas usage?:
a) Minimizing Storage Use:
- Reducing the number of storage slots utilized by a contract can lower gas costs.
- Using fewer storage slots means fewer write operations, leading to reduced gas consumption.

b) Compact Data Structures:
- Optimizing data organization within storage slots can save gas.
- Storing related data together or using packed data structures reduces the number of slots needed and, consequently, the gas required for storage operations.

c) Layout Efficiency:
- Careful planning of data layout within storage slots can optimize gas usage.
- Grouping related data together and avoiding fragmentation can minimize gas costs for read and write operations.

d) Reuse and Cleaning:
- Reusing storage slots by updating existing data instead of creating new slots whenever possible can optimize gas usage.
- Cleaning or reusing slots instead of leaving unused slots can prevent unnecessary gas consumption.
aa) Setting to Zero:
- Clearing Storage: Setting variables or storage slots to zero is a technique used to clear or reset data stored in a contract's state variables or storage slots.
bb) Refunds
bb1) Self-Destruction Mechanism:
- When a contract self-destructs (also known as self-destructing or "suicide"), Ethereum refunds excess gas to the sender.

bb2) Refundable Gas:
- Refunds occur for gas that remains unused after contract execution or self-destruction.
- The refunded gas is a portion of the gas paid upfront for the transaction.

Considerations for Optimization:
e) Storage Cost Calculation:
- The gas cost for storage operations is directly related to the number of slots used and the amount of data stored.
- Understanding the gas cost calculations is essential for efficient slot utilization.

f) Contract Architecture:
- Designing contracts with efficient data structures and managing state variables effectively can impact gas efficiency.

g) Dynamic versus Static Data:
- Dynamic data structures, such as arrays and mappings, often require more gas due to potential storage expansion.
- Employing fixed-size or more predictable data structures can optimize gas usage.

4. Memory

How To Optimize gas usage?:

a) Memory Expansion:
- Gas costs increase when memory expands dynamically, such as when using arrays or dynamic data structures.
- Reducing unnecessary memory expansion through optimized data structures and fixed-size arrays can lower gas consumption.

b) Memory Cleaning:
- Cleaning or resetting memory locations after use prevents leftover data, optimizing gas usage.
- Efficient memory management involves deallocating or clearing memory slots to reduce subsequent gas costs.

Considerations for Gas Optimization:
c) Data Packed Structures:
- Organizing data in a compact manner within memory slots minimizes memory usage and reduces gas costs associated with memory operations.

d) Data Access Patterns:
- Optimizing how data is accessed in memory—by minimizing reads or writes—can improve gas efficiency during contract execution.

Thanks for reading! If you want to see future content, you can follow me on Twitter or get connected over at LinkedIn.


Support My Content

If you find my content helpful, consider supporting a humanitarian cause (building homes for elderly people in rural Terai region of Nepal) that I am planning with your donation:

Ethereum (ETH)

0xb0b1B20062DA9Dd7BaA4D5b088DF49dbe4b46fF2

Thank you for your support!