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

Refs #454 -- Added Uint to general/build/smart-contracts/gas-optimization #483

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
displayed_sidebar: generalSidebar
sidebar_position: 1

---


# Constant vs Immutable

1. `constant`: Declares a constant that must be initialized at the time of declaration and cannot be altered thereafter.
Expand Down
81 changes: 81 additions & 0 deletions docs/general/build/smart-contracts/gas-optimization/uint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
displayed_sidebar: generalSidebar
---

# Uint Types Gas Comparison

It's a common belief that in Solidity, smaller integer types like `uint8`, `uint16`, `uint32`, `uint64`, `uint128`, and `uint256` might save gas due to their smaller size. However, this isn't always the case.

The Ethereum Virtual Machine (EVM) allocates a 256-bit slot for each stored variable. For instance, if we declare a variable of type `uint8`, the EVM fills the missing bits with zeros to fit it into a single slot. Additionally, during execution, the EVM converts `uintN` types to `uint256` for computations.

**DemoCode**

Let's test `uint8`, `uint32`, and `uint256` to observe their behavior in terms of writing data.

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract Uint8Example {
uint8 public uint8val;

// gas 22234
function set() external {
uint8val = 1;
}

// gas 53427
function compute() external returns (uint8) {
uint8 target = 50;
for (uint8 i = 0; i < target; i++) {
uint8val += 1;
}
return uint8val;
}
}

contract Uint32Example {

uint32 public uint32val;

// gas 22234
function set() external {
uint32val = 1;
}

// gas 53895
function compute() external returns (uint32) {
uint32 target = 50;
for (uint32 i = 0; i < target; i++) {
uint32val += 1;
}
return uint32val;
}
}

contract Uint256Example {
uint256 public uint256val;

// gas 22238
function set() external {
uint256val = 1;
}

// gas 42950
function compute() external returns (uint256) {
uint256 target = 50;
for (uint256 i = 0; i < target; i++) {
uint256val += 1;
}
return uint256val;
}
}


```

As observed, in loop computations, `uint256` saves over 10,000 gas. Therefore, smaller variables don't necessarily equate to gas savings.

Recommendations for gas optimization:

🌟If variables cannot be packed together, using `uint` or `uint256` is the optimal choice.