3 ms·
There's a lot of misunderstanding here. A closure does not store the variables it creates. Instead, the closure's code block will allocate space for those varia
by ridiculous_fish 6y ago
There's a lot of misunderstanding here. A closure does not store the variables it creates. Instead, the closure's code block will allocate space for those variables when it is called.
The GC must trace all reachable values. This includes the stack, and also variables captured by closures which are themselves reachable.
The same principles apply whether or not a JIT is in use.
- hajile 6y agoI didn't misunderstand what I was writing (though I may not have succeeded in conveying what I meant). At compile time, a hidden class will be created to represent all the variables (both user-visible and user-inaccessible). Every function is actually called by an internal [[Call]]. This will create an instance of that class which is placed on the heap and may or may not copy some values into the stack (it's essentially a necessary performance optimization, but not actually hard requirement in all ISAs). A closure is an object that has only one public method or property (a function application sometimes called apply). In theory, if you could guarantee that the apply method and all nested lexical scopes never accessed a variable and the variable was not reference counted, you could safely null the entry in the class and allow whatever it pointed to to be GC'd. My only contention is that this is easier to theorize about than to actually do.