Skip to content

Commit

Permalink
update general/build/smart-contracts/gas-optimization, regarding the …
Browse files Browse the repository at this point in the history
  • Loading branch information
jackleeio committed Mar 15, 2024
1 parent a6ea8b1 commit 161d4b1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
displayed_sidebar: generalSidebar
sidebar_position: 2

---
# Calldata vs Memory

1. `memory`: Typically used for function parameters and temporary variables within functions. Stored in memory and not persistent on the blockchain.

2. `calldata`: Similar to memory, stored in memory and not persistent on the blockchain. The key difference is that calldata variables are immutable and commonly used for function parameters.

Below, we demonstrate how to write data using both `calldata` and `memory`

```solidity
contract CalldataAndMemory {
struct Confi {
uint16 age;
string name;
string wish;
}
Confi John;
Confi Jane;
function writeToJohn(Confi calldata JohnData) external {
John = JohnData;
}
function writeToJane(Confi memory JaneData) external {
Jane = JaneData;
}
}
```

Recommendations for gas optimization:

🌟 In practical situations, if it's possible to use calldata, it is recommended to use `calldata` instead of `memory`.
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
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

0 comments on commit 161d4b1

Please sign in to comment.