I am writing a function that requires a large fixed uint256 array as an input. The purpose of this function is to read one element of this large array and ignore all other elements.

    uint public test;

    function readArray(uint[10][10]["x"] _array) public {
        test = _array[1][1][1];

To my (admittedly basic) knowledge, in this contract I am not doing anything explicitly gas expensive (such as altering or storing large amounts of this array). But as I increase "x" beyond around 10, everything crashes.

Is there anyway to input large amounts of data in to a contract without it crashing? (I am looking for an arbitrarily large value of x).

Thanks, Andy


Try changing your function visibility from public to external. Declaring function visibility to be external lets the EVM read data directly from the function call, rather than having to copy over data.

That being said, I don't know how large of an input you'll be able to put in using this method.

| improve this answer | |

I realize this is just a contrived example to make the question clear. It's hard to guess why you would "need" to input so much data only to ignore most of it. You don't want to do that.

In the example, the size of the interface layout increases 100x (10**2) for every 1x increase in x. That's 100 more 32-byte words to unpack. Gas cost financially constrains us from making ineffective use of the EVM. You need to aim for O(1) so whatever you're doing works at any scale.

You can accomplish that with many small operations instead of a single large operation. For example,

function setElement(uint x, uint y, uint z, bytes32 value) public {
  element[x][y][z] = value;

A client would iterate over the array and send many transactions to fill the array. If it's really true that only the element at 1,1,1 is of interest, then drop the array idea altogether.

Although the interface supports using arrays, it must be used with caution to avoid situations where it is either unaffordable or impossible to use functions.

Hope it helps.

| improve this answer | |

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.