What factors determine the memory used in lambda functions?

If you’re affected by this issue, you can send feedback to Google:

  1. Open a spreadsheet, preferably one where you bumped into the issue.
  2. Replace any sensitive information with anonymized but realistic-looking data. Remove any sensitive information that is not needed to reproduce the issue.
  3. Choose Help > Report a Problem or Help > Help Sheets Improve. If you are on a paid Google Workspace Domain, see Contact Google Workspace support.
  4. Explain why the calculation limit is an issue for you.
  5. Request:
    • Justice: Removing arbitrary limits on lambda functions
    • Equality: Avoiding discrimination against lambda functions
    • Transparency: Documenting the said discrimination in more clarity and detail
  6. Include a link to this Stack Overflow answer post.

Update Oct ’22 (Credit to MaxMarkhov)

The limit is now 10x higher at 1.9 million 1999992. This is still less than 1/5th of 10 million virtual array limit of non-lambda formulas, but much better than before. Also non-lambda formulas’s limit doesn’t reduce with number of operations. But lambda helper formulas limit still does decrease with number of operations. So, even though it’s 10x higher, that just means ~5 extra operations inside lambda(see table below).


A partial answer

We know for a fact, the following factors decide the calculation limit drum roll:

  • Number of operations
  • (Nested)LAMBDA() function calls

The base number for 1 operation seems to be 199992 1 2(=REDUCE(,SEQUENCE(199992),LAMBDA(a,c,c))). But for a zero-op or a no-op(=REDUCE(,SEQUENCE(10000000),LAMBDA(a,c,0))), the memory limit is much higher, but you’ll still run into time limit. We also know number of operations is a factor, because

  • =REDUCE(,SEQUENCE(66664/1),LAMBDA(a,c,a+c)) fails
  • =REDUCE(,SEQUENCE(66664),LAMBDA(a,c,a+c)) works.
  • =REDUCE(,SEQUENCE(66664),LAMBDA(a,c,a+c+0)) fails

Note that the size of operands doesn’t matter. If =REDUCE(,SEQUENCE(39998),LAMBDA(a,c,a+c+0)) works, =REDUCE(,SEQUENCE(39998),LAMBDA(a,c,a+c+100000)) will also work.

For each increase in number of operations inside the lambda function, the maximum allowed array size falls by 2n-1(Credit to @OlegValter for actually figuring out there’s a factor multiple here):

Maximum sequence Number of operations
(inside lambda)
Reduction
(from 199992)
Formula
199992 1 1 REDUCE(,SEQUENCE(199992),LAMBDA(a,c,c))
66664 2 1/3 REDUCE(,SEQUENCE(66664),LAMBDA(a,c,a+c))
39998 3 1/5 REDUCE(,SEQUENCE(39998),LAMBDA(a,c,a+c+10000))
28570 4 1/7 REDUCE(,SEQUENCE(28570),LAMBDA(a,c,a+c+10000+0))

Operations outside the LAMBDA functions also count. For eg, =REDUCE(,SEQUENCE(199992/1),LAMBDA(a,c,c)) will fail due to extra /1 operation, but you only need to reduce the array size linearly by 1 or 2 per operation, i.e., this =REDUCE(,SEQUENCE(199990/1),LAMBDA(a,c,c)) will work3.

In addition LAMBDA function calls itself cost more. So, refactoring your code doesn’t eliminate the memory limit, but reduces it furthermore. For eg, if your code uses LAMBDA(a,c,(a-1)+(a-1)), if you add another lambda like this: LAMBDA(a,c,LAMBDA(aminus,aminus+aminus)(a-1)), it errors out with much less array elements than before(~20% less). LAMBDA is much more expensive than repeating calls.

There are many other factors at play, especially with other LAMBDA functions. Google might change their mind about these arbitrary limits later. But this gives a start.


Possible workarounds:

  • LAMBDA itself isn’t restricted. You can nest as much as you want to. Only LAMBDA Helper Functions are restricted. (Credit to player0)

  • Named functions which don’t use LAMBDA(helper functions) themselves, aren’t subjected to the same restrictions. But they’re subject to maximum recursion restrictions.

  • Another workaround is to avoid using lambda as a arrayformula and use autofill or drag fill feature, by making the lambda function return only one value per function. Note that this might actually make your sheet slow. But apparently, Google is ok with that – multiple individual calls instead of a single array call. For example, I’ve written a permutations function here to get a list of all permutations. While it complains about “memory limit” for a array with more than 6 items, it can work easily by autofill/dragfill/copy+paste with relative ranges.

Leave a Comment