1

When mapping for example a large integer to some integer,

uint[] index;
index.length = 1000000;
index[600001] = 4;

is it cheaper to use

mapping(uint => uint) index;
index[600001] = 4;

? one benefit of using uint[] is that it can be deleted, happens to be useful in a dApp I work on.

1 Answer 1

0

They would be the same, with one modification.

You would go

uint[1000000] index;
// index.length = 1000000; not needed

The dynamic index you used is only slightly more expensive than the fixed-length array suggested because it needs another 32-byte word for the array length and the extra step of setting it.

Both methods use a 32-byte key to point to a 32-byte word. Both methods will avoid writing unnecessarily and values default to 0 in indexes that haven't been written to.

Neither method benefits from reducing the precision of the values pointed to (to, say, uint8) because a 32-byte word is the smallest space they can address.

Hope it helps.

2
  • will be continuously adjusting the length, so have to use dynamic arrays I think
    – et4erex
    Commented Nov 14, 2018 at 22:50
  • Thanks for accepting my answer. Be careful if your adjustments involve reorganizing the list. There are some non-obvious challenges to content with. Commented Nov 14, 2018 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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