4 ms·
- I'm surprised that no current JS VM does lambda lifting (which is the transformation being done manually here). The analysis is easy, the wins are big for fun
by samth 10y ago
- I'm surprised that no current JS VM does lambda lifting (which is the transformation being done manually here). The analysis is easy, the wins are big for function-heavy code, and the tech is easy and well-known. JSC should try it! ;)
- I was just last week sketching such an optimization, but in a more complicated case, and with types to guide you. Seems very hard to do in JS.
- pizlonator 10y agoWe do most of lambda lifting. The part we don't do is to address the observability of function identity: function foo() { function bar() { } return bar; } foo() !== foo() // this must be true Therefore, we will at a minimum allocate a function object that just says "hey man I'm not like any other function!". We could avoid the allocation if: - We proved that no identity operations ever happen. That's hard! - We used some other UID system to identify different functions. But that would need a GC anyway!
- samth 10y agoProving that identity isn't needed here is easy: if the function is only used in a first-order way (ie, it's always called) then the identity can't possibly matter. That should work in most cases that matter.
- pizlonator 10y agoYou're right, it's easy in strict mode. I hadn't realized this before! In sloppy mode, there's function.arguments. You can walk the stack to find the damned function instance.
- samth 10y agoIn that case, the only identity has to come via function.arguments. Could you therefore allocate the identity lazily, since there's nothing else to compare to?
- pizlonator 10y agoYes, you could. I want to implement this eventually.