Solidity Contract Design – Mapping a Struct vs Having Two Mappings

contract-designcontract-developmentsolidity

I have a mapping linking a key and a byte32, and I would like to extend it to store something as long as a byte64.

I chose to cut it in half, but what is the best (and lightest) way to store the two parts ? Should I map to a struct like this:

struct b64 {
    bytes32 part1;
    bytes32 part2;
}

mapping (uint256 => b64) public b64s;

Or use two mappings, like this:

mapping (uint256 => bytes32) public b64s_1;
mapping (uint256 => bytes32) public b64s_2;

I'm looking for the lightest solution. The only function making the mapping always writes both parts at the same time so I don't think there will be consistency issues with two mappings.

The first solution is cleaner but if I can save precious gas at every writing with the second one I'll take it.

Best Answer

I tried both, and measured gas for both transactions. I got 107864 with a struct, and 107856 with two mappings.... I'm pretty sure that the two bytes32 take all the gas, and the data structure are negligible in comparison.

So I'm using the cleaner one in my opinion, the struct.

Related Topic