Git Product home page Git Product logo

optimizooors-lab's Introduction

🧪 Optimizooor's Lab

Welcome to the 🧪 Optimizooor's Lab!

Optimize code and run tests with our equipment for optimizooors:

  • MyLab: An environment for running controlled experiments
  • GFlask: An instrument for measuring gas savings with different optimizations

Findings

Running 1 test for MyLab.json:MyLab
[PASS] test() (gas: 510664)
Logs:
  Increment with `i += 1`
  
  Use `++i` instead
  SAVES (GAS): 3
  
  Try `i = i + 1`
  No savings.
  
  Try `i++`
  [!] More expensive (gas): -2

Research

A list of common Solidity optimization tips and myths.

Instructions

Download foundryup

curl -L https://foundry.paradigm.xyz | bash

Install Foundry

foundryup

Create a new project using our template

forge init --template ZeroEkkusu/optimizooors-lab my-lab

... or install as a dependency

forge install ZeroEkkusu/optimizooors-lab

Template usage

To use MyLab, provide code samples in Samples.sol:

contract Sample0 is SharedSetup {
    function measureGas() external {
        uint i;
        i++;
    }
}

contract Sample1 is SharedSetup {
    function measureGas() external {
        uint i;
        ++i;
    }
}

Note: For more complex setups, use SharedSetup to minimize boilerplate.

Run forge test to compare all provided samples against Sample0:

00x@ubuntu:~$ forge test
SAVES (GAS): 5

It is possible to customize the console output with Labels:

abstract contract Labels {
    string label0 = "Increment with `i++`";
    string label1 = "Use `++i` instead";
    string label2 = "";
    string label3 = "";
    string label4 = "";
    string label5 = "";
}

Let's run forge test again:

00x@ubuntu:~$ forge test
Increment `i++`

Use `++i` instead
SAVES (GAS): 5

Note: Combine with other commands at your disposal for more power. You can run forge test --gas-report to print the absolute gas costs as well.

Samples.sol files are easy to share with others!

Try copy-pasting their contents to another system and they'll work out of the box. No setup required.

GFlask usage

To use GFlask in your project, mark the function with unoptimized code as unoptimized and the ones with optimized code as optimized.

It is possible to customize the console output with labels. In the example below, we left them unlabeled ("").

Then, call all the functions inside one test function, first calling the unoptimized function:

import "optimizooors-lab/biohazard/GFlask.sol"

contract Increment is GFlask {
    function iPlusPlus() public unoptimized("") {
        uint i;
        i++;
    }

    function plusPlusI() public optimized("") {
        uint i;
        ++i;
    }

    function testIncrement() {
        iPlusPlus();
        plusPlusI();
    }
}

After running forge test, you get the following output:

00x@ubuntu:~$ forge test
SAVES (GAS): 5

Note: Combine with other commands at your disposal for more power. You can run forge test --gas-report to print the absolute gas costs as well.


Thank you for visiting the 🧪 Optimizooor's Lab!

Measuring gas savings can be tricky sometimes. It is useful to have basic knowledge of how the optimizer works, so you can isolate the code properly.

Visit ⛳ Re:Golf Course to learn more about this, as well as common optimization tips and myths.

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.