Git Product home page Git Product logo

wtf-gas-optimization's Introduction

WTF Gas Optimization

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

大纲

1. use constant and immutable

2. use calldata over memory

3. use Bitmap

4. use unchecked

5. use uint256 over uint8

1. use constant and immutable

代码 |文章

Testing

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

Gas report

Function Name Gas Cost
varConstant 183
varImmutable 161 ✅
variable 2305

2. use calldata over memory

代码 | 文章

Testing

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

Gas report

Function Name Gas Cost
writeByCalldata 67905 ✅
writeByMemory 68456

3. use Bitmap

代码 | 文章

Testing

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

Gas report

Function Name Gas Cost
setDataWithBitmap 22366 ✅
setDataWithBoolArray 35729

4. use unchecked

代码 | 文章

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

代码 | 文章

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 ✅

WTF Gas Optimization 贡献者

贡献者是WTF学院的基石

wtf-gas-optimization's People

Contributors

0xkaso avatar amazingang 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.