4

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.

6

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.

  • 4
    This makes sense. Theoretically, these two methods should be identical, except that the double-mapping method would require 1 extra sha3 per read or write, which is 30 gas, and the struct method would require a few extra ADDs and POPs, which would about balance it out – Tjaden Hess Dec 2 '16 at 15:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.