diff --git a/docs/general/build/smart-contracts/gas-optimization/packing.md b/docs/general/build/smart-contracts/gas-optimization/packing.md new file mode 100644 index 0000000000..0ae2e8b28d --- /dev/null +++ b/docs/general/build/smart-contracts/gas-optimization/packing.md @@ -0,0 +1,38 @@ +--- +displayed_sidebar: generalSidebar +--- + +# Variable Packing + +The Ethereum Virtual Machine (EVM) stores variables in consecutive 32-bytes slots. When we place multiple variables within a single slot, this is referred to as variable packing. + +If the variables we try to pack exceed the 32-bytes limit of the current slot, they will be stored in a new slot. It's crucial to determine which variables are best grouped together to minimize wasted space. + +Although Solidity automatically tries to pack smaller basic types into the same slot, poor struct member ordering can prevent the compiler from doing so. + +Learn More: [Layout of State Variables in Storage](https://docs.soliditylang.org/en/v0.8.25/internals/layout_in_storage.html) + +**DemoCode** + +Below, we demonstrate how to use packing in contracts to compare gas usage. + +```solidity + +// gas: 188616 +contract Standard { + uint64 a = 5; + uint256 b = 5; + uint64 c = 5; +} + +// gas: 166178 +contract OptimizedPacking { + uint256 b = 5; + uint64 a = 5; + uint64 c = 5; +} +``` + +Recommendations for gas optimization: + +🌟Pay attention to variable packing when choosing data types. If it's possible to pack a variable with others into a single storage slot, opting for a smaller data type can be beneficial. diff --git a/docs/general/conflux-basics/glossary.md b/docs/general/conflux-basics/glossary.md index 7a83c5fa18..dc5271f1d4 100644 --- a/docs/general/conflux-basics/glossary.md +++ b/docs/general/conflux-basics/glossary.md @@ -98,6 +98,9 @@ ERC20 is a standard for tokens on the Ethereum blockchain. It specifies a set of ### **ERC721** ERC721 is a standard for non-fungible tokens (NFTs) on the Ethereum blockchain. Unlike ERC20 tokens, which are identical to each other, each ERC721 token is unique. This makes them suitable for representing ownership of unique items or assets. Like ERC20, ERC721 tokens can also exist on the Conflux network, especially if they are transferred from the Ethereum network. +### **ERC1155** +ERC1155 is an Ethereum token standard that supports multiple token types, including fungible and non-fungible tokens, within a single contract. By enabling batch transfers, it efficiently reduces transaction costs. ERC1155 tokens can also exist on the Conflux network, enhancing flexibility and interoperability for digital assets in gaming, art, and beyond. + ### **EVM (Ethereum Virtual Machine)** The Ethereum Virtual Machine (EVM) is a powerful, sandboxed virtual stack embedded within each full Ethereum node, responsible for executing contract bytecode. Contracts are written in high-level languages, like Solidity, then compiled into bytecode, which the EVM can read and execute. The EVM ensures that programs do not have access to each other's state, thus allowing for the safe execution of code without risking the network's security. It is pivotal for enabling the programmability and flexibility that smart contracts offer in the Ethereum ecosystem. In the context of Conflux, EVM compatibility allows developers to deploy Ethereum contracts on the Conflux network, benefiting from Conflux's scalability and efficiency while leveraging Ethereum's robust developer tooling and ecosystem.