Stupid linearizer (to optimize for gas)?

I’ve been told that writing contracts in a smart way is actually bad for gas performance - for instance composing different functions

it’s better that you write contracts with a lot of redundancy

how true is that?

in the case that it is true, are there any tools out there that take a well written contract and stupefy it?

it seems like that would be a trivial thing to build, especially in javascript

a stupid linearizer, so you can write smart smart contracts but get the competitive advantage of stupidity

1 Like

Really, where did you learn this tip?

1 Like

Hi @realisation,

I don’t have much experience with gas optimization.

I assume the best way is to create some test contracts and try out the theory.
Mr Black sent me the following contract that you can test on Remix:

pragma solidity ^0.5.11;

contract Test {
    constructor()public{
        test[0] = 5;
        test[1] = 2;
        test[3] = 23;
    }
    
    mapping(uint => uint) test;
    function get_test(uint i) public view returns(uint){
        return test[i];
    }
    function get_get_test(uint i) public view returns(uint){
        return get_test_internal(i);
    }
    function get_test_internal(uint i) internal view returns(uint){
        return test[i];
    }
}

I was told this at DappCon! A 10% increase. Though from how many small optimizations was never unveiled since we moved on to other topics. But I thought I would bring it up because the guy was no slouch.

1 Like

Hi @realisation,

It might be worth reaching out to the person you spoke to at DappCon. It would be great if you could then share with the community here.

It didn’t occur to me at the time but this is exactly what exists in the form of the optimizer. I just forgot about the optimizer since those I’ve spoken to about it have expressed reservations about the security of its bytecode output. But simply replacing blocks seemed fairly innocuous, so these two sections of my brain didn’t overlap…

But yes, this is basically what the optimizer does.

1 Like

@realisation
2 points come to my mind:

  1. readability is very important in smart contracts. I would say since smart contracts usually handle funds. its at least as important as gas savings. So this is one reason to keep the code nice and readable. For me shorter code is more readable.
  2. Functions should not have so much overhead. depends how you pass the arguments. a function means an additional jump and some stack operations, but if arguments are passed using a struct its passed byRef so a functions overhead shouldn’t be that big.
1 Like