For starters, return await loop should be return loop. No point in useless rewrapping.
That is correct, but my primary test has been with the plain function using then, precisely to not make mistakes like this. It makes no difference though.
the solution here has always been to unchain the tail call with setTimeout (yes, setTimeout, not setImmediate or process.nextTick)
I know what you're referring to, but your intuition here is not correct. I updated the article to use setTimeout and now it crashes even faster, managing to only go through ~ 1,000,000 iterations before it chokes.
What ends up happening is that the code is creating a chain of promises waiting on each other for completion. So it creates f1, which waits on f2, which waits on f3, creating a long chain of resolve callbacks being registered for triggering once the last promise is complete and since that never happens, the heap eventually blows up.
What I did in Funfix to fix it, is to indicate to newly created futures (in flatMap) that they need to complete the source, doing a sort of chaining in reverse. See the implementation.
And so what this does is to effectively drop intermediate future references, leaving directly accessible by the GC only the future reference you're holding on to and the active future. And so it ends up using constant memory.
Note that your loop here never ends, so you’re effectively only using Promises for side effects and therefore don’t need to chain them at all.
It's just a simple example to illustrate a point.
You can imagine a stream of tasks processed one by one, where the results are folded into a final result that doesn't have to be void.
Yes, promises get used for triggering async side effects, but many times you also want to aggregate a result as well and having your process blow up only after ~1,000,000 items or so is a big surprise, at least to an FP developer like myself, given that these are asynchronous calls that shouldn't have such problems.
Aw shucks, I thought that would fix it. For the last part, though, what I meant was (sorry, I had just woken up lol):
function loop(n) {
return signal(n).then(i => {
if (i % 1000 == 0) console.log(i)
setTimeout(loop, 0, n + 1)
})
}
Or maybe even
function loop(n) {
return signal(n).then(i => {
if (i % 1000 == 0) console.log(i)
loop(n + 1)
})
}
You can still sequence and aggregate the results without explicitly chaining the Promise object. I believe that libraries like bluebird do have this same flapMap optimization you’ve created, though (but I’ve started to prefer native for less dependencies).
I have not tried Bluebird, maybe it does it because the problem and the solution are pretty obvious, which is why I was surprised to see that the standard Promise doesn't do it.
5
u/alexelcu Oct 11 '17 edited Oct 11 '17
That is correct, but my primary test has been with the plain function using
then, precisely to not make mistakes like this. It makes no difference though.I know what you're referring to, but your intuition here is not correct. I updated the article to use
setTimeoutand now it crashes even faster, managing to only go through ~ 1,000,000 iterations before it chokes.What ends up happening is that the code is creating a chain of promises waiting on each other for completion. So it creates
f1, which waits onf2, which waits onf3, creating a long chain ofresolvecallbacks being registered for triggering once the last promise is complete and since that never happens, the heap eventually blows up.What I did in Funfix to fix it, is to indicate to newly created futures (in
flatMap) that they need to complete the source, doing a sort of chaining in reverse. See the implementation.And so what this does is to effectively drop intermediate future references, leaving directly accessible by the GC only the future reference you're holding on to and the active future. And so it ends up using constant memory.
It's just a simple example to illustrate a point.
You can imagine a stream of tasks processed one by one, where the results are folded into a final result that doesn't have to be
void.Yes, promises get used for triggering async side effects, but many times you also want to aggregate a result as well and having your process blow up only after ~1,000,000 items or so is a big surprise, at least to an FP developer like myself, given that these are asynchronous calls that shouldn't have such problems.