0

I have the code:

struct A {
        mapping(uint256 => uint256) b;
}
mapping(uint256 => A) public a;

and I want to have the following code as gas efficient as possible:

a[key_a].b[key_b] = a[key_a].b[key_b] | mask;

is it the most optimize way or there are might be some gas optimizations?

as I understood, solidity would take an address of the map a, calc the address of mapping by index key_a, take field b, calc the address of mapping item by key b - which costs more than just simple access the uint256 variable storage, am I right? Does it do twice (before and after =) or cache it somehow for optimization? Maybe something like that:

A storage a_key_a = a[key_a];
a_key_a.b[key_b] = a_key_a.b[key_b] | mask

or any approaches would make it cheaper?

1
  • 2
    I'd suggest to write a small test contract and measure the gas consumption. The compiler is simple and the bytecode generated should be similar if it were a bigger contract. – Ismael Feb 28 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.