Giter Site home page Giter Site logo

Comments (9)

PaulRBerg avatar PaulRBerg commented on August 10, 2024

Update: this conversation on Twitter is related to this issue.

As per transmission11's reply there, it does look like in an older version of Solidity, x != 0 used to be more gas efficient than x > 0.

Tagging @hrkrshnn in case he can illuminate us here?

from gas-optimizations.

hrkrshnn avatar hrkrshnn commented on August 10, 2024

I will follow up tomorrow.

from gas-optimizations.

PaulRBerg avatar PaulRBerg commented on August 10, 2024

Thanks @hrkrshnn, though no rush from my end.

from gas-optimizations.

hrkrshnn avatar hrkrshnn commented on August 10, 2024

@PaulRBerg The main difference is the use of via-ir. There are rules for transforming these expressions into an equivalent cheaper expression. But these rules are more effective in the new compilation pipeline. The legacy codegen and its optimizer works across basic blocks of assembly. And sometimes these expressions get split across basic blocks. See: https://hrkrshnn.com/t/devconnect.pdf#page=4 for another example.

I think these expressions should have the same cost if viaIR=true and the optimizer is enabled. Please don't hesitate to ping me again / open an issue in the original repo if that's not the case.

from gas-optimizations.

PaulRBerg avatar PaulRBerg commented on August 10, 2024

Thanks @hrkrshnn, makes sense.

What I don't understand though is that when I compiled the the code snippets above in Remix, I did not enable via-ir.

Is it that in the latest versions of Solidity, the compiler automatically applies via-ir to some operations, like != and >?

from gas-optimizations.

hrkrshnn avatar hrkrshnn commented on August 10, 2024

@PaulRBerg via-ir is not enabled by default yet. You can expect it to be default in an upcoming breaking release. The easiest way to test would be in Foundry, by adding viaIR = true in the toml config.

from gas-optimizations.

PaulRBerg avatar PaulRBerg commented on August 10, 2024

You can expect it to be default in an upcoming breaking release

Good to know!

via-ir is not enabled by default yet

This doesn't clear up the mystery yet of why the code snippets all yield the same gas cost even if --via-ir is not enabled.

from gas-optimizations.

hrkrshnn avatar hrkrshnn commented on August 10, 2024

This doesn't clear up the mystery yet of why the code snippets all yield the same gas cost even if --via-ir is not enabled.

It's likely because it uses different opcodes. See https://godbolt.org/z/q8K4WGqzr

from gas-optimizations.

PaulRBerg avatar PaulRBerg commented on August 10, 2024

Thank you ser, will take a look.

from gas-optimizations.

Related Issues (17)

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.