Code Monkey home page Code Monkey logo

solidityoptimizations's Introduction

Gas Optimization Examples

This repository is my collection of tests to explore different Solidity/EVM gas optimizations. I have put each optimization approach into tiers based on how complex/dangerous/obscure the approach is. Not all of these are actually optimizations, some "optimizations" cost the same or more, we're just seeing what works or not.

There is one Solidity file per optimization, each containing an unoptimized and optimized contract. You can observe the savings yourself by running forge test --gas-report.

The following is a list of what optimizations have been tested so far:

Tier1

Optimization Bad Good Improvement
Storage packing 66538 22290 Yes
Constants 247 147 Yes
Function visibility 478 478 No
Calldata vs memory 23040 22885 Yes
Caching 3312 2323 Yes

Tier2

Optimization Bad Good Improvement
i++ vs ++i 2202 2152 Yes
Unchecked 2202 1612 Yes
Batch Only EOA
Comparisons 2584 2545 Yes
Structs vs arrays 136765 135974 Yes
Unchecked 2202 1612 Yes

Tier3

Optimization Bad Good Improvement
Offchain calculations 28290 5412 Yes
Many vs single require 295 270 Yes
Short circuits 419 409 Yes
Smaller datatypes += 279 258 Yes
Smaller datatypes ++ 272 252 Yes

Tier4

Optimization Bad Good Improvement
Constructor 42381 42357 Yes*
Not caching 1929 1918 Yes
Emit string event 1350 1258 Yes
Emit word event 2353 1930 Yes
Inline swap vs tempvar 170 170 No

*Deployment cost

Tier5

Optimization Bad Good Improvement
Bitwise recycle stack 260 259 Yes
Bitwise use once 243 241 Yes
Function sig mining Only EOA
Create2 addr mining Only EOA
Gasleft branching 191321 116493 Yes

Tier6

  • Just use Huff lmao

The following resources were used to help collect these optimizations:

solidityoptimizations's People

Contributors

kalzak avatar

Stargazers

solipidy avatar

Watchers

 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.