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.

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.

  • will be continuously adjusting the length, so have to use dynamic arrays I think – et4erex Nov 14 '18 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. – Rob Hitchens - B9lab Nov 14 '18 at 22:54

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.