First of all, I just wanted to say I'm a bit embarrassed to see people getting rude over what is clearly a legitimate, well-isolated, reproducible, and well explained issue. Shitting on other people's work like that is just plain unprofessional. I think whether funfix or bluebird were used as a POC solution should have been irrelevant, and that ideally people should have been eager (or at least open) to learn about the existence of a potential freaking crash and mitigation techniques rather than bickering over the definition of a memory leak.
So I guess the important question is: can the promise spec be fixed?
To be fair, there's nothing to fix. Yes you can optimize it, but then there has to be made a good argument as to why this is needed.
As it stands the issue sounds a lot like "I filled an array with the number 0 until JavaScript crashed (out of memory). This is bad, and if only the array was run-length encoded (compressed) to support an arbitrary number of 0's, there would not be a crash.
For both the original issue and my toy array example, yes you can optimize them, but the question becomes: Are there enough legitimate cases of this in the wild to warrant making the standard more complex? I think the answer is no, and there's plenty of room to create custom implementations outside of the spec to work around this issue (like the OP did).
the question becomes: Are there enough legitimate cases of this in the wild to warrant making the standard more complex
Yeah, that's a great line of reasoning. Personally I would prefer that the solution be a native one, and I feel that if bluebird is able to get away with its optimization, it should theoretically be possible to amend the spec, or at least get vendors to agree to make the optimized implementation the de-facto standard. Admittedly, I haven't dug too deep into this yet.
From going down this rabbit hole before, I think the main issues are:
Assimilating an old Promise (or Thenable) into a new Promise without maintaining a reference to the old Promise is easy IF both Promises came from the same implementation. This is because the Promises need to be able to look into each other’s internal states in order to assimilate. Since one point of Promise/A+ was interop, it’s potentially spec-breaking to force an implementation detail like this.
There are potential GC issues when different segments of a recursive Promise chain are held by different variables.
The Promise/A+ folks have certain tests to guarantee resolution order in all kinds of edge cases, and implementing TCO seems to break that.
The ECMAScript use of the Promise/A+ spec complicates things further since now there’s another layer of approvals etc. that needs to happen for what, effectively, is an edge case with documented workarounds.
And my own spin on it:
There seems to be a major force in the JS community that wants to maintain Promises as a 1-for-1 asynchronous analog of sync code. This makes sense from the perspective of the async/await API. Just as infinite recursion will exhaust the stack, infinite Promise recursion will exhaust the heap. Perhaps, if TCO is ever shipped for sync code, it will also be shipped for Promises.
Yes, I think there are enough legitimate use cases in the wild.
you cannot describe any functional programming algorithm involving loops without tail recursion — if folds are used, then folds are described with tail recursion as well
the issue is surprising — you, as a JS developer using JS's Promise may be familiar with it, but myself, being primarily a Scala / Haskell developer, I find this behavior really, really surprising, especially since it can be fixed
It's hard btw to explain use cases, as it would go over the scope of a single blog article and should be the subject of an entire book.
But I gave this example elsewhere ... processing really long / infinite streams of events, for which it's really natural to describe algorithms using tail recursions and for which you can't really work with imperative, mutation-based loops.
I gave this sample in another comment:
type Task<A> = () => Promise<A>
interface List<A>
class Cons<A> implements List<A> {
constructor(readonly head: A, readonly rest: Task<List<A>>) {}
}
class Nil implements List<A> {
constructor(readonly error?: any) {}
}
This data structure is really cheap and effective - being a lazy, asynchronous, referentially transparent stream. But as an exercise to the reader, try describing this function:
function map<A, B>(list: List<A>, f: (a: A) => B): List<B>
HINT: if that Promise implementation wouldn't leak, then it would be really easy to do.
I hear you, it definitely would be a nice perk, but you really can't be this focused on such a specific use case. To be able to change a specification safely, you need to consider all general cases.
Promise in JavaScript came from a need to be able to chain asynchronous operations, not to chain asynchronous streams of data into a program like you would in a fully functional language†. For example, look at the implementation of fetch which is a great example of what the ES standard Promise enables. Together with async/await you get to write synchronous-looking JavaScript while making things work in a single-threaded environment.
If you truly want this optimization to make it in, then a more even-sided argument should be made, not just "I can write code that works, so why doesn't the spec also work". For example, you reference the GitHub issue on the spec in your article, which shows just how much has to be considered in these cases. And it seems to me that only considers the case where a chain of promises completely wrap another promise (so you can eliminate the intermediate promises) whereas your example seems to be about a more complex relationship.
Honestly, I'm not sure the standard Promise can ever completely support what you're asking since there are interoperability concerns to consider (compatibility between different implementations of Promise, which the spec supports).
So to repeat, I think it's great you implemented a solution to your problem as someone who comes from a functional language. This will be helpful to others in the same situation, and you can also write an article about how you can write applications that operate on asynchronous streams with this library. But an article about changing the spec would require a much more general argument.
† Sidenote: I don't think the "surprising to Haskell users" argument is very relevant since the vast majority of JavaScript developers come from different backgrounds than functional programming. You will often find that adopting a new language requires you to think differently from whatever language you were using before.
I disagree about the need for this, because one use-case of going async is to escape the limitations of the call stack, but I understand from where the philosophy comes from. And I can't be really wrong, given that actual TCO is coming to JavaScript, being part of the ECMAScript 6 spec.
I do believe that FP is even more relevant in the context of JavaScript due to its asynchronous nature and thus potential for non-deterministic results (although less worrisome than on top of a platform that also does shared memory concurrency, like Java).
I also understand the concerns expressed by people on that issue. But there are several implementations in the wild that have been used successfully, at scale I might say, without users hitting gotchas, whereas such a memory leak can be a big gotcha.
Implementations include Scala's Future, in the wild since 2013 (which was my inspiration) and Twitter's Future, which they've been using within their whole infrastructure, in Finagle, etc. I deeply believe this is the right approach.
15
u/Danmoreng Oct 11 '17 edited Oct 11 '17
I dunno...tried it Chrome and it didn't crash yet. Gets slower and uses 2.2GB of memory, but still runs:
https://i.imgur.com/t5iDB3S.png
Oh nvm, now it says "Paused before potential out-of-memory crash.", stopping at 4569000.
Wasn't that always the problem with recursive functions in JavaScript though?
https://stackoverflow.com/questions/39898773/pausable-javascript-function/39900192#39900192