-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update general/build/smart-contracts/gas-optimization, regarding the …
…issue#454 (#457) * update general/build/smart-contracts/gas-optimization, regarding the issue #454 * add related solidity official document link
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
docs/general/build/smart-contracts/gas-optimization/constant.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
docs/general/build/smart-contracts/gas-optimization/memoryAndCalldata.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
displayed_sidebar: generalSidebar | ||
sidebar_position: 2 | ||
--- | ||
# Memory vs Calldata | ||
|
||
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. | ||
|
||
|
||
Learn more: | ||
[Data location and assignment behavior](https://docs.soliditylang.org/en/latest/types.html#data-location) | ||
|
||
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`. | ||
|
||
|