3 ms·
This part on the unoptimized vs optimized code looks interesting: Some functions contain very long running loops, so it makes sense to switch execution from un
by bassman9000 8y ago
This part on the unoptimized vs optimized code looks interesting:
Some functions contain very long running loops, so it makes sense to switch execution from unoptimized to optimized code while the function is still running. This process is called on stack replacement (OSR) owing its name to the fact that a stack frame for one version of the function is transparently replaced with a stack frame for another version of the same function.
- mraleph 8y agoThat is fairly classical thing to do - a lot of VMs, including JVM and JS VMs do OSR.
- bassman9000 8y agoReplacing the whole method/function seems pretty straight forward. Any good JS/JVM articles on how code is replaced mid-function? Edit: I see the article does mention IL deoptimization IDs. I assume those are used as "anchors" to know where to continue with the optimized rest of the loop.
- mraleph 8y agoYes, your intuition is correct. The way it works in Dart VM is like so: Each loop has an interruption point somewhere (e.g. in the header). If a very hot loop is found VM would enter runtime at that interruption point, where it would synchronously initiate compilation of an optimized version of the function for OSR at this particular interruption point. After unoptimized IL is contsructed it is "shaken": a matching interruption point is found in the graph and everything not reachable from it is shaken (which drops all code before the loop). After then the graph is transformed a bit - for each local variable alive at the interrupt point we introduce artificial parameter - so that we could simply treat OSR substitution kinda like a call. Then graph is optimized and compiled. Finally when we return back to the original unoptimized code we would do a tail call to the new optimized version. (I will cover this in the "Introduction to Dart VM" in more details and with some drawings, when I reach this).