Giter Club home page Giter Club logo

wtf-gas-optimization's Introduction

WTF Gas Optimization

Solidity gas optimization techniques, using Foundry. 总结写 Solidity 智能合约更省 gas 的技巧。

Lead by @0xKaso

大纲

1. use constant and immutable

2. use calldata over memory

3. use Bitmap

4. use unchecked

5. use uint256 over uint8

6. use custom error over require/assert

7. use local variable over storage

8. use clone2 over new/create2

9. packing storage slots

10. use ++i as better increment

11. use uint in reentrancy guard

1. use constant and immutable

Code | 文章

Testing

forge test --contracts 01_Constant/Constant.t.sol --gas-report

Gas report

Function Name Gas Cost
varConstant 161 ✅
varImmutable 161 ✅
variable 2261

2. use calldata over memory

Code | 文章

Testing

forge test --contracts 02_CalldataAndMemory/CalldataAndMemory.T.sol --gas-report

Gas report

Function Name Gas Cost
writeByCalldata 67905 ✅
writeByMemory 68456

3. use Bitmap

Code | 文章

Testing

forge test --contracts 03_Bitmap/Bitmap.T.sol --gas-report

Gas report

Function Name Gas Cost
setDataWithBitmap 22366 ✅
setDataWithBoolArray 35729

4. use unchecked

Code | 文章

Testing

forge test --contracts 04_unchecked/Unchecked.T.sol --gas-report

Gas report

Function Name Gas Cost
forNormal 1910309
forUnckecked 570287 ✅

5. use uint256 over uint8

Code | 文章

Testing

forge test --contracts 05_uint/Uint.T.sol --gas-report

Gas report

Function Name Gas Cost
read Uint8 2379
read Uint128 2465
read Uint256 2317 ✅
set Uint8 5355
set Uint128 5358
set Uint256 5322 ✅

6. use custom error over require/assert

Code | 文章

Testing

forge test --contracts 06_Error/Error.T.sol --gas-report

Gas report

Error Name Gas Cost
Assert 180
Require 268
Revert 164 ✅

7. use local variable over storage

Code | 文章

Testing

forge test --contracts 07_LocalData/LocalData.T.sol --gas-report

Gas report

Data Type Gas Cost
localData 1902339 ✅
storageData 4022155

8. use clone2 over new/create2

Code | 文章

Testing

forge test --contracts 08_Clone2/Clone2.T.sol --gas-report

Gas report

Create Type Gas Cost
clone2 41493 ✅
create2 93031
new 79515

9. packing storage slots

Code | 文章

Testing

forge test --contracts 09_Packing/Packing.T.sol --gas-report

Gas report

Create Type Gas Cost
normal 133521
packing 111351 ✅

10. use ++i as better increment

Code | 文章

forge test --contracts 10_Increment/Increment.T.sol --gas-report

Gas report

Increment Gas Cost
i += 1 270
i = i +1 248
i++ 220
++i 193 ✅

11. use Uint in Reentrancy Guard

Code | 文章

Testing

forge test --contracts 11_ReentrancyGuard/ReentrancyGuard.T.sol --gas-report

Gas report

ReentrancyGuard Gas Cost tips
Bool 27757
Uint01 27604 0 to non-zero -> 20000 gas
Uint12 13908 ✅ non-zero to non-zero -> 2900 gas

WTF Gas Optimization 贡献者

贡献者是WTF学院的基石

wtf-gas-optimization's People

Contributors

0xkaso avatar 0xsimple avatar amazingang avatar big-aaron avatar confucian-e avatar merdan-9 avatar skytrc avatar zhengqian8958 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.