Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Tips and Tricks on structs initialization. #3626

Merged
merged 2 commits into from
Mar 1, 2018

Conversation

lastperson
Copy link
Contributor

It turns out that in such case:

contract Combine {
    struct Tight {
        bool a;
        bool b;
    }
    Tight t;
    function setTight() public {
        t = Tight(true, true);
    }
    function setTightOptimized() public {
        t.a = true;
        t.b = true;
    }
}

setTight() will consume ~25000 gas, while setTightOptimized() will consume ~20000 gas. It is just the way optimizer works right now.

* Initialize storage structs with a single assignment: ``x = MyStruct({a: 1, b: 2});``

.. note::
If the storage struct has tightly packed properties, initialize it with separate assignments: ``x.a = 1; x.b = 2;``. In this way it will be easier for optimizer to update storage in one go, thus making assignment cheaper.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small typo: easier for the optimizer to update

@chriseth chriseth merged commit a0d0060 into ethereum:develop Mar 1, 2018
* Initialize storage structs with a single assignment: ``x = MyStruct({a: 1, b: 2});``

.. note::
If the storage struct has tightly packed properties, initialize it with separate assignments: ``x.a = 1; x.b = 2;``. In this way it will be easier for the optimizer to update storage in one go, thus making assignment cheaper.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants