diff --git a/docs/general/build/smart-contracts/gas-optimization/constant.md b/docs/general/build/smart-contracts/gas-optimization/constant.md index 4c27947609..d294c30451 100644 --- a/docs/general/build/smart-contracts/gas-optimization/constant.md +++ b/docs/general/build/smart-contracts/gas-optimization/constant.md @@ -1,6 +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. diff --git a/docs/general/build/smart-contracts/gas-optimization/memoryAndCalldata.md b/docs/general/build/smart-contracts/gas-optimization/memoryAndCalldata.md new file mode 100644 index 0000000000..c975d5e1fb --- /dev/null +++ b/docs/general/build/smart-contracts/gas-optimization/memoryAndCalldata.md @@ -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`. + +