purescript / purescript-prelude

The PureScript Prelude
BSD 3-Clause "New" or "Revised" License
161 stars 88 forks source link

`arrayBind` causes Maximum call stack size exceeded #309

Open acple opened 7 months ago

acple commented 7 months ago

In short,

> import Data.Array
> [1] >>= \_ -> replicate 200000 0
file:///tmp/p/.psci_modules/Control.Bind/foreign.js:5
      Array.prototype.push.apply(result, f(arr[i]));
                           ^

RangeError: Maximum call stack size exceeded
    at file:///tmp/p/.psci_modules/Control.Bind/foreign.js:5:28
    at file:///tmp/p/.psci_modules/$PSCI/index.js:5:74
    at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
    at async DefaultModuleLoader.import (node:internal/modules/esm/loader:228:24)

Node.js v20.5.1

When the return value is just a huge array, RangeError is raised immediately, by passing too many arguments to Array.prototype.push function. Of course this is very rare case but I think it should not be a purescript limitation. Do someone know any solutions with minimum performance impact?