Katelyn Gadd (2015-02-15T15:27:26.000Z)
d at domenic.me (2015-02-21T00:41:47.900Z)
So there's no point in going in circles here, I just want to address the statement: Even if we allow this 'optimization' it will take the engines as much time to implement it so there is no gain from this anyway. My premise is that this is a rather obvious bottleneck and it applies to new APIs with no for-loop equivalent. As far as I know, I cannot use a for-loop to iterate over a Map or Set. So if I want to iterate over Map or Set, I'm eating the cost of the current iterator API as specified. The alternative is to use a custom implementation of those containers (that is what I do right now, and will continue to do.) Given the constraint that the .next() method is self-hosted in JS, my proposal is trivial to implement. I think this is a reasonable constraint, even though at present the API does not appear to be self-hosted in SpiderMonkey. Many builtins are self-hosted in both runtimes at present and more become self-hosted on a regular basis. The current self-hosted JS would look like this: ```js function Iterator () { // ... } function next () { // ... return { done: isDone, value: value }; } ``` The proposal would change the self-hosted JS to: ```js function Iterator () { // ... this.$cachedResultObject = { done: false, value: null }; } function next () { // ... this.$cachedResultObject.done = isDone; this.$cachedResultObject.value = value; return this.$cachedResultObject; } ``` As long as the spec allows this behavior change (it's certainly observable), that's all you really have to do at a basic level to make this work. I hope it is obvious that a C++ engine-level implementation of this optimization is not *significantly* more complex, though I will not say it is 'easy'. It is certainly, absolutely, I-will-bet-you-real-money simpler to do this than to implement sufficiently smart escape analysis to be able to optimize out the new object. I think if the next() method were self-hosted and fully inlined into the caller, at that point it would be possible for the .done and .value sets to be store-to-load forwarded and the new object optimized out. That's the most realistic case I can think of; I have yet to see a VM do that but I can imagine it happening sometime soon, given that LuaJIT does it.